TIME SERIES ANALYSIS IN R
Simulate time series of length 100 from an AR(1) model with α equal to 0.9.
Estimate the parameter of the model, make prediction for 1 to 10 steps ahead, and make a time plot
A time series is a sequence of measurements of the same variable(s) made over time. Usually the measurements are made at evenly spaced times - for example, monthly or yearly. Let us first consider the problem in which we have a y-variable measured as a time series. As an example, we might have y a measure of global temperature, with measurements observed each year. To emphasize that we have measured values over time, we use "t" as a subscript rather than the usual "i," i.e., means measured in time period . An autoregressive model is when a value from a time series is regressed on previous values from that same time series. for example, on .
In this regression model, the response variable in the previous time period has become the predictor and the errors have our usual assumptions about errors in a simple linear regression model. The order of an autoregression is the number of immediately preceding values in the series that are used to predict the value at the present time. So, the preceding model is a first-order autoregression, written as AR(1).
Here the model is given by,
where ~ White Noise(0,1) and .
The R-Code is obtained as:
set.seed(123)
ts_AR = arima.sim(n=100, list(ar=c(0.9)))
#1. Estimate parameters with arima()
model_AR = arima(ts_AR, order=c(1,0,0))
model_AR
Call:
arima(x = ts_AR, order = c(1, 0, 0))
Coefficients:
ar1 intercept
0.8781 -0.3569
s.e. 0.0456 0.7220
sigma^2 estimated as 0.8847: log likelihood = -136.5, aic = 279.01
#2. Simulating the time series of length
100
arima.sim(model=as.list(coef(model_AR)), n=100)
Time Series:
Start = 1
End = 100
Frequency = 1
-0.45510124 -0.99424924 0.77784065 0.62900670 0.67158650 0.83341871
1.96431473 1.20883364 0.06898980 1.73627803 1.08349143 0.22836590
-1.03574122 -2.19421734 -2.50075226 -1.57796639 -0.27579042
0.46541219 0.04502890 0.09929056 -0.61740777 -1.25937420
-0.22122696 -1.20985566 0.89289957 0.69375039 0.82373259
-0.01519440 -0.58773114 -1.83311264 -1.79261212 -1.15514015
-0.69004346 -1.38747514 -2.00698699 -2.26456742 -0.49249369
-1.56977053 -1.55749324 0.53470108 0.36855503 -1.03620639
-1.57467952 -0.89729233 -1.16353021 -1.58359162 -1.73449540
-1.43259265 0.34052531 0.21045583 1.26560427 1.74210230 1.41612914
-0.28937542 -0.77522269 -1.17060641 -0.98077455 0.43896421
2.67854076 3.89965230 3.29119866 1.13352895 0.60659079 0.62186469
1.39108275 2.18406089 2.60216967 0.88973417 1.63093343 0.98559259
1.04026797 0.98802780 1.29577043 1.16251257 -0.64665345 0.16865880
0.53412867 0.20337566 0.29673206 0.39460401 0.56752782 2.13920180
1.65941834 1.62522826 2.59552387 3.33335376 4.07233655 2.99851854
4.63553179 4.13723853 5.49982989 3.47858952 3.07559097 3.95064222
2.75388253 1.66554114 0.52400071 -0.59237953 -0.95733787
-0.50947515
#3. Making Prediction for 1 to 10 steps ahead
library(forecast)
forecast(model_AR)
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
101 -0.9998720 -2.205265 0.2055213 -2.843362 0.843618
102 -0.9215046 -2.525669 0.6826601 -3.374863 1.531854
103 -0.8526888 -2.706671 1.0012934 -3.688110 1.982732
104 -0.7922606 -2.817945 1.2334241 -3.890278 2.305757
105 -0.7391975 -2.887932 1.4095365 -4.025403 2.547008
106 -0.6926019 -2.931605 1.5464012 -4.116862 2.731658
107 -0.6516856 -2.957883 1.6545116 -4.178710 2.875339
108 -0.6157563 -2.972458 1.7409457 -4.220021 2.988508
109 -0.5842062 -2.979125 1.8107123 -4.246918 3.078506
110 -0.5565015 -2.980477 1.8674740 -4.263652 3.150649
#4.) Making the Time Plot
library(ggplot2)
autoplot(forecast(model_AR))
Get Answers For Free
Most questions answered within 1 hours.