[R] predict() for bootstrapped model coefficients
Sascha Vieweg
saschaview at gmail.com
Sat Jan 22 16:56:26 CET 2011
On 11-01-20 17:05, Sascha Vieweg wrote:
> I run a multinomial regression on a data set with an outcome that has three
> values. First, I build an initial model, b.mod. Then I run a loop to
> bootstrap the coefficients. For the initial model, using "predict()", I can
> print the wrong/false predictions table. But how do I get this table (that
> is, the predictions) for the bootstrapped model? Thanks for hints, *S*
>
> df <- data.frame(
> "y"=factor(sample(LETTERS[1:3], 200, repl=T)),
> "a"=rnorm(200),
> "b"=rnorm(200)
> )
> library(nnet)
> b.mod <- multinom(y ~ ., data=df, trace=F)
> i <- 1; k <- 50
> c.mat.1 <- matrix(nrow=k, ncol=length(names(df)), dimnames=list(NULL,
> c("cons", names(df)[-1])))
> c.mat.2 <- matrix(nrow=k, ncol=length(names(df)), dimnames=list(NULL,
> c("cons", names(df)[-1])))
> set.seed(123)
> while(i <= k){
> l <- sample(1:length(df[, 1]), replace=T)
> m <- update(b.mod, . ~ ., data=df[l, ], trace=F)
> c.mat.1[i, ] <- coef(m)[1, ]
> c.mat.2[i, ] <- coef(m)[2, ]
> i <- i + 1
> }
> (coefs1 <- apply(c.mat.1, 2, mean))
> (coefs2 <- apply(c.mat.2, 2, mean))
> summary(predict(b.mod)==model.frame(b.mod)$y)
I give it a second trial to poll some ideas. I *mean* that predict
uses information from the model object, here b.mod. My idea was
now to "fake" an object and fill it with relevant data from my
bootstrap to run predict on that faked object:
f.b.mod <- b.mod
f.b.mod$n <- 14:16 # senseless, anyway
f.b.mod[1:3]
What I don't know, and don't get from the predict() source code,
and thus requesting help upon is, which parts of the model object
does predict use and how can I replace them with my loop data?
Thanks for hints, *S*
--
Sascha Vieweg, saschaview at gmail.com
More information about the R-help
mailing list