วิธีแยกค่า p จากการถดถอยเชิงเส้นในแบบจำลองทางสถิติ
คุณสามารถใช้วิธีการต่อไปนี้เพื่อแยกค่า p สำหรับค่าสัมประสิทธิ์ในโมเดลการถดถอยเชิงเส้นให้พอดีโดยใช้โมดูล statsmodels ใน Python:
#extract p-values for all predictor variables for x in range(0, 3): print ( model.pvalues [x]) #extract p-value for specific predictor variable name model. pvalues . loc [' predictor1 '] #extract p-value for specific predictor variable position model. pvalues [0]
ตัวอย่างต่อไปนี้แสดงวิธีการใช้แต่ละวิธีในทางปฏิบัติ
ตัวอย่าง: แยกค่า P จากการถดถอยเชิงเส้นในแบบจำลองทางสถิติ
สมมติว่าเรามี DataFrame แพนด้าต่อไปนี้ซึ่งมีข้อมูลเกี่ยวกับชั่วโมงที่เรียน การสอบเตรียมสอบ และเกรดสุดท้ายที่นักเรียนในชั้นเรียนบางชั้นเรียนได้รับ:
import pandas as pd #createDataFrame df = pd. DataFrame ({' hours ': [1, 2, 2, 4, 2, 1, 5, 4, 2, 4, 4, 3, 6], ' exams ': [1, 3, 3, 5, 2, 2, 1, 1, 0, 3, 4, 3, 2], ' score ': [76, 78, 85, 88, 72, 69, 94, 94, 88, 92, 90, 75, 96]}) #view head of DataFrame df. head () hours exam score 0 1 1 76 1 2 3 78 2 2 3 85 3 4 5 88 4 2 2 72
เราสามารถใช้ฟังก์ชัน OLS() ของโมดูล statsmodels เพื่อให้พอดีกับ โมเดลการถดถอยเชิงเส้นหลายตัว โดยใช้ “ชั่วโมง” และ “การสอบ” เป็นตัวแปรทำนายและ “คะแนน” เป็น ตัวแปรตอบสนอง :
import statsmodels. api as sm #define predictor and response variables y = df['score'] x = df[['hours', 'exams']] #add constant to predictor variables x = sm. add_constant (x) #fit linear regression model model = sm. OLS (y,x). fit () #view model summary print ( model.summary ()) OLS Regression Results ==================================================== ============================ Dept. Variable: R-squared score: 0.718 Model: OLS Adj. R-squared: 0.661 Method: Least Squares F-statistic: 12.70 Date: Fri, 05 Aug 2022 Prob (F-statistic): 0.00180 Time: 09:24:38 Log-Likelihood: -38.618 No. Observations: 13 AIC: 83.24 Df Residuals: 10 BIC: 84.93 Df Model: 2 Covariance Type: non-robust ==================================================== ============================ coef std err t P>|t| [0.025 0.975] -------------------------------------------------- ---------------------------- const 71.4048 4.001 17.847 0.000 62.490 80.319 hours 5.1275 1.018 5.038 0.001 2.860 7.395 exams -1.2121 1.147 -1.057 0.315 -3.768 1.344 ==================================================== ============================ Omnibus: 1,103 Durbin-Watson: 1,248 Prob(Omnibus): 0.576 Jarque-Bera (JB): 0.803 Skew: -0.289 Prob(JB): 0.669 Kurtosis: 1.928 Cond. No. 11.7 ==================================================== ============================
ตามค่าเริ่มต้น ฟังก์ชัน summary() จะแสดงค่า p ของตัวแปรทำนายแต่ละตัวมีทศนิยมสูงสุดสามตำแหน่ง:
- ค่า P สำหรับการสกัดกั้น: 0.000
- ค่า P สำหรับชั่วโมง: 0.001
- ค่า P สำหรับการสอบ: 0.315
อย่างไรก็ตาม เราสามารถแยกค่า p แบบเต็มสำหรับตัวแปรทำนายแต่ละตัวจากแบบจำลองได้โดยใช้ไวยากรณ์ต่อไปนี้:
#extract p-values for all predictor variables for x in range(0, 3): print ( model.pvalues [x]) 6.514115622692573e-09 0.0005077783375870773 0.3154807854805659
สิ่งนี้ช่วยให้เราเห็นค่า p ที่มีทศนิยมมากขึ้น:
- ค่า P สำหรับการสกัดกั้น: 0.00000000651411562269257
- ค่า P สำหรับชั่วโมง: 0.0005077783375870773
- ค่า P สำหรับการสอบ: 0.3154807854805659
หมายเหตุ : เราใช้ 3 ในฟังก์ชัน range() เนื่องจากมีค่าสัมประสิทธิ์รวมสามค่าในแบบจำลองการถดถอยของเรา
นอกจากนี้เรายังสามารถใช้ไวยากรณ์ต่อไปนี้เพื่อแยกค่า p สำหรับตัวแปร “ชั่วโมง” โดยเฉพาะ:
#extract p-value for 'hours' only model. pvalues . loc [' hours '] 0.0005077783375870773
หรือเราสามารถใช้ไวยากรณ์ต่อไปนี้เพื่อแยกค่า p ของสัมประสิทธิ์ของตัวแปรในตำแหน่งเฉพาะของแบบจำลองการถดถอย:
#extract p-value for coefficient in index position 0 model. pvalues [0] 6.514115622692573e-09
แหล่งข้อมูลเพิ่มเติม
บทช่วยสอนต่อไปนี้จะอธิบายวิธีทำงานทั่วไปอื่นๆ ใน Python:
วิธีการดำเนินการถดถอยโลจิสติกใน Python
วิธีการคำนวณ AIC ของตัวแบบการถดถอยใน Python
วิธีการคำนวณ R-squared ที่ปรับแล้วใน Python