


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!



Getting started
The course home page
Bring up Internet Explorer. Type in the URL (address) of the course homepage:
www.stat.uiowa.edu/~kcowles/s138_
Look at the sections entitled “Handouts,” “Datasets,” and “Web resources.”We will download two files from the “Handouts” section.
Right-click on the file
name
bayes
; then choose “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 withouthaving to download it again, save it to your own floppy disk in the A: drive.Right-click and save
plot.beta
as well.
Notice whether the files are saved with or without a filename extension of
.txt
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 mine are adequate for the assigned work at the begin-ning 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 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 If you used a web browser that did not automatically add the extension
.txt
to the
filename, you will instead have to enter:
s138.bayes <- source("c:\temp\bayes")$value If you have access to S-Plus and are using it instead of R for your homework, youwill not have to put the “$value” on the end of the “source” command.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 a student’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
The book
Bayesian Computation Using Minitab
by Jim Albert, Wadsworth Publish-
ers, 1996, describes a sequential Bayesian analysis using generalized Bayes’ theorem.Here is an excerpt from the book:
...suppose that a new student is attending a high school, and the principalis uncertain about the student’s ability.
This administrator classifies
students as either “good,” “mediocre,” or “poor.” (These three types ofstudents will represent the
models
in this problem.) The principal knows
very little about this particular student, but se is familiar with otherstudents who have transferred in from the same community.
Of these
transfer students, she believes that 60% were good, 30% were mediocre,and only 10% were poor. The administrator thinks that the new studentis representative of other students that have come from this community,and so she believes that the student is good, mediocre, or poor with
respective probabilities .6, .3, and .1.The principal is also knowledgeable about the types of grades in coresubjects earned by students of the three types.
In these courses, the
possible grades are A, B, C, D, and F. For each course, suppose thata “good student” gets either an A, B or C with probabilities .4, .4, .2,respectively. A “mediocre student” gets A, B, C, D, or F, with respectiveprobabilities .1, .2, .4, .2, .1, and a “poor students” will only get a C, D,or F, with probabilities .3, .5, .2. ... the
data
refers to a particular grade
in a course. There are five possible
observations
, and the
likelihoods
are
the given probabilities of the five different grades for the three differenttypes of student.The principal will learn more about the ability of the student after he hastaken some classes.
Suppose that he takes four classes and the grades
in the four courses are independent (which means that the student’schances of a particular grade in one course are not dependent on hisperformance in other courses).
At the end of the term, the principal
observes that the student gets two B’s and two C’s.
What does the
principal now think about the student’s ability?
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:
at the prompt. A new window should appear, containing a plot. In the upper border of the plot, Rwill tell you the “device number” of the plot, and whether or not this is the “active”device. To print the plot in the active device, enter dev.print(device=win.print) If you ran this function many times, you would get a new graphics window everytime. If you wish to print a plot
other
than the active one, you can make a different
Ending a session
R maintains what it calls the ”workspace,” in which it stores any function or otherobjects that have created during a given session. If you exit from R without savingthe workspace image, then any new work you have done will be lost.
If you are
working on a lab computer and wish to save your work to a floppy disk so that youcan reload it into R on another computer later, use the “File” menu to
change the
working directory
to the A: drive now.
To exit from R enter > q() You will be asked whether you want to save the workspace image. If you respond”No,” then any new work done during this session will be lost. If you are workingon your own computer, and you want these functions to remain associated with Rso you don’t have to “source” them in again next time, choose “Yes.” If you areworking on a lab computer and have previously changed the working directory toA:, then the workspace image will be saved there.Be sure to log off the computer (using the All Programs menu) before you leave.To access a saved workspace next time you use R:
(a) Change dir(b) Load workspace