I wrote a small function to run multiple univariate regressions with different lags:
f <- function(x,l) {
lm.obj <- dynlm(GNP~L(get(as.character(x)),l),
data=longley)
names(lm.obj$coefficients)[2] <- paste(x,l,sep=".")
return(lm.obj)
}
this works quite well with mlply
after creating a frame of possible lag - variable combinations using expand.grid
. A reproducible example using the longley
dataset from the datasets
package looks like this:
data(longley)
require(plyr)
vars <- expand.grid(x=names(longley)[-c(1:3)],l=1:3)
regressionList <- mlply(vars,f, .progress= "text")
summaries <- lapply(regressionList,summary)
This works quite well. However now I try to do the same thing using gls.
require(nlme)
f.gls <- function(x,l) {
gls.obj <- gls(GNP~lag(get(as.character(x)),-l),
data=longley)
names(gls.obj$coefficients)[2] <- paste(x,l,sep=".")
return(gls.obj)
}
But all I get is this error:
Error in eval(expr, envir, enclos) : object 'x' not found
I tried to use debug()
but everything on the inside seems the same. I als tried to call the function outside mlply
but it did not work. Also tried with out lags. It's just that x can"t be found.