Estimating the Model in the Paper

Intro

The objective of this vignette is to provide clarity as to the estimation procedure used to obtain the results in the paper. This vignette breaks down each section in the example displayed in cIRT() function.

Data

Within this vignette, we used two different data sets to generate a Choice Item Response Theory Model with the routine located in cIRT.

library(cIRT)

The first data set, trial_matrix, contains whether or not the subject correctly identified the spatial rotation. The second dataset, choice_matrix, provides information regarding the choice decision subjects were asked to make.

data(trial_matrix)
data(choice_matrix)

Constructing the Model Matrix

Here we construct a thurstone design matrix by obtaining the IDs of hard and easy questions presented for the subject to make a decision upon.

# Create the Thurstone Design Matrices
hard_items = choice_matrix$hard_q_id
easy_items = choice_matrix$easy_q_id

D_easy = model.matrix( ~ -1 + factor(easy_items))
D_hard = -1 * model.matrix( ~ -1 + factor(hard_items))[, -c(5, 10, 15)]

Within this setting, we setup the effect-codes for different constraints.

# Defining effect-coded contrasts
high_contrasts = rbind(-1, diag(4))
rownames(high_contrasts) = 12:16
low_contrasts = rbind(-1, diag(2))
rownames(low_contrasts) = 4:6

# Creating high & low factors
high = factor(choice_matrix[, 'high_value'])
low = factor(choice_matrix[, 'low_value'])
contrasts(high) = high_contrasts
contrasts(low) = low_contrasts

fixed_effects = model.matrix( ~ high + low)
fixed_effects_base = fixed_effects[, 1]
fixed_effects_int = model.matrix( ~ high * low)

Modeling the Data

Generate the cIRT model using a Thurstone Design Matrix generated above.

# Model with Thurstone D matrix
system.time({
  out_model_thurstone = cIRT(
    choice_matrix[, 'subject_id'],
    cbind(fixed_effects[, -1], D_easy, D_hard),
    c(1:ncol(fixed_effects)),
    as.matrix(fixed_effects),
    as.matrix(trial_matrix),
    choice_matrix[, 'choose_hard_q'],
    20000,
    25000
  )
})
##    user  system elapsed 
## 271.657 455.866 182.665

We recommend saving the model object as a .rda file even though the total computational time is less than 2.5 minutes.

## Save model output to an rda file.
# save(out_model_thurstone, file='choiceMCMCoutput.rda')

## Load model output back into R.
# load(file='choiceMCMCoutput.rda')

Parameter Estimates

Next up, we obtain the parameter estimates of the model by averaging over the different estimates obtained via the Gibbs sampling technique employed.

vlabels_thurstone = colnames(cbind(fixed_effects[, -1], D_easy, D_hard))

G_thurstone = t(apply(
  out_model_thurstone$gs0,
  2,
  FUN = quantile,
  probs = c(.5, .025, .975)
))
rownames(G_thurstone) = vlabels_thurstone

B_thurstone = t(apply(
  out_model_thurstone$beta,
  2,
  FUN = quantile,
  probs = c(.5, 0.025, .975)
))
rownames(B_thurstone) = colnames(fixed_effects)

S_thurstone = solve(
  apply(out_model_thurstone$Sigma_zeta_inv, c(1, 2), FUN = mean)
)

inv_sd = diag(1 / sqrt(diag(solve(
  apply(out_model_thurstone$Sigma_zeta_inv, c(1, 2), FUN = mean)
))))

corrmat = inv_sd %*% S_thurstone %*% inv_sd
as = apply(out_model_thurstone$as, 2, FUN = mean)
bs = apply(out_model_thurstone$bs, 2, FUN = mean)

Thus, we have the following results:

