[R] Working with "ts" objects
ggrothendieck at gmail.com
Thu Dec 6 05:07:37 CET 2007
anscombe is built into R already so you don't need to read it in.
An intercept is the default in lm so you don't have to specify it.
opar <- par(mfrow = c(2,2))
plot(y1 ~ x1, anscombe)
reg <- lm(y1 ~ x1, anscombe)
Note that plot(anscombe[1:2]) and lm(anscombe[2:1]) also work.
read.table returns a data frame whereas ts requires a vector
or matrix so none of your ts code will work. as.matrix(DF)
or data.matrix(DF) will convert data frame DF to a matrix.
On Dec 5, 2007 2:30 PM, Richard Saba <sabaric at auburn.edu> wrote:
> 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.
> Richard Saba
> Department of Economics
> Auburn University
> Email: sabaric at auburn.edu
> Phone: 334 844-2922
> anscombe<-read.table(fname, header=TRUE)
> 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)
> tab6.1<-ts(read.table(fname, header=TRUE),frequency=12,start=c(1946,1))
> reg1<-lm(tab6.1~1+factor(month),data=dat2, na.action=NULL)
> lines(plot1[,2], col="red")
> plot.ts(plot[,3], ylab="Residuals")
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help