# How to limit the number of variables in the model with symbolicRegression in R

I'm aware of the documents "an introduction to rgp" and the online documentation to rgp but I still have an unanswered question.

Is it possible to limit the number of used variables in the created model? I'd like to used as input data observations (y data) and a dozen of variables. I'd like to find a mathematical relationship such as

y~var1, var2, var3.

So all variables should be "tested" during the run but only 3 at à time since I don't want to have 12 variables explaining my observations y.

This is an example with only 2 variables and let's say I wish to have a model with only one variables explaining y. I'd like to add that condition to thé symbolicRegression function.

> polyFun<-function(a, b, x) {a*xs+b} xs<-seq(from=0,to=10, by=1)
> Obs<-polyFun(a=2, b=5, X) Rand<-rnorm(length(xs))
> Mydata<-data.frame(Var1=xs, Var2=rand, Y=Obs)
> Model<-symbolicRegression(Y~Var1+Var2,
> data=Mydata,stopCondition=makeTimeStopCondition(1*60))


By running this example, the best model according to the lowest fitness value does not use Var2 (which on this case was useless and was made up for the example), so it seems that the symbolicRegression function exclude useless variables? That would be interesting too but I still would like to know if it possible to limit thé number of variables...

I didn’t find a way to insert a condition within the symbolicRegressioin function but I built a loop where I apply this function for 2 variables at a time:

library("rgp")
library("rlist")

nvar1<-ncol(variable1)
nvar2<-ncol(variable2)

functions<-functionSet("+","-","*","/","sqrt","exp","ln")

result_list<-list(list())
fit_list<-list()

# Loop accross all data
for (j in 1:nvar1) {

for (i in 1:nvar2){

#create a dataframe
df<-data.frame()
df<-data.frame(observations[,1],variable1[,j],variable2[,i])
colnames(df)<-c("Y","Var1", "Var2")

#run the symbolicRegression function for all possibilities of combination of 2 variables for one macro obs
result<-symbolicRegression(Y~Var1+Var2,data=df,functionSet=functions,stopCondition = makeTimeStopCondition(20*60))
# save the results in case of, the result from SR is a list of 22 items
name<-paste(colnames(variable1)[j])
result_list[[name]][[i]]<-result
# select the best fitnessValue for further quick look
fit<-min(result$fitnessValues) fit_list[[name]] [[i]]<-fit } } list.save(result_list, 'result_list_obs1.Rdata') list.save(fit_list,'fit_list_obs1')  Then, I look up the fit_list to find out what were the lowest fitnessValues and I have to return to the result_list to check out what is the function associated. I firstly wanted to directly save the function associated to the lowest fitnessValues but I didn’t succeed saving it in a list (within the loop): fun<-result$population[[which.min(result\$fitnessValues)]]
functions_list[[name]] [[i]]<-fun


This is the error message :

Error in functions_list[[name]][[i]] <- fun : invalid type/length (closure/0) in vector allocation