vignettes/Estimating-the-Model-in-the-Paper.Rmd
Estimating-the-Model-in-the-Paper.Rmd
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.
Within this vignette, we used two different data sets to generate a Choice Item Response Theory Model with the routine located in 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.
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)
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
## 223.594 0.293 213.969
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')
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.011649538 -0.119324868 0.09232377
## high2 -0.079607361 -0.187596490 0.02475609
## high3 0.129833910 0.024175512 0.23575297
## high4 0.154231702 0.045331576 0.26458342
## low1 0.004090263 -0.075454986 0.08310510
## low2 0.087486781 0.008177819 0.16566424
## factor(easy_items)1 -0.261607513 -0.534960585 0.01551629
## factor(easy_items)2 -0.363358553 -0.645435911 -0.08495911
## factor(easy_items)3 -0.247213978 -0.533680859 0.02972143
## factor(easy_items)4 -0.403212947 -0.675646548 -0.13532373
## factor(easy_items)5 -0.199379820 -0.486937464 0.07669560
## factor(easy_items)6 -0.447800556 -0.732335823 -0.16810746
## factor(easy_items)7 -0.299255369 -0.587652189 -0.01271575
## factor(easy_items)8 0.055529139 -0.222218581 0.32964864
## factor(easy_items)9 -0.374199378 -0.652283854 -0.10069356
## factor(easy_items)10 -0.032889577 -0.316564905 0.24396295
## factor(easy_items)11 -0.294441007 -0.572077129 -0.02319490
## factor(easy_items)12 0.038560415 -0.236474386 0.31173083
## factor(easy_items)13 -0.259686146 -0.537114448 0.02282545
## factor(easy_items)14 -0.508611032 -0.786466580 -0.23799262
## factor(easy_items)15 -0.092644289 -0.364305139 0.18795872
## factor(hard_items)16 -0.517181821 -0.787412356 -0.25506116
## factor(hard_items)17 -0.571033365 -0.840112820 -0.30594645
## factor(hard_items)18 -1.394352376 -1.680071116 -1.11331039
## factor(hard_items)19 -0.208238826 -0.479761097 0.06226429
## factor(hard_items)21 -0.716163701 -0.986212299 -0.44853397
## factor(hard_items)22 -0.251428103 -0.518209029 0.01702097
## factor(hard_items)23 -0.220256491 -0.485535456 0.04249700
## factor(hard_items)24 -1.206423966 -1.493637300 -0.92469997
## factor(hard_items)26 -0.147529423 -0.411278945 0.11117201
## factor(hard_items)27 -0.250669129 -0.514192883 0.01066457
## factor(hard_items)28 0.376542509 0.104568063 0.65027297
## factor(hard_items)29 0.756392064 0.477369694 1.03659988
# betas
B_thurstone
## 50% 2.5% 97.5%
## (Intercept) 0.692971094 0.56548382 0.83281056
## high1 -0.107430582 -0.23561575 0.02153505
## high2 -0.005353649 -0.13676877 0.13323983
## high3 0.036634422 -0.09291715 0.17045797
## high4 0.095999856 -0.04302519 0.24840219
## low1 0.020492428 -0.07526811 0.11726889
## low2 -0.113366146 -0.20970987 -0.01893016
# Sigma Thurstone
S_thurstone
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.385803006 -0.007187442 0.013220221 -0.000920910 -0.016500160
## [2,] -0.007187442 0.080083113 -0.012782856 -0.003570335 -0.025506228
## [3,] 0.013220221 -0.012782856 0.064470410 -0.007241969 -0.006773969
## [4,] -0.000920910 -0.003570335 -0.007241969 0.062509398 -0.008005838
## [5,] -0.016500160 -0.025506228 -0.006773969 -0.008005838 0.086499327
## [6,] -0.013649702 0.005758711 -0.002800530 -0.005450494 0.002266821
## [7,] -0.015138055 -0.007538131 0.002400087 0.003912724 0.005522046
## [,6] [,7]
## [1,] -0.013649702 -0.015138055
## [2,] 0.005758711 -0.007538131
## [3,] -0.002800530 0.002400087
## [4,] -0.005450494 0.003912724
## [5,] 0.002266821 0.005522046
## [6,] 0.064403393 -0.013253048
## [7,] -0.013253048 0.062211654
## Item parameters ----
# a
as
## [1] 0.6301142 0.5008629 1.1452280 0.3181689 1.0074384 1.0646990 0.8197671
## [8] 0.3745284 0.8242985 0.7417170 0.6192787 1.2096434 0.8363656 0.9740088
## [15] 0.7908059 1.0166373 0.7123065 0.5884154 0.9486842 1.0814250 1.0099990
## [22] 0.6598046 0.6317541 0.5796976 0.6150917 0.6142543 0.6655573 0.7929811
## [29] 0.5054986 0.6384347
# b
bs
## [1] -1.13912706 -1.15982533 -1.54626063 -0.88339333 -1.01102095 -0.92931135
## [7] -0.84285765 -0.43403622 -0.47655001 -0.74786783 -0.69346503 -0.59788777
## [13] -0.66468721 -0.48655225 -0.30071627 -0.59078945 -0.49836100 -0.26659978
## [19] -0.43351752 -0.52901624 -0.32362687 -0.30674461 -0.40299794 -0.22603867
## [25] -0.06220168 0.06383337 -0.31736035 0.13041778 0.49108836 0.93011413