Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Time Series Lab Adapted from Time Series Primer RH Shumway and DS Stoffer."— Presentation transcript:

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

2 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 3 rd quarter of 1950 (mydata = ts(mydata,start=c(1950,3),frequency=4))

3 # disply the sampled times time(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 series x=ts(1:5) #now add two lagged columns: 2 nd col shift time base back, 3 rd col shift forward cbind(x,lag(x),lag(x,-1)) # find the intersection covered by all series ts.intersect(x, lag(x,1), lag(x, -1))

4 #difference a time series, i.e., diff(x) #Note diff(x,2) is not 2nd order differencing #2 nd order differencing is given by diff(diff(x)) #similarly for higher order differencing

5 #create a series x=-5:5 # and another series y=5*cos(x) #set up the plotting window for 3 rows 2 cols op=par(mfrow=c(3,2)) #plot x by itself plot(x,main="plot(x)")

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

7 #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)

8 #fix the seed so we all get the same results set.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))

9 #display x and y plot(x,y) #add a fitted line to the plot abline(fit) #show the residuals resid(fit) #show the fitted values fitted(fit) #exclude the intercept lm(y~0 + x)

10 #load data sets cmort – cardiovascular mortaility and #part – particulate pollution data(cmort, part) #find the time series intersection with cmort, part, and part lagged # 4 weeks  gets rid of nonoverlaping sectons of part & part4 ded = ts.intersect(cmort,part,part4=lag(part,-4), dframe=TRUE) # perform regression on time series data  fits part & part4 fit = lm(cmort~part+part4, data=ded, na.action=NULL) #display summary statistics summary(fit)

11 #load Johnson&Johnson quarterly earnings data(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)

12 #display the model matrix model.matrix(reg) #view results summary(reg)

13 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)

14 Example: fit ARMA(1,1) #we want reproduceable results set.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)

15 #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) model sarima.for(x,10,1,0,1)


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

Similar presentations


Ads by Google