

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Lab session 1
Using R functions for simple Bayesian analysis
Aug. 29, 2003
Getting started
The course home page
Bring up Internet Explorer or Netscape. Type in the URL (address) of the coursehomepage:
www.stat.uiowa.edu/~kcowles/s138_
We will download two files from the “Handouts” section.
Right-click on the file
name
bayes
; then choose “Save link as” or “Save target as.” Save the file in the
“Temp” folder if you want to use it only during this lab. If you wish to access thefile later without having to download it again, save it to your own floppy disk in theA: drive.Right-click and save
plot.beta
as well.
Using my R functions
I have written some functions to make some simple Bayesian calculations and graph-ics easy.
Those of you who know R or Splus are welcome to write and use better
functions of your own, but these of mine are adequate for the assigned work at thebeginning of the semester. They are based on the Minitab and Matlab macros byJim Albert. Later in the semester we will use the software package WinBUGS to fitmore complicated models.Bring up R from the All Programs menu.You will need to use the “source” command to install each of my functions in yourR work area before using it.To install my function to do simple sequential Bayesian analysis with Bayes theorem,and to give it the name “s138.bayes,” enter the following command: > s138.bayes <- source("c:\temp\bayes.txt")$value To run an R function, enter its name, followed by parentheses. > s138.bayes()
Try using this function to work the problem we did in lecture with Tasha’s diseasestatus and the screening test. The prompts, and your appropriate responses are asfollows: Input number of models, followed by return key(Leave blank and hit return to exit):(Leave blank and hit return to exit):1: 22:Input names of models, one on each line:1: disease2: no disease3:Input prior probabilities of models, one on each line:1: .0012: .9993:Input number of possible outcomes, followed by return key:1: 22:Input the name of each possible outcome, one on each line:1: +2: -3:[1] "2"
"disease"
"no disease" "0.001"
Input the likelihood of each possible outcome under each model:Model 1 :1: .952: .053:Model 2 :1: .052: .953:[1] 2 2Table of priors and likelihoods 1
disease 0.
no disease
Input number of observations, followed by return key:1: 32:Input the names of the observations, one on each line:1: +2: +3:-4: The function produces the following output:
Observation: + Update Based on Bayes’ Theorem
Models
Prior
Like
Prod
Post
[1,] "disease"
[2,] "no disease" "0.999" "0.05" "0.04995" "0.98134"
Observation: + Update Based on Bayes’ Theorem
Models
Prior
Like
Prod
Post
[1,] "disease"
[2,] "no disease" "0.98134" "0.05" "0.04907" "0.7346"
Observation: - Update Based on Bayes’ Theorem
Models
Prior
Like
Prod
Post
[1,] "disease"
An example with 3 possible models and 5 possible outcomes
I have attached an excerpt from a book by Jim Albert, which describes a sequentialBayesian analysis using generalized Bayes’ theorem.
The R function may be used
to carry out this example as follows: > s138.bayes()Input number of models, followed by return key(Leave blank and hit return to exit):1: 32:Input names of models, one on each line:1: good2: mediocre3: poor4:Input prior probabilities of models, one on each line:1: .62: .33: .14:Input number of possible outcomes, followed by return key:1: 52:
Input the name of each possible outcome, one on each line:1: A2: B3: C4: D5: F6:[1] "3"
"good"
"mediocre" "poor"
Input the likelihood of each possible outcome under each model:Model 1 :1: .42: .43: .24: 05: 06:Model 2 :1: .12: .23: .44: .25: .16:Model 3 :1: 02: 03: .34: .55: .26:[1] 3 5Table of priors and likelihoods 1
good
mediocre
poor
Input number of observations, followed by return key:1: 42:Input the names of the observations, one on each line:1: B2: B3: C4: C5:
Observation: B Update Based on Bayes’ Theorem