1

在www.alasairsanderson.com/R/tutorials/linear-regression-with-a-factor/上有一段很好的 R 代码用于拟合和可视化替代线性模型。我如何推广这个框架以允许滞后预测变量,例如,通过使用 dyn 或 dynlm?

4

1 回答 1

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)
于 2013-09-11T18:15:14.867 回答