# gs0
G_thurstone
##                               50%         2.5%        97.5%
## high1                -0.010752620 -0.116123582  0.095761711
## high2                -0.080380776 -0.185098598  0.024838592
## high3                 0.128986815  0.023804982  0.237626636
## high4                 0.155617456  0.044164889  0.266653295
## low1                  0.003430492 -0.075443988  0.081802354
## low2                  0.086246846  0.007855488  0.165426711
## factor(easy_items)1  -0.255976499 -0.537510910  0.021955561
## factor(easy_items)2  -0.359902135 -0.633699702 -0.082087101
## factor(easy_items)3  -0.244831630 -0.530812636  0.040080836
## factor(easy_items)4  -0.398363384 -0.671155973 -0.124293277
## factor(easy_items)5  -0.199766525 -0.480183633  0.083490139
## factor(easy_items)6  -0.443762202 -0.727699726 -0.158309063
## factor(easy_items)7  -0.295386760 -0.580849130 -0.009052683
## factor(easy_items)8   0.060032387 -0.219656827  0.330104302
## factor(easy_items)9  -0.368787941 -0.650486468 -0.091491072
## factor(easy_items)10 -0.031678471 -0.310741484  0.252047840
## factor(easy_items)11 -0.296299231 -0.568629681 -0.023442263
## factor(easy_items)12  0.040878996 -0.234667020  0.309318956
## factor(easy_items)13 -0.260078259 -0.531823609  0.021224767
## factor(easy_items)14 -0.507914293 -0.785875566 -0.235819375
## factor(easy_items)15 -0.090038100 -0.372017261  0.190020331
## factor(hard_items)16 -0.519034413 -0.780546340 -0.256006646
## factor(hard_items)17 -0.568630811 -0.834485962 -0.299987899
## factor(hard_items)18 -1.386550497 -1.675826082 -1.106412854
## factor(hard_items)19 -0.205396706 -0.471336045  0.058756810
## factor(hard_items)21 -0.715512560 -0.985636292 -0.442261250
## factor(hard_items)22 -0.245667666 -0.513919738  0.020459568
## factor(hard_items)23 -0.220964701 -0.486001242  0.042447898
## factor(hard_items)24 -1.203956750 -1.491417154 -0.922812793
## factor(hard_items)26 -0.150031928 -0.408185482  0.107519594
## factor(hard_items)27 -0.251531362 -0.509505295  0.007826094
## factor(hard_items)28  0.371092198  0.101975539  0.646532755
## factor(hard_items)29  0.756252163  0.482270255  1.034165220
# betas
B_thurstone
##                      50%        2.5%       97.5%
## (Intercept)  0.688731142  0.56303810  0.82809705
## high1       -0.108398134 -0.23200113  0.01791586
## high2       -0.001173251 -0.13324278  0.12829745
## high3        0.033463178 -0.09480471  0.17105517
## high4        0.101953803 -0.03983224  0.24128554
## low1         0.022133499 -0.07575879  0.11697906
## low2        -0.115088012 -0.21298665 -0.01939460
# Sigma Thurstone
S_thurstone
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,]  0.382905928 -0.005818999  0.012904594 -0.003240801 -0.012443906
## [2,] -0.005818999  0.080010437 -0.012374671 -0.003758943 -0.024661876
## [3,]  0.012904594 -0.012374671  0.065515087 -0.008744158 -0.006476363
## [4,] -0.003240801 -0.003758943 -0.008744158  0.062606422 -0.006675093
## [5,] -0.012443906 -0.024661876 -0.006476363 -0.006675093  0.083081529
## [6,] -0.013678583  0.006712221 -0.003177757 -0.005021517  0.001691398
## [7,] -0.014806852 -0.007415264  0.001583077  0.004484933  0.005554314
##              [,6]         [,7]
## [1,] -0.013678583 -0.014806852
## [2,]  0.006712221 -0.007415264
## [3,] -0.003177757  0.001583077
## [4,] -0.005021517  0.004484933
## [5,]  0.001691398  0.005554314
## [6,]  0.065115390 -0.013818005
## [7,] -0.013818005  0.061830018
## Item parameters ----

# a
as
##  [1] 0.6287277 0.4962322 1.1453706 0.3172722 1.0071917 1.0715756 0.8128360
##  [8] 0.3709019 0.8161324 0.7367757 0.6129946 1.2054043 0.8326202 0.9651800
## [15] 0.7882749 1.0035191 0.7052325 0.5861337 0.9460499 1.0793732 1.0058488
## [22] 0.6578792 0.6298876 0.5765778 0.6120323 0.6154839 0.6638757 0.7919487
## [29] 0.5027949 0.6421965
# b
bs
##  [1] -1.14133293 -1.15657493 -1.54952423 -0.88599992 -1.01532532 -0.93904423
##  [7] -0.84424665 -0.43623225 -0.47680493 -0.74866454 -0.69420538 -0.60380483
## [13] -0.66757296 -0.48922676 -0.30502697 -0.58992575 -0.49943619 -0.26840693
## [19] -0.43533082 -0.53222547 -0.32546104 -0.30927095 -0.40346932 -0.22696787
## [25] -0.06490590  0.06346631 -0.31983155  0.13080333  0.48808654  0.93005298