Hello, I am a new user of R. I am trying to create a vector of time series called "LTot" from a data frame. The purpose is to call LTot element from a loop to make multiple regressions over a large number of regressors. Here a piece of R code OGData <- read.table("heartatk4R.txt", header=TRUE) OGCol <- ncol(OGData) OGRow <- nrow(OGData) S1.ts=ts(OGData[,1]) # Definition of the time series vector LTot=matrix(,nrow=OGCol) # assignment of each column of the data frame to an element of the time series vector for (i in 1:OGCol) {LTot[1:OGRow,i] <- ts(OGData[,i]);} When I run R, I got the following error message: Error in LTot[1:OGRow, i] <- ts(OGData[, i]) : subscript out of bounds I will appreciate it if somebody could help me on this issue. Thank you. X. -- View this message in context: http://r.789695.n4.nabble.com/Creation-of-a-vector-of-time-series-from-a-data-frame-tp4638543.html Sent from the R help mailing list archive at Nabble.com.
R. Michael Weylandt
2012-Jul-31 17:36 UTC
[R] Creation of a vector of time series from a data frame
Err... some fundamental problems here. On Tue, Jul 31, 2012 at 9:57 AM, monthodon <foussereauxj at hotmail.com> wrote:> Hello, > > I am a new user of R. I am trying to create a vector of time series called > "LTot" from a data frame. The purpose is to call LTot element from a loop > to make multiple regressions over a large number of regressors.This might be dangerous -- running multiple multiple-regressions seems like a way to get spurious p-values. Just be careful.>Here a piece of R code > > OGData <- read.table("heartatk4R.txt", header=TRUE) > OGCol <- ncol(OGData) > OGRow <- nrow(OGData) > S1.ts=ts(OGData[,1]) > # Definition of the time series vector"Time series vector" doesn't exist. A vector is a collection of "atomic" elements (strings, doubles = real numbers, integers, or a few others) -- e.g., x <- c(1,2,3) y <- c("A","B","C") are vectors. You can also have a "generic" vector (or list) but that's more advanced. Time series, however, cannot be elements of a vector since they are themselves vectors with some added properties.> LTot=matrix(,nrow=OGCol)This line makes a matrix (of one column) not a vector, as the name suggests. Somewhat technical note: Note however that a "matrix" is really just a vector to which we have added an additional attribute of dimensionality. Just like a time series is a vector to which we have added the additional detail of "time". I think you would be much better served to read some of the introductory material that comes with your R distribution, and then to look at time series material in particular. http://a-little-book-of-r-for-time-series.readthedocs.org/en/latest/index.html seems like a good place to start. Once you've got time series under your feet, look at the dynlm package which does these sorts of things. Best, Michael> # assignment of each column of the data frame to an element of the time > series vector > for (i in 1:OGCol) {LTot[1:OGRow,i] <- ts(OGData[,i]);} > > When I run R, I got the following error message: > > Error in LTot[1:OGRow, i] <- ts(OGData[, i]) : subscript out of bounds > > I will appreciate it if somebody could help me on this issue. > > > Thank you. > > X. > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Creation-of-a-vector-of-time-series-from-a-data-frame-tp4638543.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
William Dunlap
2012-Jul-31 19:28 UTC
[R] Creation of a vector of time series from a data frame
See in-line comments below. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of monthodon > Sent: Tuesday, July 31, 2012 7:57 AM > To: r-help at r-project.org > Subject: [R] Creation of a vector of time series from a data frame > > Hello, > > I am a new user of R. I am trying to create a vector of time series called > "LTot" from a data frame. The purpose is to call LTot element from a loop > to make multiple regressions over a large number of regressors. Here a piece > of R code > > OGData <- read.table("heartatk4R.txt", header=TRUE) > OGCol <- ncol(OGData) > OGRow <- nrow(OGData) > S1.ts=ts(OGData[,1]) > # Definition of the time series vector > LTot=matrix(,nrow=OGCol) > # assignment of each column of the data frame to an element of the time > series vector > for (i in 1:OGCol) {LTot[1:OGRow,i] <- ts(OGData[,i]);} > > When I run R, I got the following error message: > > Error in LTot[1:OGRow, i] <- ts(OGData[, i]) : subscript out of bounds > > I will appreciate it if somebody could help me on this issue.When you see 'subscript out of bounds', suspect that the subscript operator, "[", is the problem. There are two calls to "[" in the line quoted in the error message: look at the dimensions of the two objects being subscripted with dim(LTot) and dim(OGData). If those numbers look bad, figure out why the object doesn't have the expected dimensions. If those numbers look ok, change your loop a bit to print the current value of 'i' so you can tell what it was when the error occurred. If you still cannot see the problem, break up the statement in the loop so the error can occur only on a line with one call to "[" (so the error message tells you which is at fault). for(i in 1:OGCol) { cat("i=", i, "\n") tmp <- ts(OGData[,i] LTot[1:OGrow,i] <- tmp } There are more advanced methods for debugging (e.g., set options(error=recover)) but adding print statements can quickly solve a lot of debugging problems.> > > Thank you. > > X. > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Creation-of-a-vector-of- > time-series-from-a-data-frame-tp4638543.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.