楼主: moonstone
38590 48

[学习分享] R进行Logistic回归学习笔记   [推广有奖]

已卖:1872份资源

讲师

74%

还不是VIP/贵宾

-

威望
0
论坛币
10498 个
通用积分
369.1683
学术水平
160 点
热心指数
169 点
信用等级
124 点
经验
274286 点
帖子
237
精华
1
在线时间
520 小时
注册时间
2007-4-27
最后登录
2024-12-6

楼主
moonstone 发表于 2015-12-6 23:24:54 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
SAS中Proc logisitc过程提供了很完善的logistic回归的分析功能,学习R中完成此过程只是想比较一下两个软件在完成此过程的差别。虽然有很多帖子介绍如何采用R完成logistic回归过程,但是都相对过于简单,对于以下常用细节很少涉及。

1、模型筛选方法
2、如何简单设定哑变量
3、针对分类变量,如何选取特定水平作为参考水平
4、如何简单输出OR值及置信区间
5、如何构建条件logistic回归过程
6、不同模型的预测效果比较

虽然之前有很多帖子比较SAS与R的差别,但是多是基于宏观层面的。通过比较实现某一具体过程的细微差别,估计更能体会两者的功能差异。

以下是自己学习的笔记,附有一些简单说明,供参考,希望对大家有帮助,也非常欢迎跟帖交流你在运用R进行Logistic回归时的学习心得
  1. library(stats)
  2. help(infert)                     # Description of data
  3. infert <- data.frame(infert)
  4. str(infert)                      # Check type of variables
  5. summary(infert)                  # Statistical summary

  6. ## Model1 Develop a simple logistic regression:

  7. model1 <- glm(case ~ spontaneous+induced, data = infert, family = binomial())

  8. ## Model output
  9. summary(model1)                    # Output summary information
  10. confint(model1)                    # Output 95% CI for the coefficients
  11. exp(coef(model1))                  # Output OR (exponentiated coefficients)
  12. exp(confint(model1))               # 95% CI for exponentiated coefficients
  13. predict(model1, type="risk")       # predicted values
  14. residuals(model1, type="deviance") # residuals

  15. ## Model2 Develop a logistic regression adjusted for other potential confounders:
  16. model2 <- glm(case ~ age+parity+education+spontaneous+induced,data = infert,
  17.               family = binomial())
  18. summary(model2)

  19. ## Model3 Develop a conditional logistic regression

  20. library(survival)
  21. model3 <- clogit(case ~ spontaneous+induced+strata(stratum), data = infert)
  22. summary(model3)

  23. ## Model4 Conditional logistic regression  
  24. ## Transfer the numerous variable into category variable
  25. model4 <- clogit(case ~ factor(spontaneous)+factor(induced)+strata(stratum),
  26.                  data = infert)
  27. summary(model4)

  28. ## Model5 Conditional logistic regression
  29. ## Conduct a subgroup analysis
  30. ## "subset" is not aviable for "clogit"
  31. ## create the subset first

  32. str(infert$education)

  33. infert1 <- subset(infert,education =="12+ yrs")

  34. model5 <- clogit(case ~ factor(spontaneous)+ factor(induced)+ strata(stratum),
  35.                  data = infert1)
  36. summary(model5)

  37. ## Model6 General logistic regression  
  38. ## Change the reference category for category variable
  39. ## Note: Argument "contrasts" not matched in clogit

  40. infert$spontaneous <-
  41.   as.factor(infert$spontaneous)
  42. infert$induced <-
  43.   as.factor(infert$induced)

  44. infert$spontaneous <-
  45.   relevel(infert$spontaneous, ref = "2")
  46. infert$induced <-
  47.   relevel(infert$induced, ref = "1")

  48. model6 <- glm(case ~ spontaneous+induced,
  49.               data = infert)
  50. summary(model6)


  51. ## Model7 General logistic regression
  52. ## Performs stepwise model selection by AIC
  53. ## Note: Model selection in R is based on AIC but not P value
  54. model7 <- glm(case ~ spontaneous+induced+factor(education)+age+parity,data = infert)
  55. summary(model7)

  56. model8 <-step(model7)  ## Default direction is "backward"
  57. summary(model8)

  58. model9 <-step(model7,direction="both")
  59. summary(model9)

  60. ## Compare the AUC of two ROC curves
  61. infert$pred6 <-predict(model6)
  62. infert$pred8 <-predict(model8)

  63. library("pROC")

  64. rocobj1 <- plot.roc(infert$case,
  65.                     infert$pred6,
  66.                     percent=TRUE,ci=TRUE,col="#1c61b6")  
  67. rocobj2 <- plot.roc(infert$case,
  68.                     infert$pred8,
  69.                     add=TRUE,percent=TRUE,ci=TRUE,col="#008600")  
  70. testobj <- roc.test(rocobj1, rocobj2)  
  71. text(50, 50, labels=paste("p-value =",
  72.                           format.pval(testobj$p.value)), adj=c(0, .5))  
  73. legend("bottomright", legend=c("Model5", "Model8"),
  74.        col=c("#1c61b6", "#008600"),lwd=2,ncol=2)
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:logistic回归 logistic ogistic logisti logist 置信区间 模型 如何 软件

