I try to combine an output in a different way, so that in the end I get the columns values of selected variables after the backward selection. Unfortunately I have problems with the combination of the variables. After the backward selection the variables are output as formula and I would have liked to separate this formula by ,
instead of +
and put the variables in quotation marks ""
. I tried different ways and unfortunately I did not get a result.
external <- ts(mtcars, start = c(2015,1), end = c(2019,52), frequency = 52)
# define all possible variables for variable selection
x <- window(external, end= c(2018,52))
data.variables <- lm(mpg ~cyl+disp+hp+drat+wt+qsec+vs+am+gear+carb, data=window(external, start=c(2015,1), end=c(2018,52)))
# Variable selection backwards
step <- step(data.variables, direction="backward", k=log(208))
# provide matrix regression
## Here I want to paste the calculated Variables listed like this: "disp","hp","wt","qsec","am"
xregs <- as.matrix(window(external[,c(paste(gsub("+", ",",(deparse(step[["call"]][["formula"]][[3]])), fixed=T))), end = c(2018, 52)))
# the acutal line should look like this:
xregs <- as.matrix(window(external[,c("disp","hp","wt","qsec","am")], end = c(2018, 52)))
# but I only receive this:
xregs <- as.matrix(window(external[,c("disp,hp,wt,qsec,am")], end = c(2018, 52)))
Especially when I use the following:
a <-gsub("+", ",",(deparse(step[["call"]][["formula"]][[3]])), fixed=T)
cat(sapply(strsplit(a, '[, ]+'), function(x) toString(dQuote(x, FALSE))))
#"disp", "hp", "wt", "qsec", "am"
the right line is printed, but I don't know how to use the printed line in the other command.