首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >R向sarima模型中添加滞后变量

R向sarima模型中添加滞后变量
EN

Stack Overflow用户
提问于 2018-12-06 23:25:34
回答 1查看 415关注 0票数 0

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

代码语言:javascript
运行
复制
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模型时使用的代码,没有新的滞后变量:

代码语言:javascript
运行
复制
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模型。唯一需要使用的库是:

代码语言:javascript
运行
复制
library(astsa)

任何帮助都将是非常感谢的,因为我不知道如何使长度对齐,或者是否有更好的方法来做到这一点。

下面是我现在的完整代码(给出错误):

代码语言:javascript
运行
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-07 01:04:21

我认为问题来自于延迟:您正在及时地转换值,所以当您在所有时间序列上调用cbind时,最终得到的数据超出了cmort和R抱怨的最终日期。(尝试cbind(trend, temp, temp2, part, partL4),您就可以清楚地看到正在发生的事情)。如果在调用partL4之前将这些值从sarima中删除,则应该可以:

代码语言:javascript
运行
复制
partL4_new <- window(partL4, end = 1979.750)
sarima(cmort, 2,0,0, xreg=cbind(trend, temp, temp2, part, partL4_new))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53661185

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档