[R] Working with "ts" objects
Richard Saba
sabaric at auburn.edu
Wed Dec 5 20:30:34 CET 2007
I am relatively new to R and object oriented programming. I have relied on
SAS for most of my data analysis. I teach an introductory undergraduate
forecasting course using the Diebold text and I am considering using R in
addition to SAS and Eviews in the course. I work primarily with univariate
or multivariate time series data. I am having a great deal of difficulty
understanding and working with "ts" objects particularly when it comes to
referencing variables in plot commands or in formulas. The confusion is
amplified when certain procedures (lm for example) coerce the "ts" object
into a data.frame before application with the results that the output is
stored in a data.frame object.
For example the two sets of code below replicate examples from chapter 2 and
6 in the text. In the first set of code if I were to replace
"anscombe<-read.table(fname, header=TRUE)" with
"anscombe<-ts(read.table(fname, header=TRUE))" the plot() commands would
generate errors. The objects "x1", "y1" ... would not be recognized. In
this case I would have to reference the specific column in the anscombe data
set. If I would have constructed the data set from several different data
sets using the ts.intersect() function (see second code below)the problem
becomes even more involved and keeping track of which columns are associated
with which variables can be rather daunting. All I wanted was to plot actual
vs. predicted values of "hstarts" and the residuals from the model.
Given the difficulties I have encountered I know my students will have
similar problems. Is there a source other than the basic R manuals that I
can consult and recommend to my students that will help get a handle on
working with time series objects? I found the Shumway "Time series analysis
and its applications with R Examples" website very helpful but many
practical questions involving manipulation of time series data still remain.
Any help will be appreciated.
Thanks,
Richard Saba
Department of Economics
Auburn University
Email: sabaric at auburn.edu
Phone: 334 844-2922
anscombe<-read.table(fname, header=TRUE)
names(anscombe)<-c("x1","y1","x2","y2","x3","y3","x4","y4")
reg1<-lm(y1~1 + x1, data=anscombe)
reg2<-lm(y2~1 + x2, data=anscombe)
reg3<-lm(y3~1 + x3, data=anscombe)
reg4<-lm(y4~1 + x4, data=anscombe)
summary(reg1)
summary(reg2)
summary(reg3)
summary(reg4)
par(mfrow=c(2,2))
plot(x1,y1)
abline(reg1)
plot(x2,y2)
abline(reg2)
plot(x3,y3)
abline(reg3)
plot(x4,y4)
abline(reg4)
..........................................................................
fname<-file.choose()
tab6.1<-ts(read.table(fname, header=TRUE),frequency=12,start=c(1946,1))
month<-cycle(tab6.1)
year<-floor(time(tab6.1))
dat1<-ts.intersect(year,month,tab6.1)
dat2<-window(dat1,start=c(1946,1),end=c(1993,12))
reg1<-lm(tab6.1~1+factor(month),data=dat2, na.action=NULL)
summary(reg1)
hstarts<-dat2[,3]
plot1<-ts.intersect(hstarts,reg1$fitted.value,reg1$resid)
plot.ts(plot1[,1])
lines(plot1[,2], col="red")
plot.ts(plot[,3], ylab="Residuals")
More information about the R-help
mailing list