เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

การถดถอยเชิงเส้น

ทำการถดถอยเชิงเส้นสำหรับข้อมูลปี 1975 และ 2012 จากนั้นใช้ pairs bootstrap เพื่อประมาณค่าพารามิเตอร์ของการถดถอย แล้วรายงานช่วงความเชื่อมั่น 95% สำหรับค่า slope และ intercept ของเส้นถดถอย

จะใช้ฟังก์ชัน draw_bs_pairs_linreg() ที่เขียนไว้ในบทที่ 2

เพื่อทบทวน รูปแบบการเรียกใช้คือ draw_bs_pairs_linreg(x, y, size=1) และจะคืนค่า bs_slope_reps กับ bs_intercept_reps ข้อมูลความยาวปากนกเก็บอยู่ใน bl_1975 และ bl_2012 ส่วนข้อมูลความลึกปากนกเก็บอยู่ใน bd_1975 และ bd_2012

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Statistical Thinking in Python (ตอนที่ 2)

ดูคอร์ส

คำแนะนำการฝึกหัด

  • คำนวณค่า slope และ intercept สำหรับชุดข้อมูลปี 1975 และ 2012
  • สุ่ม pairs bootstrap 1000 ครั้งสำหรับการถดถอยเชิงเส้นโดยใช้ฟังก์ชัน draw_bs_pairs_linreg()
  • คำนวณช่วงความเชื่อมั่น 95% สำหรับค่า slope และ intercept

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# Compute the linear regressions
slope_1975, intercept_1975 = ____
slope_2012, intercept_2012 = ____

# Perform pairs bootstrap for the linear regressions
bs_slope_reps_1975, bs_intercept_reps_1975 = \
        ____
bs_slope_reps_2012, bs_intercept_reps_2012 = \
        ____

# Compute confidence intervals of slopes
slope_conf_int_1975 = ____
slope_conf_int_2012 = ____
intercept_conf_int_1975 = ____

intercept_conf_int_2012 = ____


# Print the results
print('1975: slope =', slope_1975,
      'conf int =', slope_conf_int_1975)
print('1975: intercept =', intercept_1975,
      'conf int =', intercept_conf_int_1975)
print('2012: slope =', slope_2012,
      'conf int =', slope_conf_int_2012)
print('2012: intercept =', intercept_2012,
      'conf int =', intercept_conf_int_2012)
แก้ไขและรันโค้ด