Two things,
1) You need to use square brackets in this case because model is an object,
not a function.
2) You probably want to use a list object to store a whole bunch of model
objects so you'll want double brackets.
model = list(NULL)
for (i in 1:1000) {
model[[i]] <- lm(Y~X1+X2())
}
This, however is not particularly efficient memory usage and you would do
better to set up the entire list object ahead of time.
Once you have this, to get the coefficients, simply
C = sapply(model, coef)
Alternatively, you can just get the coefficients from each simulation and
discard the general model object: this will be faster and allow you to avoid
the list object
model = matrix(0,nrow = 1000, ncol = 3)
for (i in 1:1000) {model[i,] = coef(lm(Y~X1 + X2())) }
Hope this helps,
Michael Weylandt
On Mon, Aug 15, 2011 at 5:22 AM, Johannes Radinger <JRadinger@gmx.at>
wrote:
> Hello...
>
> just some additional thoughts:
> Maybe I can try it in a simple way with a repeated lm-regression, like:
>
> Y=c(15,14,23,18,19,9,19,13)
> X1=c(0.2,0.6,0.45,0.27,0.6,0.14,0.1,0.52)
> X2a=c(17,22,21,18,19,25,8,19)
> X2b=c(22,22,29,34,19,26,17,22)
>
> X2 <- function()runif(length(X2a), X2a, X2b)
>
> for loop --> repeat lm(Y~X1+X2()) for let's say 1000 times
> and write the regression coefficients into a vector. Afterwards
> just get e.g the mean and standard deviation for Intercept, and beta.
> How is the for loop done in this case? I tried
>
> for(i in 1:1000) model(i) <-lm(Y~X1+X2()) but that is not working....
>
> /johannes
>
>
>
> -------- Original-Nachricht --------
> Datum: Mon, 15 Aug 2011 10:20:53 +0200
> Von: "Johannes Radinger" <JRadinger@gmx.at>
> An: r-help@r-project.org
> Betreff: MCMC regress, using runif()
>
> Hello,
>
> just to follow up a question from last week. Here what I've done so far
> (here an example):
>
>
> library(MCMCpack)
>
> Y=c(15,14,23,18,19,9,19,13)
> X1=c(0.2,0.6,0.45,0.27,0.6,0.14,0.1,0.52)
> X2a=c(17,22,21,18,19,25,8,19)
> X2b=c(22,22,29,34,19,26,17,22)
>
> X2 <- function()runif(length(X2a), X2a, X2b)
>
> model1 <- MCMCregress(Y~X1+X2())
> summary(model1)
>
>
> but I am not sure if my X2-function is working in the MCMCpack?
> Is a random number drawn each iteration step? I don't think so
> as the results are varying greatly if I run the script several times.
>
> Is there any other way to do several thousand runs of a linear
> regression,always drawing a random number for X2 and then compute average
> values for the regressions?
>
> /Johannes
>
>
> --
>
>
>
>
> --
>
> ______________________________________________
> R-help@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.
>
[[alternative HTML version deleted]]