[R] for loop for extracting linear model info

Uwe Ligges ligges at statistik.tu-dortmund.de
Fri Apr 3 16:47:20 CEST 2009



Melissa2k9 wrote:
> Hi,
> 
> I have written a for loop as such:
> 
> model<-lm(Normalised~Frame,data=All,subset=((Subject==1)&(Filmclip=="Strand")))
> summary(model)
> 
> #######################################
> #To extract just the Adjusted R squared
> #######################################
> 
> rsq<-summary(model)[[9]]
> 
> ###########################
> #To extract just the slope
> ###########################
> 
> slope<-summary(model)[[4]][[2]]
> 
> #######################################################
> #To extract only the p value from the t test for slope
> #######################################################
> 
> pvalue<-summary(model)[[4]][[8]]
> 
> ####################################
> data<-data.frame(slope,pvalue,rsq)
> ####################################
> 
> 
> #######################################
> #To extract this info for all films
> ########################################
> 
> for (i in c(1:8,10:20,22:29))
> {
> model_1<-lm(Normalised~Frame,data=All,subset=((Subject==i)&(Filmclip=="Strand")))
> summary(model_1)
> slope<-summary(model_1)[[4]][[2]]
> pvalue<-summary(model_1)[[4]][[8]]
> rsq<-summary(model_1)[[9]]
> data2<-data.frame(slope,pvalue,rsq)
> data2<-rbind(data,data2)
> }
> 
> I want this to run for all i but so far I am only getting two entries in my
> data frame, one for the first subject, and another.


You are overwriting the old data2 with a new one that consists of those 
two in each iteration of the loop .......


Uwe Ligges


> Does anyone know where I am going wrong in my code so I can have this data
> for all subjects 1-8,10-20, and 22-29.
> 
> Thanks




More information about the R-help mailing list