What does R do when it plots the residuals of an AR fit? - Cross Validated most recent 30 from stats.stackexchange.com 2019-10-22T02:14:30Z https://stats.stackexchange.com/feeds/question/27206 https://creativecommons.org/licenses/by-sa/4.0/rdf https://stats.stackexchange.com/q/27206 4 What does R do when it plots the residuals of an AR fit? Néstor https://stats.stackexchange.com/users/9174 2012-04-27T01:01:01Z 2012-05-04T06:36:42Z <p>This is a question that's been bugging me for some time. The problem is this: I'm modelling the residuals of a model $f(t,\vec{\theta})$ with (what I think is) an AR process plus a white noise process via MCMC using a multivariate gaussian likelihood, where I model the covariance matrix elements equal to the elements of the autocovariance of an $AR(1)$ process plus i.i.d. white noise $\varepsilon(t)$, i.e., I model</p> <p>$$r(t)=AR(1)+\varepsilon(t),$$ where $r(t)$ are the residuals of my model, $r(t)=d(t)-f(t,\vec{\theta})$, where $d(t)$ is the data. The thing is that I wanted a visual check to see if my fit was ok (because I'm pretty sure via some previous analysis that my residuals can be modelled efficiently by an $AR(1)$ plus white noise: I just want to show this result to other people "quickly").</p> <p>What I did then was to analize the residuals via the <code>arima</code> and <code>arma</code> functions in the <code>tseries</code> library in R, and I saw that they actually fitted the residuals with the same coefficient as the MCMC result (within the error bounds and not taking in consideration the additive white noise, which has small variance anyways). However, I also saw that the <code>arma</code> function has some nice plots: it plots the "residuals" of the AR fit.</p> <p>This is my question: <strong>how do you plot the residuals of an AR fit if an AR model is stochastic by nature?</strong> I think my question goes down to: <strong>how do you generate a realization of your model that matches your particular realization of the AR process (i.e. your data) in order to substract this realization from your data?</strong> The only way I could think of is to simulate various realizations of the $AR(1)$ model until one fits your data, but this seems like stacking the deck.</p> https://stats.stackexchange.com/questions/27206/-/27328#27328 4 Answer by Rob Hyndman for What does R do when it plots the residuals of an AR fit? Rob Hyndman https://stats.stackexchange.com/users/159 2012-04-29T07:26:49Z 2012-04-29T07:26:49Z <p>First, an AR(1) plus white noise is equivalent to an ARMA(1,1) process. So unless there is a good reason to formulate your model with a latent AR(1) process observed with error, I would suggest you use the simpler and equivalent model with ARMA(1,1) errors.</p> <p>Then you can write $$d_t = f(t,\theta) + r_t$$ where $$r_t = \phi r_{t-1} + \gamma e_{t-1} + e_t$$ and $e_t$ is white noise. The residuals are $$e_t = r_t - \phi r_{t-1} - \gamma e_{t-1}$$ which can easily be found recursively beginning with $r_0=e_0=0$ so that $e_1=r_1$.</p> <p>If you wish to persist with the AR(1) + WN formulation, you can compute the errors via a Kalman filter.</p> https://stats.stackexchange.com/questions/27206/-/27746#27746 3 Answer by Yves for What does R do when it plots the residuals of an AR fit? Yves https://stats.stackexchange.com/users/10479 2012-05-04T06:10:30Z 2012-05-04T06:36:42Z <p>As Rob Hyndman wrote, you can use Kalman filter. Your interest seems on estimating the unobservable AR(1) component $\alpha_t$ in your model written as $$\left\{\begin{array}{r l} \alpha_{t} &amp;= \phi \,\alpha_{t-1} +\eta_{t}\\ y_t &amp;= \alpha_t + \varepsilon_t \end{array}\right.$$ where $\eta_{t}$ and $\varepsilon_t$ are independent gaussian noises. This is a standard linear State Space (SS) model. I used $y_t$ in place of your $r(t)$ to stick to more conventional notations. Assuming $\alpha_t$ to be centered as here is not essential.</p> <p>Kalman filtering techniques are dedicated to the recursive computations of the conditional expectations $$a_{t|u} := \mathbb{E}\left[\alpha_t \,\vert\, y_1,\,y_2,\,\dots,\,y_u\right]$$ for times $t$ and $u$. The estimation task is called <em>prediction</em> if $t&gt;u$, <em>filtering</em> if $t=u$ and <em>smoothing</em> for $t &lt; u$. The best estimation $a_{t|u}$ of $\alpha_t$ uses all observations, hence is for $u=n$, the last observation. This kind of estimation is often called <em>signal extraction</em>, which makes sense for a signal $+$ noise'' model as yours.</p> <p>In R, you can use e.g. the <code>KalmanSmooth</code> function (from the stats package), which will require the SS model, and will give you the estimated or "de-noised" signal $a_{t|n}$. To assess the effect of the measurement noise, then simply display a time plot of the estimation, or of the estimate of the observation noise $\varepsilon_t$. The best estimate of $\varepsilon_t$ is its expectation conditional on the whole series $e_{t|n}:=y_t - a_{t|n}$. The parameters to be estimated are $\phi$, $\sigma^2_\eta$ and also $\sigma^2_\varepsilon$, at least if you do not consider it as perfectly known. You can use the <code>KalmanLike</code> and <code>optim</code> to get Maximum Likelihood estimates which will require a little program.</p> <p>The problem of simulating the series $\alpha_t$ (or the noises) conditional on the observations is known as simulation smoothing''. Efficient algorithms have been designed by (among others) Durbin and Koopman and seem to be available in the R package KFAS.</p> <pre><code>## parameters phi &lt;- 0.8; sig.eta &lt;- 1; sig.epsilon &lt;- 2 n &lt;- 200 ## simulate AR(1) + noise y &lt;- arima.sim(n = n, list(ar = phi, ma = numeric(0), sd = 1)) y &lt;- y + rnorm(n, sd = sig.epsilon) plot(y, type = "o", pch = 16, cex = 0.6) ## write the State Space model mod &lt;- list(T = phi, Z = 1, h = sig.epsilon^2, V = sig.eta^2) ## initialisation part P &lt;- sig.eta^2 / (1-phi^2) mod &lt;- c(mod, list(a = 0, P = P, Pn = P)) ## smooth res &lt;- KalmanSmooth(y = y, mod = mod, nit = -1) lines(res\$smooth, col = "orangered", lwd = 2) legend("topleft", lty = rep(1, 1), lwd = c(1, 2), pch = c(16, NA), col = c("black", "orangered"), legend = c("y (raw)", "a (smoothed)")) </code></pre>