Bootstrap函数返回NA值求助:为何所有t1*值均为NA?
为什么Bootstrap结果中所有t1*值都是NA?
运行的R代码
library(MASS) library(boot) library(shiny) mean_func <- function(data, indices) { mean_score <- mean(data[indices, "Score"]) return(mean_score) } paintersnum <- painters[,1:4] #making sure that there are only numeric variables in the dataset paintersnum set.seed(123) boot_result <- boot(data = paintersnum, statistic = mean_func, R = 1000) print(boot_result) validate(boot_result) sum(is.na(painters)) boot_ci <- boot.ci(boot_result, type = "bca") print(boot_ci)
得到的警告信息
ORDINARY NONPARAMETRIC BOOTSTRAP Call: boot(data = paintersnum, statistic = mean_func, R = 1000) Bootstrap Statistics : WARNING: All values of t1* are NA
期望的正常结果
Bootstrap Statistics : original bias std. error t1* 1.5203 0.036974 0.21489
问题原因与解决方法
核心原因:你的
mean_func函数试图提取名为Score的列,但paintersnum(即painters数据集的前4列)中根本不存在这个列。painters数据集的前4列是Composition、Drawing、Colour、Expression,所以data[indices, "Score"]会返回全NA,计算均值后自然也是NA,最终导致所有Bootstrap统计量t1*都为NA。解决方法:修改函数中的列名为
paintersnum实际存在的列名即可。比如如果要计算Composition列的均值,修改后的函数如下:
mean_func <- function(data, indices) { mean_score <- mean(data[indices, "Composition"]) return(mean_score) }
重新运行代码就能得到正常的Bootstrap统计结果。
内容的提问来源于stack exchange,提问作者Ondřej Matoušek
相关产品推荐
相关产品推荐

