Appendix D: Using the Included R Functions
William M. Bolstad · 2007
R functions for performing Bayesian analysis and for doing Monte Car10 simulations are included.The address may be downloaded from the Web page for this text on the site www.wiley.com.The R functions are zipped up in a package called Bolstad-0.2-1 1. zip.The latest version of R (currently 2.41) may always be found at www,r-project.org.Compiled versions of R for Linux, Mac 0s (System 8.6 to 9.1 and Mac 0s X), Mac 0s X (DarwidXl 1) and Windows (95 and later), and the source code (for those who wish to compile R themselves) may also be found at this address.To install R for Windows, double click on the file nv241.exeand follow the installer functions.In the following discussion it is assumed that you have copied the file Bolstad-0.2-ll.zip to a location on your computer.You can find it in this way: 1 .Start R from the Start menu or by double clicking on the icon on your desktop.Title: a brief title that gives some idea of what the function is supposed to do or show Description: a fuller description of the what the function is supposed to do or show Usage: the formal calling syntax of the function Arguments: a description of each of the arguments of the function Values: a description of the values (if any) returned by the function See also: a reference to related functions Examples: some examples of how the function may be used.These examples may be run either by using the example command (see above) or copied and pasted into the R console window variable ordering of arguments.An R function may have arguments for which the author has specified a default value.Let's take the function binobp as an example.The syntax of binobp is binobp (x, n, a = 1, b = 1, ret = FALSE).The function takes five arguments x, n, a, b, and ret.However, the author has specified default values for a, b, and ret, namely a = 1, b = 1 and ret = FALSE.This means that the user only has to supply the arguments x and n.Therefore the arguments a, b and ret are said to be optional or default.In this example, by default, a betu(a = 1, b = 1) prior is used and the prior, likelihood, and posterior distributions (along with some associated information) are not returned (ret = FALSE).Hence the simplest example for binobp is given as binobp (6, 8 ) .If the user wanted to change the prior used, say to betu(5,6), then they would type binobp ( 6 , 8 , 5, 6 ) .There is a slight catch here, which leads into the next feature.Assume that the user wanted to use a beta( 1,l) prior, but wanted to return the output.One might be tempted to type binobp (6 , 8 , FALSE) .This is incorrect.R will think that the value FALSE is the value being assigned to the parameter a, and convert it from a logical value, FALSE, to the numerical equivalent, 0, which will of course give an error because the parameters of the beta distribution must be greater than zero.The correct way to make such a call is to use named arguments, such as binobp (6, 8 , ret=FALSE) .This specifically tells R which argument is to be assigned the value FALSE.This feature also makes the calling syntax more flexible because it means that the order of the arguments does not need to be adhered to.For example, binobp (n=8, x=6, ret=FALSE, a=l , b=3 would be a perfectly legitimate function call. CHAPTER 2: SCIENTIFIC DATA GATHERINGIn this chapter we use the function sscsample to perform a small-scale Monte Car10 study on the efficiency of simple, stratified, and cluster random sampling on 10.