Estimate the random walk model
For a given time series y we can fit the random walk model with a drift by first differencing the data, then fitting the white noise (WN) model to the differenced data using the arima() command with the order = c(0, 0, 0)) argument.
The arima() command displays information or output about the fitted model. Under the Coefficients: heading is the estimated drift variable, named the intercept. Its approximate standard error (or s.e.) is provided directly below it. The variance of the WN part of the model is also estimated under the label sigma^2.
This exercise is part of the course
Time Series Analysis in R
Exercise instructions
- The time series
random_walkhas already been loaded, and is shown in the adjoining figure. Usediff()to generate the first difference of the data. Save this torw_diff. - Use
ts.plot()to plot your differenced data - Use
arima()to fit the WN model for the differenced data. To do so, set thexargument torw_diffand set theorderargument toc(0, 0, 0). Store the model inmodel_wn. - Store the
interceptvalue ofmodel_wninint_wn. You can obtain this value usingmodel_wn$coef. - Use
ts.plot()to reproduce your original plot ofrandom_walk. - Add the estimated time trend to the adjoining plot with the function
abline(). You can useint_wnas the second argument.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Difference your random_walk data
rw_diff <-
# Plot rw_diff
# Now fit the WN model to the differenced data
model_wn <-
# Store the value of the estimated time trend (intercept)
int_wn <-
# Plot the original random_walk data
# Use abline(0, ...) to add time trend to the figure