使用 CAR 包中的 boxTidwell 函数并得到一个奇怪的错误

using the boxTidwell function in the CAR package and getting a bizarre error

我正在尝试使用 r 中 CAR 包中的 boxTidwell 函数来 运行 对连续数据进行大量测试。我的数据看起来像这样:

Gender Age    X1      X2   Outcome
  M    20.1   1.23   4.43     1
  F    19.5   2.33   3.21     0
  M    18.0   1.33   7.55     1
  M    17.2   3.22   6.44     0
  M    12.5   4.15   8.99     1
  F    14.2   5.15  10.22     0
  F    13.9   6.12  12.34     1 
  F     9.4   7.12   3.21     1

当我在数据框上使用 boxTidwell 时,出现错误

library(car)    
gender<-c("M","F","M","M","M","F","F","F")
    age<-c(20.1, 19.5, 18.0, 17.2, 12.5, 14.2, 13.9, 9.4)
    X1<-c(1.23,2.33,1.33,3.22,4.15,5.15,6.12,7.12)
    X2<-c(4.43,3.21,7.55,6.44,8.99,10.22,12.34,3.21)
    outcome<-c(1,0,1,0,1,0,1,1)
    df<-cbind(gender,age,X1,X2,outcome)
    as.data.frame(df)
    boxTidwell(outcome~age+X1+X2, ~gender, data=df)

Error in boxTidwell.default(y, X1, X2, max.iter = max.iter, tol = tol, : the variables to be transformed must have only positive values In addition: Warning message: In model.response(mf, "numeric") : using type = "numeric" with a factor response will be ignored

我不确定问题出在哪里,我想是因为我使用的是二进制结果。任何建议将不胜感激

数据不足,算法无法得出解决方案

boxTidwell(outcome~age+X1+X2, ~gender, data=df)
#     Score Statistic   p-value MLE of lambda
#age      -0.3575862 0.7206530      4.339394
#X1        0.3081380 0.7579773      3.377788
#X2       -0.9979096 0.3183232     29.886634

值得注意的是,如果我们对下面创建的数据进行子集化以模仿 OP 的数据(9 行)

boxTidwell(outcome~age+X1+X2, ~gender, data=df[1:8,])

Error in lm.fit(cbind(1, x.log.x, x1.p, x2), y, ...) : NA/NaN/Inf in 'x'

注意:在 OP 的 post 中,data.frame 是在转换为 matrix(使用 cbind)之后创建的。这是有问题的,因为 matrix 只能容纳一个 class,并且所有列都转换为 factoras.data.frame(或 character,如果 stringsAsFactors = FALSE

数据

set.seed(24)
df <- data.frame(gender = sample(c("M", "F"), 100, replace = TRUE),
    age = rnorm(100, 20, 1), X1 = rnorm(100, 4, 1), X2 = rnorm(100, 10, 1),
    outcome = sample(0:1, 100, replace = TRUE))   

聚会迟到了,但其他人可能会觉得这很有用:

我收到了同样的错误,因为我的二进制结果被编码为一个级别为 0、1 的因子。我将其更改为整数和警告 "In model.response(mf, "numeric") :使用 type = "numeric" 与一个因素的反应将被忽略”消失了。

这应该也消除了其他警告;这似乎是由于 boxTidwell 不使用零。一旦结果为 integer/numeric,响应应为 1、2,并且由于您的预测变量值中没有零或负数,这一定是罪魁祸首。