## Using the sapply function in R

In R, I think one of the most useful functions is sapply. By using this function, one can avoid writing a loop. Let me illustrate using this function for the binomial/beta exchangeable model.

The model is

1. distributed binomial()

2. are iid beta()

3. have a vague prior

The log posterior of is programmed in the function betabinexch in the LearnBayes package.

Suppose we wish to obtain a 90% interval estimate for . To simulate from the posterior, we first simulate a value of from its posterior and then simulate from the posterior beta(

Let’s consider the famous Efron and Morris baseball data — is the number of hits in at-bats for the th player.

I’ll read in the data, get a normal approximation to the posterior (function laplace), and then do a random walk Metropolis chain — I obtain a simulated sample from .

n=rep(45,18)

data=as.matrix(cbind(y,n))

fit=laplace(betabinexch,c(-1,4),data)

mcmc.fit=rwmetrop(betabinexch,list(var=fit$var,scale=2),c(-1,4),10000,data)

{

K=exp(mcmc.fit$par[,2])

eta=exp(mcmc.fit$par[,1])/(1+exp(mcmc.fit$par[,1]))

quantile(thetaj,c(.05,.95))

}

5% 95%

0.2052578 0.3288738

[,1] [,2] [,3] [,4] [,5] [,6] [,7]

## Robust modeling

The Phillies are still alive in the World Series and so I’m allowed to continue to talk about baseball.

In the situation where we observe a sample of measurement data , a common assumption to assume that they are a random sample from a normal population with mean and standard deviation . But sometimes this assumption is inappropriate. One measure of hitting effectiveness is the OBP, the proportion of time that a hitter gets on base. I collected the OBPs (actually 1000 x OBP) for a group of 24 players who played for the west coast NL teams (Dodgers, Giants, and Padres) in the 2004 season. Here’s a jittered dotplot of the OBPs:

Clearly, a normal population is inappropriate here since we have one obvious outlier — Barry Bonds in that season had an usually large OBP of 609.

In many situations, it makes more sense to assume that are distributed from a “flat-tailed” distribution where outliers are more likely to occur. One example of a flat-tailed distribution is the t form that depends on a location , a scale and a degrees of freedom . Here we assume a small value of the degrees of freedom and focus on estimating .

This is an attractive way of fitting a t sampling model. First, a t distribution can represented as a scale mixtures of normals. Suppose we let be distributed normal with mean and variance , and then we let the parameter be distributed Gamma with shape and rate . When you integrate out , one sees this is equivalent to assuming is N(.

Suppose we use this scale mixture representation for each observation. We can write this as a hierarchical model:

1. is distributed N()

2. is distributed Gamma ()

3. has the prior .

In class, we’ll show that can sample from this model easily using Gibbs sampling. This algorithm is implemented using the function in the LearnBayes package.

The posteriors of the extra scale parameters are of interest, since they reflect the influence of the th observation on the estimation of the mean and variance. Below I display error bars that show 90% interval estimates for each of the for my OBP example. Note that the posterior for the scale parameter for Barry Bonds is concentrated near 0. This is reasonable, since it is desirable to downweight the influence of Bonds OBP in the estimation of, say, the average OBP.

## Home run hitting — priors?

At our department’s colloquium last week, there was an interesting presentation on the use of an exchangeable prior in a regression context. The analysis was done using WinBUGS using some standard choices of “weekly informative” priors. That raises the natural question — how did you decide on this prior?

Let’s return to the home run hitting example where an exchangeable prior was placed on the set of home run hitting probabilities. We assumed that were iid and then the hyperparameters and were assumed independent where was assigned the prior and was assigned the log logistic form .

When I described these priors, I just said that they were “noninformative” and didn’t explain how I obtained them.

The main questions are:

1. Does the choice of vague prior matter?

2. If the answer to question 1 is “yes”, what should we do?

When one fits an exchangeable model, one is shrinking the observed rates towards a common value. The posterior of tells us about the common value and the posterior of tells us about the degree of shrinkage. The choice of vague prior for is not a concern — we can assume is uniform or distributed according to the improper prior . However, the prior on can be a concern. If one assigns the improper form , it can be shown that the posterior may be improper. So in general one needs to assign a proper prior to . One convenient choice is to assume that has a logistic density with location and scale . In my example, I assumed that .

Since I was arbitrary in my choice of parameters for this logistic prior on , I tried different choices for and . I tried values of between -4 and 4 and values of between 0.2 and 10. What did I find? The posterior mode of stayed in the 4.56 – 4.61 range for all of the priors I considered. In this example, we have data from 605 players and clearly the likelihood is driving the inference.

What if the choice of and does matter? Then one has to think more carefully about these parameters. In this baseball context, I am pretty familiar with home run hitting rates. Based on this knowledge, I have to figure out a reasonable guess at the variability of the home run probabilities that would give me information about .