TSA R Summary
TSA R Summary
R Summary
General:
For instance, we generally use larain datasets that are found in TSA library that are
imported using data() function (e.g: data(larain) after importing TSA).
We also use globaltemps and huron datasets, that are available in the following links:
Huron: http://people.stat.sc.edu/hansont/stat520/huron.txt
Globaltemps: http://people.stat.sc.edu/hansont/stat520/globaltemps.txt
in the start argument we specify which period ( “year” ) to begin with in our
observations.
The ts function converts any vector/list into a time series object.
Simulating and plotting time series:
To simulate a time series, we use the arima.sim function like such:
data= arima.sim( list(order=c(2,0,1), ar=c(0.5,-0.25), ma=c(0.7)), n=100)
Important: sometimes, before any simulation or random numbers generations we see the
function “ set.seed(140)” (140 is just a random number that can be changed to anything).
This is because our computers generate “random numbers” in a non-random way, on dirait
9a3ed ye7seb fi les termes mta3 une suite mathématique kol ma t9ollou enti simulate wala
generate yehseblek el terme elli mba3dou, tellement el suite w les calculs complex ma3morha
la ynajem yaatik nafs laadad martin consecutivement, w kima na3rfou, ki bech te7seb enti les
termes el jeyin mta3 suite dima lezmek you specify el “premier terme”, a7na kif nhottou
set.seed(140), 9och alina 7attina el premier terme bch ykoun 140, w hedhi naamlouha bech
ken zouz mennes y7ebou yaamlou une simulation fiha random numbers w yal9aw same
results, yestaamlou set.seed() fi awel el program 9bal ay haja.
Data Transformations
The following lines of code plots transformed versions of our data.
log(data) applies the log-transformation into our simulated series
diff(data, lag=2) yields second difference of our data, notice we specified lag=2.
these functions plot the ACF and PACF for our simulated AR(1)
This also works for simulated MA models.
The function ARMAacf ouputs the theoretical ACF of an ARMA(p,q) model for a
specified number of lags.
we’ve put the ARMAacf inside of round() to round all the results to the nearest
thousandth.
We can also use it to output PACF of the model.
library(TSA)
data(larain)
eacf(log(larain))
Stationarity Unit Root test
To perform ADF and KPSS tests on data, we need to have the tseries library.
adf.test and kpss.test gives us the hypothesis testing results for each of them.
adf.test(larain)
kpss.test(larain)
Notice the p-value in each test, in order to accept H0, the p-value has to be > 0.1
Notice how in the adf, we reject H0 (p-value<0.1) and conclude that the process is
stationary.
However, in kpss, we accept H0 (p-value=0.1) and also conclude that the process is
stationary.
Chapter 6: Estimation
Statistical functions:
mean(dataset_name) – gives us the mean of the dataset
var(dataset_name) – gives us the name of the dataset
In general, we can fit AR, MA, ARMA and ARIMA models using the arima() function.
in the order argument, we specify our model parameters like we specify ARIMA(p,d,q)
model. In the above example, it fits our dataset in an AR(2) model.
However, if we have order=c(1,1,1), it would fit our model in an ARIMA(1,1,1) model
c(0,0,2) would be an MA(2)/ c(1,0,1) would be an ARMA(1,1) and etc…
To select the best model from the grid, the lower the BIC (the darker the square), the
better. (the 1st row represents the lowest BIC for each term -> the best model to
consider)
We can also consider the other rows as long as we have dark squares, we can consider
the 2nd and 3rd rows here and make comparisons.
Chapter 7: Forecasting
The n.ahead=20 parameter indicates that we want to forecast the next 20 values (from
L= 1 to L=20). the results of this function will contain the forecast values as well as the
standard error of the predictions:
the first value represents 𝑌̂𝑡 (1) , the second 𝑌̂𝑡 (2) etc.. until 𝑌̂𝑡 (20)
Respectively, each value represents the standard error of each forecast value.
To represent the