On Sat, 18 Apr 2009, Ron Burns wrote:
I want to set up a model with a formula and then run dynlm(formula) because I
ultimately want to loop over a set of formulas (see end of post)
R> form <- gas~price
R> dynlm(form)
Time series regression with "ts" data:
Start = 1959(1), End = 1990(4)
<snip>
Works OK without a Lag term
R> dynlm(gas ~ L(gas,1))
Time series regression with "ts" data:
Start = 1959(2), End = 1990(4)
<snip>
Works OK with a Lag with this type of call
R> form <- gas~L(gas,1)
R> dynlm(form)
Error in merge.zoo(gas, L(gas, 1), retclass = "list", all = FALSE) :
could not find function "L"
Does not work using a predefined formula with a Lag (This type of call works
using dyn$lm from library(dyn))
The problem with "dynlm" is that it defines it's lag/diff functionality
locally (unlike "dyn" which re-uses the usual lag/diff functions) and in
the setting above this conflicts with the non-standard evaluation,
unfortunately. I don't know a good solution to this...
How do I make the call (or how do I setup form) so that this works in dynlm?
In your specific problem, I think it is worth to take the extra step and
do the processing yourself because...
To be specific the following is an example of what I was attempting to do:
m1 <- gas ~ L(gas,1)
m2 <- gas ~ L(gas,1) + price
m3 <- gas ~ L(gas,1) + price + d(gas)
m4 <- gas ~ L(gas,1) + price + d(gas) + L(d(gas),1)
...these models correspond to different samples. m4 will lose one more
observation at the beginning by lag+diff. Of course, it is possible to
address this in dynlm as well but I (personally) find it simpler to do
the data processing first and then the modeling and model selection. I
would do something like:
## data processing
dat <- ts.intersect(gas, price,
gas1 = lag(gas, k = -1),
dgas = diff(gas),
dgas1 = lag(diff(gas), k = -1))
## models
form <- list(
gas ~ gas1,
gas ~ gas1 + price,
gas ~ gas1 + price + dgas,
gas ~ gas1 + price + dgas + dgas1)
## fitting
mod <- lapply(form, lm, data = dat)
## evaluation
sapply(mod, AIC)
sapply(mod, AIC, k = log(nrow(dat)))
hth,
Z
M <- c(m1,m2,m3,m4)
A <- array(0,c(4,2))
for(i in 1:4){
g <- dynlm(M[[i]]) ## works if use dyn$lm from library(dyn) and use
appropriate m's
A[i,1] <- AIC(g,k=2)
A[i,2] <- AIC(g,k=log(length(fitted(g))))
}
colnames(A) <- c("AIC","BIC")
rownames(A) <- c("m1","m2","m3","m4")
A
--
R. R. Burns
Retired in Oceanside, CA
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.