4 ## df: dataframe to model
5 ## outcome_formula: formula for y | x, z
6 ## outcome_family: family for y | x, z
7 ## proxy_formula: formula for w | x, z, y
8 ## proxy_family: family for w | x, z, y
9 ## truth_formula: formula for x | z
10 ## truth_family: family for x | z
12 ### ideal formulas for example 1
13 # test.fit.1 <- measerr_mle(df, y ~ x + z, gaussian(), w_pred ~ x, binomial(link='logit'), x ~ z)
15 ### ideal formulas for example 2
16 # test.fit.2 <- measerr_mle(df, y ~ x + z, gaussian(), w_pred ~ x + z + y + y:x, binomial(link='logit'), x ~ z)
19 ## outcome_formula <- y ~ x + z; proxy_formula <- w_pred ~ y + x + z + x:z + x:y + z:y
20 measerr_mle_dv <- function(df, outcome_formula, outcome_family=binomial(link='logit'), proxy_formula, proxy_family=binomial(link='logit')){
22 nll <- function(params){
23 df.obs <- model.frame(outcome_formula, df)
24 proxy.variable <- all.vars(proxy_formula)[1]
25 proxy.model.matrix <- model.matrix(proxy_formula, df)
26 response.var <- all.vars(outcome_formula)[1]
27 y.obs <- with(df.obs,eval(parse(text=response.var)))
28 outcome.model.matrix <- model.matrix(outcome_formula, df.obs)
31 n.outcome.model.covars <- dim(outcome.model.matrix)[2]
32 outcome.params <- params[param.idx:n.outcome.model.covars]
33 param.idx <- param.idx + n.outcome.model.covars
35 if((outcome_family$family == "binomial") & (outcome_family$link == 'logit')){
36 ll.y.obs <- vector(mode='numeric', length=length(y.obs))
37 ll.y.obs[y.obs==1] <- plogis(outcome.params %*% t(outcome.model.matrix[y.obs==1,]),log=TRUE)
38 ll.y.obs[y.obs==0] <- plogis(outcome.params %*% t(outcome.model.matrix[y.obs==0,]),log=TRUE,lower.tail=FALSE)
41 df.obs <- model.frame(proxy_formula,df)
42 n.proxy.model.covars <- dim(proxy.model.matrix)[2]
43 proxy.params <- params[param.idx:(n.proxy.model.covars+param.idx-1)]
45 param.idx <- param.idx + n.proxy.model.covars
46 proxy.obs <- with(df.obs, eval(parse(text=proxy.variable)))
48 if( (proxy_family$family=="binomial") & (proxy_family$link=='logit')){
49 ll.w.obs <- vector(mode='numeric',length=dim(proxy.model.matrix)[1])
50 ll.w.obs[proxy.obs==1] <- plogis(proxy.params %*% t(proxy.model.matrix[proxy.obs==1,]),log=TRUE)
51 ll.w.obs[proxy.obs==0] <- plogis(proxy.params %*% t(proxy.model.matrix[proxy.obs==0,]),log=TRUE, lower.tail=FALSE)
54 ll.obs <- sum(ll.y.obs + ll.w.obs)
56 df.unobs <- df[is.na(df[[response.var]])]
57 df.unobs.y1 <- copy(df.unobs)
58 df.unobs.y1[[response.var]] <- 1
59 df.unobs.y0 <- copy(df.unobs)
60 df.unobs.y0[[response.var]] <- 0
63 outcome.model.matrix.y1 <- model.matrix(outcome_formula, df.unobs.y1)
65 if((outcome_family$family == "binomial") & (outcome_family$link == 'logit')){
66 ll.y.unobs.1 <- vector(mode='numeric', length=dim(outcome.model.matrix.y1)[1])
67 ll.y.unobs.0 <- vector(mode='numeric', length=dim(outcome.model.matrix.y1)[1])
68 ll.y.unobs.1 <- plogis(outcome.params %*% t(outcome.model.matrix.y1),log=TRUE)
69 ll.y.unobs.0 <- plogis(outcome.params %*% t(outcome.model.matrix.y1),log=TRUE,lower.tail=FALSE)
72 proxy.model.matrix.y1 <- model.matrix(proxy_formula, df.unobs.y1)
73 proxy.model.matrix.y0 <- model.matrix(proxy_formula, df.unobs.y0)
74 proxy.unobs <- with(df.unobs, eval(parse(text=proxy.variable)))
76 if( (proxy_family$family=="binomial") & (proxy_family$link=='logit')){
77 ll.w.unobs.1 <- vector(mode='numeric',length=dim(proxy.model.matrix.y1)[1])
78 ll.w.unobs.0 <- vector(mode='numeric',length=dim(proxy.model.matrix.y0)[1])
79 ll.w.unobs.1[proxy.unobs==1] <- plogis(proxy.params %*% t(proxy.model.matrix.y1[proxy.unobs==1,]),log=TRUE)
80 ll.w.unobs.1[proxy.unobs==0] <- plogis(proxy.params %*% t(proxy.model.matrix.y1[proxy.unobs==0,]),log=TRUE, lower.tail=FALSE)
82 ll.w.unobs.0[proxy.unobs==1] <- plogis(proxy.params %*% t(proxy.model.matrix.y0[proxy.unobs==1,]),log=TRUE)
83 ll.w.unobs.0[proxy.unobs==0] <- plogis(proxy.params %*% t(proxy.model.matrix.y0[proxy.unobs==0,]),log=TRUE, lower.tail=FALSE)
86 ll.unobs.1 <- ll.y.unobs.1 + ll.w.unobs.1
87 ll.unobs.0 <- ll.y.unobs.0 + ll.w.unobs.0
88 ll.unobs <- sum(colLogSumExps(rbind(ll.unobs.1,ll.unobs.0)))
89 ll <- ll.unobs + ll.obs
93 params <- colnames(model.matrix(outcome_formula,df))
94 lower <- rep(-Inf, length(params))
95 proxy.params <- colnames(model.matrix(proxy_formula, df))
96 params <- c(params, paste0('proxy_',proxy.params))
97 lower <- c(lower, rep(-Inf, length(proxy.params)))
98 start <- rep(0.1,length(params))
99 names(start) <- params
101 fit <- optim(start, fn = nll, lower=lower, method='L-BFGS-B', hessian=TRUE, control=list(maxit=1e6))
105 ## Experimental, and not necessary if errors are independent.
106 measerr_irr_mle <- function(df, outcome_formula, outcome_family=gaussian(), rater_formula, proxy_formula, proxy_family=binomial(link='logit'), truth_formula, truth_family=binomial(link='logit')){
108 ### in this scenario, the ground truth also has measurement error, but we have repeated measures for it.
110 ## probability of y given observed data.
111 df.obs <- df[!is.na(x.obs.1)]
112 proxy.variable <- all.vars(proxy_formula)[1]
113 df.x.obs.1 <- copy(df.obs)[,x:=1]
114 df.x.obs.0 <- copy(df.obs)[,x:=0]
117 nll <- function(params){
118 outcome.model.matrix.x.obs.0 <- model.matrix(outcome_formula, df.x.obs.0)
119 outcome.model.matrix.x.obs.1 <- model.matrix(outcome_formula, df.x.obs.1)
122 n.outcome.model.covars <- dim(outcome.model.matrix.x.obs.0)[2]
123 outcome.params <- params[param.idx:n.outcome.model.covars]
124 param.idx <- param.idx + n.outcome.model.covars
126 sigma.y <- params[param.idx]
127 param.idx <- param.idx + 1
129 ll.y.x.obs.0 <- dnorm(y.obs, outcome.params %*% t(outcome.model.matrix.x.obs.0),sd=sigma.y, log=TRUE)
130 ll.y.x.obs.1 <- dnorm(y.obs, outcome.params %*% t(outcome.model.matrix.x.obs.1),sd=sigma.y, log=TRUE)
132 ## assume that the two coders are statistically independent conditional on x
133 ll.x.obs.0.x0 <- vector(mode='numeric', length=nrow(df.obs))
134 ll.x.obs.1.x0 <- vector(mode='numeric', length=nrow(df.obs))
135 ll.x.obs.0.x1 <- vector(mode='numeric', length=nrow(df.obs))
136 ll.x.obs.1.x1 <- vector(mode='numeric', length=nrow(df.obs))
138 rater.model.matrix.x.obs.0 <- model.matrix(rater_formula, df.x.obs.0)
139 rater.model.matrix.x.obs.1 <- model.matrix(rater_formula, df.x.obs.1)
141 n.rater.model.covars <- dim(rater.model.matrix.x.obs.0)[2]
142 rater.0.params <- params[param.idx:(n.rater.model.covars + param.idx - 1)]
143 param.idx <- param.idx + n.rater.model.covars
145 rater.1.params <- params[param.idx:(n.rater.model.covars + param.idx - 1)]
146 param.idx <- param.idx + n.rater.model.covars
148 # probability of rater 0 if x is 0 or 1
149 ll.x.obs.0.x0[df.obs$x.obs.0==1] <- plogis(rater.0.params %*% t(rater.model.matrix.x.obs.0[df.obs$x.obs.0==1,]), log=TRUE)
150 ll.x.obs.0.x0[df.obs$x.obs.0==0] <- plogis(rater.0.params %*% t(rater.model.matrix.x.obs.0[df.obs$x.obs.0==0,]), log=TRUE, lower.tail=FALSE)
151 ll.x.obs.0.x1[df.obs$x.obs.0==1] <- plogis(rater.0.params %*% t(rater.model.matrix.x.obs.1[df.obs$x.obs.0==1,]), log=TRUE)
152 ll.x.obs.0.x1[df.obs$x.obs.0==0] <- plogis(rater.0.params %*% t(rater.model.matrix.x.obs.1[df.obs$x.obs.0==0,]), log=TRUE, lower.tail=FALSE)
154 # probability of rater 1 if x is 0 or 1
155 ll.x.obs.1.x0[df.obs$x.obs.1==1] <- plogis(rater.1.params %*% t(rater.model.matrix.x.obs.0[df.obs$x.obs.1==1,]), log=TRUE)
156 ll.x.obs.1.x0[df.obs$x.obs.1==0] <- plogis(rater.1.params %*% t(rater.model.matrix.x.obs.0[df.obs$x.obs.1==0,]), log=TRUE, lower.tail=FALSE)
157 ll.x.obs.1.x1[df.obs$x.obs.1==1] <- plogis(rater.1.params %*% t(rater.model.matrix.x.obs.1[df.obs$x.obs.1==1,]), log=TRUE)
158 ll.x.obs.1.x1[df.obs$x.obs.1==0] <- plogis(rater.1.params %*% t(rater.model.matrix.x.obs.1[df.obs$x.obs.1==0,]), log=TRUE, lower.tail=FALSE)
160 proxy.model.matrix.x0 <- model.matrix(proxy_formula, df.x.obs.0)
161 proxy.model.matrix.x1 <- model.matrix(proxy_formula, df.x.obs.1)
163 n.proxy.model.covars <- dim(proxy.model.matrix.x0)[2]
164 proxy.params <- params[param.idx:(n.proxy.model.covars+param.idx-1)]
165 param.idx <- param.idx + n.proxy.model.covars
167 proxy.obs <- with(df.obs, eval(parse(text=proxy.variable)))
169 if( (proxy_family$family=="binomial") & (proxy_family$link=='logit')){
170 ll.w.obs.x0 <- vector(mode='numeric',length=dim(proxy.model.matrix.x0)[1])
171 ll.w.obs.x1 <- vector(mode='numeric',length=dim(proxy.model.matrix.x1)[1])
173 # proxy_formula likelihood using logistic regression
174 ll.w.obs.x0[proxy.obs==1] <- plogis(proxy.params %*% t(proxy.model.matrix.x0[proxy.obs==1,]),log=TRUE)
175 ll.w.obs.x0[proxy.obs==0] <- plogis(proxy.params %*% t(proxy.model.matrix.x0[proxy.obs==0,]),log=TRUE, lower.tail=FALSE)
177 ll.w.obs.x1[proxy.obs==1] <- plogis(proxy.params %*% t(proxy.model.matrix.x1[proxy.obs==1,]),log=TRUE)
178 ll.w.obs.x1[proxy.obs==0] <- plogis(proxy.params %*% t(proxy.model.matrix.x1[proxy.obs==0,]),log=TRUE, lower.tail=FALSE)
181 ## assume that the probability of x is a logistic regression depending on z
182 truth.model.matrix.obs <- model.matrix(truth_formula, df.obs)
183 n.truth.params <- dim(truth.model.matrix.obs)[2]
184 truth.params <- params[param.idx:(n.truth.params + param.idx - 1)]
186 ll.obs.x0 <- plogis(truth.params %*% t(truth.model.matrix.obs), log=TRUE)
187 ll.obs.x1 <- plogis(truth.params %*% t(truth.model.matrix.obs), log=TRUE, lower.tail=FALSE)
189 ll.obs <- colLogSumExps(rbind(ll.y.x.obs.0 + ll.x.obs.0.x0 + ll.x.obs.1.x0 + ll.obs.x0 + ll.w.obs.x0,
190 ll.y.x.obs.1 + ll.x.obs.0.x1 + ll.x.obs.1.x1 + ll.obs.x1 + ll.w.obs.x1))
192 ### NOW FOR THE FUN PART. Likelihood of the unobserved data.
193 ### we have to integrate out x.obs.0, x.obs.1, and x.
197 df.unobs <- df[is.na(x.obs)]
198 df.x.unobs.0 <- copy(df.unobs)[,x:=0]
199 df.x.unobs.1 <- copy(df.unobs)[,x:=1]
200 y.unobs <- df.unobs$y
202 outcome.model.matrix.x.unobs.0 <- model.matrix(outcome_formula, df.x.unobs.0)
203 outcome.model.matrix.x.unobs.1 <- model.matrix(outcome_formula, df.x.unobs.1)
205 ll.y.unobs.x0 <- dnorm(y.unobs, outcome.params %*% t(outcome.model.matrix.x.unobs.0), sd=sigma.y, log=TRUE)
206 ll.y.unobs.x1 <- dnorm(y.unobs, outcome.params %*% t(outcome.model.matrix.x.unobs.1), sd=sigma.y, log=TRUE)
209 ## THE UNLABELED DATA
212 ## assume that the two coders are statistically independent conditional on x
213 ll.x.unobs.0.x0 <- vector(mode='numeric', length=nrow(df.unobs))
214 ll.x.unobs.1.x0 <- vector(mode='numeric', length=nrow(df.unobs))
215 ll.x.unobs.0.x1 <- vector(mode='numeric', length=nrow(df.unobs))
216 ll.x.unobs.1.x1 <- vector(mode='numeric', length=nrow(df.unobs))
218 df.x.unobs.0[,x.obs := 1]
219 df.x.unobs.1[,x.obs := 1]
221 rater.model.matrix.x.unobs.0 <- model.matrix(rater_formula, df.x.unobs.0)
222 rater.model.matrix.x.unobs.1 <- model.matrix(rater_formula, df.x.unobs.1)
225 ## # probability of rater 0 if x is 0 or 1
226 ## ll.x.unobs.0.x0 <- colLogSumExps(rbind(plogis(rater.0.params %*% t(rater.model.matrix.x.unobs.0), log=TRUE),
227 ## plogis(rater.0.params %*% t(rater.model.matrix.x.unobs.0), log=TRUE, lower.tail=TRUE)))
229 ## ll.x.unobs.0.x1 <- colLogSumExps(rbind(plogis(rater.0.params %*% t(rater.model.matrix.x.unobs.1), log=TRUE),
230 ## plogis(rater.0.params %*% t(rater.model.matrix.x.unobs.1), log=TRUE, lower.tail=TRUE)))
232 ## # probability of rater 1 if x is 0 or 1
233 ## ll.x.unobs.1.x0 <- colLogSumExps(rbind(plogis(rater.1.params %*% t(rater.model.matrix.x.unobs.0), log=TRUE),
234 ## plogis(rater.1.params %*% t(rater.model.matrix.x.unobs.0), log=TRUE, lower.tail=TRUE)))
236 ## ll.x.unobs.1.x1 <- colLogSumExps(rbind(plogis(rater.1.params %*% t(rater.model.matrix.x.unobs.1), log=TRUE),
237 ## plogis(rater.1.params %*% t(rater.model.matrix.x.unobs.1), log=TRUE, lower.tail=TRUE)))
240 proxy.unobs <- with(df.unobs, eval(parse(text=proxy.variable)))
241 proxy.model.matrix.x0.unobs <- model.matrix(proxy_formula, df.x.unobs.0)
242 proxy.model.matrix.x1.unobs <- model.matrix(proxy_formula, df.x.unobs.1)
244 if( (proxy_family$family=="binomial") & (proxy_family$link=='logit')){
245 ll.w.unobs.x0 <- vector(mode='numeric',length=dim(proxy.model.matrix.x0)[1])
246 ll.w.unobs.x1 <- vector(mode='numeric',length=dim(proxy.model.matrix.x1)[1])
249 # proxy_formula likelihood using logistic regression
250 ll.w.unobs.x0[proxy.unobs==1] <- plogis(proxy.params %*% t(proxy.model.matrix.x0.unobs[proxy.unobs==1,]),log=TRUE)
251 ll.w.unobs.x0[proxy.unobs==0] <- plogis(proxy.params %*% t(proxy.model.matrix.x0.unobs[proxy.unobs==0,]),log=TRUE, lower.tail=FALSE)
253 ll.w.unobs.x1[proxy.unobs==1] <- plogis(proxy.params %*% t(proxy.model.matrix.x1.unobs[proxy.unobs==1,]),log=TRUE)
254 ll.w.unobs.x1[proxy.unobs==0] <- plogis(proxy.params %*% t(proxy.model.matrix.x1.unobs[proxy.unobs==0,]),log=TRUE, lower.tail=FALSE)
257 truth.model.matrix.unobs <- model.matrix(truth_formula, df.unobs)
259 ll.unobs.x0 <- plogis(truth.params %*% t(truth.model.matrix.unobs), log=TRUE)
260 ll.unobs.x1 <- plogis(truth.params %*% t(truth.model.matrix.unobs), log=TRUE, lower.tail=FALSE)
262 ll.unobs <- colLogSumExps(rbind(ll.unobs.x0 + ll.w.unobs.x0 + ll.y.unobs.x0,
263 ll.unobs.x1 + ll.w.unobs.x1 + ll.y.unobs.x1))
265 return(-1 *( sum(ll.obs) + sum(ll.unobs)))
268 outcome.params <- colnames(model.matrix(outcome_formula,df))
269 lower <- rep(-Inf, length(outcome.params))
271 if(outcome_family$family=='gaussian'){
272 params <- c(outcome.params, 'sigma_y')
273 lower <- c(lower, 0.00001)
275 params <- outcome.params
278 rater.0.params <- colnames(model.matrix(rater_formula,df))
279 params <- c(params, paste0('rater_0',rater.0.params))
280 lower <- c(lower, rep(-Inf, length(rater.0.params)))
282 rater.1.params <- colnames(model.matrix(rater_formula,df))
283 params <- c(params, paste0('rater_1',rater.1.params))
284 lower <- c(lower, rep(-Inf, length(rater.1.params)))
286 proxy.params <- colnames(model.matrix(proxy_formula, df))
287 params <- c(params, paste0('proxy_',proxy.params))
288 lower <- c(lower, rep(-Inf, length(proxy.params)))
290 truth.params <- colnames(model.matrix(truth_formula, df))
291 params <- c(params, paste0('truth_', truth.params))
292 lower <- c(lower, rep(-Inf, length(truth.params)))
293 start <- rep(0.1,length(params))
294 names(start) <- params
296 fit <- optim(start, fn = nll, lower=lower, method='L-BFGS-B', hessian=TRUE, control=list(maxit=1e6))
301 measerr_mle <- function(df, outcome_formula, outcome_family=gaussian(), proxy_formula, proxy_family=binomial(link='logit'), truth_formula, truth_family=binomial(link='logit')){
303 measrr_mle_nll <- function(params){
304 df.obs <- model.frame(outcome_formula, df)
305 proxy.variable <- all.vars(proxy_formula)[1]
306 proxy.model.matrix <- model.matrix(proxy_formula, df)
307 response.var <- all.vars(outcome_formula)[1]
308 y.obs <- with(df.obs,eval(parse(text=response.var)))
310 outcome.model.matrix <- model.matrix(outcome_formula, df)
313 n.outcome.model.covars <- dim(outcome.model.matrix)[2]
314 outcome.params <- params[param.idx:n.outcome.model.covars]
315 param.idx <- param.idx + n.outcome.model.covars
317 ## likelihood for the fully observed data
318 if(outcome_family$family == "gaussian"){
319 sigma.y <- params[param.idx]
320 param.idx <- param.idx + 1
322 # outcome_formula likelihood using linear regression
323 ll.y.obs <- dnorm(y.obs, outcome.params %*% t(outcome.model.matrix),sd=sigma.y, log=TRUE)
326 df.obs <- model.frame(proxy_formula,df)
327 n.proxy.model.covars <- dim(proxy.model.matrix)[2]
328 proxy.params <- params[param.idx:(n.proxy.model.covars+param.idx-1)]
329 param.idx <- param.idx + n.proxy.model.covars
330 proxy.obs <- with(df.obs, eval(parse(text=proxy.variable)))
332 if( (proxy_family$family=="binomial") & (proxy_family$link=='logit')){
333 ll.w.obs <- vector(mode='numeric',length=dim(proxy.model.matrix)[1])
335 # proxy_formula likelihood using logistic regression
336 ll.w.obs[proxy.obs==1] <- plogis(proxy.params %*% t(proxy.model.matrix[proxy.obs==1,]),log=TRUE)
337 ll.w.obs[proxy.obs==0] <- plogis(proxy.params %*% t(proxy.model.matrix[proxy.obs==0,]),log=TRUE, lower.tail=FALSE)
340 df.obs <- model.frame(truth_formula, df)
341 truth.variable <- all.vars(truth_formula)[1]
342 truth.obs <- with(df.obs, eval(parse(text=truth.variable)))
343 truth.model.matrix <- model.matrix(truth_formula,df)
344 n.truth.model.covars <- dim(truth.model.matrix)[2]
346 truth.params <- params[param.idx:(n.truth.model.covars + param.idx - 1)]
348 if( (truth_family$family=="binomial") & (truth_family$link=='logit')){
349 ll.x.obs <- vector(mode='numeric',length=dim(truth.model.matrix)[1])
351 # truth_formula likelihood using logistic regression
352 ll.x.obs[truth.obs==1] <- plogis(truth.params %*% t(truth.model.matrix[truth.obs==1,]),log=TRUE)
353 ll.x.obs[truth.obs==0] <- plogis(truth.params %*% t(truth.model.matrix[truth.obs==0,]),log=TRUE, lower.tail=FALSE)
356 # add the three likelihoods
357 ll.obs <- sum(ll.y.obs + ll.w.obs + ll.x.obs)
359 ## likelihood for the predicted data
360 ## integrate out the "truth" variable.
362 if(truth_family$family=='binomial'){
363 df.unobs <- df[is.na(eval(parse(text=truth.variable)))]
364 df.unobs.x1 <- copy(df.unobs)
365 df.unobs.x1[,'x'] <- 1
366 df.unobs.x0 <- copy(df.unobs)
367 df.unobs.x0[,'x'] <- 0
368 outcome.unobs <- with(df.unobs, eval(parse(text=response.var)))
370 outcome.model.matrix.x0 <- model.matrix(outcome_formula, df.unobs.x0)
371 outcome.model.matrix.x1 <- model.matrix(outcome_formula, df.unobs.x1)
372 if(outcome_family$family=="gaussian"){
374 # likelihood of outcome
375 ll.y.x0 <- dnorm(outcome.unobs, outcome.params %*% t(outcome.model.matrix.x0), sd=sigma.y, log=TRUE)
376 ll.y.x1 <- dnorm(outcome.unobs, outcome.params %*% t(outcome.model.matrix.x1), sd=sigma.y, log=TRUE)
379 if( (proxy_family$family=='binomial') & (proxy_family$link=='logit')){
381 proxy.model.matrix.x0 <- model.matrix(proxy_formula, df.unobs.x0)
382 proxy.model.matrix.x1 <- model.matrix(proxy_formula, df.unobs.x1)
383 proxy.unobs <- df.unobs[[proxy.variable]]
384 ll.w.x0 <- vector(mode='numeric', length=dim(df.unobs)[1])
385 ll.w.x1 <- vector(mode='numeric', length=dim(df.unobs)[1])
387 # likelihood of proxy
388 ll.w.x0[proxy.unobs==1] <- plogis(proxy.params %*% t(proxy.model.matrix.x0[proxy.unobs==1,]), log=TRUE)
389 ll.w.x1[proxy.unobs==1] <- plogis(proxy.params %*% t(proxy.model.matrix.x1[proxy.unobs==1,]), log=TRUE)
391 ll.w.x0[proxy.unobs==0] <- plogis(proxy.params %*% t(proxy.model.matrix.x0[proxy.unobs==0,]), log=TRUE,lower.tail=FALSE)
392 ll.w.x1[proxy.unobs==0] <- plogis(proxy.params %*% t(proxy.model.matrix.x1[proxy.unobs==0,]), log=TRUE,lower.tail=FALSE)
395 if(truth_family$link=='logit'){
396 truth.model.matrix <- model.matrix(truth_formula, df.unobs.x0)
397 # likelihood of truth
398 ll.x.x1 <- plogis(truth.params %*% t(truth.model.matrix), log=TRUE)
399 ll.x.x0 <- plogis(truth.params %*% t(truth.model.matrix), log=TRUE, lower.tail=FALSE)
403 ll.x0 <- ll.y.x0 + ll.w.x0 + ll.x.x0
404 ll.x1 <- ll.y.x1 + ll.w.x1 + ll.x.x1
405 ll.unobs <- sum(colLogSumExps(rbind(ll.x0, ll.x1)))
406 return(-(ll.unobs + ll.obs))
409 outcome.params <- colnames(model.matrix(outcome_formula,df))
410 lower <- rep(-Inf, length(outcome.params))
412 if(outcome_family$family=='gaussian'){
413 params <- c(outcome.params, 'sigma_y')
414 lower <- c(lower, 0.00001)
416 params <- outcome.params
419 proxy.params <- colnames(model.matrix(proxy_formula, df))
420 params <- c(params, paste0('proxy_',proxy.params))
421 lower <- c(lower, rep(-Inf, length(proxy.params)))
423 truth.params <- colnames(model.matrix(truth_formula, df))
424 params <- c(params, paste0('truth_', truth.params))
425 lower <- c(lower, rep(-Inf, length(truth.params)))
426 start <- rep(0.1,length(params))
427 names(start) <- params
429 fit <- optim(start, fn = measrr_mle_nll, lower=lower, method='L-BFGS-B', hessian=TRUE, control=list(maxit=1e6))