[R] predict() for bootstrapped model coefficients

Sascha Vieweg saschaview at gmail.com
Thu Jan 20 17:05:09 CET 2011


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)

-- 
Sascha Vieweg, saschaview at gmail.com



More information about the R-help mailing list