This case study uses Stan to fit the Partial Credit Model (PCM) and Generalized Partial Credit Model (GPCM), including a latent regression for person ability for both. Analysis is performed with R, making use of the rstan and edstan packages. rstan is the implementation of Stan for R, and edstan provides Stan models for item response theory and several convenience functions.

The edstan package is available on CRAN, but a more up to date version may often be found on Github. The following R code may be used to install the package from Github.

# Install edstan from Github rather than CRAN
install.packages("devtools")
devtools::install_github("danielcfurr/edstan")

The following R code loads the necessary packages and then sets some rstan options, which causes the compiled Stan model to be saved for future use and the MCMC chains to be executed in parallel.

The case study uses R version 3.3.2, rstan version 2.14.1, ggplot2 version 2.2.1, and edstan version 1.0.7. Also, the example data are from TAM version 1.995.0. Readers may wish to check the versions for their installed packages using the packageVersion() function.

1 Partial credit model with latent regression

1.1 Overview of the model

The PCM (Masters 1982) is appropriate for item response data that features more than two ordered response categories for some or all items. The items may have differing numbers of response categories. For dichotomous items (items with exactly two response categories), the partial credit model is equivalent to the Rasch model. The version presented includes a latent regression. However, the latent regression part of the model may be restricted to an intercept only, resulting in the standard partial credit model.

\(m_i\) is simultaneously the maximum score and number of step difficulty parameters for item \(i\).

\(w_{j}\) is the vector of covariates for person \(j\), the first element of which must equal one for a model intercept. \(w_{j}\) may be assembled into a \(J\)-by-\(K\) covariate matrix \(W\), where \(K\) is number of elements in \(w_j\).

Parameters:

\(\beta_{is}\) is the \(s\)-th step difficulty for item \(i\).

\(\theta_j\) is the ability for person \(j\).

\(\lambda\) is a vector of latent regression parameters of length \(K\).

\(\sigma^2\) is the variance for the ability distribution.

Constraints:

The last step difficulty parameter is constrained to be the negative sum of the other difficulties, resulting in the average difficulty parameter being zero.

\(\lambda \sim t_3(0, 1)\), where \(t_3\) is the Student’s \(t\) distribution with three degrees of freedom, and the covariates have been transformed as follows: (1) continuous covariates are mean-centered and then divided by two times their standard deviations, (2) binary covariates are mean-centered and divided their maximum minus minimum values, and (3) no change is made to the constant, set to one, for the model intercept. This approach to setting priors is similar to one that has been suggested for logistic regression (Gelman et al. 2008). It is possible to adjust the coefficients back to the scales of the original covariates.

1.2Stan code for a simple partial credit model

A simple Stan model is described before discussing the complete model, as the code for the complete model is somewhat cumbersome. The simpler model, printed below, omits the latent regression and so does not require rescaling of the person covariates or lambda. The mean of the person distribution is set to zero and the constraint is removed from the item difficulties, which also differs from the complete model.

The functions block includes a user-specified function pcm(), which accepts a response y, a value for theta, and a vector of parameters beta for one item. With these inputs, it returns a vector of model-predicted log probability for the response. Later, in the model block, pcm() is used to get the likelihood of the observed item responses.

Looking to the data block, data are fed into the model in vector form. That is, y is a long vector of scored item responses, and ii and jj indicate with which item and person each element in y is associated. These three vectors are of length N, which is either equal to I times J or less if there are missing responses. In the transformed data block, the variable m is created, which represents the number of steps per item.

In the parameters block, beta is declared to be I vectors of length m. Each vector in beta contains the step difficulties for a given item. In this simplified model, all items must have the same number of response categories. The other parameters are handled in conventional ways, with sigma being assigned a lower bound of zero because it is a standard deviation.

The model block indicates the priors and the likelihood. The prior for beta requires a loop because beta is an array rather than a vector. The likelihood manually increments the log posterior using the target += ... syntax.

1.3Stan code for the partial credit model with latent regression

The PCM with latent regression will be discussed in relation to the simpler model, and both models are equivalent when the latent regression is restricted to an intercept only. The model with latent regression, which is featured in edstan, is printed below. It is more complicated than is typically necessary for a Stan model because it is written to apply sensible priors automatically for parameters associated with arbitrarily scaled covariates.

The complete model adds obtain_adjustments() to the functions block, which is used to adjust the covariate matrix. In brief, the model operates on the adjusted covariate matrix, W_adj, and then in the generated quantities block determines what the latent regression coefficients would be on the original scale of the covariates. For a more in depth discussion of obtain_adjustments() and the transformations related to the latent regression, see the Rasch and 2PL case study.

In the data block, the number of covariates (plus the intercept) K is now required, as is the matrix of covariates W. Otherwise this block is the same as before. An import change in this model is that beta is now a single, long vector rather than an array. This set up allows items to have different numbers of steps but requires additional programming. To that end, two variables are created in the transformed data block, and these are used to access the elements in beta relevant to a given item: pos indicates the position in beta of the first parameter for a given item, and m indicates the count of parameters for an item.

