Instead of specifying the predictors as regression arguments, I would like to just pass on a string and change it into the right syntax before it is used for penalized regression. It has been explained in Loop function to add large numbers of predictors in regression function or How to use paste to get formula how this can be done for lm but it does not work for penalized regression.
Here is my code:
df<-data.frame(date=seq(as.Date("2018-01-01"), as.Date("2018-10-01"), by="days"))
df$month<-format(as.Date(df$date), "%m")
df$y<-runif(nrow(df),1,100)
df$time<- -floor(nrow(df)/2):(ceiling(nrow(df)/2)-1)/1000
df$month<-as.factor(df$month)
yname<-"y"
xnames<-colnames(training)
xnames<-xnames[-which(xnames==yname)]
xnames<-xnames[-which(xnames=="date")]
yname<-paste(yname,",")
formula<-paste(yname,"~",paste(xnames,collapse="+"))
ens<-penalized(formula, ~ 0,lambda1=1, lambda2=1, positive =TRUE, data=training)
I tried using as.formula for the formula but it does not work with the comma. It is all working fine though if I put the variable names in manually and it is working for lm but not for penalized. Any ideas?
Please note that I have edited the question to make it more specific for penalized.