初级
多元回归-工资决定因素
未完成
初级参考
完整示例代码供参考,建议自己理解后重新输入
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
👑
升级 VIP
解锁全部题目,畅通无阻地学习
- ✓ 解锁全部训练包所有题目
- ✓ 查看完整参考代码和提示
- ✓ 浏览器内直接运行 Python 代码
- ✓ 自动批改 + 进度追踪
30天
¥18
1年
¥99
2年
¥158
3年
¥199