Skip to content

Commit 39eab0b

Browse files
committed
Modify prior in IRT case studies
1 parent d930776 commit 39eab0b

File tree

6 files changed

+222
-195
lines changed

6 files changed

+222
-195
lines changed

education/pcm_and_gpcm/pcm_and_gpcm.Rmd

+13-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,17 @@ nand <- function(x) {
2929
}
3030
```
3131

32-
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 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.
32+
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.
33+
34+
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.
35+
36+
```{r github, eval = FALSE}
37+
# Install edstan from Github rather than CRAN
38+
install.packages("devtools")
39+
devtools::install_github("danielcfurr/edstan")
40+
```
41+
42+
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.
3343

3444
```{r, warning=FALSE, results="hide"}
3545
# Load R packages
@@ -93,7 +103,7 @@ Constraints:
93103
Priors:
94104

95105
* $\sigma \sim \mathrm{Exp}(.1)$ is weakly informative for the person standard deviation.
96-
* $\beta_1 \ldots \beta_{I-1} \sim \mathrm{N}(0, 9)$ is weakly informative, and no prior is needed for the constrained difficulty $\beta_I$.
106+
* $\beta_{is} \sim \mathrm{N}(0, 9)$ is also weakly informative.
97107
* $\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 [@gelman2008weakly]. It is possible to adjust the coefficients back to the scales of the original covariates.
98108

99109

@@ -131,7 +141,7 @@ The complete model adds `obtain_adjustments()` to the functions block, which is
131141

132142
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.
133143

134-
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. Priors are placed on `beta_free` rather than on `beta`, as the prior does not apply to the constrained parameter.
144+
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.
135145

136146

137147
## Simulation for parameter recovery

education/pcm_and_gpcm/pcm_and_gpcm.html

+72-68
Large diffs are not rendered by default.

education/rasch_and_2pl/rasch_and_2pl.Rmd

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ Constraints:
9696
Priors:
9797

9898
* $\sigma \sim \mathrm{Exp}(.1)$ is weakly informative for the person standard deviation.
99-
* $\beta_1 \ldots \beta_{I-1} \sim \mathrm{N}(0, 9)$ is weakly informative, and no prior is needed for the constrained difficulty $\beta_I$.
99+
* $\beta_i \sim \mathrm{N}(0, 9)$ is also weakly informative.
100100
* $\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 [@gelman2008weakly]. It is possible to adjust the coefficients back to the scales of the original covariates.
101101

102102

@@ -131,7 +131,7 @@ In the transformed data block, the `obtain_adjustments()` function is called and
131131

132132
The generated quantities block is used to calculate what the regression coefficients and intercept would have been on the scales of the original covariates. For the coefficients this is determined simply by dividing the coefficients by the same value of `spread` used to modify the scale the original covariates. The intercept given the original scale is then recovered with some algebra. The `obtain_adjustments()` function and related code for adjusting the covariates and regression coefficients is used in the same way across **edstan** models.
133133

134-
There are a few other differences from the simple model. In the data block, the number of covariates (plus the intercept) `K` is now required, as is the matrix of covariates `W`. The first column of `W` must have all elements equal to one. Also, the unconstrained item parameters are contained in `beta_free`, which is why it has a length of $I-1$. In the transformed parameters block, `beta` is created by appending the constrained item difficulty to `beta_free`. Priors are placed on `beta_free` rather than on `beta`, as the prior does not apply to the constrained parameter.
134+
There are a few other differences from the simple model. In the data block, the number of covariates (plus the intercept) `K` is now required, as is the matrix of covariates `W`. The first column of `W` must have all elements equal to one. Also, the unconstrained item parameters are contained in `beta_free`, which is why it has a length of $I-1$. In the transformed parameters block, `beta` is created by appending the constrained item difficulty to `beta_free`. 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.
135135

136136

137137
## Simulation for parameter recovery

education/rasch_and_2pl/rasch_and_2pl.html

+64-65
Large diffs are not rendered by default.

education/rsm_and_grsm/rsm_and_grsm.Rmd

+15-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,17 @@ nand <- function(x) {
2929
}
3030
```
3131

32-
This case study uses **Stan** to fit the Rating Scale Model (RSM) and Generalized Rating Scale Model (GRSM), 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 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.
32+
This case study uses **Stan** to fit the Rating Scale Model (RSM) and Generalized Rating Scale Model (GRSM), 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.
33+
34+
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.
35+
36+
```{r github, eval = FALSE}
37+
# Install edstan from Github rather than CRAN
38+
install.packages("devtools")
39+
devtools::install_github("danielcfurr/edstan")
40+
```
41+
42+
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.
3343

3444
```{r, warning=FALSE, results="hide"}
3545
# Load R packages
@@ -95,8 +105,8 @@ Constraints:
95105
Priors:
96106

97107
* $\sigma \sim \mathrm{Exp}(.1)$ is weakly informative for the person standard deviation.
98-
* $\beta_1 \ldots \beta_{I-1} \sim \mathrm{N}(0, 9)$ is weakly informative, and no prior is needed for the constrained difficulty $\beta_I$.
99-
* $\kappa_1 \ldots \kappa_{m-1} \sim \mathrm{N}(0, 9)$ is weakly informative, and no prior is needed for the constrained difficulty $\kappa_m$.
108+
* $\beta_i \sim \mathrm{N}(0, 9)$ is also weakly informative.
109+
* $\kappa_s \sim \mathrm{N}(0, 9)$ is weakly informative.
100110
* $\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 [@gelman2008weakly]. It is possible to adjust the coefficients back to the scales of the original covariates.
101111

102112

@@ -117,7 +127,7 @@ Looking to the data block, data are fed into the model in vector form. That is,
117127

118128
In the parameters block, `kappa_free` is declared as a vector of length `m - 1` and represents the unconstrained step parameters. In the transformed parameters block, the constrained step parameter is appened to `kappa_free` to make `kappa`, which is the complete vector of step difficulties. The other parameters are handled in conventional ways, with `sigma` being assigned a lower bound of zero because it is a standard deviation.
119129

120-
The model block indicates the priors and the likelihood. The prior for step difficulties is placed on `kappa_free` rather than on `kappa` in order to avoid placing a prior on the constrained parameter. The likelihood manually increments the log posterior using the `target += ...` syntax with the `rsm()` function.
130+
The model block indicates the priors and the likelihood. The `target += ...` syntax for the prior on `kappa` is a manual way of incrementing the log posterior used when the prior is placed on a transformed parameter. The likelihood uses similar syntax along with the `rsm()` function.
121131

122132

123133
## **Stan** code for the rating scale model with latent regression
@@ -132,7 +142,7 @@ cat(readLines(rsm_file), sep = "\n")
132142

133143
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](http://mc-stan.org/documentation/case-studies/rasch_and_2pl.html).
134144

135-
In the data block, the number of covariates (plus the intercept) `K` is now required, as is the matrix of covariates `W`. The parameters `beta_free`, `kappa_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. Priors are placed on `beta_free` rather than on `beta`, as the prior does not apply to the constrained parameter. The same approach is taken with `kappa` and `kappa_free`.
145+
In the data block, the number of covariates (plus the intercept) `K` is now required, as is the matrix of covariates `W`. The parameters `beta_free`, `kappa_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.
136146

137147

138148
## Simulation for parameter recovery

0 commit comments

Comments
 (0)