I am running multiple univariate regressions, like in this reproducible example:
require(dynlm)
data(USeconomic)
US<-USeconomic
vars<-colnames(US)[-2]
a<
To construct R formula, you must paste it all together, not just the predictor side of it. So you need something like:
formula <- as.formula(
paste("log(GNP)~",
paste("L(",rep(vars,each=3),",",l,")",sep=""),
sep = ""
)
)
and then run
dynlm(formula, data = ...)
Here is an approach using plyr
library(plyr); library(dynlm); library(tseries)
# FUNCTION TO RUN A SINGLE REGRESSION
foo = function(x, l) dynlm(log(GNP) ~ L(get(as.character(x)), l), data = US)
# CREATE PARAMETER GRID
params = expand.grid(x = colnames(US)[-2], l = c(0, 1, 4))
# RUN REGRESSIONS
regressions = mlply(params, foo)
Each element of this list contains details on a single regression from which you can extract your desired output