编辑代码

# coding:utf-8
#JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 
print("Hello world!   -  python.jsrun.net .")
import pandas as pd
import statsmodels.api as sm
import statsmodels.formula.api as smf

# 构造基础数据
data = {
    'Education': ['文盲', '小学', '初中', '高中中专', '大专大学'],
    'Deaths': [43086, 21917, 10741, 4038, 1991],
    'Population': [1461971, 1870095, 3113930, 2091845, 687195]
}

df = pd.DataFrame(data)
df['Survivals'] = df['Population'] - df['Deaths']

# 构造逻辑回归需要的格式
expanded_rows = []
for _, row in df.iterrows():
    expanded_rows.append({
        'Education': row['Education'],
        'Died': 1,
        'Count': row['Deaths']
    })
    expanded_rows.append({
        'Education': row['Education'],
        'Died': 0,
        'Count': row['Survivals']
    })

logit_df = pd.DataFrame(expanded_rows)

# 逻辑回归拟合(按频数加权)
model = smf.glm(
    formula='Died ~ C(Education)',
    data=logit_df,
    family=sm.families.Binomial(),
    freq_weights=logit_df['Count']
).fit()

model_summary = model.summary2().as_text()
model_params = model.params

model_summary, model_params