【发布时间】:2018-12-06 23:25:34
【问题描述】:
我试图在我的回归中引入一个变量的滞后值,然后在新的变量集合上使用一个 arima 模型。例如,我试图使用死亡率对温度和污染颗粒水平的回归来模拟死亡率、温度和污染颗粒水平之间的关系。然后,引入四个星期前的粒子水平的滞后变量。代码如下:
temp = tempr-mean(tempr)
ded = ts.intersect(cmort, trend=time(cmort), temp, temp2=temp^2, part, partL4=lag(part,-4))
summary(fit <- lm(cmort~trend + temp + temp2 + part + partL4, data=ded))
pairs(ded) # easiest way is to do all of them
cor(ded)
AIC(fit)/nrow(ded) - log(2*pi)
BIC(fit)/nrow(ded) - log(2*pi)
其中 temp 是中心温度值,temp2 是中心温度的平方,part 是空气中的污染颗粒水平,partL4 是 4 周前的颗粒水平。这种回归按预期工作,没有给我任何问题。但是,当我尝试对这个新的变量集合使用 arima 模型时,问题就出现了。这是我用于在没有新滞后变量的原始变量集合上使用 arima 模型的代码:
trend = time(cmort); temp = tempr - mean(tempr); temp2 = temp^2
fit = lm(cmort~trend + temp + temp2 + part, na.action=NULL)
acf2(resid(fit), 52) # implies AR2
sarima(cmort, 2,0,0, xreg=cbind(trend, temp, temp2, part) )
此模型也适用。但是,当我尝试引入 partL4 滞后变量时,我收到以下错误:
统计错误::arima(xdata, order = c(p, d, q),seasonal = list(order = c(P, : 'x' 和 'xreg' 的长度不匹配
当我检查 cmort 的长度和 xreg 中使用的新变量集合时,长度略有偏差。但是,当我删除原始代码中的 partL4 变量时,长度匹配。
我真的不知道如何解决这个问题并在新的变量集合上运行 arima 模型。唯一需要使用的库是:
library(astsa)
任何帮助将不胜感激,因为我不确定如何使长度对齐,或者是否有其他更好的方法来做到这一点。
这是我现在的完整代码(给出错误):
library(astsa)
temp = tempr-mean(tempr)
temp2=temp^2
trend=time(cmort)
partly=lag(part, -4)
ded = ts.intersect(cmort, trend, temp, temp2, part, partL4, dframe=TRUE)
fit <- lm(cmort~trend + temp + temp2 + part + partL4, data=ded, na.action=NULL)
summary(fit)
attach(ded)
tsplot(resid(fit))
acf2(resid(fit)) #implies AR2
sarima(cmort, 2,0,0, xreg=cbind(trend, temp, temp2, part, partL4))
# pairs(ded) # easiest way is to do all of them
# cor(ded)
# AIC(fit)/nrow(ded) - log(2*pi)
# BIC(fit)/nrow(ded) - log(2*pi)
detach(ded)
【问题讨论】: