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

为何R与Python的Logistic模型置信区间结果存在差异?

问题:Logistic回归模型在R与Python中置信区间结果差异的原因

我正在构建Logistic回归模型,用于预测个体健康状况(0=良好,1=不佳)是否受二手烟暴露及其他人口统计学因素的影响。

R中的模型实现

我在R中使用以下代码构建模型(变量已重命名以标识类型):

shs_health_model <- glm(health_binary ~ continuous1 + continuous2 +
                          continuous3 + binary1 + binary2 + 
                          binary3 + binary_smoke, data=mydata, family="binomial")

提取优势比(OR)、置信区间和p值的代码:

shs_coef <- exp(cbind(OR = coef(shs_health_model), confint(shs_health_model)))
cbind(shs_coef, P = summary(shs_health_model$coefficients[,'Pr(>|z|)']))

R的输出结果:

变量OR2.5 %97.5 %P
(Intercept)4.31659240.4096554356.11209930.239687729
continuous11.03513810.993525651.07990270.101217153
continuous20.55326950.314464300.94861170.032469282
continuous30.97181810.927379031.01952140.231590917
binary16.83876671.2330639641.00868960.027803143
binary20.17968480.058992180.48677870.001274915
binary30.60385200.168439001.88541960.405517065
binary_smoke0.56012020.142534532.43500020.412941761

Python中的模型实现

合作者在Python中使用statsmodels.api的Logit函数,基于相同变量和数据集构建模型:

model = sm.Logit(y, X).fit()

提取优势比、置信区间和p值的代码:

import numpy as np
import pandas as pd
import statsmodels.api as sm

odds_ratios = np.exp(model.params)
conf = np.exp(model.conf_int())
p_values = model.pvalues
logistic_results['total_environ_index_cat'] = pd.DataFrame({
    'Odds Ratios': odds_ratios,
    'Confidence Intervals (Lower)': conf[0],
    'Confidence Intervals (Upper)': conf[1],
    'p-values': p_values
})

Python的输出结果(可见部分变量的置信区间值存在差异,但OR和p值与R的输出一致):

ORCI(low)CI(high)p
const4.3170.37749.420.24
continuous11.0350.9931.0790.101
continuous20.5530.3220.9520.032
continuous30.9720.9271.0180.232
binary16.8391.23337.9180.028
binary20.180.0630.5110.001
binary30.6040.1841.9820.406
binary_smoke0.560.142.2430.413

疑问

为何两者的输出会存在差异?并非其中一方的置信区间始终更宽,且据我所知两者计算的均为95%置信区间。


内容的提问来源于stack exchange,提问作者AIRE Study

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 23:17:32