有一段很好的R代码用于拟合和可视化替代线性模型at www.alastairsanderson.com/R/tutorials/linear-regression-with-a-factor/。我如何推广这个框架以允许滞后预测器,例如,使用dyn或dynlm?
答案 0 :(得分:2)
试试这个:
library(dyn)
library(ggplot2)
forms.ch <- c(
"y ~ x",
"y ~ class / x",
"y ~ class / Lag(x, 0:1)",
"y ~ class / Lag(x, 0:2)"
)
forms <- sapply(forms.ch, as.formula)
Lag <- function(x, k = 1) lag(x, -k)
# L is a list of zoo objects which is fit to each formula
L <- lapply(mydata, zoo, order.by = mydata$x)
models <- lapply(forms, dyn$lm, data = L)
# create zero width zoo object, width0, which is merged with fitted. fitted would
# otherwise be shorter than mydata (since we can't fit points at beginning due to
# lack of laggged points at boundary). Also we convert mydata$x to numeric,
# from integer, to avoid warnings later on.
width0 <- zoo(, as.numeric(mydata$x))
models.sum <- lapply(models, function(x)
data.frame(mydata,
fitted = coredata(merge(fitted(x), width0)),
strip = paste(format(formula(x)), "AIC:", round(AIC(x), 1)),
formula = format(formula(x))
)
)
models.long <- na.omit(do.call(rbind, models.sum))
models.long$class[ models.long$formula == forms.ch[1] ] <- NA # first model has no class
ggplot(models.long, aes(x, y, colour = class)) +
geom_line(aes(y = fitted)) +
geom_point() +
facet_wrap(~ strip)