[R] loop

Erik Iverson eriki at ccbr.umn.edu
Wed Oct 13 20:53:28 CEST 2010


Julia,

Can you provide a reproducible example? Your code calls the
'rq' function which is not found on my system.

Any paring down of the code to make it more readable would
help us help you better, too.


Julia Lira wrote:
> Dear all,
> 
>  
> 
> I am trying to run a loop in my codes, but the software returns an error: "subscript out of bounds"
> 
>  
> 
> I dont understand exactly why this is happenning. My codes are the following:
> 
>  
> 
> rm(list=ls()) #remove almost everything in the memory
> 
> set.seed(180185)
> nsim <- 10
> mresultx <- matrix(-99, nrow=1000, ncol=nsim)
> mresultb <- matrix(-99, nrow=1000, ncol=nsim)
> N <- 200
> I <- 5
> taus <- c(0.480:0.520)
> h <- c(1:20/1000)
> codd <- c(1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59,61,63,65,67,69,71,73,75,77,79,81)
> ceven <- c(2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82)
> cevenl <- c(2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40)
> #Create an object to hold results.
> M <- matrix(0, ncol=82, nrow=nsim)
> Mhb0 <- matrix(0, ncol=20, nrow=nsim)
> Mhb1 <- matrix(0, ncol=20, nrow=nsim)
> Mchb0 <- matrix(0, ncol=20, nrow=nsim)
> Mchb1 <- matrix(0, ncol=20, nrow=nsim)
> for (i in 1:nsim){
> # make a matrix with 5 cols of N random uniform values
> u <- replicate( 5, runif(N, 0, 1) )
> # fit matrix u in another matrix of 1 column
> mu <- matrix(u, nrow=1000, ncol=1)
> # make auction-specific covariate
> x <- runif(N, 0, 1)
> mx <- matrix(rep(x,5), nrow=1000, ncol=1)
> b0 <- matrix(rep(c(1),1000), nrow=1000, ncol=1)
> #function for private cost
> cost <- b0+b0*mx+mu
> #bidding strategy
> bid <- mx+((I+1)/I)+((I-1)/I)*mu
> mresultb[,i] <- bid
> mresultx[,i] <- mx
> qf <- rq(formula = mresultb[,i] ~ mresultx[,i], tau= 480:520/1000)
> # Storing result and does not overwrite prior values
> M[i, ] <- coef(qf)
> QI <- (1-0.5)/(I-1)
> M50b0 <- M[,41]
> M50b1 <- M[,42] 
> Mb0 <- matrix(M[,codd], nrow=nsim, ncol=20)
> Mb1 <- matrix(M[,ceven], nrow=nsim, ncol=20)
> for (t in cevenl){
> Mhb0[ ,t] <- M[,(41+t)]-M[,(41-t)]
> Mhb1[ ,t] <- M[,(42+t)]-M[,(42-t)]
> }
> }
> 
>  
> 
> Problem: the problem is in the red part of the loop. I want that the software takes the column (41+t) from the matrix called M and subtract from it the cloumn (41-t) of the same matrix M, such that the value of t varies according to the vector cevenl above.
> 
>  
> 
> Why is this looping not working?
> 
>  
> 
> Thanks in advance!!!
> 
>  
> 
> Julia
>  		 	   		  
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.



More information about the R-help mailing list