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

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

问题描述

我试图在回归中引入一个变量的滞后值,然后在新的变量集合上使用 arima 模型.例如,我正在尝试使用死亡率对温度和污染颗粒水平的回归来模拟死亡率、温度和污染颗粒水平之间的关系.然后,引入 4 周前粒子水平的滞后变量.代码如下:

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 是中心温度的平方,part 是空气中污染颗粒的水平,partL4 是 4 周前的颗粒水平.这种回归按预期工作,没有给我带来任何问题.但是,当我尝试在这个新的变量集合上使用 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),season = 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)

推荐答案

我相信问题来自于滞后:您正在及时移动值,因此当您在所有时间序列,您最终得到的数据超出了 cmort 的最终日期并且 R 抱怨.(试试 cbind(trend, temp, temp2, part, 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天全站免登陆