Reputation: 121
I have to bootstrap a logistic regression model on the 'birthwt' dataset from MASS. However, when I run my code for the bootstrap, I get the following error
Error in eval(predvars, data, env) : object 'low' not found
I don't understand why I get this error, since 'low' is among the variable names in the model.
Why do I get this error? Any feedback is appreciated!
data(birthwt, package = "MASS")
data <- as.data.frame(birthwt)
# converting predictor variable into factor
data$low <- as.factor(data$low)
pmod <- glm(low ~ . - bwt - age - ftv, data = data, family = binomial)
boot_results <- car::Boot(pmod, R = 100)
Upvotes: 1
Views: 29
Reputation: 17656
I've only ever used the boot::boot
function for bootstrapping, but looking at the help page for car::Boot
, it states:
"This function provides a simple front-end to the
boot
function in theboot
package that is tailored to bootstrapping based on regression models..."
So the long way around using boot::boot
would be to first define your logistic regression for bootstrapping and then bootstrap on that function:
boot_logreg <- function(my_data, indices) {
coef(glm(low ~ . - bwt - age - ftv, data = my_data[indices, ], family = binomial))
}
set.seed(123) # Seed for reproducibility, not required
boot_results <- boot::boot(data, boot_logreg, R = 1000)
# ORDINARY NONPARAMETRIC BOOTSTRAP
#
# Call:
# boot::boot(data = data, statistic = boot_logreg, R = 1000)
# Bootstrap Statistics :
# original bias std. error
# t1* -0.80364418 0.070440226 1.238398503
# t2* -0.01294747 -0.001190249 0.008038214
# t3* 0.46913122 0.012766086 0.228166001
# t4* 0.94817154 0.021266241 0.426519541
# t5* 0.49145146 0.050717609 0.450790533
# t6* 1.83315979 0.198129583 1.311930532
# t7* 0.74793544 0.027657403 0.559801377
Upvotes: 0