擬合線性迴歸模型
自 2015 年起,每年都會在歐洲的 IT 專業人員中進行匿名薪資調查。到了 2018 年,有數百位受訪者自願參與。 調查資料包含受訪者的年資與目前薪資。
你要分析這兩個變數之間的關係,看看更長的年資是否會帶來更高或更低的薪資。
自變數是 experience_years,依變數是 current_salary。
資料已載入為 data,並已將 statsmodels.api 與 pandas 分別匯入為 sm 與 pd。
本練習屬於課程
使用 Python 分析問卷資料
練習說明
- 定義變數
x與y。 - 加入常數項。
- 執行
OLS()迴歸並對模型呼叫.fit()。 - 列印摘要表。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Define variable, x and y
x = salary_survey.____.____
y = salary_survey.____.____
# Add the constant term
x = ____.____(x)
# Perform .OLS() regression and fit
result = ____.____(y,x).____()
# Print the summary table
print(____.____())