已有 6 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
gsjqwuyao + 2 + 4 + 2 热心帮助其他会员
飞天玄舞6 + 100 + 2 + 2 精彩帖子
wu88172968 + 1 精彩帖子
日新少年 + 2 + 2 + 2 精彩帖子
李会超 + 60 + 20 精彩帖子
求证1加1 + 40 + 1 + 1 鼓励积极发帖讨论

总评分: 经验 + 200  论坛币 + 21  学术水平 + 7  热心指数 + 9  信用等级 + 4   查看全部评分

本帖被以下文库推荐

沙发
dujt11972900 学生认证  发表于 2015-12-6 23:35:47 来自手机
moonstone 发表于 2015-12-6 23:24
SAS中Proc logisitc过程提供了很完善的logistic回归的分析功能,学习R中完成此过程只是想比较一下两个软件在 ...
恩,谢谢分享

藤椅
颸若辰 学生认证  发表于 2015-12-6 23:36:26
谢谢楼主分享

板凳
lyh0114 学生认证  发表于 2015-12-6 23:37:13 来自手机
moonstone 发表于 2015-12-6 23:24
SAS中Proc logisitc过程提供了很完善的logistic回归的分析功能,学习R中完成此过程只是想比较一下两个软件在 ...
谢谢分享

报纸
moretc 学生认证  发表于 2015-12-7 09:58:22
还有其他资料吗

地板
noisyman 发表于 2015-12-7 10:21:12
mark~~~~~~~~

7
xiaolovesfan 发表于 2015-12-7 13:17:25
  1. rocobj1 <- plot.roc(infertcase,infertpred6,
  2.                percent=TRUE,ci=TRUE,col="#1c61b6")
复制代码

请问这个infertcase是什么,报错了

8
moonstone 发表于 2015-12-7 14:02:59
xiaolovesfan 发表于 2015-12-7 13:17
请问这个infertcase是什么,报错了
这是论坛对R语言编码显示的问题造成的,应该是infert$case,中间的“$”显示不全

9
xiaolovesfan 发表于 2015-12-7 14:15:54
moonstone 发表于 2015-12-7 14:02
这是论坛对R语言编码显示的问题造成的,应该是infert$case,中间的“$”显示不全
还是没懂,infert和case的字体怎么不一样?

10
moonstone 发表于 2015-12-7 14:21:52
xiaolovesfan 发表于 2015-12-7 14:15
还是没懂,infert和case的字体怎么不一样?
实在是抱歉,这是论坛的原因,infert和case中间应该有一个美元"$"的符号,连起来的意思就是选中infert数据框的case变量,也即构建ROC曲线的真实患病信息。
这个问题不止一次出现了,不知道为什么论坛显示编码时,对这个符号不能正确显示。

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-1 23:51