This blog is about my research and what I am interested in. I will keep updating new information. Hope all of you enjoy it.

2009/01/19

How to use Winbugs and related issues

WinBUGS軟體安裝提示
作者:林永雋
日期:2009-01-19~20
<目的>
數學模型中除了自變量、依變量,就是參數了。原則上,不論自變量或參數,可以是固定效果(fixed effect)也可以是隨機效果(random effect)。雖然大一統計課所教的線性回歸有討論自變量是固定效果與是隨機效果的這兩種情形,但是都是把模型參數視為固定效果。假如要考慮某參數是隨機效果, 亦即視該參數為隨機變數,則推估出的參數會是一個機率分配,而不是一個固定的數值。既然要去推論出一個機率分配,則統計的方法也會較複雜一些,幸好我們可以應用貝式定理的觀念—將經驗法則換算成理性的預測—有效率地推論機率分配,而WinBUGS就是一個基於貝氏定理作模型參數推估的應用軟體,該軟體實作了以貝氏定理為基礎原理的演算法。
<電腦環境>
Windows
<教程>
目標一:用WinBUGS來實作Bayesian modeling

1.Install WinBUGS
1-1.Install WinBUGS 1.4
http://www.mrc-bsu.cam.ac.uk/bugs/winbugs/contents.shtml
http://www.mrc-bsu.cam.ac.uk/bugs/winbugs/WinBUGS14.exe
下載並安裝好WinBUGS14.exe

1-2.Patch WinBUGS (1.4->1.43)
http://www.mrc-bsu.cam.ac.uk/bugs/winbugs/patches.shtml
http://www.mrc-bsu.cam.ac.uk/bugs/winbugs/WinBUGS14_cumulative_patch_No3_06_08_07_RELEASE.txt

下載WinBUGS14_cumulative_patch_No3_06_08_07_RELEASE.txt
用WinBUGS將下載回來的檔案開啟成Text,然後遵循裡面的指示進行安裝

1-3.Register WinBUGS 1.4x
http://www.mrc-bsu.cam.ac.uk/bugs/winbugs/contents.shtml
http://www.mrc-bsu.cam.ac.uk/bugs/winbugs/WinBUGS14_immortality_key.txt
下載WinBUGS14_immortality_key.txt
用WinBUGS將下載回來的檔案開啟成Text,然後遵循裡面的指示進行安裝


目標二:匯入外來的模型或是自訂額外的模型

2.Install WBDev into WinBUGS
WinBUGS本身似乎不能自訂模型,WBDev應該是開發模型給WinBUGS用的套件
http://www.winbugs-development.org.uk/download_wbdev.html
下載並安裝好wbdev.exe
安裝的效果是Program Files/WinBUGS14/中會多出wbdev_01_09_04.txt
用WinBUGS將Program Files/WinBUGS14/wbdev_01_09_04.txt開啟成Text,然後遵循裡面的指示進行安裝

3.Install BlackBox 1.5 (not 1.6 !)
BlackBox應該是發展出WinBUGS的基礎
http://www.oberon.ch/blackbox.html
下載並安裝SetupBlackBox15.exe

4.匯入外來模型
索取wiener.odc與wienereta.odc這兩個檔案
為了能運用wiener.odc與wienereta.odc這兩個diffusion模型,必須在安裝WBDev與Blackbox之後,
將C:\Program Files\WinBUGS14中所有的檔案拷貝到C:\Program Files\BlackBox Component Builder 1.5,全部取代
並將將wiener.odc與wienereta.odc拷貝到C:\Program Files\BlackBox Component Builder 1.5\WBDev\Mod,並用BlackBox分別開啟這兩個檔案按Ctrl+K組譯,
這麼做會使得C:\Program Files\BlackBox Component Builder 1.5\WBDev\Code與C:\Program Files\BlackBox Component Builder 1.5\WBDev\Sym產生新的檔案
接著將C:\Program Files\BlackBox Component Builder 1.5\WBDev\Rsrc\Distributions.odc用BlackBox開啟,修改成:
s ~ "dnorm.trunc0"(s, s)I(s, s) "WBDevUnivariateTemplate.Install"
s ~ "dwiener"(s, s, s, s)I(s, s) "WBDevWiener.Install"
s ~ "dwiener.eta"(s, s, s, s, s)I(s, s) "WBDevWienerEta.Install"
END
最後把整個C:\Program Files\BlackBox Component Builder 1.5\WBDev資料夾內的檔案覆蓋回C:\Program Files\WinBUGS14\WBDev即可


目標三:如果能用R操作WinBUGS會更便利

5.Install R 2.81
http://cran.csie.ntu.edu.tw/
下載並安裝R-2.8.1-win32.exe

6.Run WinBUGS from R
http://www.stat.columbia.edu/~gelman/bugsR/

6-1.Just once in R
install.packages("arm")
install.packages("BRugs")
install.packages("Matrix")
install.packages("lme4")

6-2.Each time in R
library("arm")
每次從R呼叫WinBUGS之前,只輸入這個就會自動引用其他三個安裝好的套件


<參考文件>
AppendixBWinbugs.pdf
manual14.pdf WinBUGS User Manual by Spiegelhalter, D. et. al

沒有留言:

About Me

我的相片
I am Yang Cheng-Ta. I am a assistant professor at the department of psychology and institute of cognitive science, National Cheng Kung University (NCKU). I graduated from National Taiwan University (NTU). My supervisors were Prof. Yeh Yei-Yu and Prof. Hsu Yung-Fong. My major is cognitive psychology and mathematical psychology. My research interests are human attention and memory. My research topic is about why people cannot detect a change in the visual environment which is so-called “change Blindness”. I investigate the mechanism underlying change detection and how people make a correct detection decision. I am also interested in the mathematical modeling of human behavior. Besides, I like to play volleyball, go to gym, and swim when I am free. I also like to listen to the Chinese opera and still keep learning it. These are brief descriptions about me. If you are interested in me or share interests with me, contact with me at yangct@mail.ncku.edu.tw.