NU Science Journal 2012; 9(1): 30- 44 การวิเคราะห์เบส์จากโปรแกรมวินบัËกสู ่โปรแกรมอาร์ อัชฌา อระวีพร Bayesian Analysis from WinBUGS Program to R Program Autcha Araveeporn สาขาวิชาสถิติ คณะวิทยาศาสตร์ สถาบันเทคโนโลยีพระจอมเกล้าเจ้าคุณทหารลาดกระบัง กรุงเทพมหานคร *Corresponding author. E-mail: [email protected]บทคัดย่อ โปรแกรมวินบัËกเป็นโปรแกรมทางสถิติสําหรับการประมาณค่าประมาณเบส์โดยใช้วิธีของ มาร์คอฟ เชน มอนติคาร์โล (MCMC) ซึ ÉงชุดคําสัÉง R2WinBUGS สร้างขึ ÊนเพืÉออํานวยความสะดวกให้ ผู้ใช้สามารถเรียกโปรแกรมวินบัËกได้จากโปรแกรมอาร์ซึ ÉงสามารถเขียนคําสัÉง ข้อมูล และประมวลผล โดยใช้โปรแกรมวินบั Ëกพร้อมๆกับโปรแกรมอาร์ โดยผลลัพธ์ทีÉได้สามารถเรียกดูได้จากโปรแกรมอาร์ ซึ Éงบทความนี Êนําเสนอตัวอย่างจากตัวแบบเบส์เพืÉอวิเคราะห์ด้วยโปรแกรมวินบัËก และเขียนคําสัÉง จากโปรแกรมอาร์เพืÉอเรียกโปรแกรมวินบัËกมาประมวลผล ซึ Éงผลลัพธ์จากตัวประมาณทีÉได้ให้ค่าทีÉ ใกล้เคียงกัน คําสําคัญ: โปรแกรมวินบัËก โปรแกรมอาร์ มาร์คอฟ เชน มอนติคาร์โล Abstract WinBUGS is statistical software to estimate Bayes estimator using Markov Chain Monte Carlo (MCMC) method. The R2WinBUGS package provides convenient functions to call WinBUGS from R program. It automatically writes the scripts and data by WinBUGS processing with R program. After the WinBUGS process has finished, the results can seen from R program. This article shows the example from Bayesian analysis with WinBUGS program and the code from R program that linked to Win BUGS for processing. However, the output from 2 program is similar for parameter estimation. Keywords: WinBUGS program, R program, Markov Chain Monte Carlo
15
Embed
Bayesian Analysis from WinBUGS Program to R Program
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
WinBUGS is statistical software to estimate Bayes estimator using Markov Chain Monte Carlo (MCMC) method. The R2WinBUGS package provides convenient functions to call WinBUGS from R program. It automatically writes the scripts and data by WinBUGS processing with R program. After the WinBUGS process has finished, the results can seen from R program. This article shows the example from Bayesian analysis with WinBUGS program and the code from R program that linked to Win BUGS for processing. However, the output from 2 program is similar for parameter estimation. Keywords: WinBUGS program, R program, Markov Chain Monte Carlo
NU Science Journal 2012; 9(1) 31
บทนา
กฎของเบสเปนสวนหนงของวชาความนาจะเปนโดยเปนการประมาณคาความนาจะเปนจากเหตการณหรอคาสงเกตของตวแปรสมโดยใชทฤษฎของเบส (Carlin and Louis, 2009) ซงเรมตนจากความนาจะเปนแบบมเงอนไข กฎการคณ และกฎความนาจะเปนรวม นอกจากนในวชาการอนมานเชงสถตกยงสามารถนาการวเคราะหตวแบบเบสมาใชในการประมาณคาแบบจด โดยพจาณาจากฟงกชนการแจกแจงของคาสงเกตของตวแปรสมเมอกาหนดคาพารามเตอรโดยเรยกวา ฟงกชนภาวะความนาจะเปน (Likelihood Function) ซงทราบลกษณะการแจกแจงของคาพารามเตอรซงเรยกวา ฟงกชนการแจกแจงโดยหลกเกณฑ (Prior Density Function) เพอใชประมาณคาพารามเตอรเมอกาหนดคาสงเกตจากฟงกชนการแจกแจงโดยประสบการณ (Posterior Density Function) วธการวเคราะหดวยตวแบบเบสคอนขางยงยากสาหรบผทไมถนดเนอหาวชาทางสถต จงไดมนกสถตพฒนาวธของ มารคอฟ เชน มอนตคารโล (Markov Chain Monte Carlo) หรอเรยกวาวธ MCMC โดยวธนเปนทนยมใชในการหาตวประมาณจากฟงกชนการแจกแจงโดยประสบการณจงมการพฒนาเปนโปรแกรมสาเรจรปเรยกวา โปรแกรมวนบก (WinBUGS) ซงยอมาจากคา Bayesian Inference Using Gibbs Sampling โดย Spiegelhalter และคณะ (2003) โปรแกรมวนบกเปนโปรแกรมทใชวธของ MCMC (Gilks et al., 1996)ในการวเคราะหตวแบบเบสทซบซอน ซงเปนการสมตวอยางดวยวธกบส (Gibbs Sampling) (Geman and Geman, 1984; Gelfand and Smith, 1990; Casella and George, 1992) เพอสรางคาจากวธ มารคอฟ เชน (Markov Chain) โดยการสมตวอยางจากการแจกแจง แลวจงใชวธการประมาณคาของมารคอฟ (Markov) ในการประมาณคาพารามเตอร ซงโปรแกรมวนบกเปนโปรแกรมทสามารถดาวนโหลดมาใชไดโดยไมเสยคาใชจายทเวบไซต http://www.mrc-bsu.cam.ac.uk/bugs/ ในการใชโปรแกรมวนบกผใชตองสรางตวแบบทใชในการประมวลผล เรยกขอมล และกาหนดคาเรมตนสาหรบจานวนรอบของมารคอฟ เชน ซงปญหาทเกดสาหรบผทใชโปรแกรมวนบกคอผลลพธทไดจากการประมวลผลแตละครงเกบอยในโปรแกรมวนบกเทานน แตกระนนในบางครงผใชตองการจาลองขอมลหลายๆ รอบเพอหาผลลพธจากการวเคราะหเบสและตองการเกบผลลพธในลกษณะของโปรแกรมอาร (R Program) (R Development Core Team, 2004) จงไดมการสรางชดคาสง R2WinBUGS เพอใชในการเรยกโปรแกรม WinBUGS เพอประมวลผลและกลบมาเกบผลลพธในโปรแกรมอาร
Becker, R.A. and Chambers, J.M. (1984). S. An Interactive Environment for Data Analysis and Graphics. Wadsworth and Brooks/Cole, Monterey.
Becker, R. A., Chambers, J. M., and Wilk, A. R. (1988). The NEW S Language-A Programming Environment for Data Analysis and Graphics. Chapman & Hall, New York.
Carlin, B. P. and Louis, T. A. (2009). Bayesian Methods for Data analysis. Chapman & Hall / CRC, Florida.
Casella, G. and George , E.I. (1992). Explaining the Gibbs Sampler. The American Statistician, 46, 167-174.
Chambers, J. M. (1998). Programming with Data. A Guide to the S Language. Springer-Verlag, New York.
44 NU Science Journal 2012; 9(1)
Chambers, J. M. and Hastie, T. J. (1992). Statistical Models in S. Chapman & Hall, NewYork. Gelfand, A.E. and Smith, A.F.M. (1990). Sampling-based Approaches to Calculating Marginal
Densities. Journal of the American Statistical Association, 85, 398-409. Geman, S. and Geman, D. (1984). Stochastic Relaxation, Gibbs Distribution and the Bayesian
Restoration of Images. IEEE Transactions on Pattern analysis and Machine Intelligence, 6, 721-741.
Gilk, W., Richardson, S. and Spiegelhalter. D. (1996). Markov Chain Monte Carlo in Practice. Chapman & Hall, London.
Ntzoufras, I. (2009). Bayesian Modeling Using WinBUGS, John Wiley & Sons, Inc., Hoboken, New Jersey, 2009.
Plummer, M., Best, N.G., Cowles, K., and Vines, K. (2004). coda : Output Analysis and Diagnostics for MCMC. R package version 0.9-1, URL http://www-fis.iarc.fr/coda/.
R Development Core Team. (2004). R: A Language and Environment for Statistical Computing. R Foundation forStatistical Computing, Vienna, Austria.
Spiegelhalter, D., Thomas, A., Best, N. and Lunn, D. (2003). WinBUGS User Manual, Version 1.4 , MRC Biostatistics Unit, Institute of Public Health and Department of Epidemiology and Public Health, Imperial College School of Medicine, UK.