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

การใช้ smoothing parameters เพื่อป้องกันการ overfit

Smoothing parameter ทำหน้าที่สร้างสมดุลระหว่าง likelihood และความคดเคี้ยวของเส้นโค้ง เพื่อให้โมเดล fit ข้อมูลได้ดีที่สุด ในแบบฝึกหัดนี้ จะได้ศึกษา smoothing parameters และฝึก fit โมเดลด้วยค่า smoothing parameter ที่กำหนดไว้ล่วงหน้าแบบต่างๆ

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

การสร้างโมเดลแบบไม่เชิงเส้นด้วย Generalized Additive Models (GAMs) ใน R

ดูคอร์ส

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

  • ดูค่า smoothing parameter (\(\lambda\)) ของโมเดล gam_mod ที่กำหนดให้ โดยดึงค่า sp ออกจากโมเดล
  • Fit โมเดลสองแบบกับข้อมูล mcycle โดยให้ accel เป็น smooth function ของ times และกำหนด smoothing parameter เป็น:
    • 0.1
    • 0.0001
  • Visualize โมเดลทั้งสอง

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

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

library(mgcv)
# Extract the smoothing parameter
gam_mod <- gam(accel ~ s(times), data = mcycle, method = "REML")
___

# Fix the smoothing parameter at 0.1
gam_mod_s1 <- gam(accel ~ s(times), data = mcycle, sp = ___)

# Fix the smoothing parameter at 0.0001
gam_mod_s2 <- gam(___)

# Plot both models
par(mfrow = c(2, 1))
plot(gam_mod_s1, residuals = TRUE, pch = 1)
plot(gam_mod_s2, residuals = TRUE, pch = 1)
แก้ไขและรันโค้ด