
Dear Eriki and all

 

To run Quantile regression, it is necessary to install the following package in R:

 

install.packages("quantreg")

 

Then, write:

 

library(quantreg)

 

And the software will run.

 

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)
alpha1 <- c(1:82)
aeven1 <- alpha1[2 * 1:41]
aodd1 <- alpha1[-2 * 1:41]
alpha2 <- c(1:40)
aeven2 <- alpha2[2 * 1:20]
#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[,aodd1], nrow=nsim, ncol=20)
Mb1 <- matrix(M[,aeven1], nrow=nsim, ncol=20)
 for (t in aeven2){
 Mhb0[,t] <- M[,(41+t)]-M[,(41-t)]
 Mhb1[,t] <- M[,(42+t)]-M[,(42-t)]
 }
}

 

Here, the matrix M stores the result for all quantiles (by column) considering each simulation i (by row).

Then, I try to make some algebric simulations. I need to calculate Mhb0 such that for each value of t, the column called t in matrix Mhb0 will be the result of the subtraction of column (41-t) from the column (41+t) of the matrix M. To be more precise:

 

for t = 2, 4, 6,...

 

Mhb0 will be a matrix such that in the first column I will have {column (41+t) of M - column (41-t) of M}

 This should be the loop, and I would have 20 column for all the even number from 2 to 82 (specified in aeven2).

 

Is that clear now? But the software says: "Error in Mhb0[, t] <- M[, (41 + t)] - M[, (41 - t)] :   subscript out of bounds"

 

What am I doing wrong?

 

Thanks a lot!

 

Julia


 
> Date: Wed, 13 Oct 2010 13:53:28 -0500
> From: eriki@ccbr.umn.edu
> To: julia.lira@hotmail.co.uk
> CC: r-help@r-project.org
> Subject: Re: [R] loop
> 
> 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@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]]

