【问题标题】:Adding variables to a fixed model in logistic regression将变量添加到逻辑回归中的固定模型
【发布时间】:2017-12-30 23:08:19
【问题描述】:

一个关于 R 中逻辑回归的快速问题。假设我使用 Cell.size 和 Cell.shape 作为预测变量拟合乳腺癌数据集的逻辑回归,以预测患者是良性还是恶性。

library(mlbench)
data(BreastCancer)
data1<- BreastCancer
levels(data1$Class)<- c(0,1) # binary outcome
data1<- data.frame(sapply(data1, as.numeric)) #converting from factors to num
data1$Class<- data1$Class-1
mylogit1<- glm(Class~Cell.size+Cell.shape, data=data1, family="binomial")

这会给我精确的模型: Y= -5.4771+0.7672*Cell.size+0.8223*Cell.shape 来预测患者的类别(我会使用 1/(1+exp(-Y) 作为相应的风险) .

现在我想修复这个模型并尝试添加新变量,例如 Cl.thickness。我的目标是以

的形式将逻辑回归与基数相匹配

Y= -5.4771+ 0.7672*Cell.size+0.8223*Cell.shape+a+b*Cl.thickness

其中 a、b 是我要估计的参数。

如何在 R 中做到这一点?不是我想修复拦截,而是我想修复模型的某些部分。谢谢!

【问题讨论】:

  • 想要在你的新模型中使用“a”吗?

标签: r regression


【解决方案1】:

通常人们会像这样更新公式并再次运行模型:

update(mylogit1, . ~ . + Cl.thickness)

但如果你真的想修复系数,那么

update(mylogit1, . ~ Cl.thickness + offset(predict(mylogit1)))

【讨论】:

  • 感谢@stackoverflow.com/users/516548/g-grothendieck 的回复!我按照你说的做了,但是当我将 as.numeric(predict(mylogit2, data1, type="response", se=TRUE)$fit) 与 1/(1+exp(-(-5.4771-4.9645+0.7672*data1) 进行比较时$Cell.size+0.8223*data1$Cell.shape+0.8155*data1$Cl.thickness)) 我发现它们是不同的,即函数没有达到我的预期。你知道为什么吗?非常抱歉弄乱我的评论-我看不到如何从我的计算机上正确格式化它。只是为了澄清一下:mylogit2
  • fitted 应该是 predict,因为 fitted 返回响应尺度上的值,而我们想要线性预测尺度上的值,predict 默认情况下给出了它。请参阅修改后的答案。
猜你喜欢
  • 2020-07-30
  • 1970-01-01
  • 2020-05-26
  • 2016-06-30
  • 1970-01-01
  • 2018-05-21
  • 2018-08-24
  • 2017-12-22
  • 1970-01-01
相关资源
最近更新 更多