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

Trace plot ของ Markov chain

Trace plot คือการแสดงภาพพฤติกรรมตามลำดับเวลา (longitudinal behavior) ของ Markov chain โดยเฉพาะอย่างยิ่ง trace plot ของ chain \(m\) จะพล็อตค่าที่สังเกตได้ของ chain (แกน y) เทียบกับหมายเลข iteration ที่สอดคล้องกัน (แกน x)

จะสร้าง trace plot ของ chain \(m\) ด้วยสองวิธี ได้แก่ การใช้ฟังก์ชัน plot() ที่มีอยู่ในตัวกับออบเจ็กต์ mcmc.list ชื่อ sleep_sim และการใช้ ggplot() กับออบเจ็กต์ data.frame ชื่อ sleep_chains เพื่อควบคุมกราฟิกได้ละเอียดยิ่งขึ้น (รวมถึงการวิเคราะห์ในบทถัดไป) ทั้ง sleep_sim และ sleep_chains อยู่ใน workspace แล้ว:

sleep_sim <- coda.samples(model = sleep_jags, variable.names = c("m", "s"), n.iter = 10000)
sleep_chains <- data.frame(sleep_sim[[1]], iter = 1:10000)

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

การสร้างแบบจำลอง Bayesian ด้วย RJAGS

ดูคอร์ส

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

  • ใช้ plot() กับ sleep_sim โดยตั้งค่า density = FALSE เพื่อสร้าง trace plot สำหรับ chain \(m\) และ \(s\) หมายเหตุ: 10,000 Iterations ที่บันทึกไว้เริ่มต้นหลังจากช่วง "burn-in" ที่ตัดตัวอย่างออกไป ดังนั้น Iterations จึงไม่ได้เริ่มต้นที่ 1

  • ใช้ ggplot() ร่วมกับ layer geom_line() กับ sleep_chains เพื่อสร้าง trace plot ของ chain \(m\) ขึ้นมาใหม่

  • ซูมเข้า: สร้าง trace plot ด้วย ggplot() สำหรับ 100 iteration แรก ของ chain \(m\)

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

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

# Use plot() to construct trace plots of the m and s chains


# Use ggplot() to construct a trace plot of the m chain
ggplot(___, aes(x = ___, y = ___)) + 
    geom_line()

# Trace plot the first 100 iterations of the m chain
ggplot(___, aes(x = ___, y = ___)) + 
    geom_line()
แก้ไขและรันโค้ด