Skip to contents

Scores many respondents at once using Thurstonian IRT parameters and returns MAP (empirical Bayes modal) latent trait estimates for each row in patterns.

In simple terms: this function applies score_tirt_pattern to every respondent, after first checking and aligning item columns so they match lambda row order.

Usage

score_tirt(patterns, lambda, theta_diag, tau, Psi, nu = NULL)

Arguments

patterns

A matrix or data.frame of response patterns (rows = respondents, columns = pair / items).

lambda

Loading matrix (rows = pair / items, columns = latent traits).

theta_diag

Numeric vector of residual variances aligned to rows of lambda.

tau

Numeric vector of thresholds aligned to rows of lambda.

Psi

Latent covariance matrix (traits x traits).

nu

Optional numeric vector of indicator intercepts aligned to rows of lambda. If NULL, zeros are used in score_tirt_pattern.

Value

A numeric matrix of latent scores: rows correspond to respondents in patterns, columns correspond to traits in colnames(lambda).

Details

Name alignment is the key safeguard: when both rownames(lambda) and colnames(patterns) are present, patterns is reordered to match lambda row order before scoring.

If required lambda names are missing from patterns, the function stops with an informative error. If names are unavailable, positional alignment is assumed and a warning is issued.

Examples

library(thurstonianIRT)
data("triplets")
# define the blocks of items
blocks <-
  set_block(c("i1", "i2", "i3"), traits = c("t1", "t2", "t3"),
            signs = c(1, 1, 1)) +
  set_block(c("i4", "i5", "i6"), traits = c("t1", "t2", "t3"),
            signs = c(-1, 1, 1)) +
  set_block(c("i7", "i8", "i9"), traits = c("t1", "t2", "t3"),
            signs = c(1, 1, -1)) +
  set_block(c("i10", "i11", "i12"), traits = c("t1", "t2", "t3"),
            signs = c(1, -1, 1))
# generate the data to be understood by 'thurstonianIRT'
triplets_long <- make_TIRT_data(
  data = triplets, blocks = blocks, direction = "larger",
  format = "pairwise", family = "bernoulli", range = c(0, 1)
)
# fit the data using lavaan
fit <- fit_TIRT_lavaan(triplets_long)
pars <- extract_tirt_params(fit)
patterns<-as.matrix(triplets)
score_tirt(patterns,lambda=pars$lambda,theta_diag=pars$theta_diag,
           tau=pars$tau,Psi=pars$Psi,nu=NULL)
