## Estimating a mean for a normal model using WinBUGS

In estimating the mean diameter of trees, the prior has a mean of 53 cm and a standard deviation of 5 cm. In WinBUGS, the width of a normal distribution is expressed using the precision (1/variance = 1/sd2), which in this case is equal to

In this example, the variance of the data is assumed to be known, making it equivalent to using a z-value rather than a t-value in a frequentist analysis. However, uncertainty in estimating the precision of the data can be included easily in the WinBUGS analysis (Chapter 3).

The pseudo-code for the WinBUGS analysis is:

1. Specify the prior for the mean diameter of trees in the remnant as being normally distributed with a mean of 53 and precision of 0.04 (standard deviation of 5).

### 2. Calculate the standard deviation of the data.

3. Specify the precision of the data as the inverse of the variance of the diameter of trees in the remnant (the variance equals 184.9 in this example).

4. For each of the ten trees that were measured, assume that their diameter is drawn from a normal distribution with the mean and precision as specified in steps 1 and 3.

The WinBUGS code is: model m~dnorm(53, 0.04) # prior for mean stdev <- sd(Y[]) # calculate std deviation of data prec <- 1/(stdev*stdev) # precision of the data =

1/variance for (i in 1:10) # for each of the ten trees ...

Y[i] ~ dnorm(m, prec) # diameter drawn from a normal distribution

list(Y = c(42, 43, 58, 70, 47, 51, 85, 63, 58, 46))

The 'for loop', designated by the line for (i in 1:10) and subsequent line within the curly brackets, is equivalent to ten lines of code, one for each of the ten trees, i.e. Y[1] ~ dnorm(m, prec), up to Y[10] ~ dnorm(m, prec). It is shorthand to replace repetitive sections of code.

The data are provided in the line:

list(Y = c(42, 43, 58, 70, 47, 51, 85, 63, 58, 46))

The 'c' before the brackets indicates that the following data are concatenated (linked together) into the one variable, with the first variable represented by Y[1], the second by Y[2] , etc.

This analysis also requires that the user specifies an initial value of mean for the Markov chain (Box 1.4). The choice is not important because the chain converges quickly to the posterior distribution in this case, and could be generated randomly. However, in some cases the speed of convergence is increased if the Markov chain is initiated with values that are close to the posterior distribution, so the following arbitrary value was used:

list(m = 55) # an arbitrary initial value

After discarding the first 1000 samples as a burn-in, 100000 samples were generated in WinBUGS. Of these samples, 2.5% were less than 48.5 and 2.5% were more than 61.3. Therefore the 95% credible interval is 48.5—61.3 cm for the mean diameter of trees in the remnant. This result is insensitive to the choice of the initial value.

of trees in the remnant, and then constructing a model in which the measured diameters are drawn from a distribution with that mean. The posterior distribution calculated in WinBUGS is the same as that obtained using the analytical solution (Box 1.9).

0 0