gpt4 book ai didi

r - 指定试验次数、 Bootstrap

转载 作者:行者123 更新时间:2023-11-30 08:42:10 24 4
gpt4 key购买 nike

对于一项作业,我正在使用 R 上的 mixtools 包应用混合建模。当我尝试使用 bootstrap 找出最佳组件数量时。我收到以下错误

Error in boot.comp(y, x, N = NULL, max.comp = 2, B = 5, sig = 0.05, arbmean = TRUE,  : 
Number of trials must be specified!

我发现我必须填写一个 N:逻辑回归类型 logisregmix 的试验次数的 n 向量。如果NULL,则 N 是二元逻辑回归的 1 的 n 向量。

但是,我不知道如何找出 N 实际上是什么以使我的 Bootstrap 正常工作。

链接到我的代码: https://www.kaggle.com/blastchar/telco-customer-churn

我的代码:

data <- read.csv("Desktop/WA_Fn-UseC_-Telco-Customer-Churn.csv", stringsAsFactors = FALSE,
na.strings = c("NA", "N/A", "Unknown*", "NULL", ".P"))
data <- droplevels(na.omit(data))
data <- data[c(1:5032),]
testdf <- data[c(5033:7032),]
data <- subset(data, select = -customerID)
set.seed(100)
library(plyr)
library(mixtools)
data$Churn <- revalue(data$Churn, c("Yes"=1, "No"=0))
y <- as.numeric(data$Churn)
x <- model.matrix(Churn ~ . , data = data)
x <- x[, -1] #remove intercept
x <-x[,-c(7, 11, 13, 15, 17, 19, 21)] #multicollinearity


a <- boot.comp(y, x, N = NULL, max.comp = 2, B = 100,
sig = 0.05, arbmean = TRUE, arbvar = TRUE,
mix.type = "logisregmix", hist = TRUE)

下面是有关我的预测变量的更多信息:

dput(x[1:4,]) structure(c(0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 34, 2, 45, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 29.85, 56.95, 53.85, 42.3, 29.85, 1889.5, 108.15, 1840.75), .Dim = c(4L, 23L), .Dimnames = list(c("1", "2", "3", "4"), c("genderMale", "SeniorCitizen", "PartnerYes", "DependentsYes", "tenure", "PhoneServiceYes", "MultipleLinesYes", "InternetServiceFiber optic", "InternetServiceNo", "OnlineSecurityYes", "OnlineBackupYes", "DeviceProtectionYes", "TechSupportYes", "StreamingTVYes", "StreamingMoviesYes", "ContractOne year", "ContractTwo year", "PaperlessBillingYes", "PaymentMethodCredit card (automatic)", "PaymentMethodElectronic check", "PaymentMethodMailed check", "MonthlyCharges", "TotalCharges" )))

我的响应变量是二进制的

希望大家能帮帮我!

最佳答案

查看 mixtools::boot.comp 的源代码,该代码令人恐惧,因为它有 800 多行长,并且严重需要重构,有问题的行是:

 if (mix.type == "logisregmix") {
if (is.null(N))
stop("Number of trials must be specified!")

无论文档如何规定,都必须指定 N

尝试将其设置为 1 的向量:N =rep(1, length(y))N =rep(1, nrow(x))

事实上,如果您查看由 boot.comp 调用的内部函数 mixtools::logisregmixEM,您将看到 N 如果 NULL 则设置:

n <- length(y)
if (is.null(N)) {
N = rep(1, n)
}

太糟糕了,如果NNULL,则永远无法达到此目的,因为它之前因错误而停止。这是一个错误。

关于r - 指定试验次数、 Bootstrap ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58802514/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com