I am trying to loop over a list of character vectors and use other functions to form linear models and calculate values for me:
all_model_error<-function(explan_vars,depvar,dataset1,dataset2)
{
x<-list()
model_combinations<-all_models(explan_vars)
for(i in 1:length(model_combinations))
{
x[[i]]<-error_rates(model_combinations[[i]],depvar,dataset1,dataset2)
}
return(x)
}
However when I run this function I get this error in parse. parse()
is not used in any function within this function, and I am confused as to why I am receiving this error.
The underlying functions are defined as such:
gen_formula<-function(depvar,explan_vars)
{
reg_form<-as.formula(paste(depvar,"~",paste(explan_vars,collapse = "+")))
return(reg_form)
}
This just returns your input in a form that the lm() function takes.Then:
error_rates<-function(indvars,explan_vars,dataset1,dataset2)
{
reg_results<-lm(gen_formula(depvar,explan_vars),data=dataset1)
summary(reg_results)
df_training<-dataset1 %>%
add_residuals(reg_results) %>%
summarize(error_rate=mean(resid^2))
training_error<-df_training[1,1]
df_test<-dataset2 %>%
add_residuals(reg_results) %>%
summarize(error_rate=mean(resid^2))
test_error<-df_test[1,1]
return(c(test_error,training_error))
}
this just calculates the error of the model against your test and training data. then:
name_from_bin<-function(b,vars)
{
return(vars[as.logical(b)])
}
all_models<-function(variables)
{
k<-length(variables)
bin_vec<-rep(list(0:1),k)
bin_mat<-expand.grid(bin_vec)
list_of_RHS<-list()
for(i in 1:nrow(bin_mat))
{
list_of_RHS[[i]]<-name_from_bin(bin_mat[i,],variables)
}
return(list_of_RHS)
}
These functions are used to create a matrix of all possible combinations of the number of variables on which to base a model. Then it returns these character vectors as a list, of every possible combination of variables.
I want to run all_model_error to find the error_rates() of all_models(). The underlying functions perform the task I want them to, and dont incude parse() which is why I'm confused with the error.
I am running all_model_error(explan_vars,depvar,crime_weather_1,crime_weather_2) and getting this error.
Where depvar is a single variable OffenseAgainst
in my dataset, and explan_vars is a vector of 14 variables in the dataset. Crime_weather_1 and 2 are training and test datasets.