如何手动计算自回归模型的残差

huangapple go评论59阅读模式
英文:

how to get residuals from an autoregressive model by hand

问题

我想知道如何手动计算拟合AR模型的残差。我现在可以使用 stats::ar 拟合自回归模型,通过拟合模型的 $resid 得到残差,但我希望能够根据估计的AR参数自己计算这些残差。因此,我想要一个函数,该函数接受AR模型的估计参数和时间序列,返回残差或自相关已移除的时间序列。

我的最终目标是能够在一个数据集上估计AR系数,然后将其应用到另一个数据集,或者尝试从相同时间序列中移除略有不同的AR系数。

例如,在这里,我希望能够通过将估计的系数应用到原始的多变量 usconsumption 时间序列上,重新生成与 ar_model$resid 数据相同的结果。

我尝试使用 timsac::mfilter 函数,但无法得到与原始 ar 残差接近的结果。

英文:

I would like to know how to calculate residuals from a fitted AR model by hand. Now I can use stats::ar to fit an autoregresive model and and get the residuals by from $resid of the fitted model, however I would like to be able to calculate these myself given the estimated AR parameters. So I would like to have a function that takes the estimated AR parameters from the ar model and a time-series and returns the residuals or a time-series with the auto-correlation removed.

My ultimate goal is to be able to estimate AR coefficients on one dataset and apply them to another, or to try to remove slightly different AR coefficients from the same time-series.

So for example here I would lieke to be able to reproduce the same ar_model$resid data by applying the estimated coefs to the original multivariate usconsumption timeseries.

> library(fpp)
> ar_model <- ar(usconsumption, aic=FALSE, order.max=1)
> ar_model$ar
, , consumption

  consumption    income
1   0.3088803 0.5627686

, , income

  consumption     income
1  0.08269438 -0.2310507

> head(ar_model$resid)
         consumption     income
1970 Q1          NA         NA
1970 Q2  -0.2363646  1.0249511
1970 Q3   0.1294457  1.0084068
1970 Q4  -1.1150108 -0.9913129
1971 Q1   1.5423841  1.5613124
1971 Q2  -0.2947244  0.3983440

I tried to use timsac::mfilter function, but I wasn't able to get anything close to the original ar residuals.

答案1

得分: 1

我编写了这段代码来尝试回答你的问题。它将根据ar帮助页面上的这个公式将AR系数与滞后值相乘。

不幸的是,结果并不完全正确 如何手动计算自回归模型的残差 我对预测值的计算与fitted(model)不一样。

尽管这不能回答你的问题,但我会在这里发布它,以防其他用户能够找出这段代码的问题,或将其用作解决方案的模板。

公式
x
t

−μ=a
1

(x
t−1

−μ)+⋯+a
p

(x
t−p

−μ)+e
t

英文:

I wrote this code to try to answer your question. It will multiply the AR coefficients by the lagged values according to this formula from the ar help page.

Unfortunately, the result is not exactly correct 如何手动计算自回归模型的残差 My calculation of the predicted values is not the same as fitted(model).

Although this does not answer your question, I will post it here in case other users are able identify what's wrong wit this code, or use it as a template for a full solution.

formula:
x
t

−μ=a
1

(x
t−1

−μ)+⋯+a
p

(x
t−p

−μ)+e
t

library(fpp, quietly = TRUE)

# fit
model <- ar(usconsumption, aic = FALSE, order.max = 1)

# initialize
pred <- matrix(nrow = nrow(usconsumption), ncol = 2)
colnames(pred) <- colnames(usconsumption)

# predict
for (i in 2:nrow(usconsumption)) {
  pred[i, "consumption"] <- model$x.mean["consumption"] + sum((usconsumption[i-1, ] - model$x.mean) * model$ar[, ,"consumption"])
  pred[i, "income"] <- model$x.mean["income"] + sum((usconsumption[i-1, ] - model$x.mean) * model$ar[, ,"income"])
}

# compare
head(fitted(model))
#>         getResponse(object).consumption getResponse(object).income
#> 1970 Q1                              NA                         NA
#> 1970 Q2                       0.6912944                  0.7115085
#> 1970 Q3                       0.7452274                  0.3364742
#> 1970 Q4                       0.8424964                  0.6631670
#> 1971 Q1                       0.3498029                  0.4041199
#> 1971 Q2                       1.2081025                  1.0924131

head(pred)
#>      consumption    income
#> [1,]          NA        NA
#> [2,]   0.5760684 0.7801837
#> [3,]   1.2252548 0.4806877
#> [4,]   1.1345370 0.6058726
#> [5,]  -0.1613336 0.8975607
#> [6,]   1.7980539 0.5466365

<sup>Created on 2023-05-22 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年5月22日 18:57:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305469.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定