DOC PREVIEW
UI STAT 4520 - Using R functions for simple Bayesian analysis

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

22S:138Lab session 1Using R functions for simple Bayesian analysisAug. 28, 20091 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_2009Look 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; then choose “Save target as.” Save the file on the Desktop if you wantto use it only during this lab. If you wish to access the file later without having todownload it again, save it to your own H: directory or to a flash drive.Right-click and save plot.beta as well.Notice whether the files are saved with or without a fi lename extension of .txt or.htm. Different web browsers will do this differently. You can right-click on the iconof the saved file, and then click “Properties” to see the full path name of th e savedfile.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 R libraries written by others, as well asthe 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 th e following command (this assumesyou saved into the Documents folder on the desktop; substitute your user name f orkcowles):1s138.bayes <- source("C:\\users\\kcowles\\Documents\\bayes.txt")$valueIf you used a web browser that did not automatically add the extension .txt tothe filename, or added a different extemsion, you will ins tead have to to modify thefilename accordingly when you read the function into R.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 n ame, f ollowed 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.952Input 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’ 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,3and 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 withrespective 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 for the three differenttypes of s tu dent.The principal will learn more about 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 s tudent’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:


View Full Document

UI STAT 4520 - Using R functions for simple Bayesian analysis

Documents in this Course
Load more
Download Using R functions for simple Bayesian analysis
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 Using R functions for simple Bayesian analysis 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 Using R functions for simple Bayesian analysis 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?