admin管理员组

文章数量:1133929

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)

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)
Share Improve this question edited Jan 7 at 15:00 jpsmith 16.9k6 gold badges20 silver badges44 bronze badges asked Jan 7 at 11:45 enfieldenfield 1213 bronze badges 1
  • 2 Columns are not first class objects in R. Users of tidyverse conventions are blinded to this fact because of the widespread use of NSE. You will also get confusing error messages if you persist in naming objects data since that is a commonly used function name as is df. – IRTFM Commented Jan 7 at 11:53
Add a comment  | 

1 Answer 1

Reset to default 0

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 the boot 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

本文标签: rPredictor variable not found for bootstrappingStack Overflow