使用具有滞后新变量的线性模型进行更新

时间:2017-06-27 13:50:23

标签: r

我有一个基本模型y ~ x1 + x2

我想更新模型以包含y ~ x1 + x2 + lag(x3, 2) + lag(x4, 2)

x3和x4也是动态选择的。

fmla <- as.formula(paste('.~.', paste(c(x3, x4), collapse = '+')))

我的更新公式:update(fit, fmla)

我收到一条错误,说as.formula函数找不到x3 / x4。我理解错误不是如何绕过我想做的事情。

1 个答案:

答案 0 :(得分:1)

您的问题的可能解决方案可以是:

# Data generating process
yX <- as.data.frame(matrix(rnorm(1000),ncol=5))
names(yX) <- c("y", paste("x",1:4,sep=""))

# Start with a linear model with x1 and x2 as explanatory variables
f1 <- as.formula(y ~ x1 + x2)
fit <- lm(f1, data=yX)

# Add lagged x3 and x4 variables
fmla <- as.formula(paste('~.+',paste("lag(",addvars,",2)", collapse = '+')))
update(fit,  fmla)

# Call:
# lm(formula = y ~ x1 + x2 + lag(x3, 2) + lag(x4, 2), data = yX)
# 
# Coefficients:
# (Intercept)           x1           x2   lag(x3, 2)   lag(x4, 2)  
#   -0.083180     0.015753     0.041998     0.000612    -0.093265

下面是dynlm包的示例。

data("USDistLag", package = "lmtest")

# Start with a dynamic linear model with gnp as explanatory variables
library(dynlm)
f1 <- as.formula(consumption ~ gnp)
( fit <- dynlm(f1, data=USDistLag) )

# Time series regression with "ts" data:
# Start = 1963, End = 1982
# 
# Call:
# dynlm(formula = f1, data = USDistLag)
# 
# Coefficients:
# (Intercept)          gnp  
#    -24.0889       0.6448

# Add lagged gnp 
addvars <- c("gnp")
fmla <- as.formula(paste('~.+',paste("lag(",addvars,",2)", collapse = '+')))
update(fit,  fmla)

# Time series regression with "ts" data:
# Start = 1963, End = 1980
# 
# Call:
# dynlm(formula = consumption ~ gnp + lag(gnp, 2), data = USDistLag)
# 
# Coefficients:
# (Intercept)          gnp  lag(gnp, 2)  
#    -31.1437       0.5366       0.1067