5 ## df: dataframe to model
6 ## outcome_formula: formula for y | x, z
7 ## outcome_family: family for y | x, z
8 ## proxy_formula: formula for w | x, z, y
9 ## proxy_family: family for w | x, z, y
10 ## truth_formula: formula for x | z
11 ## truth_family: family for x | z
13 ### ideal formulas for example 1
14 # test.fit.1 <- measerr_mle(df, y ~ x + z, gaussian(), w_pred ~ x, binomial(link='logit'), x ~ z)
16 ### ideal formulas for example 2
17 # test.fit.2 <- measerr_mle(df, y ~ x + z, gaussian(), w_pred ~ x + z + y + y:x, binomial(link='logit'), x ~ z)
20 ## outcome_formula <- y ~ x + z; proxy_formula <- w_pred ~ y + x + z + x:z + x:y + z:y
21 measerr_mle_dv <- function(df, outcome_formula, outcome_family=binomial(link='logit'), proxy_formula, proxy_family=binomial(link='logit'),method='optim'){
23 nll <- function(params){
24 df.obs <- model.frame(outcome_formula, df)
25 proxy.variable <- all.vars(proxy_formula)[1]
26 proxy.model.matrix <- model.matrix(proxy_formula, df)
27 response.var <- all.vars(outcome_formula)[1]
28 y.obs <- with(df.obs,eval(parse(text=response.var)))
29 outcome.model.matrix <- model.matrix(outcome_formula, df.obs)
32 n.outcome.model.covars <- dim(outcome.model.matrix)[2]
33 outcome.params <- params[param.idx:n.outcome.model.covars]
34 param.idx <- param.idx + n.outcome.model.covars
36 if((outcome_family$family == "binomial") & (outcome_family$link == 'logit')){
37 ll.y.obs <- vector(mode='numeric', length=length(y.obs))
38 ll.y.obs[y.obs==1] <- plogis(outcome.params %*% t(outcome.model.matrix[y.obs==1,]),log=TRUE)
39 ll.y.obs[y.obs==0] <- plogis(outcome.params %*% t(outcome.model.matrix[y.obs==0,]),log=TRUE,lower.tail=FALSE)
42 df.obs <- model.frame(proxy_formula,df)
43 n.proxy.model.covars <- dim(proxy.model.matrix)[2]
44 proxy.params <- params[param.idx:(n.proxy.model.covars+param.idx-1)]
46 param.idx <- param.idx + n.proxy.model.covars
47 proxy.obs <- with(df.obs, eval(parse(text=proxy.variable)))
49 if( (proxy_family$family=="binomial") & (proxy_family$link=='logit')){
50 ll.w.obs <- vector(mode='numeric',length=dim(proxy.model.matrix)[1])
51 ll.w.obs[proxy.obs==1] <- plogis(proxy.params %*% t(proxy.model.matrix[proxy.obs==1,]),log=TRUE)
52 ll.w.obs[proxy.obs==0] <- plogis(proxy.params %*% t(proxy.model.matrix[proxy.obs==0,]),log=TRUE, lower.tail=FALSE)
55 ll.obs <- sum(ll.y.obs + ll.w.obs)
57 df.unobs <- df[is.na(df[[response.var]])]
58 df.unobs.y1 <- copy(df.unobs)
59 df.unobs.y1[[response.var]] <- 1
60 df.unobs.y0 <- copy(df.unobs)
61 df.unobs.y0[[response.var]] <- 0
64 outcome.model.matrix.y1 <- model.matrix(outcome_formula, df.unobs.y1)
66 if((outcome_family$family == "binomial") & (outcome_family$link == 'logit')){
67 ll.y.unobs.1 <- vector(mode='numeric', length=dim(outcome.model.matrix.y1)[1])
68 ll.y.unobs.0 <- vector(mode='numeric', length=dim(outcome.model.matrix.y1)[1])
69 ll.y.unobs.1 <- plogis(outcome.params %*% t(outcome.model.matrix.y1),log=TRUE)
70 ll.y.unobs.0 <- plogis(outcome.params %*% t(outcome.model.matrix.y1),log=TRUE,lower.tail=FALSE)
73 proxy.model.matrix.y1 <- model.matrix(proxy_formula, df.unobs.y1)
74 proxy.model.matrix.y0 <- model.matrix(proxy_formula, df.unobs.y0)
75 proxy.unobs <- with(df.unobs, eval(parse(text=proxy.variable)))
77 if( (proxy_family$family=="binomial") & (proxy_family$link=='logit')){
78 ll.w.unobs.1 <- vector(mode='numeric',length=dim(proxy.model.matrix.y1)[1])
79 ll.w.unobs.0 <- vector(mode='numeric',length=dim(proxy.model.matrix.y0)[1])
80 ll.w.unobs.1[proxy.unobs==1] <- plogis(proxy.params %*% t(proxy.model.matrix.y1[proxy.unobs==1,]),log=TRUE)
81 ll.w.unobs.1[proxy.unobs==0] <- plogis(proxy.params %*% t(proxy.model.matrix.y1[proxy.unobs==0,]),log=TRUE, lower.tail=FALSE)
83 ll.w.unobs.0[proxy.unobs==1] <- plogis(proxy.params %*% t(proxy.model.matrix.y0[proxy.unobs==1,]),log=TRUE)
84 ll.w.unobs.0[proxy.unobs==0] <- plogis(proxy.params %*% t(proxy.model.matrix.y0[proxy.unobs==0,]),log=TRUE, lower.tail=FALSE)
87 ll.unobs.1 <- ll.y.unobs.1 + ll.w.unobs.1
88 ll.unobs.0 <- ll.y.unobs.0 + ll.w.unobs.0
89 ll.unobs <- sum(colLogSumExps(rbind(ll.unobs.1,ll.unobs.0)))
90 ll <- ll.unobs + ll.obs
94 params <- colnames(model.matrix(outcome_formula,df))
95 lower <- rep(-Inf, length(params))
96 proxy.params <- colnames(model.matrix(proxy_formula, df))
97 params <- c(params, paste0('proxy_',proxy.params))
98 lower <- c(lower, rep(-Inf, length(proxy.params)))
99 start <- rep(0.1,length(params))
100 names(start) <- params
103 fit <- optim(start, fn = nll, lower=lower, method='L-BFGS-B', hessian=TRUE, control=list(maxit=1e6))
105 quoted.names <- gsub("[\\(\\)]",'',names(start))
107 text <- paste("function(", paste0(quoted.names,'=',start,collapse=','),"){params<-c(",paste0(quoted.names,collapse=','),");return(nll(params))}")
109 measerr_mle_nll <- eval(parse(text=text))
110 names(start) <- quoted.names
111 names(lower) <- quoted.names
112 fit <- mle2(minuslogl=measerr_mle_nll, start=start, lower=lower, parnames=params,control=list(maxit=1e6),method='L-BFGS-B')
118 measerr_mle <- function(df, outcome_formula, outcome_family=gaussian(), proxy_formula, proxy_family=binomial(link='logit'), truth_formula, truth_family=binomial(link='logit'),method='optim'){
120 df.obs <- model.frame(outcome_formula, df)
121 response.var <- all.vars(outcome_formula)[1]
122 proxy.variable <- all.vars(proxy_formula)[1]
123 truth.variable <- all.vars(truth_formula)[1]
124 outcome.model.matrix <- model.matrix(outcome_formula, df)
125 proxy.model.matrix <- model.matrix(proxy_formula, df)
126 y.obs <- with(df.obs,eval(parse(text=response.var)))
128 measerr_mle_nll <- function(params){
130 n.outcome.model.covars <- dim(outcome.model.matrix)[2]
131 outcome.params <- params[param.idx:n.outcome.model.covars]
132 param.idx <- param.idx + n.outcome.model.covars
134 ## likelihood for the fully observed data
135 if(outcome_family$family == "gaussian"){
136 sigma.y <- params[param.idx]
137 param.idx <- param.idx + 1
138 # outcome_formula likelihood using linear regression
139 ll.y.obs <- dnorm(y.obs, outcome.params %*% t(outcome.model.matrix),sd=sigma.y, log=TRUE)
142 df.obs <- model.frame(proxy_formula,df)
143 n.proxy.model.covars <- dim(proxy.model.matrix)[2]
144 proxy.params <- params[param.idx:(n.proxy.model.covars+param.idx-1)]
145 param.idx <- param.idx + n.proxy.model.covars
146 proxy.obs <- with(df.obs, eval(parse(text=proxy.variable)))
148 if( (proxy_family$family=="binomial") & (proxy_family$link=='logit')){
149 ll.w.obs <- vector(mode='numeric',length=dim(proxy.model.matrix)[1])
151 # proxy_formula likelihood using logistic regression
152 ll.w.obs[proxy.obs==1] <- plogis(proxy.params %*% t(proxy.model.matrix[proxy.obs==1,]),log=TRUE)
153 ll.w.obs[proxy.obs==0] <- plogis(proxy.params %*% t(proxy.model.matrix[proxy.obs==0,]),log=TRUE, lower.tail=FALSE)
156 df.obs <- model.frame(truth_formula, df)
158 truth.obs <- with(df.obs, eval(parse(text=truth.variable)))
159 truth.model.matrix <- model.matrix(truth_formula,df)
160 n.truth.model.covars <- dim(truth.model.matrix)[2]
162 truth.params <- params[param.idx:(n.truth.model.covars + param.idx - 1)]
164 if( (truth_family$family=="binomial") & (truth_family$link=='logit')){
165 ll.x.obs <- vector(mode='numeric',length=dim(truth.model.matrix)[1])
167 # truth_formula likelihood using logistic regression
168 ll.x.obs[truth.obs==1] <- plogis(truth.params %*% t(truth.model.matrix[truth.obs==1,]),log=TRUE)
169 ll.x.obs[truth.obs==0] <- plogis(truth.params %*% t(truth.model.matrix[truth.obs==0,]),log=TRUE, lower.tail=FALSE)
172 # add the three likelihoods
173 ll.obs <- sum(ll.y.obs + ll.w.obs + ll.x.obs)
175 ## likelihood for the predicted data
176 ## integrate out the "truth" variable.
178 if(truth_family$family=='binomial'){
179 df.unobs <- df[is.na(eval(parse(text=truth.variable)))]
180 df.unobs.x1 <- copy(df.unobs)
181 df.unobs.x1[,'x'] <- 1
182 df.unobs.x0 <- copy(df.unobs)
183 df.unobs.x0[,'x'] <- 0
184 outcome.unobs <- with(df.unobs, eval(parse(text=response.var)))
186 outcome.model.matrix.x0 <- model.matrix(outcome_formula, df.unobs.x0)
187 outcome.model.matrix.x1 <- model.matrix(outcome_formula, df.unobs.x1)
188 if(outcome_family$family=="gaussian"){
190 # likelihood of outcome
191 ll.y.x0 <- dnorm(outcome.unobs, outcome.params %*% t(outcome.model.matrix.x0), sd=sigma.y, log=TRUE)
192 ll.y.x1 <- dnorm(outcome.unobs, outcome.params %*% t(outcome.model.matrix.x1), sd=sigma.y, log=TRUE)
195 if( (proxy_family$family=='binomial') & (proxy_family$link=='logit')){
197 proxy.model.matrix.x0 <- model.matrix(proxy_formula, df.unobs.x0)
198 proxy.model.matrix.x1 <- model.matrix(proxy_formula, df.unobs.x1)
199 proxy.unobs <- df.unobs[[proxy.variable]]
200 ll.w.x0 <- vector(mode='numeric', length=dim(df.unobs)[1])
201 ll.w.x1 <- vector(mode='numeric', length=dim(df.unobs)[1])
203 # likelihood of proxy
204 ll.w.x0[proxy.unobs==1] <- plogis(proxy.params %*% t(proxy.model.matrix.x0[proxy.unobs==1,]), log=TRUE)
205 ll.w.x1[proxy.unobs==1] <- plogis(proxy.params %*% t(proxy.model.matrix.x1[proxy.unobs==1,]), log=TRUE)
207 ll.w.x0[proxy.unobs==0] <- plogis(proxy.params %*% t(proxy.model.matrix.x0[proxy.unobs==0,]), log=TRUE,lower.tail=FALSE)
208 ll.w.x1[proxy.unobs==0] <- plogis(proxy.params %*% t(proxy.model.matrix.x1[proxy.unobs==0,]), log=TRUE,lower.tail=FALSE)
211 if(truth_family$link=='logit'){
212 truth.model.matrix <- model.matrix(truth_formula, df.unobs.x0)
213 # likelihood of truth
214 ll.x.x1 <- plogis(truth.params %*% t(truth.model.matrix), log=TRUE)
215 ll.x.x0 <- plogis(truth.params %*% t(truth.model.matrix), log=TRUE, lower.tail=FALSE)
219 ll.x0 <- ll.y.x0 + ll.w.x0 + ll.x.x0
220 ll.x1 <- ll.y.x1 + ll.w.x1 + ll.x.x1
221 ll.unobs <- sum(colLogSumExps(rbind(ll.x0, ll.x1)))
222 return(-(ll.unobs + ll.obs))
225 outcome.params <- colnames(model.matrix(outcome_formula,df))
226 lower <- rep(-Inf, length(outcome.params))
228 if(outcome_family$family=='gaussian'){
229 params <- c(outcome.params, 'sigma_y')
230 lower <- c(lower, 0.00001)
232 params <- outcome.params
235 proxy.params <- colnames(model.matrix(proxy_formula, df))
236 params <- c(params, paste0('proxy_',proxy.params))
237 lower <- c(lower, rep(-Inf, length(proxy.params)))
239 truth.params <- colnames(model.matrix(truth_formula, df))
240 params <- c(params, paste0('truth_', truth.params))
241 lower <- c(lower, rep(-Inf, length(truth.params)))
242 start <- rep(0.1,length(params))
243 names(start) <- params
246 fit <- optim(start, fn = measerr_mle_nll, lower=lower, method='L-BFGS-B', hessian=TRUE, control=list(maxit=1e6))
247 } else { # method='mle2'
249 quoted.names <- gsub("[\\(\\)]",'',names(start))
251 text <- paste("function(", paste0(quoted.names,'=',start,collapse=','),"){params<-c(",paste0(quoted.names,collapse=','),");return(measerr_mle_nll(params))}")
253 measerr_mle_nll_mle <- eval(parse(text=text))
254 names(start) <- quoted.names
255 names(lower) <- quoted.names
256 fit <- mle2(minuslogl=measerr_mle_nll_mle, start=start, lower=lower, parnames=params,control=list(maxit=1e6),method='L-BFGS-B')
262 ## Experimental, but probably works.
263 measerr_irr_mle <- function(df, outcome_formula, outcome_family=gaussian(), coder_formulas=list(x.obs.0~x, x.obs.1~x), proxy_formula, proxy_family=binomial(link='logit'), truth_formula, truth_family=binomial(link='logit'),method='optim'){
265 ### in this scenario, the ground truth also has measurement error, but we have repeated measures for it.
266 # this time we never get to observe the true X
267 outcome.model.matrix <- model.matrix(outcome_formula, df)
268 proxy.model.matrix <- model.matrix(proxy_formula, df)
269 response.var <- all.vars(outcome_formula)[1]
270 proxy.var <- all.vars(proxy_formula)[1]
271 param.var <- all.vars(truth_formula)[1]
272 truth.var<- all.vars(truth_formula)[1]
273 y <- with(df,eval(parse(text=response.var)))
275 nll <- function(params){
277 n.outcome.model.covars <- dim(outcome.model.matrix)[2]
278 outcome.params <- params[param.idx:n.outcome.model.covars]
279 param.idx <- param.idx + n.outcome.model.covars
282 if(outcome_family$family == "gaussian"){
283 sigma.y <- params[param.idx]
284 param.idx <- param.idx + 1
288 n.proxy.model.covars <- dim(proxy.model.matrix)[2]
289 proxy.params <- params[param.idx:(n.proxy.model.covars+param.idx-1)]
290 param.idx <- param.idx + n.proxy.model.covars
294 if((truth_family$family == "binomial")
295 & (truth_family$link=='logit')){
296 integrate.grid <- expand.grid(replicate(1 + length(coder_formulas), c(0,1), simplify=FALSE))
297 ll.parts <- matrix(nrow=nrow(df),ncol=nrow(integrate.grid))
298 for(i in 1:nrow(integrate.grid)){
299 # setup the dataframe for this row
300 row <- integrate.grid[i,]
302 df.temp[[param.var]] <- row[[1]]
304 for(coder_formula in coder_formulas){
305 coder.var <- all.vars(coder_formula)[1]
306 df.temp[[coder.var]] <- row[[ci]]
310 outcome.model.matrix <- model.matrix(outcome_formula, df.temp)
311 if(outcome_family$family == "gaussian"){
312 ll.y <- dnorm(y, outcome.params %*% t(outcome.model.matrix), sd=sigma.y, log=TRUE)
315 if(proxy_family$family=="binomial" & (proxy_family$link=='logit')){
316 proxy.model.matrix <- model.matrix(proxy_formula, df.temp)
317 ll.w <- vector(mode='numeric', length=dim(proxy.model.matrix)[1])
318 proxyvar <- with(df.temp,eval(parse(text=proxy.var)))
319 ll.w[proxyvar==1] <- plogis(proxy.params %*% t(proxy.model.matrix[proxyvar==1,]),log=TRUE)
320 ll.w[proxyvar==0] <- plogis(proxy.params %*% t(proxy.model.matrix[proxyvar==0,]),log=TRUE,lower.tail=FALSE)
323 ## probability of the coded variables
324 coder.lls <- matrix(nrow=nrow(df.temp),ncol=length(coder_formulas))
326 for(coder_formula in coder_formulas){
327 coder.model.matrix <- model.matrix(coder_formula, df.temp)
328 n.coder.model.covars <- dim(coder.model.matrix)[2]
329 coder.params <- params[param.idx:(n.coder.model.covars + param.idx - 1)]
330 param.idx <- param.idx + n.coder.model.covars
331 coder.var <- all.vars(coder_formula)[1]
332 x.obs <- with(df.temp, eval(parse(text=coder.var)))
333 true.codervar <- df[[all.vars(coder_formula)[1]]]
335 ll.coder <- vector(mode='numeric', length=dim(coder.model.matrix)[1])
336 ll.coder[x.obs==1] <- plogis(coder.params %*% t(coder.model.matrix[x.obs==1,]),log=TRUE)
337 ll.coder[x.obs==0] <- plogis(coder.params %*% t(coder.model.matrix[x.obs==0,]),log=TRUE,lower.tail=FALSE)
339 # don't count when we know the observed value, unless we're accounting for observed value
340 ll.coder[(!is.na(true.codervar)) & (true.codervar != x.obs)] <- NA
341 coder.lls[,ci] <- ll.coder
345 truth.model.matrix <- model.matrix(truth_formula, df.temp)
346 n.truth.model.covars <- dim(truth.model.matrix)[2]
347 truth.params <- params[param.idx:(n.truth.model.covars + param.idx - 1)]
349 for(coder_formula in coder_formulas){
350 coder.model.matrix <- model.matrix(coder_formula, df.temp)
351 n.coder.model.covars <- dim(coder.model.matrix)[2]
352 param.idx <- param.idx - n.coder.model.covars
355 x <- with(df.temp, eval(parse(text=truth.var)))
356 ll.truth <- vector(mode='numeric', length=dim(truth.model.matrix)[1])
357 ll.truth[x==1] <- plogis(truth.params %*% t(truth.model.matrix[x==1,]), log=TRUE)
358 ll.truth[x==0] <- plogis(truth.params %*% t(truth.model.matrix[x==0,]), log=TRUE, lower.tail=FALSE)
360 true.truthvar <- df[[all.vars(truth_formula)[1]]]
362 if(!is.null(true.truthvar)){
363 # ll.truth[(!is.na(true.truthvar)) & (true.truthvar != truthvar)] <- -Inf
364 # ll.truth[(!is.na(true.truthvar)) & (true.truthvar == truthvar)] <- 0
366 ll.parts[,i] <- ll.y + ll.w + apply(coder.lls,1,sum) + ll.truth
370 lls <- rowLogSumExps(ll.parts,na.rm=TRUE)
372 ## likelihood of observed data
373 target <- -1 * sum(lls)
378 outcome.params <- colnames(model.matrix(outcome_formula,df))
379 lower <- rep(-Inf, length(outcome.params))
381 if(outcome_family$family=='gaussian'){
382 params <- c(outcome.params, 'sigma_y')
383 lower <- c(lower, 0.00001)
385 params <- outcome.params
388 proxy.params <- colnames(model.matrix(proxy_formula, df))
389 params <- c(params, paste0('proxy_',proxy.params))
390 positive.params <- paste0('proxy_',truth.var)
391 lower <- c(lower, rep(-Inf, length(proxy.params)))
392 names(lower) <- params
393 lower[positive.params] <- 0.01
396 for(coder_formula in coder_formulas){
397 coder.params <- colnames(model.matrix(coder_formula,df))
398 params <- c(params, paste0('coder_',ci,coder.params))
399 positive.params <- paste0('coder_', ci, truth.var)
401 lower <- c(lower, rep(-Inf, length(coder.params)))
402 names(lower) <- params
403 lower[positive.params] <- 0.01
406 truth.params <- colnames(model.matrix(truth_formula, df))
407 params <- c(params, paste0('truth_', truth.params))
408 lower <- c(lower, rep(-Inf, length(truth.params)))
409 start <- rep(0.1,length(params))
410 names(start) <- params
411 names(lower) <- params
415 fit <- optim(start, fn = nll, lower=lower, method='L-BFGS-B', hessian=TRUE, control=list(maxit=1e6))
418 quoted.names <- gsub("[\\(\\)]",'',names(start))
420 text <- paste("function(", paste0(quoted.names,'=',start,collapse=','),"){params<-c(",paste0(quoted.names,collapse=','),");return(nll(params))}")
422 measerr_mle_nll <- eval(parse(text=text))
423 names(start) <- quoted.names
424 names(lower) <- quoted.names
425 fit <- mle2(minuslogl=measerr_mle_nll, start=start, lower=lower, method='L-BFGS-B',control=list(maxit=1e6))
431 ## Experimental, and does not work.
432 measerr_irr_mle_dv <- function(df, outcome_formula, outcome_family=binomial(link='logit'), coder_formulas=list(y.obs.0~y+w_pred+y.obs.1,y.obs.1~y+w_pred+y.obs.0), proxy_formula=w_pred~y, proxy_family=binomial(link='logit'),method='optim'){
433 integrate.grid <- expand.grid(replicate(1 + length(coder_formulas), c(0,1), simplify=FALSE))
434 print(integrate.grid)
437 outcome.model.matrix <- model.matrix(outcome_formula, df)
438 n.outcome.model.covars <- dim(outcome.model.matrix)[2]
441 ### in this scenario, the ground truth also has measurement error, but we have repeated measures for it.
442 # this time we never get to observe the true X
443 nll <- function(params){
445 outcome.params <- params[param.idx:n.outcome.model.covars]
446 param.idx <- param.idx + n.outcome.model.covars
447 proxy.model.matrix <- model.matrix(proxy_formula, df)
448 n.proxy.model.covars <- dim(proxy.model.matrix)[2]
449 response.var <- all.vars(outcome_formula)[1]
451 if(outcome_family$family == "gaussian"){
452 sigma.y <- params[param.idx]
453 param.idx <- param.idx + 1
456 proxy.params <- params[param.idx:(n.proxy.model.covars+param.idx-1)]
457 param.idx <- param.idx + n.proxy.model.covars
461 if((outcome_family$family == "binomial")
462 & (outcome_family$link=='logit')){
463 ll.parts <- matrix(nrow=nrow(df),ncol=nrow(integrate.grid))
464 for(i in 1:nrow(integrate.grid)){
465 # setup the dataframe for this row
466 row <- integrate.grid[i,]
468 df.temp[[response.var]] <- row[[1]]
470 for(coder_formula in coder_formulas){
471 codervar <- all.vars(coder_formula)[1]
472 df.temp[[codervar]] <- row[[ci]]
476 outcome.model.matrix <- model.matrix(outcome_formula, df.temp)
477 if(outcome_family$family == "gaussian"){
478 ll.y <- dnorm(df.temp[[response.var]], outcome.params %*% t(outcome.model.matrix), sd=sigma.y, log=T)
481 if(outcome_family$family == "binomial" & (outcome_family$link=='logit')){
482 ll.y <- vector(mode='numeric',length=nrow(df.temp))
483 ll.y[df.temp[[response.var]]==1] <- plogis( outcome.params %*% t(outcome.model.matrix), log=TRUE)
484 ll.y[df.temp[[response.var]]==0] <- plogis( outcome.params %*% t(outcome.model.matrix), log=TRUE,lower.tail=FALSE)
487 if(proxy_family$family=="binomial" & (proxy_family$link=='logit')){
488 proxy.model.matrix <- model.matrix(proxy_formula, df.temp)
489 ll.w <- vector(mode='numeric', length=dim(proxy.model.matrix)[1])
490 proxyvar <- with(df.temp,eval(parse(text=all.vars(proxy_formula)[1])))
491 ll.w[proxyvar==1] <- plogis(proxy.params %*% t(proxy.model.matrix[proxyvar==1,]),log=TRUE)
492 ll.w[proxyvar==0] <- plogis(proxy.params %*% t(proxy.model.matrix[proxyvar==0,]),log=TRUE,lower.tail=FALSE)
495 ## probability of the coded variables
496 coder.lls <- matrix(nrow=nrow(df.temp),ncol=length(coder_formulas))
498 for(coder_formula in coder_formulas){
499 coder.model.matrix <- model.matrix(coder_formula, df.temp)
500 n.coder.model.covars <- dim(coder.model.matrix)[2]
501 coder.params <- params[param.idx:(n.coder.model.covars + param.idx - 1)]
502 param.idx <- param.idx + n.coder.model.covars
503 codervar <- with(df.temp, eval(parse(text=all.vars(coder_formula)[1])))
504 true.codervar <- df[[all.vars(coder_formula)[1]]]
506 ll.coder <- vector(mode='numeric', length=dim(coder.model.matrix)[1])
507 ll.coder[codervar==1] <- plogis(coder.params %*% t(coder.model.matrix[codervar==1,]),log=TRUE)
508 ll.coder[codervar==0] <- plogis(coder.params %*% t(coder.model.matrix[codervar==0,]),log=TRUE,lower.tail=FALSE)
510 # don't count when we know the observed value, unless we're accounting for observed value
511 ll.coder[(!is.na(true.codervar)) & (true.codervar != codervar)] <- NA
512 coder.lls[,ci] <- ll.coder
516 for(coder_formula in coder_formulas){
517 coder.model.matrix <- model.matrix(coder_formula, df.temp)
518 n.coder.model.covars <- dim(coder.model.matrix)[2]
519 param.idx <- param.idx - n.coder.model.covars
522 ll.parts[,i] <- ll.y + ll.w + apply(coder.lls,1,function(x) sum(x))
526 lls <- rowLogSumExps(ll.parts,na.rm=TRUE)
528 ## likelihood of observed data
529 target <- -1 * sum(lls)
536 outcome.params <- colnames(model.matrix(outcome_formula,df))
537 response.var <- all.vars(outcome_formula)[1]
538 lower <- rep(-Inf, length(outcome.params))
540 if(outcome_family$family=='gaussian'){
541 params <- c(outcome.params, 'sigma_y')
542 lower <- c(lower, 0.00001)
544 params <- outcome.params
547 ## constrain the model of the coder and proxy vars
548 ## this is to ensure identifiability
549 ## it is a safe assumption because the coders aren't hostile (wrong more often than right)
550 ## so we can assume that y ~Bw, B is positive
551 proxy.params <- colnames(model.matrix(proxy_formula, df))
552 positive.params <- paste0('proxy_',response.var)
553 params <- c(params, paste0('proxy_',proxy.params))
554 lower <- c(lower, rep(-Inf, length(proxy.params)))
555 names(lower) <- params
556 lower[positive.params] <- 0.001
559 for(coder_formula in coder_formulas){
560 coder.params <- colnames(model.matrix(coder_formula,df))
561 latent.coder.params <- coder.params %in% response.var
562 params <- c(params, paste0('coder_',ci,coder.params))
563 positive.params <- paste0('coder_',ci,response.var)
565 lower <- c(lower, rep(-Inf, length(coder.params)))
566 names(lower) <-params
567 lower[positive.params] <- 0.001
570 ## init by using the "loco model"
572 temp.df <- temp.df[y.obs.1 == y.obs.0, y:=y.obs.1]
573 loco.model <- glm(outcome_formula, temp.df, family=outcome_family)
575 start <- rep(1,length(params))
576 names(start) <- params
577 start[names(coef(loco.model))] <- coef(loco.model)
578 names(lower) <- params
581 fit <- optim(start, fn = nll, lower=lower, method='L-BFGS-B', hessian=TRUE,control=list(maxit=1e6))
584 quoted.names <- gsub("[\\(\\)]",'',names(start))
586 text <- paste("function(", paste0(quoted.names,'=',start,collapse=','),"){params<-c(",paste0(quoted.names,collapse=','),");return(nll(params))}")
588 measerr_mle_nll <- eval(parse(text=text))
589 names(start) <- quoted.names
590 names(lower) <- quoted.names
591 fit <- mle2(minuslogl=measerr_mle_nll, start=start, lower=lower, parnames=params,control=list(maxit=1e6),method='L-BFGS-B')