Catarina Ferreira
2013-Dec-10 20:44 UTC
[R] Get average model after dredge function ran in a loop
Dear all I'm a beginner in R and I'm trying to get the final model after I run the dredge function for 10 times (with a loop): ###Building the Model Coy.glm0<-glm(pa ~ shrub + snowdep + tree + bio5 + bio6 + bio12 + log(human+1), data=Coy.pa, family=binomial) summary(Coy.glm0) install.packages('MuMIn') library(MuMIn) Coy.dredge<-dredge(Coy.glm0) head(Coy.dredge) ######################Look in which colum is AIC ###Building a simulation Coy.models<-Coy.dredge[1,c(1:13)] Coy.models ###Turn a loop who will create 10 models run=1 while(run<11) #<11 means 10 models. { Coy.abs$Long<-runif(300,498,2579440) Coy.abs$Lat<-runif(300,-51483,1377669) Coy.pa<-rbind(Coy.train, Coy.abs) ####???? train ou prSS Coy.ppp<-ppp(Coy.pa$Long,Coy.pa$Lat, window=win, unitname="meters") Coy.pa$snowdep<-snowdepz.im[Coy.ppp, drop=F] Coy.pa$tree<-treez.im[Coy.ppp, drop=F] Coy.pa$bio5<-bio5z.im[Coy.ppp, drop=F] Coy.pa$bio6<-bio6z.im[Coy.ppp, drop=F] Coy.pa$bio12<-bio12z.im[Coy.ppp, drop=F] Coy.pa$human<-humanz.im[Coy.ppp, drop=F] Coy.pa$shrub<-shrub.im[Coy.ppp, drop=F] Coy.glm0<-glm(pa ~ shrub + snowdep + tree + bio5 + bio6 + bio12+ log(human+1), data=Coy.pa, family=binomial) Coy.dredge<-dredge(Coy.glm0) Coy.models<-rbind(Coy.models, Coy.dredge[1,c(1:13)]) run=run+1 } I do get a best model for each run which I then hand pick and add to a table. The problem is that I have 11 models now in this table and I want their average to get the final model. I don't know how to do it from the table (as the model.avg() will tell me I only have one model in the table, because it's not recognizing the different rows as different models), but on the other hand there must be a way to do it directly in the loop, only I'm not sure at what point of the script I should be asking for it and how the code should be written. I would very much appreciate any help you can give me. Thank you. Cat [[alternative HTML version deleted]]
Kamil BartoĊ
2013-Dec-13 12:56 UTC
[R] Get average model after dredge function ran in a loop
You are trying to average coefficients from models fitted to different data (as you have manipulated Lat+Long values), you cannot do it using AIC weights. kamil On 2013-12-11 11:00, r-help-request at r-project.org wrote:> Message: 26 > Date: Tue, 10 Dec 2013 15:44:28 -0500 > From: Catarina Ferreira<catferreira at gmail.com> > To: r-help<r-help at r-project.org> > Subject: [R] Get average model after dredge function ran in a loop > Message-ID: > <CAAiga1sKpKS9aQHYs+RNrBWJxmxhRf3qn6DgaEN1P8QSt-293w at mail.gmail.com> > Content-Type: text/plain > > Dear all > > I'm a beginner in R and I'm trying to get the final model after I run the > dredge function for 10 times (with a loop): > > ###Building the Model > Coy.glm0<-glm(pa ~ shrub + snowdep + tree + bio5 + bio6 + bio12 + > log(human+1), data=Coy.pa, family=binomial) > summary(Coy.glm0) > > install.packages('MuMIn') > library(MuMIn) > Coy.dredge<-dredge(Coy.glm0) > head(Coy.dredge) ######################Look in which colum is AIC > ###Building a simulation > Coy.models<-Coy.dredge[1,c(1:13)] > Coy.models > > > ###Turn a loop who will create 10 models > run=1 > while(run<11) #<11 means 10 models. > { > Coy.abs$Long<-runif(300,498,2579440) > Coy.abs$Lat<-runif(300,-51483,1377669) > Coy.pa<-rbind(Coy.train, Coy.abs) ####???? train ou prSS > Coy.ppp<-ppp(Coy.pa$Long,Coy.pa$Lat, window=win, unitname="meters") > Coy.pa$snowdep<-snowdepz.im[Coy.ppp, drop=F] > Coy.pa$tree<-treez.im[Coy.ppp, drop=F] > Coy.pa$bio5<-bio5z.im[Coy.ppp, drop=F] > Coy.pa$bio6<-bio6z.im[Coy.ppp, drop=F] > Coy.pa$bio12<-bio12z.im[Coy.ppp, drop=F] > Coy.pa$human<-humanz.im[Coy.ppp, drop=F] > Coy.pa$shrub<-shrub.im[Coy.ppp, drop=F] > > Coy.glm0<-glm(pa ~ shrub + snowdep + tree + bio5 + bio6 + bio12+ > log(human+1), data=Coy.pa, family=binomial) > Coy.dredge<-dredge(Coy.glm0) > Coy.models<-rbind(Coy.models, Coy.dredge[1,c(1:13)]) > run=run+1 > } > > I do get a best model for each run which I then hand pick and add to a > table. The problem is that I have 11 models now in this table and I want > their average to get the final model. I don't know how to do it from the > table (as the model.avg() will tell me I only have one model in the table, > because it's not recognizing the different rows as different models), but > on the other hand there must be a way to do it directly in the loop, only > I'm not sure at what point of the script I should be asking for it and how > the code should be written. > > I would very much appreciate any help you can give me. > > Thank you. > > CatThe University of Aberdeen is a charity registered in Scotland, No SC013683.