-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.R
More file actions
178 lines (129 loc) · 5.38 KB
/
map.R
File metadata and controls
178 lines (129 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#' Maximum a Posteriori of person parameters for the Joint Model of IRM and RTM
#'
#' The function estimates the person parameters of the joint model of the logistic
#' item-response model and log-normal response-time model using known item parameters
#'
#' @param resp item response matrix
#' @param RT response time matrix
#' @param ipar item parameter matrix (nitem * 5)
#' @param D scaling constant that approximates the logistic model to normal-ogive model
#' @param mu_p mean of latent ability and speedness; The default is set to 0 for both
#' @param cov_p covariance matrix for latent trait parameters; if not known, assume identity matrix (i.e., no collateral information)
#' @keywords MAP joint reponse time model IRT
#' @export
#' @examples
#' out <- map(resp, RT)
map <- function(resp, RT, ipar,
ppar_prior=list(mu_p=NULL, cov_p=NULL),
SE=FALSE, D=1.702,
maxitr=200, tol=list(nr=1.0e-4, loglike=1.0e-6),
trunc=c(-3.5, 3.5) ){
## #~#~#~#~#~#~#~# ##
## Setting up ##
## #~#~#~#~#~#~#~# ##
nexaminee <- nrow(resp)
nitem <- ncol(resp)
## Priors (if specified)
for (v in 1:length(ppar_prior)){
eval(parse(text=paste0(names(ppar_prior)[v], " <- ppar_prior[[", v, "]]") ))
}
## if not specified
if(is.null(mu_p)){mu_p <- c(0,0)}
if(is.null(cov_p)){cov_p <- diag(2)}
cov_p_inv <- solve(cov_p)
## Functions
irf <- function(th, xi){
# one person multiple items
(p <- xi[,3] + (1 - xi[,3]) / ( 1 + exp(-D * xi[,1] * (th - xi[,2])) ))
}
rtf <- function(tau, time, xi){
# one person multiple items
(p <- xi[,1] / (time * sqrt(2*pi)) * exp(- 1/2 * xi[,1]^2 * (log(time) - (xi[,2] - tau))^2))
}
maxradius <- 2
pest_init <- cbind(qnorm(rowMeans(resp)), -as.numeric(scale(rowMeans(log(RT)))))
colnames(pest_init) <- c("th", "tau")
pest_init[pest_init[,"th"] < trunc[1] - 0.5,1] <- trunc[1] - 0.5
pest_init[pest_init[,"th"] > trunc[2] + 0.5,1] <- trunc[2] + 0.5
pest_init[pest_init[,"tau"] < trunc[1] - 0.5,2] <- trunc[1] - 0.5
pest_init[pest_init[,"tau"] > trunc[2] + 0.5,2] <- trunc[2] + 0.5
pest <- matrix(NA, nexaminee, 2, dimnames=list(NULL, c("th", "tau")))
nonconv <- rep(NA, nexaminee)
for (i in 1:nexaminee) {# i<-3
itr <- 0
eta_curr <- pest_init[i,]
track_nr <- list()
track_nr$loglike <- rep(NA, maxitr+1); track_nr$loglike[1] <- 100
while (itr < maxitr){
itr <- itr + 1
eta_try <- eta_curr
for (k in 1:2){
eta_try[k] <- ifelse(eta_curr[k] <= trunc[1], trunc[1], eta_curr[k])
eta_try[k] <- ifelse(eta_curr[k] >= trunc[2], trunc[2], eta_curr[k])
}
## first and second partial derivatives
pr <- irf(eta_try[1], ipar)
l1 <- D * sum(ipar[,1] * (resp[i,] - pr) * (pr - ipar[,3]) / (pr * (1 - ipar[,3]))) -
sum(cov_p_inv[,1] * t(eta_try - mu_p) )
l2 <- - sum( ipar[,4]^2 * (log(RT[i,]) - (ipar[,5] - eta_try[2]))) -
sum(cov_p_inv[,2] * t(eta_try - mu_p))
## Fisher's scoring
lam11 <- D^2 * sum(ipar[,1]^2 * (1 - pr) * (pr - ipar[,3]) * (ipar[,3] - pr) /
(pr * (1 - ipar[,3])^2 )) - cov_p_inv[1,1]
lam22 <- - sum(ipar[,4]^2) - cov_p_inv[2,2]
lam12 <- - cov_p_inv[1,2]
## hessian
hess <- matrix(c(lam11, lam12, lam12, lam22), 2, 2, byrow=T)
delta <- c(l1, l2) %*% solve(hess)
## Normalize delta if it exceeds maximum radius.
if (sqrt(sum(delta^2)) > maxradius){
delta <- delta * maxradius / abs(sqrt(sum(delta^2)))
}
track_nr$loglike[itr] <- log( prod ( pr^resp[i,] * (1-pr)^(1-resp[i,]) * rtf(eta_try[2], RT[i,], ipar[,4:5]) ) )
if ( ( max(abs(delta)) < tol$nr ) &&
(itr > 1 && abs( diff(track_nr$loglike[(itr-1):(itr)])) < tol$loglike ) ){
eta_hat <- eta_try
break;
}
eta_curr <- eta_try - delta
} # end of while
if (itr <= maxitr){
## If converged within the tolerance criterion
if (any(abs(eta_hat) > (trunc[2] + 0.5))){
nonconv[i] <- 2 # out of bounds
eta_hat[eta_hat < (trunc[1] - 0.5)] <- trunc[1] - 0.5
eta_hat[eta_hat > (trunc[2] + 0.5)] <- trunc[2] + 0.5
}
pest[i,] <- eta_hat
} else {
## If not converged
nonconv[i] <- 1
eta_curr[eta_curr < (trunc[1] - 0.5)] <- trunc[1] - 0.5
eta_curr[eta_curr > (trunc[2] + 0.5)] <- trunc[2] + 0.5
pest[i,] <- eta_curr
}
} # end of i (examinee)
if (isTRUE(SE)){
pest_se <- matrix(NA, nexaminee, 2, dimnames=list(NULL, c("th", "tau")))
for (i in 1:nexaminee){# i <- 1
pr <- irf(pest[i,"th"], ipar)
lam11 <- D^2 * sum(ipar[,1]^2 * (1 - pr) * (pr - ipar[,3]) * (ipar[,3] - pr) /
(pr * (1 - ipar[,3])^2 ))
lam22 <- - sum(ipar[,4]^2)
lam12 <- 0
hess <- matrix(c(lam11, lam12, lam12, lam22), 2, 2, byrow=T)
pest_se[i,] <- diag(sqrt(-solve(hess)))
}
} else {
pest_se <- NULL
}
### #~#~#~#~#~#~#~#~#~# ###
### Export results ###
### #~#~#~#~#~#~#~#~#~# ###
output <- list(est=pest,
se=pest_se,
nonconv=nonconv,
init=pest_init,
track=track_nr)
return(output)
}