Calling R
Introductionβ
R language is one of powerful data mining tools, and its community is very large in the world (See the website: http://www.r-project.org/). Adding the R language into GAMA is our strong endeavors to accelerate many statistical, data mining tools into GAMA.
RCaller 2.0 package (Website: http://code.google.com/p/rcaller/) is used for GAMA 1.6.1.
Table of contentsβ
- Introduction
- Load the package:
- Read data from iris:
- Build the decision tree:
- Build the random forest of 50 decision trees:
- Predict the acceptance of test set:
- Calculate the accuracy:
Configuration in GAMAβ
-
Install R language into your computer.
-
In GAMA, select menu option: Edit/Preferences.
-
In "Config RScript's path", browse to your "Rscript" file (R language installed in your system).
Notes: Ensure that install.packages("Runiversal") is already applied in R environment.
Calling R from GAMLβ
Calling the built-in operatorsβ
Example 1β
model CallingR
global {
list X <- [2, 3, 1];
list Y <- [2, 12, 4];
list result;
init{
write corR(X, Y); // -> 0.755928946018454
write meanR(X); // -> 2.0
}
}
Calling R codes from a text file (.R,.txt) WITHOUT the parametersβ
Using R_compute(String RFile) operator. This operator DOESNβT ALLOW to add any parameters form the GAML code. All inputs is directly added into the R codes. Remarks: Donβt let any white lines at the end of R codes. R_compute will return the last variable of R file, this parameter can be a basic type or a list. Please ensure that the called packages must be installed before using.
Example 2β
model CallingR
global
{
list result;
init{
result <- R_compute("C:/YourPath/Correlation.R");
write result at 0;
}
}
Above syntax is deprecated, use following syntax with R_file instead of R_compute:
model CallingR
global
{
file result;
init{
result <- R_file("C:/YourPath/Correlation.R");
write result.contents;
}
}
Correlation.R fileβ
x <- c(1, 2, 3)
y <- c(1, 2, 4)
result <- cor(x, y, method = "pearson")
Outputβ
result::[0.981980506061966]
Example 3β
model CallingR
global
{
list result;
init{
result <- R_compute("C:/YourPath/RandomForest.R");
write result at 0;
}
}
RandomForest.R fileβ
library(randomForest)
data(iris)
nrow<-length(iris[,1])
ncol<-length(iris[1,])
idx<-sample(nrow,replace=FALSE)
trainrow<-round(2*nrow/3)
trainset<-iris[idx[1:trainrow],]
trainset<-iris[idx[1:trainrow],]
testset<-iris[idx[(trainrow+1):nrow],]
model<-randomForest(x= trainset[,-ncol], y= trainset[,ncol], mtry=3, ntree=50)
pred<-predict(model, testset[,-ncol], type="class")
acc<-sum(pred==testset[, ncol])/(nrow-trainrow)
Outputβ
acc::[0.98]
Calling R codes from a text file (.R, .txt) WITH the parametersβ
Using R_compute_param(String RFile, List vectorParam) operator. This operator ALLOWS to add the parameters from the GAML code.
Remarks: Donβt let any white lines at the end of R codes. R_compute_param will return the last variable of R file, this parameter can be a basic type or a list. Please ensure that the called packages must be installed before using.
Example 4β
model CallingR
global
{
list X <- [2, 3, 1];
list result;
init{
result <- R_compute_param("C:/YourPath/Mean.R", X);
write result at 0;
}
}
Mean.R fileβ
result <- mean(vectorParam)
Outputβ
result::[3.33333333333333]
Example 5β
model CallingR
global {
list X <- [2, 3, 1];
list result;
init{
result <- R_compute_param("C:/YourPath/AddParam.R", X);
write result at 0;
}
}
AddParam.R fileβ
v1 <- vectorParam[1]
v2<-vectorParam[2]
v3<-vectorParam[3]
result<-v1+v2+v3
Outputβ
result::[10]