我试图在回归中引入一个变量的滞后值,然后在新的变量集合上使用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)
其中温度为中心温度,temp2为平方中心温度,部分为空气中污染颗粒的水平,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) ),季节性=列表(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)
发布于 2018-12-07 01:04:21
我认为问题来自于延迟:您正在及时地转换值,所以当您在所有时间序列上调用cbind
时,最终得到的数据超出了cmort
和R抱怨的最终日期。(尝试cbind(trend, temp, temp2, part, partL4)
,您就可以清楚地看到正在发生的事情)。如果在调用partL4
之前将这些值从sarima
中删除,则应该可以:
partL4_new <- window(partL4, end = 1979.750)
sarima(cmort, 2,0,0, xreg=cbind(trend, temp, temp2, part, partL4_new))
https://stackoverflow.com/questions/53661185
复制相似问题