Bootstrap Multinomial Regression in R

I try to load just multi-min regression into R and I get an error:

Error in is.data.frame (data): object 'd' not found

What is really strange is that I use the same code (given this particular problem) as in the tutorial for the download package in Quick-R , and the same code also worked when I use another function (e.g. lm ( )). Of course I'm doing something stupid, but I don't understand that. Please, if someone can help, I would really appreciate it.

This is an example:

require(foreign) require(nnet) require(boot) # an example for multinomial logistic regression ml = read.dta('http://www.ats.ucla.edu/stat/data/hsbdemo.dta') ml = ml[,c(5,7,3)] bs <- function(formula, data, indices) { d = data[indices,] # allows boot to select sample fit = multinom(formula, data=d) s = summary(fit) return(list(fit$coefficients, fit$standard.errors)) } # 5 replications results = list() results <- boot( data=ml, statistic=bs, R=5, parallel='multicore', formula=prog~write ) 
+5
source share
2 answers

The error in the summary() , also the object returned by multinom() , has no coefficients and standard.errors . It seems that summary.multinom() in turn calculates the hessian from your data, d , which for some reason (probably a problem with defining the scope) cannot be found. A quick fix is ​​to add Hess = TRUE :

 bs <- function(formula, data, indices) { d = data[indices,] # allows boot to select sample fit = multinom(formula, data=d, Hess = TRUE) s = summary(fit) return( cbind(s$coefficients, s$standard.errors) ) } # 5 replications results = list() results <- boot( data=ml, statistic=bs, R=5, parallel='multicore', formula=prog~write ) 
0
source

Multilinear logistic regression returns a matrix of coefficients using the coef() function. This is different from the lm or glm , which returns a vector of coefficients.

 library(foreign) # read.dta() library(nnet) # multinom() require(boot) # boot() # an example for multinomial logistic regression ml = read.dta('http://www.ats.ucla.edu/stat/data/hsbdemo.dta') ml = ml[,c(5,7,3)] names(ml) bs <- function(formula, data, indices) { d = data[indices,] # allows boot to select sample fit = multinom(formula, data=d, maxit=1000, trace=FALSE) #s = summary(fit) #return(list(fit$coefficients, fit$standard.errors)) estimates <- coef(fit) return(t(estimates)) } # enable parallel library(parallel) cl <- makeCluster(2) clusterExport(cl, "multinom") # 10000 replications set.seed(1984) results <- boot( data=ml, statistic=bs, R=10000, parallel = "snow", ncpus=2, cl=cl, formula=prog~write ) # label the estimates subModelNames <- colnames(results$t0) varNames <- rownames(results$t0) results$t0 estNames <- apply(expand.grid(varNames,subModelNames),1,function(x) paste(x,collapse="_")) estNames colnames(results$t) <- estNames # summary of results library(car) summary(results) confint(results, level=0.95, type="norm") confint(results, level=0.95, type="perc") confint(results, level=0.95, type="bca") # plot the results hist(results, legend="separate") 
0
source

Source: https://habr.com/ru/post/1233863/


All Articles