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]]
Dear all,
I have just sent an email with my problem, but I think no one can see the red
part, beacuse it is black. So, i am writing again the codes:
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)]
}
}
The problem is in the part:
for (t in aeven2){
Mhb0[,t] <- M[,(41+t)]-M[,(41-t)]
Mhb1[,t] <- M[,(42+t)]-M[,(42-t)]
}
Since I want the software to subtract from column (41+t) of matrix called M the
column (41-t), in such a way that the matrix Mhb0 will show me the result for
each t organized by columns.
Does anybody know what exactly I am doing wrong?
Thanks in advance!
Julia
[[alternative HTML version deleted]]
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.
Hi all, I managed to do what i want (with the great help of thi mailing list) manually . Now i would like to automate it. I would probably need a for loop for to help me with this...but of course I have no idea how to do that in R. Bellow is the code that i would like to be replicated for a number of times (let say 20). I would like to achieve that w1 would change to w2, w3, w4 ... up to w20 and by that create 20 data.frames that I would than bind together with cbind. (i did it like shown bellow -manually) w1<-table(lit$W1) w1<-as.data.frame(w1) write.table(w1,file="w1.csv",sep=";",row.names=T, dec=".") w1 <- w1[order(w1$Freq, decreasing=TRUE),] w1<-head(w1, 20) w2<-table(lit$W2) w2<-as.data.frame(w2) write.table(w2,file="w2.csv",sep=";",row.names=T, dec=".") w2 <- w2[order(w2$Freq, decreasing=TRUE),] w2<-head(w2, 20) . . . Thanks for the help,m [[alternative HTML version deleted]]
Hi,
Thanks for the help and the manuals. Will come very handy i am sure.
But regarding the code i don't hink this is what i want....basically i would
like to repeat bellow code :
w1<-table(lit$W1)
w1<-as.data.frame(w1)
write.table(w1,file="w1.csv",sep=";",row.names=T,
dec=".")
w1<- w1[order(w1$Freq, decreasing=TRUE),] w1<-head(w1, 20)
20 times, where W1-20 (capital letters) are the fields in a data.frame called
"lit" and w1-20 are the data.frames being created.
Hope that explains it better,
m
-----Original Message-----
From: Patrick Burns [mailto:pburns at pburns.seanet.com]
Sent: Wednesday, November 03, 2010 9:30 PM
To: Matev? Pavli?
Subject: Re: [R] Loop
If I understand properly, you'll want
something like:
lit[["w2"]]
instead of
lit$w2
more accurately:
for(i in 1:20) {
vari <- paste("w", i)
lit[[vari]]
...
}
The two documents mentioned in my
signature may help you.
On 03/11/2010 20:23, Matev? Pavli? wrote:> Hi all,
>
>
>
> I managed to do what i want (with the great help of thi mailing list)
manually . Now i would like to automate it. I would probably need a for loop for
to help me with this...but of course I have no idea how to do that in R.
Bellow is the code that i would like to be replicated for a number of times (let
say 20). I would like to achieve that w1 would change to w2, w3, w4 ... up to
w20 and by that create 20 data.frames that I would than bind together with
cbind.
>
>
>
> (i did it like shown bellow -manually)
>
>
>
> w1<-table(lit$W1)
>
> w1<-as.data.frame(w1)
>
> write.table(w1,file="w1.csv",sep=";",row.names=T,
dec=".")
>
> w1<- w1[order(w1$Freq, decreasing=TRUE),]
>
> w1<-head(w1, 20)
>
>
>
>
>
> w2<-table(lit$W2)
>
> w2<-as.data.frame(w2)
>
> write.table(w2,file="w2.csv",sep=";",row.names=T,
dec=".")
>
> w2<- w2[order(w2$Freq, decreasing=TRUE),]
>
> w2<-head(w2, 20)
>
>
>
> .
>
> .
>
> .
>
> Thanks for the help,m
>
>
>
>
>
>
>
>
> [[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.
>
--
Patrick Burns
pburns at pburns.seanet.com
http://www.portfolioprobe.com/blog
http://www.burns-stat.com
(home of 'Some hints for the R beginner'
and 'The R Inferno')