The parameters beta_free, theta, sigma, and lambda are declared in the parameters block. The unconstrained item parameters are contained in beta_free. In the transformed parameters block, beta is created by appending the constrained item difficulty to beta_free. The model block contains the priors and the likelihood. The target += ... syntax for the prior on beta is a manual way of incrementing the log posterior used when the prior is placed on a transformed parameter.

1.4 Simulation for parameter recovery

The Stan model is fit to a simulated dataset to evaluate it’s ability to recover the generating parameter values. The R code that follows simulates a dataset conforming to the model.

The simulated data consists of 20 items having 3 response categories and 500 persons. The person covariate vectors \(w_j\) include (1) a value of one for the model intercept, (2) a random draw from a normal distribution with mean of 10 and standard deviation of 5, (3) an indicator variable taking values of zero and one, and (4) an interaction between the two. These are chosen to represent a difficult case for assigning automatic priors for the latent regression coefficients. The generating coefficients \(\lambda\) for the latent regression are -0.5, 0.05, 0.5, and -0.025. The abilities \(\theta\) are random draws from a normal distribution with a mean generated from the latent regression and a standard deviation \(\sigma = 1.2\).

Mean of generated abilities as a function of the continuous covariate. A line is shown separately for the two groups identified by the binary variable.

The simulated dataset is next fit with Stan using irt_stan() from the edstan package. irt_stan() is merely a wrapper for stan() in rstan. Using 1,000 posterior draws per chain may be somewhat excessive as we are mainly interested in the posterior means of the parameters. However, as parameter recovery will be evaluated using the 2.5th and 97.5th percentiles of the posterior, the large number of posterior samples is warranted.

The highest value for \(\hat R\) was 1.009 for all parameters and the log posterior, suggesting that the chains have converged. The Stan model is evaluated in terms of its ability to recover the generating values of the parameters. The R code below prepares a plot in which the points indicate the difference between the posterior means and generating values for the parameters of main interest. This difference is referred to as discrepancy. The lines indicate the 95% poster intervals for the difference, defined as the 2.5th and 97.5th percentiles of the posterior draws. Ideally, (nearly) all the 95% intervals would include zero.

Discrepancies between estimated and generating parameters. Points indicate the difference between the posterior means and generating values for a parameter, and horizontal lines indicate 95% posterior intervals for the difference. Most of the discrepancies are about zero, indicating that Stan successfully recovers the true parameters.

2 Generalized partial credit model with latent regression

2.1 Overview of the model

The GPCM (Muraki 1992) extends the PCM by including a discrimination term. For dichotomous items (items with exactly two response categories), the generalized partial credit model is equivalent to the two-parameter logistic model. The version presented includes a latent regression. However, the latent regression may be restricted to a model intercept, resulting in the standard generalized partial credit model.

Many aspects of the GPCM are similar to the PCM described earlier. Parameters \(\beta_i\), \(\theta_j\), and \(\lambda\) have the same interpretation, but the GPCM adds a discrimination parameter \(\alpha_i\) and constrains the variance of \(\theta_j\) to one. The prior \(\alpha_i \sim \mathrm{log~N}(1, 1)\) is added, which is weakly informative but assumes positive discriminations. The same priors are placed on \(\beta_i\) and \(\lambda\), and the same constraint is placed on \(\beta_I\).

2.3 Simulation for parameter recovery

The Stan model is fit to a simulated dataset to evaluate it’s ability to recover the generating parameter values. The R code that follows simulates a dataset conforming to the model. The step difficulties and some other elements are borrowed from the PCM simulation.

The highest value for \(\hat R\) was 1.006 for all parameters and the log posterior. The Stan model is evaluated in terms of its ability to recover the generating values of the parameters. The R code below prepares a plot in which the points indicate the difference between the posterior means and generating values for the parameters of main interest. This difference is referred to as discrepancy. The lines indicate the 95% poster intervals for the difference, defined as the 2.5th and 97.5th percentiles of the posterior draws. Ideally, (nearly) all the 95% intervals would include zero.

Discrepancies between estimated and generating parameters. Points indicate the difference between the posterior means and generating values for a parameter, and horizontal lines indicate 95% posterior intervals for the difference. Most of the discrepancies are about zero, indicating that Stan successfully recovers the true parameters.

3 Example application

3.1 Data

The example data are from the TIMSS 2011 mathematics assessment (Mullis et al. 2012) of Australian and Taiwanese students. For convenience, a subset of 500 students is used. The subsetted data is then divided into a person covariate matrix and an item response matrix.

The above results show that the data are a mixture of dichotomous item and polytomous items with three responses categories. The first and second items are dichotomous, while the third and fourth are polytomous, for example. Consequently, the first and second items will have one step parameter each, while the third and fourth will have two each.

If person covariates are unavailable, or their inclusion unwanted, the model may be fit restricting the matrix of person covariates to an intercept only. In this case, the vector lambda contains only one element, which will represent the mean of the ability distribution. The code below is an example of how to create the data list for this purpose.