微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用泰坦尼克号数据集绘制 Bootstrap 中的 R 逻辑回归

如何解决使用泰坦尼克号数据集绘制 Bootstrap 中的 R 逻辑回归

我正在做一个统计在线课程的练习。我需要使用泰坦尼克号数据集在 R 中创建逻辑回归。因此,我想应用 bootstrap 方法来创建和绘制逻辑回归预测的 95% 置信区间。

当我运行 bootstrap 命令并想要绘制它时,出现错误:“t* 的所有值都等于 0.0159971772980342”。此外,我得到的偏差和标准误差为 0,这不可能是真的。我猜是在设置 bootstrap 命令时出错,但不幸的是我找不到它。我可以尝试什么?

我的代码

library(boot)

set.seed(50000)

logit_test <- function(data,indices) {  
  dt <- data[indices,]  
  fit <- glm(Clean_data$Survived ~ fare,data = Clean_data,family = "binomial")  
  return(coef(fit))  
}
boot_strap <- boot(  
  data = Clean_data,statistic = logit_test,R = 100)

boot.ci(boot.out = boot_strap,type = c("basic"))


#Now we look at the results and plot them

boot_strap

plot(boot_strap,index=2)

我的输出

> library(boot)
> 
> set.seed(50000)
> 
> logit_test <- function(data,indices) {  
+   dt <- data[indices,]  
+   fit <- glm(Clean_data$Survived ~ fare,family = "binomial")  
+   return(coef(fit))  
+ }
> boot_strap <- boot(  
+   data = Clean_data,+   statistic = logit_test,+   R = 100)
> 
> boot.ci(boot.out = boot_strap,+         type = c("basic"))
BOOTSTRAP CONFIDENCE INTERVAL CALculaTIONS
Based on 100 bootstrap replicates

CALL : 
boot.ci(boot.out = boot_strap,type = c("basic"))

Intervals : 
Level      Basic         
95%   (-0.8968,-0.8968 )  
Calculations and Intervals on Original Scale
Some basic intervals may be unstable
> boot_strap

ORDINARY NONParaMETRIC BOOTSTRAP


Call:
boot(data = Clean_data,R = 100)


Bootstrap Statistics :
       original  bias    std. error
t1* -0.89682819       0           0
t2*  0.01599718       0           0
> plot(boot_strap,index=2)
[1] "All values of t* are equal to  0.0159971772980342"

解决方法

问题在于您的引导函数没有使用引导数据来拟合模型。你有这个功能:

logit_test <- function(data,indices){
  dt <- data[indices,]
  fit <- glm(Clean_Travelers$Survived ~ FARE,data=Clean_Travelers,family=binomial)
  return(coef(fit))
}

请注意,有几个问题,一个是您应该在 dt 参数中使用 data=,但您也不应该使用 Clean_Travelers$Survived 作为因变量,它应该只是 Survived 因为您想确保您不是从原始数据中获取该变量,而是从引导数据中获取该变量。像这样的引导函数应该可以工作:

logit_test <- function(data,]
  fit <- glm(Survived ~ FARE,data=dt,family=binomial)
  return(coef(fit))
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。