Skip to content

Instantly share code, notes, and snippets.

@idrissrasheed
Last active August 13, 2017 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idrissrasheed/9d532367b3626a2bbc463666dd6465e3 to your computer and use it in GitHub Desktop.
Save idrissrasheed/9d532367b3626a2bbc463666dd6465e3 to your computer and use it in GitHub Desktop.
Time Series HW assignments in R
library(tseries)
# Simulate Gaussian white noise, find the sample acf, and compare with theoretical acf
x <- rnorm(300, 0, 1)
# Calculate and plot sample acf
myacf = acf(x,plot=FALSE)
# Simulate from an MA(1) process, e.g. X_t = Z_t + .8 Z_{t-1}
y <- arima.sim(list(ma=1), n = 300, sd = 1)
acf(y)
#(b) Solve the given system of equations using R:
#(i) Create the matrix A and the vector b defined in part (a) in R. Hint: Look at the lab 1 files.
#(ii) Run �solve(A)� in R to calculate A-1
#(iii) Compute A-1b in R.> B = matrix( + c(2, 4, 3, 1, 5, 7), + nrow=3, + ncol=2) > B # B has 3 rows and 2 columns. ...
A <- matrix(c(6, 4, -2, 1, -1, 2, -2, 1, -1), #list of elements
nrow = 3, #3 rows
ncol = 3, #3 columns
byrow = TRUE) #compile by rows
b <- c(2,-1,0) #Set b as this vector elements
A_inv <- solve(A) #Compute the inverse matrix
x <- A_inv*b #Computes x
x
---
title: "Time Series Homework 2"
author: "Idris Rasheed"
date: "April 18, 2017"
output: word_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r Problem 1 - iii}
library(tseries)
ma1 <- arima.sim(model=list(ma=c(-0.5)), n=200)
plot(ma1)
acf(ma1, type="correlation", plot=T)
plot(0:20, ARMAacf(ar=c(0), ma=c(-0.5), lag.max=20), xlim=c(1,40),ylab="r",type="h", main="ACF for
MA(1) c=-0.5")
abline (h=0)
```
## Including Plots
You can also embed plots, for example:
```{r Problem 3ii}
ma2 <- arima.sim(model=list(ma=c(2,-8)), n=300)
plot(ma2)
acf(ma2, type="correlation", plot=T)
plot(0:20, ARMAacf(ar=c(0), ma=c(2,-8), lag.max=20), xlim=c(1,40),ylab="r",type="h", main="ACF for
MA(2) c=2,-8")
abline (h=0)
```
N
---
title: "PSTAT 174 HW 3"
author: "Idris Rasheed"
date: "April 26, 2017"
output: word_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
###Problem 6A
####i. 100 simulations
```{r 6Ai}
#Assigns model 1 to arima simulation with phi -.6 and theta = 1.2
model1 <- arima.sim(n = 100,model = list(ar = -0.6,ma = 1.2))
plot(model1, xlab = "t",ylab = expression(X[t]),main = expression(paste(ARMA(1,1),phi == -0.6,theta == 1.2,sep = " "))) #Plots time series
```
####ii. Theoritical ACF and PACF
```{r 6Aii}
#Computes and outputs ACF and PACF values
acf_model1 = ARMAacf(ar = -0.6,ma = 1.2,pacf = FALSE,lag.max = 10)
pacf_model1 = ARMAacf(ar = -0.6,ma = 1.2,pacf = TRUE,lag.max = 10)
acf_model1
pacf_model1
# Plot
graph <- par(mfrow = c(1,2)) #Combines plots
# Plots ACF
plot(x = 0:10,y = acf_model1,type = "h",col = "red",ylab = "ACF",xlab = "h")
abline(h = 0)
#Plots PACF from h = 0
plot(x = 1:10,y = pacf_model1,type = "h",col = "red",ylab = "PACF",xlab = "h")
abline(h = 0)
title(expression(paste("Theoritical ARMA(1,1)",phi == -0.6,theta == 1.2,sep = " ")),
line = -1, outer=TRUE)
#Plots ACF and PACF on one graph
par(graph)
```
####iii. Sample ACF and PACF
```{r 6Aiii}
#Combines plots
graph <- par(mfrow = c(1,2))
# Plots sample ACF
acf(model1,lag.max = 10,main = "")
# #Plots PACF from h = 0
pacf(model1,lag.max = 10,main = "")
title(expression(paste("Sample ARMA(1,1) ",phi == -0.6,theta == 1.2,sep = " ")),
line = -1, outer=TRUE)
#Plots sample ACF and PACF on one graph
par(graph)
```
####iv. Smoothness analysis
###6B
####i. 100 simulations
```{r 6bi}
#Assigns model 2 to arima simulation with phi(1) = -0.2 and phi(2) = 0.48
model2 <- arima.sim(n = 100,model = list(ar = c(-0.2,0.48)))
#Plots time series for model 2
plot(model2, xlab = "Time",ylab = expression(X[t]),main = expression(paste(AR(2),phi [1] == -0.2, phi [2] == 0.48,sep = " ")))
```
####ii. Theoritical ACF and PACF
```{r Problem 6Bii}
#Computes and outputs ACF and PACF values of Model 2
acf_model2 = ARMAacf(ar = c(-0.2, 0.48), pacf = FALSE,lag.max = 10)
pacf_model2 = ARMAacf(ar = c(-0.2, 0.48), pacf = TRUE,lag.max = 10)
acf_model2
pacf_model2
# Plot
graph <- par(mfrow = c(1,2)) #Combines plots
# Plots ACF
plot(x = 0:10,y = acf_model2,type = "h",col = "red",ylab = "ACF",xlab = "h")
abline(h = 0)
#Plots PACF from h = 0
plot(x = 1:10,y = pacf_model2,type = "h",col = "red",ylab = "PACF",xlab = "h")
abline(h = 0)
title(expression(paste("Theoritical AR(2)",phi [1] == -0.2, phi[2] == 0.48,sep = " ")),
line = -1, outer=TRUE)
#Plots ACF and PACF on one graph
par(graph)
```
####iii. Sample ACF and PACF
```{r 6Biii}
graph <- par(mfrow = c(1,2)) #Combines plots
# Plots sample ACF
acf(model2,lag.max = 10,main = "")
# #Plots PACF from h = 0
pacf(model2,lag.max = 10,main = "")
title(expression(paste("Sample AR(2): ",phi [1] == -0.2, phi [2] == 0.48,sep = " ")),
line = -1, outer=TRUE)
#Plots sample ACF and PACF on one graph
par(graph)
```
####iv. Smoothness analysis
---
title: "PSTAT 174 HW 4 R Code"
author: "Idris Rasheed"
date: "May 4, 2017"
output: word_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
###Problem 4
(a) Plot the time series and find its mean, variance, acf and pacf. Notice the presence of a strong seasonal component with period 12 in the graphs of the data and in the sample acf. Comment on all plots.
```{r Series Plot}
#Opens tools from tseries package
library(tseries)
#reads in deaths dataset
deaths = read.table("deaths.txt", sep = ",")
#Plot time series for deaths
ts.plot(deaths)
title(expression(paste("Series")), line = -1, outer=TRUE)
```
```{r ACF and PACF}
#Makes deaths txt a time series object
deaths_ts <- ts(deaths)
#combines plots
graph <- par(mfrow = c(1,2))
#Plots ACF for deaths data
acf(deaths_ts)
#Plots PACF
pacf(deaths_ts)
title(expression(paste("Deaths")), line = -1, outer=TRUE)
#Plots ACF and PACG sibe by side
par(graph)
```
```{r Mean}
#computes Mean
mean(deaths_ts)
```
```{r Variance}
#Computes Variance
var(deaths)
```
(b) Use differencing to deseasonalize and detrend the data. On each step include graphs of the
data, corresponding variance, acf and pacf, and comment on your decisions to difference (or not)
and at what lag.
```{r differencing 1}
#Differencing at lag 1
dif_lag_one <- diff(deaths_ts, lag = 1)
ts.plot(dif_lag_one ,main = "Differenced Deaths at lag = 1")
```
```{r Var of Dif 1}
var(dif_lag_one)
```
```{r differencing 1}
#combines plots
graph <- par(mfrow = c(1,2))
#Plots ACF for difference at lag 1
acf(dif_lag_one)
#Plots PACF for difference at lag 1
pacf(dif_lag_one)
title(expression(paste("Difference at Lag 1")), line = -1, outer=TRUE)
#Plots ACF and PACG sibe by side
par(graph)
```
```{r differencing 2}
#Differencing at lag 2
dif_lag_two <- diff(deaths_ts, lag = 2)
ts.plot(dif_lag_two ,main = "Differenced Deaths at lag = 2")
```
```{r Var of Dif 2}
var(dif_lag_two)
```
```{r differencing 2}
#combines plots
graph <- par(mfrow = c(1,2))
#Plots ACF for difference at lag 1
acf(dif_lag_two)
#Plots PACF for difference at lag 1
pacf(dif_lag_two)
title(expression(paste("Difference at Lag 2")), line = -1, outer=TRUE)
#Plots ACF and PACG sibe by side
par(graph)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment