R将滞后变量添加到sarima模型 [英] R adding lagged variable to sarima model

查看:278
本文介绍了R将滞后变量添加到sarima模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在回归中引入变量的滞后值,然后在新的变量集合上使用arima模型.例如,我正在尝试使用死亡率对温度和污染水平的回归来对死亡率,温度和污染水平之间的关系进行建模.然后,在四周前引入颗粒水平的滞后变量.这是此代码:

I'm trying to introduced a lagged value of a variable already within my regression and then use an arima model on that new collection of variables. For example, I'm trying to model the relationship between mortality, temperature, and pollution particle levels using the regression of mortality rates on temperature and pollution particle levels. Then, introducing a lagged variable of the particles levels of four weeks prior. Here is the code for this:

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是中心温度的平方,部分是空气中污染颗粒的水平,而部分L4是四个星期前的颗粒水平.此回归按预期工作,并且没有任何问题.但是,当我尝试在新的变量集合上使用Arima模型时,就会出现问题.这是我用于在没有新的滞后变量的原始变量集合上使用arima模型的代码:

Where temp is the centered temperature values, temp2 is the squared centered temperature, part is the level of pollution particles in the air and partL4 is the particle levels of four weeks prior. This regression works as intended and gives me no issues. However, the issues arise when I attempt to use an arima model on this new collection of variables. Here is the code I am using for using an arima model on the original collection of variables without the new lag variable:

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滞后变量时,出现以下错误:

This model works as well. However, when I try to introduce the partL4 lagged variable, I receive an error of:

stats :: arima(xdata,order = c(p,d,q),季节= list(order = c(P,:'x'和'xreg'的长度不匹配')错误

Error in stats::arima(xdata, order = c(p, d, q), seasonal = list(order = c(P, : lengths of 'x' and 'xreg' do not match

当我检查cmort的长度以及xreg中使用的新变量集合时,长度略有不同.但是,当我按原代码中的方式删除partL4变量时,长度匹配.

When I check the length of cmort and the new collection of variables being used in xreg, the lengths are slightly off. However, when I remove the partL4 variable as it was in the original code, the lengths match.

我真的不知道如何解决此问题并在新的变量集合上运行arima模型.唯一需要使用的库是:

I'm really lost on how to fix this issue and run the arima model on the new variable collection. The only library that needs to be used is:

library(astsa)

任何帮助将不胜感激,因为我不确定如何使长度对齐,或者是否还有其他更好的方法可以做到这一点.

Any help would be much appreciated, as i'm not sure how to get the lengths to align, or if there is another better way to do this.

这是我目前的完整代码(出现错误):

Here is my full code as of right now (Gives Error):

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(趋势,温度,temp2,部分,partL4),您可以清楚地看到正在发生的事情).如果在调用 sarima 之前从 partL4 中删除了这些值,则它应该起作用:

I believe the problem comes from the lag: you're shifting the values in time, so when you call cbind on all the time series, you're end up with data that extends beyond the final date for cmort and R complains. (Try cbind(trend, temp, temp2, part, partL4) and you can see clearly what is happening). If you drop those values from partL4 before you call sarima it should work:

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

这篇关于R将滞后变量添加到sarima模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