Time Series Lab Adapted from Time Series Primer RH Shumway and DS Stoffer.

15
Time Series Lab Adapted from Time Series Primer RH Shumway and DS Stoffer

Transcript of Time Series Lab Adapted from Time Series Primer RH Shumway and DS Stoffer.

Page 1: Time Series Lab Adapted from Time Series Primer RH Shumway and DS Stoffer.

Time Series Lab

Adapted from Time Series PrimerRH Shumway and DS Stoffer

Page 2: Time Series Lab Adapted from Time Series Primer RH Shumway and DS Stoffer.

Crawl before walking

# toy data set(mydata = c(1,2,3,2,1))

#convert to time series(mydata = as.ts(mydata))

#start the series in 1950(mydata = ts(mydata,start=1950))

#quarterly series starting in the 3rd quarter of 1950(mydata = ts(mydata,start=c(1950,3),frequency=4))

Page 3: Time Series Lab Adapted from Time Series Primer RH Shumway and DS Stoffer.

# disply the sampled timestime(mydata)

# use window() to get part of the series object(x=window(mydata,start=c(1951,1),end=c(1951,3)))

#create a new time seriesx=ts(1:5)

#now add two lagged columns: 2nd col shift time base back, 3rd col shift forwardcbind(x,lag(x),lag(x,-1))

# find the intersection covered by all series ts.intersect(x, lag(x,1), lag(x, -1))

Page 4: Time Series Lab Adapted from Time Series Primer RH Shumway and DS Stoffer.

#difference a time series, i.e.,diff(x)

#Note diff(x,2) is not 2nd order differencing#2nd order differencing is given by diff(diff(x))

#similarly for higher order differencing

Page 5: Time Series Lab Adapted from Time Series Primer RH Shumway and DS Stoffer.

#create a seriesx=-5:5

# and another seriesy=5*cos(x)

#set up the plotting window for 3 rows 2 colsop=par(mfrow=c(3,2))

#plot x by itself plot(x,main="plot(x)")

Page 6: Time Series Lab Adapted from Time Series Primer RH Shumway and DS Stoffer.

#plot x and y plot(x,y,main="plot(x,y)")

#plot time series component xplot.ts(x,main="plot.ts(x)")

#plot time series component x and y plot.ts(x,y,main="plot.ts(x,y)")

Page 7: Time Series Lab Adapted from Time Series Primer RH Shumway and DS Stoffer.

#time series plot function on x ts.plot(x,main="ts.plot(x)")

#time series plot function on x & y ts.plot(ts(x),ts(y),col=1:2,main="ts.plot(x,y)")

#reset the graphical paramters par(op)

Page 8: Time Series Lab Adapted from Time Series Primer RH Shumway and DS Stoffer.

#fix the seed so we all get the same resultsset.seed(1999)

#sample 10 points normally distributed w mu=0 and sd=1 x = rnorm(10,0,1)

#sample another 10 points & sum with x y=x+rnorm(10,0,1)

#get summary statistics summary(fit <- lm(y~x))

Page 9: Time Series Lab Adapted from Time Series Primer RH Shumway and DS Stoffer.

#display x and yplot(x,y)

#add a fitted line to the plot abline(fit)

#show the residualsresid(fit)

#show the fitted valuesfitted(fit)

#exclude the interceptlm(y~0 + x)

Page 10: Time Series Lab Adapted from Time Series Primer RH Shumway and DS Stoffer.

#load data sets cmort – cardiovascular mortaility and #part – particulate pollutiondata(cmort, part)

#find the time series intersection with cmort, part, and part lagged# 4 weeks gets rid of nonoverlaping sectons of part & part4ded = ts.intersect(cmort,part,part4=lag(part,-4), dframe=TRUE)

# perform regression on time series data fits part & part4fit = lm(cmort~part+part4, data=ded, na.action=NULL)

#display summary statisticssummary(fit)

Page 11: Time Series Lab Adapted from Time Series Primer RH Shumway and DS Stoffer.

#load Johnson&Johnson quarterly earningsdata(jj)

#create a line spanning the time of jj data trend = time(jj)-1970

#Create an array of factors corresponding to quarters Q = factor(rep(1:4,21))

# perform a regression without intercept reg = lm(log(jj)~0 + trend + Q, na.action=NULL)

Page 12: Time Series Lab Adapted from Time Series Primer RH Shumway and DS Stoffer.

#display the model matrix model.matrix(reg)

#view results summary(reg)

Page 13: Time Series Lab Adapted from Time Series Primer RH Shumway and DS Stoffer.

Examples of ARIMA simulations

#AR(1) with mean 50 x = arima.sim(list(order=c(1,0,0),ar=.9),n=100)+50 plot(x)

#AR(2) x = arima.sim(list(order=c(2,0,0),ar=c(1,-.9)),n=100) plot(x)

#ARIMA(1,1,1)x = arima.sim(list(order=c(1,1,1),ar=.9,ma=-.5),n=200)plot(x)

Page 14: Time Series Lab Adapted from Time Series Primer RH Shumway and DS Stoffer.

Example: fit ARMA(1,1)

#we want reproduceable resultsset.seed(666)

# run simulation for 200 points x = 50 + arima.sim(list(order=c(1,0,1),ar=.9, ma=-.5),n=200)

# display autocorrelation function & partial autocorrelation function acf(x); pacf(x)

#or use this function to plot both acf2(x)

Page 15: Time Series Lab Adapted from Time Series Primer RH Shumway and DS Stoffer.

#fit to model(x.fit = arima(x,order=c(1,0,1)))

#fit the ARIMA model assuming ARIMA(1,0,1)sarima(x,1,0,1)

# finally forecast using x and an ARIMA(1,0,1) modelsarima.for(x,10,1,0,1)