Docsity

Lab Session 1: Bayesian Analysis using R Functions - Prof. Mary Kathryn Cowles, Lab Reports of Statistics

Toggle sidebar
bg1
22S:138
Lab session 1
Using R functions for simple Bayesian analysis
Aug. 29, 2003
1Getting started
Log onto the lab computers using you HawkID and password. If you do not know
these, you can use the temporary password for the day.
2The course home page
Bring up Internet Explorer or Netscape. Type in the URL (address) of the course
homepage:
www.stat.uiowa.edu/~kcowles/s138_2000
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 the
file later without having to download it again, save it to your own floppy disk in the
A: drive.
Right-click and save plot.beta as well.
3Using 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 the
beginning of the semester. They are based on the Minitab and Matlab macros by
Jim Albert. Later in the semester we will use the software package WinBUGS to fit
more 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 your
R work area before using it.
To install myfunction 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()
1
Try using this function to work the problem we did in lecture with Tasha’s disease
status and the screening test. The prompts, and your appropriate responses are as
follows:
Input number of models, followed by return key
(Leave blank and hit return to exit):
(Leave blank and hit return to exit):
1: 2
2:
Input names of models, one on each line:
1: disease
2: no disease
3:
Input prior probabilities of models, one on each line:
1: .001
2: .999
3:
Input number of possible outcomes, followed by return key:
1: 2
2:
Input the name of each possible outcome, one on each line:
1: +
2: -
3:
[1] "2" "disease" "no disease" "0.001" "0.999"
Input the likelihood of each possible outcome under each model:
Model 1 :
1: .95
2: .05
3:
Model 2 :
1: .05
2: .95
3:
[1] 2 2
Table of priors and likelihoods
1 disease 0.001 0.95 0.05
2 no disease 0.999 0.05 0.95
Input number of observations, followed by return key:
1: 3
2:
Input the names of the observations, one on each line:
1: +
2: +
3:-
4:
The function produces the following output:
2
pf3

Partial preview of the text

Download Lab Session 1: Bayesian Analysis using R Functions - Prof. Mary Kathryn Cowles and more Lab Reports Statistics in PDF only on Docsity!

22S:

Lab session 1

Using R functions for simple Bayesian analysis

Aug. 29, 2003

Getting started

Log onto the lab computers using you HawkID and password. If you do not knowthese, you can use the temporary password for the day. 2

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"

[2,] "no disease" "0.7346" "0.95" "0.69787" "0.98134" Notice that at each step, the “posterior” from the previous step becomes the new“prior.” 4

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