1 / 28

More BUGS

More BUGS. “You can, for example, never foretell what any one man will do, but you can say with precision what an average number will be up to. Individuals vary, but percentages remain constant. So says the statistician.” Sherlock Holmes The Sign of Four.

una
Download Presentation

More BUGS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More BUGS “You can, for example, never foretell what any one man will do, but you can say with precision what an average number will be up to. Individuals vary, but percentages remain constant. So says the statistician.” Sherlock Holmes The Sign of Four James B. Elsner Department of Geography, Florida State University Portions of this presentation are taken from Implementing MCMC Models in WinBUGS, Jim Peterson, September 6, 2004.

  2. WinBUGS (Bayesian inference Using Gibbs Sampling) is relatively user-friendly computer software for Bayesian analysis using Markov Chain Monte Carlo (MCMC) methods. WinBUGS implements both Gibbs and Metropolis-Hastings algorithms. It was developed and is supported jointly between Medical Research Council (UK) Biostatistics Unit and the Imperial College School of Medicine at St Mary's, London. The software is currently free and can be downloaded at the BUGS website. However, users must register at the websiteto obtain a license to run the software. While at the BUGS website, users are encouraged to download CODA--a suite of Splus/R functions for convergence diagnostics. WinBUGS comes with extensive documentation and numerous examples, primarily from medical applications. Statistical modeling often begins by building a graphical representation of candidate models. WinBUGS can be used to build graphical models using a specialized drawing tool DoodleBUGS. The graphical models then can be fit in WinBUGS or alternatively, WinBUGS will automatically write the text-based WinBUGS language code. While graphical models are indeed useful, we find it easier to work directly in the WinBUGS language code format. Note that WinBUGS allows users to save models in WinBUGS “.odc” and other formats. It might be a better idea to save programs in plain text “.txt” format.

  3. Model and data structure in WinBUGS We illustrate the WinBUGS language format using the simple example of estimating a mean and variance of 20 observations using a diffuse prior for the mean and variance (precision), below. # the model model { # Prior on normal mean mu ~ dnorm(0,0.00001) # Prior on normal precision tau ~dgamma(0.01, 0.01) # variance = 1/precision sigma2 <-1/tau # likelihood note that tau is precision not variance for (i in 1:N) { y[i] ~ dnorm(mu,tau)} } # the data list(N=20, Y=c(98,160,136,128,130,114,123,134,128,107,123,125,129,132,154,115,126,132,136,130))

  4. Models are delineated using brackets “{}” and are headed by the model statement. Comments are delimited using “#”. Random variables (also called stochastic nodes) are represented by the variable name (Note: WinBUGS iscase sensitive.) followed by a twiddle “~”, the distribution name, and a comma-separated list of parameters enclosed in parentheses “( )”. For the above example, the parameter mu is normally distributed “dnorm” with a mean of zero and precision 0.00001, whereas the parameter tau is distributed as a gamma “dgamma” with shape 0.01 and inverse scale 0.01. Logical assignments are represented by the variable name followed by a left pointing arrow “<-” and the logical expression. For example, sigma2 (i.e., the variance) is calculated as one divided by tau. As we shall see, a link function also can be specified on the left hand side of a logical assignment, such as a logit link used in the logistic regression model: logit(p[i]) <- beta0 + beta1 * x[i] Arrays are indexed using brackets “[ ]” and are in the form [row, column]. Basic integer operations such as addition, subtraction and multiplication are allowed within the brackets, e.g., [(i +1), j]. Other array conventions include: i:j includes the values, i, i + 1, ..., j. x[] includes all values of vector x. x[,2] includes all values of the second column of a two-dimensional array x. For the above example, Y[i] represents the ith value in the vector Y.

  5. Loops are used for a variety of tasks including, most importantly, reading in data. They are specified using the for-loop structure “for (j in a:b)” delimited using brackets “{}”. For the above example, a for-loop is used to read a row vector (length N) of data Y for updating the mean mu and precision tau. There are several acceptable data formats for WinBUGS. Here we will discuss most commonly used, data lists. Data list format generally follows that of Splus and can consist of scalars, row vectors, and arrays. Complete data lists are delineated using parentheses “( )” and are headed by the list statement. The elements of the data list are separated by a comma. The format of the data list depends on the type of data and multiple data types (scalar, vector, array) can be contained within a single data list. For scalars, the format consists of the variable name, an equal sign, and the value of the variable. For the above example, the number of observations N is a scalar with a value of 20. Row vectors are identified using the variable name, an equal sign, and the vector values separated by commas and contained within “c(comma separated values here).” The format for data arrays is a bit more complicated and consists of the variable name, an equal sign, and array values separated by commas and contained with the declaration of the array format. For example, below is an example of data in a 4 row, 3 column array X. The statement “structure(.data = c(” declares that the following data will be in an array format and the last statement “.Dim = c(4, 3))” declares a 4 row by 3 column array. X = structure(.Data = c(1, 199, 61, 3, 199, 29, 9, 180, 19, 13, 200, 44), .Dim = c(4, 3)) Referencing vector and array data within the WinBUGS model follows the same format as the data list. For example, the 4th element in the vector y above is 128 and is identified as Y[4]. Similarly, the value of the variable in the 2nd row, 3rd column of the array X is 29 and is identified as X[2,3].

  6. Commonly used WinBUGS distributions r ~ dbin(p, n) Binomial with n trials and probability of success p r ~ dpois(lambda) Poisson with mean lambda p ~ dbeta(a, b) Beta with parameters a, b x ~ dgamma(a, b) Gamma with shape a and inverse scale b x ~ dnorm(mu, tau) Normal with mean mu and precision (1/variance) tau Commonly used WinBUGS functions + addition, - subtraction, * multiplication, / division abs(x) absolute value of x, exp(x) exponent, log(x) natural logarithm, ln(x) logit(p) logit link, ln(p/ (1 - p)) max(x1, x2) returns x1 if x 1 > x 2; x 2 otherwise mean(v) mean of components in vector v, min(x1, x2) returns x 1 if x 1 < x 2; x 2 otherwise sqrt(x) square root of x, round(x) round x to nearest integer, sd(v) standard deviation of components of vector v, sum(v) sum of components in vector v trunc(x) returns greatest integer less than or equal to x.

  7. Compiling and fitting a model in WinBUGS Once the model is written (or doodled then converted) and data are formatted, the model must be compiled. Models are compiled in WinBUGS using the following 5 steps: Step 1: Select “Model” and “Specification” from the WinBUGS menu. The Specification Tool window should appear. Step 2: Highlight the model statement at the beginning of the model (e.g., by double clicking on the statement) and click on “check model” in the Specification Tool window. If there are any problems with the model syntax, a message will be displayed at the bottom of model window. Step 3: Highlight the list statement at the beginning of data list and click on “load data.” Step 4: Click on “compile.” If there are any problems with the model, a message will be displayed at the bottom of model window. Often it is very useful to run multiple MCMC chains simultaneously using different initial values for the parameters. To specify multiple chains, enter the number of chains in the location indicated in the Specification Tool window before clicking “compile.” Step 5: Load initial values (a) by clicking on the “gen inits,” which randomly generates initial values or (b) by entering specific values using data lists and by highlighting the appropriate list statements. Sometimes WinBUGS will select a bad initial value when using the “gen inits” option. To avoid these problems, we recommend choosing (several) plausible initial values.

  8. Model output and monitoring in WinBUGS Parameter estimates will only be provided for the parameters that are explicitly identified before model-fitting. Once a model has been compiled, we then need to identify the parameters of interest and specify other factors such as burn-in and thinning. This can be accomplished by selecting “Inference” and “Samples…” from the WinBUGS menu. The Sample Monitoring Tool will then appear. Type the name of the parameter in the “node” window (remember: case sensitive), choose the burn in by entering the value in the “beg” window, enter the desired thinning in the “thin,” select the statistics desired (e.g., mean, median, etc.), and click on “set”. Repeat the process for additional parameters. To monitor the progress of each chain, enter an asterisk “*” in the node window and click on “trace.” To initiate model-fitting, select “Model” and “Update” from the WinBUGS menu and the Update Tool will appear. Enter the number of MCMC iterations in the “updates” window and thinning (if desired) and click on “update.” The trace will reveal the progress of each MCMC chain. After the iterations are complete, click on “density” to examine the posterior distribution of the parameter estimates and “stats” to examine the parameter statistics.

  9. Example #1: Inference is required about the proportion of people in the population who are unemployed. Let us call this value “pi”. Think: The true value of pi is in the range between 0 and 1, where 0 means no one is unemployed and 1 means everyone is. Realistically, we might have some prior information on the value of pi. For instance, newspaper reports, economic theory, previous surveys, etc. Your prior can be as informative as you like. To get things started, lets assume that you choose a prior as a random value from a beta distribution restricted between the values of 0.1 and 0.6. Specifying your prior Create a directed graph. In WinBUGS, select Doodle > New... In the New Doodle dialog, click ok on the default options. You will see a blank worksheet called “untitled”. Left click anywhere in the middle of the sheet to create a node. A node has a name and type along with other characteristics and parameters depending on its type. Notes: To delete a node, highlight it, then Ctrl-Delete. To highlight a node, click on it. Use the Help > Doodle helpto learn more.

  10. Click on name, then type “pi”. Note the graphical node is labeled simultaneously. Leave type as “stochastic”. Click on density and change to “dbeta”. This means winBUGS will choose a random value from the beta distribution. The beta distribution is useful for modeling random probabilities and proportions, particularly in the context of Bayesian analysis. The distribution has two parameters and yet a rich variety of shapes. Click on a and type “1” then on b and type “1”. These are the parameters of the beta distribution. Uniform distribution. Click on lower bound at type “0.1” then on upper bound and type “0.6”. These are the bounds we set on the true value of our prior. We will begin by using winBUGS to look at samples from our prior. To do this, we need to write code using this doodle. WinBUGS executes from the code. The doodle helps us keep track of our model, which at this stage consists of a single node called pi. On the main menu, select Doodle > Write Code.

  11. A new window opens that displays the model code. Left click over the text to highlight it. Click on Attributes > 16 point. The model selects a random number from a beta distribution with parameters a=b=1, keeping only those values that lie between 0.1 and 0.6. Note ~ is read “distributed as”. To run the model, select Model > Specification... to bring up the Specification Tool dialog box. Click . In the lower left corner of the main dialog box you should see the words “model is syntactically correct”. The compile button in the Specification Tool becomes active. Click . In the lower left corner you should see the words “model compiled”. Click . This creates a starting value for the model. You should see the words “initial values generated, model initialized”.

  12. Before you produce results, select Options > Output options Select the log radio button. Note here you can also select the output precision. Select Inference > Samples... This brings up the Sample Monitor Tool. In the node window type “pi”, then select . Note here you can also select the percentiles of interest. To run the model, select Model > Update... This opens the Update Tool. Change updates to 5000 then press . Watch as the iterations are counted by 100 to 5000. Return to Inference > Samples... to open the Sample Monitor Tool. Scroll to pi in the node window, then press .

  13. The Log window displays text indicating the model code is correct, and that the model compiled and was initialized. After pressing , the Log window displays a plot showing the distribution of prior values. The x-axis is the set of possible values for pi and the y-axis indicates how often the model chooses a particular value. The table-top shape to the graph indicates that all values of pi between 0.1 and 0.6 are equally likely. This is what we decided that we know about the unemployment rate before we look at our data sample. There are other useful buttons on the Sample Monitor Tool. For example, by pressing we get the following table appended in the Log window. The mean value for our prior is 0.349, with 95% of the 5000 values in the range between 0.113 and 0.5869.

  14. The MC error is the Monte Carlo error, it decreases as the number of samples increases. It helps in deciding when enough samples have been taken. Since we know the density is flat on top, we can reduce the wiggles by increasing the number of samples. Try running 50,000 samples. Note the smoother density and the reduction in the MC error from 0.002 with 5000 samples to 0.0006 for 50000 samples.

  15. For comparison and practice, let's rerun the analysis with a slightly different model. Let's restrict our prior to the range between 0.2 and 0.45. In words, we are more precise about what we know concerning the value of pi. Go back to the Doodle window and change the upper and lower bounds accordingly. Then select Write Code. Check to see if the code is consistent with the Doddle. With the Model window highlighted (window containing the code), select Model > Specification... to open the Specification Tool. First press . You will receive the following warning. Click . Then click and . Select Inference > Samples... to bring up the Sample Monitor Tool. Type in “pi” and the press . Select Model > Update... to open the Update Tool. Change updates to 5000 then press . Select Inference > Samples... Scroll to pi in the node window and press and .

  16. The new results are added to the Log window. Note that the x- and y-axes scales are different in the two graphs. We see that the range of possible values is constrained and that the mean value shifts to the left (is smaller). The graph indicates that we believe the true value for pi is bounded, but that within the bounds any value is equally likely. This simple example demonstrates how WinBUGS works. It shows how to start with a doodle and end up with a set of random numbers that encapsulate our belief about the unknown population parameter.

  17. Inference by combining your prior with data The real power of WinBUGS comes from the ability to combine your prior beliefs with data you have in hand, thus allowing you to make inferences. Continuing with our unemployment example, suppose you have results from a small sample of 14 people. A total of n = 14 people were surveyed and r = 4 of them were unemployed. These data will have a binomial distribution with proportion pi and denominator N. Go back to your directed graph and add two additional nodes. Add a constant node N. Add a stochastic node r with binomial density, proportion equal to pi and order N. Left click in your Doodle window, change type to constant and name to N. Left click again to add a stochastic node with name r, density dbin, proportion pi and order N. To add links between the nodes, click on node r to highlight it. With the Ctrl key held, click on node pi and node N. Arrows will be added to the nodes. The arrows will point to the highlighted node. The solid arrows indicates a statistical relationship (is distributed as).

  18. The directed graph describes the new model. The number of unemployed r is estimated from our prior and data as a random variable having a binomial distribution with proportion pi and order N. N is a constant, and the prior for pi is a beta distribution with two parameters (a=b=1) and restricted between 0.2 and 0.45. Select Doodle > Write Code To enter the data, type the following in the model code window. list(N=14,r=4) When you make you add arrows, make sure the parameters of the stochastic node do not change.

  19. Select Model > Specification... Click . Highlight the word “list” and press . If everything is well you will see the message “data loaded”. Press then . Select Inference > Samples... Type “pi” and press . WinBUGS has data for a node with a distribution, so it will calculate the appropriate likelihood function and prior for pi, and combine them into a posterior distribution. It knows about conjugate pair of distributions, so the calculation is straightforward. Select Model > Update... Change updates to 5000, then press . WinBUGS generates updated samples of pi (updated from the initial) by combining the prior information on pi and the new information on pi given by the data r and N.

  20. Prior Posterior Note how the data changes our view of the unemployment rate. For one thing, the data gives us reason to think that the unemployment rate pi is less than 0.4. There is still plenty of uncertainty, but it is less than before we took the sample. The standard deviation (sd) of pi from the prior is 0.072 but is 0.067 from the posterior. The 95% credible interval shrinks accordingly. For more practice, and to see the effect of a larger sample on the posterior, rerun the model with n=140, and r = 40.

  21. Posterior Thus as we increase our information about pi through more data, the influence of the prior on the posterior decreases. This is seen by the fact that the posterior density looks less like the original prior distribution. ________________________________________________ Assignment: Teen recidivism: Newspaper accounts suggest a teen recidivism rate between 0.1 and 0.4 nationwide. Of 10 kids released from a local prison, 6 returned before 3 years. Use a Bayesian model to determine the posterior distribution on the local teen recidivism rate. To help specify your prior, check out the quantile applet http://www.ds.unifi.it/VL/VL_EN/special/special9.html Notes: EX = a /(a + b), VarX = ab/[(a+b)^2 (a+b+1)] In R, type: plot(density(rbeta(1000,2,5))) summary(rbeta(1000,2,5)) qbeta(0.75,2.2,5)

  22. Example #2: Suppose that we take another survey, perhaps at some time later. This time 12 different people were asked and 5 said they were unemployed. What is the evidence that the underlying rates for the two surveys is really different? Note: From the first sample, 4/14 or 29% of the people were not employed. From the second sample, 5/12 or 42% of the people are unemployed. Looking only at the percentages, the difference appears to be large. Using WinBUGS we can determine how much we know about the differences in the rates from these two small surveys by calculating a posterior for the difference. Alternatively, we can calculate a posterior for the ratio.

  23. A model for the ratio and difference of two binomial samples Use the model from example #1 and add a second set of nodes for the new survey. Call the prior pi2, the number of unemployed r2, and the number of surveyed N2. Notes: The nodes can be rearranged using click and drag. pi2 is set up the same as pi using a beta density (a=b=1) and bounded. N2 is a constant like N. r2 is set up the same as r using a binomial density with proportion pi2 and order N2.

  24. To get the differences and ratios we create two logical nodes. Left click to create a node. Change type to logical, name to ratio, leave link as identity, and set value to pi2/pi. Left click to create a node. Change type to logical, name to difference, leave link as identity, and set value to pi2-pi.

  25. Add the links to finish the model. Note that the links to the logical nodes are made with a hollow arrow which indicates a deterministic relationship as opposed to the solid arrow which indicates a stochastic relationship. The final doodle and corresponding code for the two proportion model are: Directed Graph A deterministic relationship as indicated by a hollow arrow gets coded in the model using the <- symbol as is used in R or Splus. A statistical relationship gets coded in the model with a ~ symbol. Add the data. In the model window type: list(N=14, r=4, N2=12, r2=5).

  26. Use the Specification Tool to check the model, load the data, compile, and generate the initial values. Use the Sample Monitor Tool to set ratio and difference. Use the Update Tool to generate 5000 values of the ratio and difference.

  27. Does it appear as if the sample rates of unemployment are significantly different? Hint: The area under the difference curve to the right of zero is not much larger than the area to the left of zero. Also the area to the right of one under the ratio curve is not much larger than the area to the left of one. Also, the mean difference is close to zero and the mean ratio is close to one. The 95% credible interval for the difference in unemployment rates is (-0.15, 0.20), which is interpreted to mean that there is a 95% probability that the difference lies somewhere in this interval. This interval includes 0. Similarly the 95% credible interval for the ratio of unemployment rates is (0.61, 1.90), which includes the value of 1. In Bayesian analysis, the 95% credibleinterval is the analogue of the 95% confidenceinterval in conventional statistics. However, with a Bayesian analysis we state that there is a 95% probability that the parameter is between the interval values. Whereas in a conventional analysis we state that 95% of all such intervals will contain the true, but unknown value for the parameter assuming the null hypothesis is correct.

  28. The WinBUGS user manual and Examples Vol I and Vol II provide a reference for beginners and novices. A good way to approach a problem with WinBUGS is to scan through the examples to find a problem like yours. You can then modify the code to fit your problem. When you click on examples, you will open them in what is called a compound document. This organizes programs, graphs, and explanations into a single file. A good example is the surgical: institutional ranking.

More Related