[R] Odp: Loop overwrite and data output problems

Petr PIKAL petr.pikal at precheza.cz
Wed Mar 3 17:01:55 CET 2010


Hi

r-help-bounces at r-project.org napsal dne 01.03.2010 16:36:27:

> 
> Hi Petr,
> 
> Thanks again for trying again with these data, I really appreciate it.
> 
> Your script works perfectly, but the problem I'm having is how to store 
the
> model results so after your script I would do: 
> 
>       m1.R<-glm(cbind(res$BEH_T, res$BEH_F) ~ res$SITE + res$YEAR +
> res$PRECIP_MM_DAY + res$PUP_AGE_EST + res$MO_AIR_TEMP, 
family="binomial")
>       mod<-dredge(m1.R)
> 
> where mod is a list not a vector. 
> 
> So your example has 10 iterations of the loop so there should therefore 
be
> 10 different mod[1,] that I want to store and that is what I can't work 
out
> how to do, for example I can do this:
> 
>          if (i>=1) print (mod[1,]) else print ("NO")}
> 
> And I will get a print of each of the 10 model outputs that I want, but 
I
> want to store these somewhere. I did try to adjust your value <- matrix
> section of the script but had no luck.
> 
> I hope this is a little clearer?

Well, I am still a bit unsure what is your problem. You used slower way 
but i believe you should use quicker one as you want to cycle it 100 times

Assuming your data are named data

dat.o<-data[order(data$ID2),]
len<-rle(dat.o$ID2)$lengths
shift.len<-c(0,cumsum(len))[-(length(len)+1)]

# declare object for storing your results (the structure depends on 
structure of the result)
mod<- vector(10, mode="list")

for(i in 1:10) {
samp<-sapply(lapply(split(dat.o$ID2, dat.o$ID2), function(x) 1:length(x)), 
sample, 1)
Sample.dat <- dat.o[shift.len+samp,]


# do any of your stuff here (put Sample.dat instead of res)

m1.R<-glm(cbind(res$BEH_T, res$BEH_F) ~ res$SITE + res$YEAR + 
res$PRECIP_MM_DAY + res$PUP_AGE_EST + res$MO_AIR_TEMP, family="binomial")
mod[[i]]<-dredge(m1.R)

}

then you shall get a list with 10 components each one for one result.
You can extract each component acccordingly

mod[[1]]

Regards
Petr


> 
> Thank you again for your help, I really appreciate it!
> 
> Ross
> 
> 
> -- 
> View this message in context: 
http://n4.nabble.com/Loop-overwrite-and-data-
> output-problems-tp1570593p1573703.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> 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