【问题标题】:R adding lagged variable to sarima modelR将滞后变量添加到sarima模型
【发布时间】: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)

【问题讨论】:

    标签: r arima


    【解决方案1】:

    我认为问题出在滞后:您正在及时改变值,因此当您在所有时间序列上调用 cbind 时,您最终会得到超出 @987654322 最终日期的数据@ 和 R 抱怨。 (试试cbind(trend, temp, temp2, part, partL4),你可以清楚地看到发生了什么)。如果在调用sarima 之前从partL4 中删除这些值,它应该可以工作:

    partL4_new <- window(partL4, end = 1979.750)
    sarima(cmort, 2,0,0, xreg=cbind(trend, temp, temp2, part, partL4_new))
    

    【讨论】:

      猜你喜欢
      • 2012-10-17
      • 1970-01-01
      • 1970-01-01
      • 2010-11-21
      • 2022-11-23
      • 2023-02-09
      • 2012-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多