I am currently trying to speed up a nested if else statement that selects a model based on a parameter. The function below receives input from previous function in the form of the "model" argument. The model argument can be either "glm", "logarithmic", or "squared". This is currently done with a nested if ,else if, else if statement. I have tried framing this as two ifelse statement and my benchmarks do not show there is a performance increase. My program has to do this many times over for many different datasets so any increase in performance would be huge.
I am trying a case when from dpylr but having never used it I'm not sure if it is appropriate, any suggestions?
the code below gives me the following; Error: must be list, not lm
Edited to add sample data and previous function for reproducibility
data <- sample(1000)
df <- data.frame(data)
df[2] <- sample(1000)
names(df) <- c(y,x)
#previous function
produce_model <- function(model,df){
if (model=="glm")
{
model<-lm(y~x,df)
}
else if (model=="logarithmic")
{
model<-lm(log(y)~x,df)
}
else if (model=="squared")
{
model<-lm(y^2~x,df)
}
return(model)
}
#Trying to improve with case_when()
library(dplyr)
produce_model <- function(model,df) {
case_when(
model == 'glm' ~ lm(y~x,df=df),
model == 'logarithmic' ~ lm(log(y)~x,df=df),
model == 'squared' ~ lm(y^2~x,df=df)
)
return(model)
}