[R] How to run a model 1000 times, while saving coefficients each time?

Derrick Lee d.lee at stat.ubc.ca
Mon Aug 25 23:05:01 CEST 2008


Hey Mark,

I'm not sure if anyone gave this answer, but I have a similar setup in 
one of my codes, to get the coefficients and r^2 value, you can use the 
commands:

model1$coefficients (or any part of the term since R can recognize the 
first couple of letters)
summary(model1)$r.squared

as for saving the values in your loop, you could try something like this:

coef1 <- c()
for(i in 1000){

# rest of the code...

newcoef1 <- model1$coef

coef1 <- rbind(coef1,newcoef1)
}

HTH. Cheers.


 - Derrick

Greg Snow wrote:
> ?replicate
>
> --
> Gregory (Greg) L. Snow Ph.D.
> Statistical Data Center
> Intermountain Healthcare
> greg.snow at imail.org
> (801) 408-8111
>
>
>
>   
>> -----Original Message-----
>> From: r-help-bounces at r-project.org
>> [mailto:r-help-bounces at r-project.org] On Behalf Of Mark Na
>> Sent: Monday, August 25, 2008 2:45 PM
>> To: R-help at stat.math.ethz.ch
>> Subject: [R] How to run a model 1000 times, while saving
>> coefficients each time?
>>
>> Hello,
>>
>> We have written a program (below) to model the effect of a
>> covariate on observed values of a response variable (using
>> only 80% of the rows in our dataframe) and then use that
>> model to calculate predicted values for the remaining 20% of
>> the rows. Then, we compare the observed vs.
>> predicted values using a linear model and inspect that
>> model's coefficients and its R2 value.
>>
>> We wish to run this program 1000 times, and to save the
>> coefficients and
>> R2 values into a separate dataframe called results.
>>
>> We have a looping structure (also below) but we do not know
>> how to save the coefficients and R2 values. We are missing
>> some code (indicated)
>>
>> Any assistance would be greatly appreciated.
>>
>> Thanks,
>>
>>
>> library(sampling)
>>
>> mall<-read.csv("mall.csv")
>>
>> for (j in 1:1000) {
>>
>> s<-srswor(2840,3550)
>> mall80<-mall[s==1,]
>> mall20<-mall[s==0,]
>> model1<-lm(count~habitat,data=mall80)
>> summary(model1)
>> mall20$predicted<-predict(model1,newdata=mall20)
>> model2<-lm(count~predicted,data=mall20)
>>
>> MISSING CODE: SAVE MODEL COEFFICIENTS AND R2 VALUE TO A
>> DATAFRAME CALLED RESULTS
>>
>> }
>>
>> ______________________________________________
>> 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.
>>
>>     
>
> ______________________________________________
> 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.
>   


-- 
Sincerely

 - Derrick


 Derrick Lee, MSc Candidate
 Department of Statistics
 The University of British Columbia
 LSK-314A | 604 - 822 - 1299 x532
 d.lee at stat dot ubc dot ca | dgylee at mun dot ca
 www.stat.ubc.ca/~d.lee/ | www.math.mun.ca/~derrick0/



More information about the R-help mailing list