[R] storing data from loops

From: Cecilia Pinto <pinto.cecilia85_at_gmail.com>
Date: Mon, 23 May 2011 17:23:13 +0200


Dear R-help list,
I have a problem storing results from a bootstrap loop. What I doing is creating an empty matrix before the loop, then run the loop. Before the end of the loop I have a command line which defines which results inside the loop will have to go in the empty matrix. Here is the code I am using:

eigen.values<-rep(NA,boots)
#storage file for eigen values (1 for simulation)
ssd.values<-matrix(NA,nrow=boots,ncol=9, byrow=TRUE) #storage file for ssd (9 for each simulation)
for(i in
1:boots){
#beginning of bootstrap

B<-A
s<-rbeta(1,a,b)
#survival is sampled from beta distribution defined by a and b
B[2,1]<-s
B[3,2]<-s
#position of matrix where to substitute survival values

B[4,3]<-s
B[5,4]<-s
B[6,5]<-s
B[7,6]<-s
B[8,7]<-s
B[9,8]<-s
B[9,9]<-s
B[1,5]<-s*B[1,5]

#positions where to substitute fecundity values
B[1,6]<-s*B[1,6]
B[1,7]<-s*B[1,7]
B[1,8]<-s*B[1,8]
B[1,9]<-s*B[1,9]

eigs.B<-eigen(B)
#eigs.B
#obtain $values(growth rate) and $vectors(stable stage dist)
dom.pos<-which.max(eigs.B[["values"]])
#extract the dominant eigen value

L1<-Re(eigs.B[["values"]][dom.pos])
#extract only the real part of the eigenvalue (Re)
#L1
###ssd

dom.pos2<-which.max(eigs.B[["vectors"]])
#create index for dominant eigenvector

w<-Re(eigs.B[["vectors"]][,dom.pos2])
#extracting the real part of the eigenvector (Re)
ssd<-w/sum(w)

eigen.values[i]<-L1
#storing values from each iteration

ssd.values[i,]<-ssd

#rv.values[i]<-RV

}

The red bits are the one giving me problems. What I should get writing ssd.values after the loop is ended is a matrix(100,9) storing in each row 9 values obtained by 1 run of the loop (each row should be different being the output of a different simulation) of ssd<-w/sum(w). Unfortunately I get the same 9 values of the first row in the subsequent 99. I have tried to change the code of the last line ssd.values[i,]<-ssd many times but I only manged to obtain an incomplete matrix or a single value repeated 100 times. Can you help me?
Thanks a lot, Cecilia.

        [[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. Received on Mon 23 May 2011 - 17:36:45 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 23 May 2011 - 17:40:08 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive