首页 > 解决方案 > 如何在 R 中将 Box-Tidwell 函数与逻辑回归一起使用

问题描述

使用逻辑回归模型的“汽车”包中的 boxTidwell 函数时出现错误。

我要建模

fatalCancer ~ globy1,其中 fatalCancer 是具有两个级别的因子, globy1 是数字(全为正)。我正在对此进行测试,以检查 globy1 的线性假设与结果的 logit。

查看错误消息(如下)和 boxTidwell 函数代码,似乎可能有问题,因为 fatalCancer 是一个因素。boxTidwell 文档中没有任何关于指定它是逻辑模型的内容。在 Fox 的 _An R Companion to Applied Regression (p.312) 第 6.4 节的示例中,示例逻辑回归不需要任何规范。

有没有办法修复下面 boxTidwell 函数的语法?

> library(car)
Loading required package: carData
> 
> load("m2dat.RData")
> m2dat <- na.omit(m2dat)
> dim(m2dat) 
[1] 116   3    
> head(m2dat)
   dog globy1 fatalCancer
1 101A    3.1          No
2 102A    2.9          No
3 103A    4.9          No
4 104A    3.1         Yes     
5 105A    2.8         Yes
6 106A    3.5          No
> boxTidwell(fatalCancer ~ globy1, data=m2dat)
 MLE of lambda Score Statistic (z) Pr(>|z|)    
        6.5694                  NA       NA

iterations =  21      
There were 48 warnings (use warnings() to see them)
> warnings()    
Warning messages:
1: In model.response(mf, "numeric") :
  using type = "numeric" with a factor response will be ignored
2: In model.response(mf, "numeric") :
  using type = "numeric" with a factor response will be ignored
3: In Ops.factor(y, z$residuals) : ‘-’ not meaningful for factors
4: In model.response(mf, "numeric") :
  using type = "numeric" with a factor response will be ignored
5: In Ops.factor(y, z$residuals) : ‘-’ not meaningful for factors
6: In Ops.factor(r, 2) : ‘^’ not meaningful for factors
7: In Ops.factor(y, z$residuals) : ‘-’ not meaningful for factors
8: In Ops.factor(y, z$residuals) : ‘-’ not meaningful for factors    
9: In Ops.factor(y, z$residuals) : ‘-’ not meaningful for factors
10: In Ops.factor(y, z$residuals) : ‘-’ not meaningful for factors
...
46: In Ops.factor(y, z$residuals) : ‘-’ not meaningful for factors 
47: In Ops.factor(y, z$residuals) : ‘-’ not meaningful for factors
48: In Ops.factor(y, z$residuals) : ‘-’ not meaningful for factors

分数统计结果为 NA,我想成功运行测试。

标签: rlogistic-regression

解决方案


逻辑回归的线性假设在对数赔率和预测变量之间,而不是在结果变量和预测变量之间(因为您已将它们输入到函数中)。

lreg <- glm(fatalCancer ~ globy1, data=m2dat, family = binomial(link="logit"))
logodds <- lreg$linear.predictors
boxTidwell(logodds ~ globy1)

或者,您可以使用散点图进行评估:

plot(logodds ~ globy1)

我希望这有帮助!


推荐阅读