#>        trait1    trait2    trait3
#> 1    0.299584 -1.207679  0.142628
#> 2   -0.915490  0.857886  0.640157
#> 3   -0.630524  1.344044  0.876887
#> 4   -0.952308 -0.400254  0.062053
#> 5    0.910671 -0.013031 -0.103811
#> 6    0.534702 -0.657240 -1.496231
#> 7    0.500861  0.417293 -0.100791
#> 8    0.642759  0.509066  0.600512
#> 9   -0.147997 -0.201002  0.452788
#> 10  -1.673023 -0.067635 -0.090041
#> 11   0.191321 -0.531539 -1.166400
#> 12   1.225771 -0.312791  1.046537
#> 13   1.068179  0.055166  0.653773
#> 14  -0.633225 -0.092301  0.042130
#> 15  -0.923229  0.508130  0.957096
#> 16  -1.368762 -0.349545 -0.985873
#> 17   0.336108 -1.317170  0.289523
#> 18  -0.258124 -0.918406 -0.372316
#> 19  -0.676044  0.272419 -0.186268
#> 20   1.022448 -0.418967  0.167891
#> 21  -0.188234 -0.086501  0.396841
#> 22   1.465963 -0.090623  0.214585
#> 23   0.525822  0.215122  0.232383
#> 24  -1.232942 -0.478590  0.423087
#> 25  -0.188234 -0.086501  0.396841
#> 26   1.422811 -0.928103 -0.796576
#> 27   0.622486  0.530456  0.678341
#> 28  -1.461796  0.919056  0.361938
#> 29   0.151835  1.359843  0.461502
#> 30  -0.215902 -1.289406 -0.080471
#> 31   0.895685  0.754321 -0.077896
#> 32  -0.461511  0.594858  1.523781
#> 33   0.735954 -1.306342 -0.108313
#> 34  -0.131839  1.167958  1.267026
#> 35  -0.303512  0.094676  0.078603
#> 36  -0.381914  1.323560 -0.001166
#> 37  -0.234215 -1.255578 -0.863948
#> 38  -0.768957  1.549136  0.515953
#> 39   1.195335 -0.818765  0.536250
#> 40   0.552003 -0.889552 -0.753535
#> 41  -0.164235  0.185322  0.076991
#> 42  -0.451112 -0.372365  0.275609
#> 43   1.004998 -1.312297 -0.919935
#> 44   0.199561  1.075842  0.909804
#> 45  -0.182257 -0.128730  0.389039
#> 46  -0.601785 -0.046262 -1.301456
#> 47  -0.931572 -0.754926  0.369512
#> 48   0.191387 -0.902913 -0.808788
#> 49   0.164798 -0.877443 -0.897284
#> 50  -0.225497  1.586101  1.009028
#> 51   0.478586 -0.030020 -0.523112
#> 52   0.382179 -0.342971  1.322792
#> 53  -0.495058 -0.048776  0.104577
#> 54  -1.016889  0.788141 -0.926204
#> 55   0.280777  0.699173  1.174863
#> 56   0.534371 -0.408769 -0.294129
#> 57  -0.243730  1.114821  0.342852
#> 58   0.838918 -0.114656  1.522447
#> 59  -1.310740 -0.006271 -0.731031
#> 60   1.068179  0.055166  0.653773
#> 61   0.610524 -0.161365  0.500117
#> 62  -0.174634  0.720618  0.589522
#> 63  -1.049688  1.125806 -0.412370
#> 64  -0.174634  0.720618  0.589522
#> 65   0.325032  0.372179 -0.150519
#> 66   0.550764  0.909481  0.411284
#> 67   0.622486  0.530456  0.678341
#> 68  -0.530896 -1.712997 -0.913628
#> 69  -1.122689  1.339618  0.463371
#> 70  -0.131839  1.167958  1.267026
#> 71   0.567382  0.050232  0.159715
#> 72  -0.239326  0.712978 -0.280436
#> 73  -0.023242  0.846726 -0.079400
#> 74   0.610524 -0.161365  0.500117
#> 75  -0.867621  0.177340  1.325927
#> 76   1.034332 -0.378986  0.254047
#> 77   0.972227  0.446584  0.377126
#> 78  -0.131839  1.167958  1.267026
#> 79  -0.207125  0.828507  0.751953
#> 80  -1.842287  0.924733 -0.031368
#> 81   0.222295 -0.937166 -0.033838
#> 82  -0.926190 -0.503583 -0.643351
#> 83   0.201970  0.299775  0.534617
#> 84   1.465963 -0.090623  0.214585
#> 85   0.735954 -1.306342 -0.108313
#> 86  -1.467760  1.340232  0.065165
#> 87  -0.281152  0.423650 -0.794148
#> 88  -0.569520 -0.121138 -1.309271
#> 89   0.222327  0.956786  0.723904
#> 90   0.382179 -0.342971  1.322792
#> 91   0.622486  0.530456  0.678341
#> 92  -0.923229  0.508130  0.957096
#> 93   0.741811 -0.515489  0.870822
#> 94   1.068179  0.055166  0.653773
#> 95   0.222327  0.956786  0.723904
#> 96   0.897053 -0.529374 -0.693273
#> 97  -0.527337  0.054858  0.843033
#> 98  -0.945908 -0.880802 -1.169507
#> 99   0.134870  0.669615  0.280039
#> 100 -0.952308 -0.400254  0.062053
#> 101 -0.832236  0.461778 -0.339639
#> 102 -0.110437 -0.221052  0.465419
#> 103 -1.158258 -0.327577  0.280137
#> 104  0.054195 -0.483781 -1.012557
#> 105  1.662968 -0.510788  0.599569
#> 106 -0.270470 -0.785767 -0.380625
#> 107  0.054195 -0.483781 -1.012557
#> 108  0.158807  0.087422  0.477564
#> 109 -1.394130 -0.024408 -1.392539
#> 110  0.164798 -0.877443 -0.897284
#> 111  0.991076 -1.083986 -0.872468
#> 112 -0.596569 -0.954209 -1.623896
#> 113  0.084688 -0.687205 -1.180527
#> 114 -0.243730  1.114821  0.342852
#> 115 -0.926190 -0.503583 -0.643351
#> 116 -0.464141 -0.262557  1.201191
#> 117  1.022448 -0.418967  0.167891
#> 118 -1.016889  0.788141 -0.926204
#> 119  0.713213 -1.503322 -0.936687
#> 120  0.117203 -0.477823 -0.529375
#> 121  0.037842 -0.142224 -1.384232
#> 122  0.485560 -0.105623 -0.313262
#> 123  0.318902  0.743274 -0.535569
#> 124 -0.557612  0.103198 -1.098873
#> 125  0.290079  0.909933  0.572237
#> 126  0.222295 -0.937166 -0.033838
#> 127  0.622486  0.530456  0.678341
#> 128  1.422811 -0.928103 -0.796576
#> 129  0.200511 -0.532825  0.359081
#> 130 -0.291789 -0.518056 -1.452566
#> 131 -1.707496  0.084598 -0.361408
#> 132 -0.210206  0.500319  0.526967
#> 133  1.433434 -0.559781 -0.228962
#> 134 -0.243730  1.114821  0.342852
#> 135 -0.002978  1.173395 -0.509449
#> 136  0.816956  0.051638 -1.011027
#> 137  1.291100 -0.471597 -1.061313
#> 138  1.349386  0.328558 -0.059944
#> 139 -0.207125  0.828507  0.751953
#> 140 -1.767559  0.404620 -0.487424
#> 141 -1.741455  0.469251  0.212294
#> 142  0.222295 -0.937166 -0.033838
#> 143 -0.131839  1.167958  1.267026
#> 144 -0.595420  1.082928 -0.070731
#> 145 -0.923229  0.508130  0.957096
#> 146  0.109760 -0.654788 -1.844788
#> 147  1.627338 -1.397597 -0.424087
#> 148 -0.471613 -0.036820 -0.703055
#> 149  0.991076 -1.083986 -0.872468
#> 150 -0.139550 -0.935132 -0.387984
#> 151 -0.130223 -0.432719  0.733106
#> 152 -0.552681  0.726195 -0.600266
#> 153  0.234680 -1.264860 -0.530476
#> 154 -0.923229  0.508130  0.957096
#> 155 -0.278640 -0.849318 -1.086673
#> 156 -1.014124  0.919485 -0.437706
#> 157 -1.806492  0.529045 -0.617256
#> 158 -0.046471  0.142137  1.396226
#> 159 -1.685538 -0.021859 -0.234617
#> 160 -1.047139  0.711246 -0.124976
#> 161 -0.630524  1.344044  0.876887
#> 162  0.550764  0.909481  0.411284
#> 163 -0.519872 -0.684060 -0.092381
#> 164 -0.977804  0.376495 -1.288609
#> 165  0.607582 -0.541679  0.142233
#> 166 -0.251921 -0.893528 -1.228891
#> 167  0.416734 -0.855954 -0.747486
#> 168  1.225771 -0.312791  1.046537
#> 169  0.199362 -0.580007 -0.331184
#> 170  1.068179  0.055166  0.653773
#> 171  0.134870  0.669615  0.280039
#> 172  0.307477  0.742294  1.322602
#> 173 -0.948277 -0.833686 -1.160482
#> 174 -0.992132 -0.445707 -1.403771
#> 175 -1.396157  0.415050 -0.991297
#> 176 -0.084565 -1.093779  0.529419
#> 177 -0.156872 -0.178991 -0.291482
#> 178  0.642759  0.509066  0.600512
#> 179  0.167159  0.590703  0.265677
#> 180 -0.136020  0.457797  1.007904
#> 181  1.225771 -0.312791  1.046537
#> 182 -0.195992 -1.489235 -1.730002
#> 183  0.816956  0.051638 -1.011027
#> 184 -0.497739  0.516957  0.180248
#> 185 -1.122689  1.339618  0.463371
#> 186  0.628619 -1.463992 -0.982336
#> 187  1.381754 -0.438013 -0.356014
#> 188  1.004998 -1.312297 -0.919935
#> 189  0.912122 -0.810814 -0.469254
#> 190 -0.179701 -0.058660 -0.972140
#> 191  0.111765  0.319578 -0.297899
#> 192  0.461050  0.330186 -0.890316
#> 193  0.382179 -0.342971  1.322792
#> 194  0.150633 -0.158890 -1.402063
#> 195  0.912122 -0.810814 -0.469254
#> 196 -0.110676 -1.276512 -0.880491
#> 197 -0.630524  1.344044  0.876887
#> 198 -1.461796  0.919056  0.361938
#> 199  0.610524 -0.161365  0.500117
#> 200  0.344034 -1.670651 -0.223795
# Check same scores from thurstonianIRT package
triplets_long <- make_TIRT_data(data = triplets,
                                blocks = blocks, 
                                direction = "larger",
                                format = "pairwise", 
                                family = "bernoulli", 
                                range = c(0, 1))
scores_thurstonianIRT<-predict(fit)
scores<-score_tirt(patterns,lambda=pars$lambda,theta_diag=pars$theta_diag,
        tau=pars$tau,Psi=pars$Psi,nu=NULL)
head(reshape2::recast(scores_thurstonianIRT,formula=id~trait,id.var=1:2))
#>   id  trait1   trait2   trait3
#> 1  1  0.2996 -1.20767  0.14264
#> 2  2 -0.9155  0.85794  0.64017
#> 3  3 -0.6305  1.34407  0.87695
#> 4  4 -0.9523 -0.40027  0.06206
#> 5  5  0.9106 -0.01306 -0.10390
#> 6  6  0.5347 -0.65726 -1.49623
head(scores)
#>    trait1   trait2   trait3
#> 1  0.2996 -1.20768  0.14263
#> 2 -0.9155  0.85789  0.64016
#> 3 -0.6305  1.34404  0.87689
#> 4 -0.9523 -0.40025  0.06205
#> 5  0.9107 -0.01303 -0.10381
#> 6  0.5347 -0.65724 -1.49623