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

Greg Snow Greg.Snow at imail.org
Mon Aug 25 22:47:31 CEST 2008


?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.
>



More information about the R-help mailing list