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.
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
arhelp 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
Created on 2023-05-22 with reprex v2.0.2