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