0% found this document useful (0 votes)
12 views8 pages

TSA R Summary

The document provides an overview of using R for time series analysis, detailing the installation and usage of TSA and tseries libraries, as well as various datasets. It covers simulation, plotting, data transformations, model specification, estimation, and forecasting techniques in time series analysis. Key functions and their applications, such as arima.sim, plot, adf.test, and predict, are explained with examples.

Uploaded by

asmajradi8b6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views8 pages

TSA R Summary

The document provides an overview of using R for time series analysis, detailing the installation and usage of TSA and tseries libraries, as well as various datasets. It covers simulation, plotting, data transformations, model specification, estimation, and forecasting techniques in time series analysis. Key functions and their applications, such as arima.sim, plot, adf.test, and predict, are explained with examples.

Uploaded by

asmajradi8b6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

TSA BA320

R Summary

General:

Libraries and datasets:


Throughout the course we used TSA and tseries libraries in R to perform time series
analysis, these libraries contain datasets and specific functions.
To install them, we use install.packages(package_name) function.
Once installed, we import them into the program with library(package_name) function
(e.g: library(tseries)).

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

To use these 2 datasets, you can import them this way:

data= ts(read.table(file= paste_link_here),start=1807)

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)

order=c(2,0,1) represents that we want to simulate an ARIMA(2,0,1) model which is an


ARMA(2,1) model in this case, since d=0.
ar=c(0.5,-0.25) represents the coefficients of the AR(2) part of the model
ma=c(0.7) represents the coefficients of the MA(1) part of the model
n is the number of observations we want to simulate (generate 100 random
observations)

if we want to simulate AR(1) or MA(1), we specify order=c(1,0,0) or order=c(0,0,1),


respectively, then we either use the ar or the ma argument only.

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.

if we want to plot a time series, we use the plot function:


plot(data, main=”Main title”, xlab=”x label here”, ylab=”y label here”)
we specify the dataset to plot, and the labels to display.

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) represents the first difference of our data

diff(data, lag=2) yields second difference of our data, notice we specified lag=2.

plot(log(data), main=”Main title”, xlab=”x label here”, ylab=”y label here”)


plot(diff(data), main=”Main title”, xlab=”x label here”, ylab=”y label here”)
plot(diff(data,lag=2), main=”Main title”, xlab=”x label here”, ylab=”y label here”)

Boxcox.ar(data) specifies which power transformation to use (determines lambda).

Chapter 5: Model Specification


Assume the following simulation of an AR(2):

set.seed(140) # to obtain similar results


ar2= arima.sim( list(order=c(2,0,0), ar=c(0.5,-0.25)), n=100)

find the ACF and PACF of the series:

acf(ar2, main=”AR(1) ACF plot”)


pacf(ar2, main=”AR(2) PACF plot”)

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.

round(ARMAacf(ar=c(0.7), lag.max=5), digits=3)

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.

round(ARMAacf(ar=c(0.7), lag.max=5, pacf= TRUE), digits=3)

we specify pacf= TRUE after the lag.max

Find the Extended ACF function:


The eacf function gives us the extended ACF representation for a certain dataset,
assume the larain dataset found in the TSA library, we represent its EACF on the log-
transformed version of the larain values:

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

Model parameters estimation:

ar(dataset_name, order.max= 1, AIC=F, method=”yw”) – try to fit an AR(1) (order.max=1)


model to our “dataset_name” using the methods of moments (method=”yw”, change to
“mle” for maximum likelihood, “ols” for least squares)
notes: to fit an AR(2), set order.max=2

In general, we can fit AR, MA, ARMA and ARIMA models using the arima() function.

arima(dataset_name , order=c(2,0,0), method=”CSS” ) # least squares


arima(dataset_name , order=c(2,0,0), method=”ML” ) # maximum likelihood

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…

The model in this case: 𝑌𝑡 = (1 − 0.8870)𝑌𝑡−1 + 𝑒𝑡 − 𝑒𝑡−1


Arima(1,1,1) with Φ = −0.8870 𝑎𝑛𝑑 Θ = 1
Note: arima() only estimates using maximum likelihood and least squares, the method
of moments could only used in ar() function. ( as far as chatgpt and gemini said, there
isn’t a popular function that does the estimation for MA or ARMA with method of
moments).
armasubsets() tries different combinations of arma models, plotting its output returns
the BIC criterion grid.

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

to obtain forecast values for a specific series, we use predict() function

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

You might also like