Unformatted text preview:

22S:138Lab session 1Using R functions for simple Bayesian analysisAug. 31, 20071 Getting startedLog onto the lab computers using your HawkID and password. If you do not knowthese, you can use the temporary password for the day.2 The course home pageBring up Internet Explorer. Type in the URL (address) of the course homepage:www.stat.uiowa.edu/~kcowles/s138_2006Look at the sections entitled “Handouts,” “Datasets,” and “Web resources.”We will download two files from the “Handouts” s ection. Right-click on the filename bayes; th en choose “Save target as.” Save the file in the “Temp” folder ifyou 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.Different web browsers will do this differently.3 Using my R functionsI 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 betterfunctions 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 JimAlbert. Later in the semester we will use the software package WinBUGS to fitmore complicated mo dels.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")$valueIf you used a web browser that did not automatically add the extension .txt to thefilename, you will instead have to enter:1> s138.bayes <- source("c:\\temp\\bayes")$valueIf 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 en d of the “source” command.To run an R function, enter its name, followed by parentheses.> s138.bayes()Tr y 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" "0.999"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 likelihoods1 disease 0.001 0.95 0.052 no disease 0.999 0.05 0.95Input number of observations, followed by return key:1: 32:2Input the names of the observations, one on each line:1: +2: +3:-4:The function produces the following outpu t:Observation: +Update Based on Bayes’ TheoremModels Prior Like Prod Post[1,] "disease" "0.001" "0.95" "0.00095" "0.01866"[2,] "no disease" "0.999" "0.05" "0.04995" "0.98134"Observation: +Update Based on Bayes’ TheoremModels Prior Like Prod Post[1,] "disease" "0.01866" "0.95" "0.01773" "0.2654"[2,] "no disease" "0.98134" "0.05" "0.04907" "0.7346"Observation: -Update Based on Bayes’ TheoremModels Prior Like Prod Post[1,] "disease" "0.2654" "0.05" "0.01327" "0.01866"[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 outcomesThe 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 classifiesstudents as either “good,” “mediocre,” or “poor.” (These three types ofstudents will represent the models in this problem.) The principal knowsvery little about this particular student, but se is familiar with otherstudents who have transferred in from the same community. Of thesetransfer 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 stud ents that have come from this community,and so she believes that the student is good, mediocre, or poor with3respective probabilities .6, .3, and .1.The principal is also k nowledgeable about the types of grades in coresubjects earned by students of the three types. In these courses, thepossible grades are A, B, C, D, and F. For each course, suppose thata “good stud ent” 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 gradein a course. There are five possible observations, and the likelihoods arethe given probabilities of the five different grades f or the three differenttypes of stu dent.The principal will learn more ab out the ability of the student after he hastaken some classes. Suppose that he takes four classes and th e gradesin 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 principalobserves that the student gets two B’s and two C’s. What does theprincipal now think about the stu dent’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" "0.6" "0.3" "0.1"Input the likelihood of each possible outcome under each


View Full Document

UI STAT 4520 - Bayesian Statistics

Documents in this Course
Load more
Download Bayesian Statistics
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Bayesian Statistics and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Bayesian Statistics 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?