← 返回题库
初级

多元回归-工资决定因素

未完成
初级参考 完整示例代码供参考,建议自己理解后重新输入
def solve():
    import pandas as pd
    import statsmodels.api as sm
    import matplotlib.pyplot as plt
    import numpy as np
    
    df = pd.read_csv('https://liangdaima.com/static/data/wooldridge/wage1.csv')
    X = sm.add_constant(df[['educ', 'exper', 'tenure']])
    model = sm.OLS(df['wage'], X).fit()
    
    plt.figure(figsize=(10,6))
    coef_names = ['educ', 'exper', 'tenure']
    coef_values = [model.params[c] for c in coef_names]
    coef_errors = [model.bse[c] * 1.96 for c in coef_names]
    
    x_pos = np.arange(len(coef_names))
    plt.bar(x_pos, coef_values, yerr=coef_errors, capsize=5, color='steelblue', alpha=0.7)
    plt.axhline(y=0, color='k', linestyle='-', linewidth=0.5)
    plt.xticks(x_pos, coef_names)
    plt.title('工资决定因素回归系数', fontsize=14)
    plt.xlabel('变量', fontsize=12)
    plt.ylabel('系数值', fontsize=12)
    plt.grid(axis='y', alpha=0.3)
    plt.tight_layout()
    plt.show()
    
    print(round(model.params['educ'], 4))

示例

输入
solve()
期望输出
0.599
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码