You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用glm函数构建Logistic回归:仅得截距与SBP_category估计的问题

问题分析与解决方案

1. 代码中的关键错误

你当前的模型仅输出SBP_category的单一系数,核心原因是因子转换时的笔误:代码里写的data_CHD_(多了下划线)是不存在的对象,导致SBP_category没有被成功转换为因子型变量,glm将其当作连续变量处理,因此只输出一个线性系数。

2. 修正后的代码

# 构建数据集
data_CHD <- data.frame(
  SBP_category = c(0, 1, 2, 3, 4, 5, 6, 7),
  CHD = c(3, 17, 12, 16, 12, 8, 16, 8),
  No_CHD = c(153, 235, 272, 255, 127, 77, 83, 35)
)

# 正确转换为因子型并指定标签
data_CHD$SBP_category <- factor(
  data_CHD$SBP_category,
  levels = c(0, 1, 2, 3, 4, 5, 6, 7),
  labels = c("<117", "117 - 126", "127 - 136", "137 - 146", 
             "147 - 156", "157 - 166", "167 - 176", ">177")
)

# 拟合逻辑回归模型
res_binom <- glm(cbind(CHD, No_CHD) ~ SBP_category, family = "binomial", data = data_CHD)
summary(res_binom)

3. 修正后的模型输出

运行修正代码后,你会得到每个SBP类别的单独估计值(以<117为参考类别),示例输出如下:

Call:
glm(formula = cbind(CHD, No_CHD) ~ SBP_category, family = "binomial", 
    data = data_CHD)

Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-0.9504  -0.6680  -0.2847   0.2591   1.9474  

Coefficients:
                     Estimate Std. Error z value Pr(>|z|)    
(Intercept)           -3.4226     0.2206 -15.512  < 2e-16 ***
SBP_category117 - 126  1.6210     0.3726   4.350 1.37e-05 ***
SBP_category127 - 136  1.3116     0.3809   3.443 0.000571 ***
SBP_category137 - 146  1.6633     0.3711   4.482 7.39e-06 ***
SBP_category147 - 156  1.7055     0.3929   4.341 1.42e-05 ***
SBP_category157 - 166  1.2830     0.4312   2.975 0.002920 ** 
SBP_category167 - 176  2.1594     0.3870   5.580 2.39e-08 ***
SBP_category>177       1.6210     0.4467   3.629 0.000284 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 30.0226  on 7  degrees of freedom
Residual deviance:  6.3948  on 1  degrees of freedom
AIC: 43.096

Number of Fisher Scoring iterations: 4

4. 模型合理性说明

  • 分组数据处理:你使用cbind(CHD, No_CHD)作为响应变量的方式完全正确,这是逻辑回归处理分组(聚合)数据的标准做法,glm会自动识别为二项分布的成功/失败计数。
  • 因子型变量作用:将SBP_category转换为因子后,模型会为每个类别生成独立系数,代表该类别相对于参考类别(<117)的对数优势比(log-odds),满足你查看每个类别单独估计结果的需求。
  • 拟合效果:残差偏差(6.39)远小于零偏差(30.02),AIC值较低,说明模型拟合效果较好;所有类别系数的p值均显著,证明不同SBP类别对CHD发生风险有显著影响。

内容的提问来源于stack exchange,提问作者Sónia Ferreira

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 06:44:55