# Two-part Random Effects Models for Longitudinal Data

### Overview

This page describes the dynamic two-part model that I have used in my research (see here). It extends the cross-sectional two-part model to a longitudinal setting and provides the R code necessary for estimation.

With longitudinal data, it is necessary to model the persistence in spending from one period to the next. The model does this in two ways: first, it includes lagged dependent variables and second, it allows for individual specific random regression coefficients. I focus on the case in which only intercepts vary across individuals so the model can be referred to as a dynamic random-intercept two-part model.

To replicate this page you will need this R script and these functions.

### The Model

Let so that the latent variable describes whether expenditures are positive or zero. The model can then be written as,

where and are the vectors of coefficients for the explanatory variables, and and are the random intercepts. We assume that so that the binomial component is a probit model and so that the continuous component is a lognormal model. Importantly, both and contain lagged values of (functions of) spending. In particular, contains and contains a variable equal to if and otherwise. Other functions of lagged spending are possible but we will use those here for illustrative purposes.

The random intercepts are assumed to be jointly normal,

where $\rho$ is the correlation between and . It is worth noting that if and contain an intercept, then and can be treated as error terms that are constant across individuals. In some disciplines, such as economics, these error terms are referred to as unobserved heterogeneity.

To illustrate, consider the second part of the two-part model and let . The variances and covariances of are then,

In other words the error terms are correlated within individuals but not over time across different individuals.

### Mortality

In order to model spending over time it is necessary to model mortality as well. Here, we ignore possible correlations between spending and mortality and assume that individuals die during during period at a rate, , that is increasing with age. We model the death rate with a simple probit model,

where contains an intercept and age covariates, is the corresponding vector of coefficients, and is the cumulative distribution function (CDF) of the standard normal density. To obtain a reasonable estimate for , we can download estimates of the probability of dying within one year by age from an actuarial life table published by Social Security using the `XML`

package. For simplicity, we will use the death rates for males.

The function `readHTMLTable`

returns a list of HTML tables. We extract the first and second columns from the second table which contains the information we want.

With the data in hand, we can model the death rates as a function of age. To ensure that coefficient estimates are on a reasonable scale and that the intercept has an interesting interpretation, we center and scale all age variables using the function `CSage`

. Since we are assuming that death rates can be modeled with a probit model, it follows that where . We can therefore estimate the regression coefficients with simple OLS.

The model fits the data quite well after age 25 or so, which suggests that we can predict mortality very accurately as a function of age.

### Simulating Data

A great way to understand a model is to simulate data according to the assumed data generating process. That is, first set the parameters to their “true” values and simulate the model given these parameter values. Next, estimate parameters based on the simulated data using a chosen statistical method and compare the estimated values to the true values.

Before doing this, we will load a couple of R packages. The `mvtnorm`

package can be used to draw the random intercepts from their bivariate normal distribution and the `data.table`

package is useful for manipulating larger datasets. We should also set the seed to ensure that the results are reproducible.

In order to simulate the model, we will need period data, which is assumed to be known as baseline. For simplicity, we will use small design matrices, and , that contain an intercept, a covariate for age, and a function of lagged spending. The model is therefore only dependent on initial spending levels and age. The function `InitData`

creates the necessary initial values for a desired sample size. Values for and age are drawn from distributions so that they are reasonably consistent with the distributions of observed health spending and age in the United States.

The true values for the parameters are set as follows.

We can now create a function that simulates longitudinal data using the model. The simulation begins with a set number of individuals alive during period . Expenditures are predicted for a chosen number of simulation periods, say , although some individuals die before reaching period .

The function can be summarized as follows. First, it draws the random intercepts from their bivariate normal distribution. Second, it draws period data and initializes the vectors that we would like to store for analysis. Third, it recursively simulates the model for the desired number of periods by:

- Updating the design matrices to reflect spending in the previous period and being 1 year older.
- Drawing death indicators.
- For survivors, simulating expenditures according to the two-part model.
- Incrementing the number of periods by 1 unless t = T.

The simulation ultimately returns a dataset with all relevant variables from the simulation.

### A Model with No Random Intercepts

Before moving on to the full model, we will investigate a model in which everyone has the same intercept. This is similar to a cross-sectional two-part model except that it includes lagged dependent variables. We will simulate data for 10, 000 individuals over 5 periods using the `sim`

function.

The parameters can be estimated using a probit model for the first part and OLS for the second part. The sample size is reasonably large so the estimated parameters should be close to their true values.

As expected, the estimated parameters are very close to the true values and that they fall within the 95% confidence intervals. The OLS estimates are more precisely estimated and even closer to the true values.

it is important to note however that although the confidence intervals worked for this particular simulation, we can’t be sure that the coverage probabilities are correct. To check coverage probabilities we would need to simulate the data multiple times, estimate the parameters for each simulation, and check whether the true values were contained within the 95% confidence intervals for 95% of the simulations. As an example, let’s check coverage for the second part of the model by simulating data 1000 times,

The confidence intervals are working as intended as the true coefficient values are contained within the 95% confidence interval approximately 950 out of 1000 times.

### MCMC

Estimating a model with random intercepts that are correlated across both components of the two-part model is more difficult. As a result, this section is somewhat technical and requires Bayesian Markov Chain Monte Carlo (MCMC) methods. This means that we will need to write down the full joint posterior density for the model. Letting and represent the number of years that individual is observed before death, the conditional density of expenditures for individual , , can (under the model) be written as,

where is the lognormal distribution. Given prior distributions, and , the joint posterior density is then,

where $y$ is the stacked vector of and there are individuals.

A Gbbs sampling algorithm estimates the parameters by partitioning the joint posterior distribution into conditional distributions. For full details see Appendix C here. The R function `gibbs`

contained in the R file twopart_re_mcmc.R implements the Gibbs sampler. The function `gibbs`

relies on five functions which sample , , , and . Conjugate priors were chosen for all of the parameters except so sampling is straightforward. The conditional distribution of is nonstandard so it is sampled using a random-walk Metropolis step.

Before implementing the Gibbs sampler we will simulate data with random intercepts.

We must then specify priors and initial values. We need the `MCMCpack`

package to sample from an inverse Wishart distribution.

The Gibbs sampler is run on 10,000 iterations. The sequence is thinned by keeping every 10th draw and the first 5,000 iterations are discarded as burn-in. The simulation takes around 17 minutes on my computer, so it is not a bad idea to save the output.

The simulated posterior densities are returned in a list. It is useful to convert the parameter vectors and matrices to a Markov Chain Monte Carlo object using the `coda`

package.

Before summarizing the posterior densities, we must test to see whether our chains have converged. Convergence can be inspected visually with a traceplot, which plots a draw from the posterior density at each iteration against the iteration number. A chain that has converged should have reached a stationary distribution with a relatively constant mean and variance. The parameter values should jump around the posterior density rather than getting stuck in certain regions. To illustrate, consider the traceplot for .

The variance parameter appears to have converged; however, this plot could could be misleading if has only converged to a local region and has not explored the full posterior. It is, in general, a good idea to run multiple chains with dispersed starting values to ensure that this is not the case. The Gelman-Rubin convergence diagnostic can then be used to test convergence. We will not do that here to keep things simple, but it is good practice.

Now lets look at the posterior quantiles for some of the parameters and compare them to their true values.

The estimated parameters seem to be converging to their true values, which suggests that the Bayesian algorithm is working as intended.