[R] calling objects in a foreloop

From: Benjamin M. Osborne <Benjamin.Osborne_at_uvm.edu>
Date: Tue 15 Mar 2005 - 08:00:33 EST


I want to organize outputs from several regressions into a handy table. When I try the following, each of my "fit_s" is replaces instead of read. Is there a way to read from the regression summaries that does not require writing separate lines of code for each?
-Ben Osborne

> fit1<-lm(dBA.spp16$sp2.dBA.ha~dBA.spp16$sp1.dBA.ha)
> fit2<-lm(dBA.spp16$sp3.dBA.ha~dBA.spp16$sp1.dBA.ha)
> fit3<-lm(dBA.spp16$sp3.dBA.ha~dBA.spp16$sp2.dBA.ha)
> fit4<-lm(dBA.spp16$sp5.dBA.ha~dBA.spp16$sp4.dBA.ha)
> fit5<-lm(dBA.spp16$sp6.dBA.ha~dBA.spp16$sp4.dBA.ha)
> fit6<-lm(dBA.spp16$sp5.dBA.ha~dBA.spp16$sp6.dBA.ha)
> fit7<-lm(dBA.spp16$sp1.dBA.ha~dBA.spp16$sp4.dBA.ha)
> fit8<-lm(dBA.spp16$sp1.dBA.ha~dBA.spp16$sp5.dBA.ha)
>
> dBA.spp16.fits<-matrix(NA, nrow=8, ncol=5)
> colnames(dBA.spp16.fits)<-c("formula","intercept","slope","R^2","adj.R^2")
>
> for (i in 1:8){

+ dBA.spp16.fits[i,1]<-summary(as.name(paste("fit",i,sep="")))$call
+ dBA.spp16.fits[i,2]<-summary(as.name(paste("fit",i,sep="")))$coef[1,1]
+ dBA.spp16.fits[i,3]<-summary(as.name(paste("fit",i,sep="")))$coef[2,1]
+ dBA.spp16.fits[i,4]<-summary(as.name(paste("fit",i,sep="")))$r.squared
+ dBA.spp16.fits[i,5]<-summary(as.name(paste("fit",i,sep="")))$adj.r.squared
+ }
Error in "[<-"(`*tmp*`, i, 1, value = NULL) :
        number of items to replace is not a multiple of replacement length

# Because:
> i<-1
> summary(as.name(paste("fit",i,sep="")))
Length Class Mode

     1 name name
>

-- 
Botany Department
University of Vermont
109 Carrigan Drive
Burlington, VT 05405

benjamin.osborne@uvm.edu
phone: 802-656-0297
fax: 802-656-0440

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Tue Mar 15 08:04:50 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:30:47 EST