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,000Iterationsที่บันทึกไว้เริ่มต้นหลังจากช่วง "burn-in" ที่ตัดตัวอย่างออกไป ดังนั้นIterationsจึงไม่ได้เริ่มต้นที่ 1ใช้
ggplot()ร่วมกับ layergeom_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()