R在sarima模型中加入滞后变量



我试图在回归中引入一个变量的滞后值,然后在新的变量集合上使用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是四周前的颗粒水平。这种回归按预期工作,不会给我带来任何问题。然而,当我试图在这个新的变量集合上使用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(,季节性=list(order=c(p,:"x"one_answers"xreg"的长度与不匹配

当我检查cmort的长度和外部参照中使用的新变量集合时,长度略有偏离。然而,当我像原始代码中那样删除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)

我认为问题来自滞后:您在时间上移动值,所以当您在所有时间序列上调用cbind时,您最终得到的数据超出了cmort的最终日期,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))

最新更新