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

ความแปรปรวนรอบเส้นแนวโน้ม

ข้อมูลไม่จำเป็นต้องเป็นเส้นตรงอย่างสมบูรณ์แบบ — อาจมีความแปรปรวนแบบสุ่มหรือ "การกระจาย" ในการวัด ซึ่งส่งผลต่อความแปรปรวนของพารามิเตอร์โมเดลด้วย ความแปรปรวนของพารามิเตอร์นี้วัดด้วย "ค่าความคลาดเคลื่อนมาตรฐาน (standard error)" และตีความได้ว่าเป็น "ความไม่แน่นอน" ในการประมาณพารามิเตอร์ของโมเดล

ในแบบฝึกหัดนี้ จะใช้ ols จาก statsmodels เพื่อสร้างโมเดลและดึงค่า standard error ของแต่ละพารามิเตอร์

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

Introduction to Linear Modeling in Python

ดูคอร์ส

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

  • เก็บข้อมูลที่โหลดไว้แล้วลงใน DataFrame df โดยกำหนดชื่อ x_data เป็น times และ y_data เป็น distances
  • ใช้ model_fit = ols().fit() เพื่อ fit โมเดลเชิงเส้นในรูปแบบ formula="distances ~ times" กับ data=df
  • ดึงค่าจุดตัดแกน (intercept) ที่ประมาณได้จาก model_fit.params['Intercept'] และ standard error ของ intercept จาก model_fit.bse['Intercept']
  • ทำซ้ำสำหรับ slope จากนั้นพิมพ์ค่าทั้ง 4 พร้อมชื่อที่สื่อความหมาย

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

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

# Store x_data and y_data, as times and distances, in df, and use ols() to fit a model to it.
df = pd.DataFrame(dict(____=x_data, ____=y_data))
model_fit = ols(____="distances ~ times", data=____).____()

# Extact the model parameters and their uncertainties
a0 = model_fit.____['Intercept']
e0 = model_fit.____['Intercept']
a1 = model_fit.____['times']
e1 = model_fit.____['times']

# Print the results with more meaningful names
print('Estimate    of the intercept = {:0.2f}'.format(____))
print('Uncertainty of the intercept = {:0.2f}'.format(____))
print('Estimate    of the slope = {:0.2f}'.format(____))
print('Uncertainty of the slope = {:0.2f}'.format(____))
แก้ไขและรันโค้ด