Dear all, I am trying to set up a list with 1:c objects each meant to capture the coefficients for one coefficient and 100 replications. I receive the following error message: Error in betaboot[[p]] : subscript out of bounds. My code is below. Where is my mistake? Many thanks, Thomas _________________________________ betaboot<-list(NULL) for (i in 1:c) { betaboot[[i]]<-cbind() } num <- 100 # this is the number of bootstraps for (i in 1:num) { [BOOTSTRAP] coef.temp <- coef(model.temp, data=newdata) for (p in 1:c){ betaboot[[p]] <- cbind(betaboot[[p]], coef.temp[,p]) } }
Dear Thomas, On Tue, Apr 5, 2011 at 8:33 AM, Thomas <thomas.triebs at cantab.net> wrote:> Dear all, > > I am trying to set up a list with 1:c objects each meant to capture the > coefficients for one coefficient and 100 replications. I receive the > following error message: > > Error in betaboot[[p]] : subscript out of bounds. > > My code is below. Where is my mistake? > > Many thanks, > > Thomas > > _________________________________ > betaboot<-list(NULL)if you know the number of bootstraps (which you seem to later on), a preferred way to instatiate the list would be: betaboot <- vector(mode = "list", length = yourlength)> > for (i in 1:c) {because "c()" is such an important function, I would strongly encourage you not to use it also as a variable.> betaboot[[i]]<-cbind()Don't use this to build an empty list.> } > > > num <- 100 # this is the number of bootstraps > > for (i in 1:num) { > > ? ?[BOOTSTRAP] > > ?coef.temp <- coef(model.temp, data=newdata) > > ?for (p in 1:c){ > ?betaboot[[p]] <- cbind(betaboot[[p]], coef.temp[,p])This should work assuming betaboot is instatiated properly. That said, it looks like you have a nested for loop and then just keep cbind()ing each element of betaboot bigger and bigger. You may get a performance increase if you also instantiate each matrix/dataframe inside betaboot. Then the call would become something like: betaboot[[i]][,p] <- coef.temp[,p] that is, you can use a chained series of extraction operators to get to the appropriate column in the matrix/dataframe inside the appropriate list element. Then rather than constantly using cbind(), you just place coef.temp[,p] where you want it. The only requirement is that you know the sizes of the matrices/dataframes going in so you can create empty ones from the get go. Cheers, Josh> ?} > > ?} > > ______________________________________________ > 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.-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Dear all, I am trying to implement the following in a loop: g <- cbind(c(1, 2, 3), c(1, 2, 3)) h <- cbind(c(1, 2, 3), c(1, 2, 3)) c <- cbind(g[,1]*h[,1], g[,2]*h[,2]) g<-rowSums(c) My attempt looks like this but does not produce the desired results as above. for (i in 1:2) {g<- rowSums(cbind(g[,i]*h[,i]))} How do I do this correctly? Many thanks, Thomas
Many thanks for all the quick replies... On 14/04/2011 13:37, Hao Wu wrote:> you can use this instead, apply(g*h,1,sum) > > On Thu, Apr 14, 2011 at 7:28 PM, Thomas <thomas.triebs@cantab.net > <mailto:thomas.triebs@cantab.net>> wrote: > > Dear all, > > I am trying to implement the following in a loop: > g <- cbind(c(1, 2, 3), c(1, 2, 3)) > h <- cbind(c(1, 2, 3), c(1, 2, 3)) > c <- cbind(g[,1]*h[,1], g[,2]*h[,2]) > g<-rowSums(c) > > My attempt looks like this but does not produce the desired > results as above. > > for (i in 1:2) {g<- rowSums(cbind(g[,i]*h[,i]))} > > How do I do this correctly? > > Many thanks, > > Thomas > > ______________________________________________ > R-help@r-project.org <mailto:R-help@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. > >[[alternative HTML version deleted]]