[R] How to run a model 1000 times, while saving coefficients each time?
Mark Na
mtb954 at gmail.com
Mon Aug 25 22:45:18 CEST 2008
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
}
More information about the R-help
mailing list