+ n.proxy.model.covars <- dim(proxy.model.matrix)[2]
+ proxy.params <- params[param.idx:(n.proxy.model.covars+param.idx-1)]
+ param.idx <- param.idx + n.proxy.model.covars
+
+ df.temp <- copy(df)
+
+ if((truth_family$family == "binomial")
+ & (truth_family$link=='logit')){
+ integrate.grid <- expand.grid(replicate(1 + length(coder_formulas), c(0,1), simplify=FALSE))
+ ll.parts <- matrix(nrow=nrow(df),ncol=nrow(integrate.grid))
+ for(i in 1:nrow(integrate.grid)){
+ # setup the dataframe for this row
+ row <- integrate.grid[i,]
+
+ df.temp[[param.var]] <- row[[1]]
+ ci <- 2
+ for(coder_formula in coder_formulas){
+ coder.var <- all.vars(coder_formula)[1]
+ df.temp[[coder.var]] <- row[[ci]]
+ ci <- ci + 1
+ }
+
+ outcome.model.matrix <- model.matrix(outcome_formula, df.temp)
+ if(outcome_family$family == "gaussian"){
+ ll.y <- dnorm(y, outcome.params %*% t(outcome.model.matrix), sd=sigma.y, log=TRUE)
+ }
+
+ if(proxy_family$family=="binomial" & (proxy_family$link=='logit')){
+ proxy.model.matrix <- model.matrix(proxy_formula, df.temp)
+ ll.w <- vector(mode='numeric', length=dim(proxy.model.matrix)[1])
+ proxyvar <- with(df.temp,eval(parse(text=proxy.var)))
+ ll.w[proxyvar==1] <- plogis(proxy.params %*% t(proxy.model.matrix[proxyvar==1,]),log=TRUE)
+ ll.w[proxyvar==0] <- plogis(proxy.params %*% t(proxy.model.matrix[proxyvar==0,]),log=TRUE,lower.tail=FALSE)
+ }
+
+ ## probability of the coded variables
+ coder.lls <- matrix(nrow=nrow(df.temp),ncol=length(coder_formulas))
+ ci <- 1
+ for(coder_formula in coder_formulas){
+ coder.model.matrix <- model.matrix(coder_formula, df.temp)
+ n.coder.model.covars <- dim(coder.model.matrix)[2]
+ coder.params <- params[param.idx:(n.coder.model.covars + param.idx - 1)]
+ param.idx <- param.idx + n.coder.model.covars
+ coder.var <- all.vars(coder_formula)[1]
+ x.obs <- with(df.temp, eval(parse(text=coder.var)))
+ true.codervar <- df[[all.vars(coder_formula)[1]]]
+
+ ll.coder <- vector(mode='numeric', length=dim(coder.model.matrix)[1])
+ ll.coder[x.obs==1] <- plogis(coder.params %*% t(coder.model.matrix[x.obs==1,]),log=TRUE)
+ ll.coder[x.obs==0] <- plogis(coder.params %*% t(coder.model.matrix[x.obs==0,]),log=TRUE,lower.tail=FALSE)
+
+ # don't count when we know the observed value, unless we're accounting for observed value
+ ll.coder[(!is.na(true.codervar)) & (true.codervar != x.obs)] <- NA
+ coder.lls[,ci] <- ll.coder
+ ci <- ci + 1
+ }
+
+ truth.model.matrix <- model.matrix(truth_formula, df.temp)
+ n.truth.model.covars <- dim(truth.model.matrix)[2]
+ truth.params <- params[param.idx:(n.truth.model.covars + param.idx - 1)]
+
+ for(coder_formula in coder_formulas){
+ coder.model.matrix <- model.matrix(coder_formula, df.temp)
+ n.coder.model.covars <- dim(coder.model.matrix)[2]
+ param.idx <- param.idx - n.coder.model.covars
+ }
+
+ x <- with(df.temp, eval(parse(text=truth.var)))
+ ll.truth <- vector(mode='numeric', length=dim(truth.model.matrix)[1])
+ ll.truth[x==1] <- plogis(truth.params %*% t(truth.model.matrix[x==1,]), log=TRUE)
+ ll.truth[x==0] <- plogis(truth.params %*% t(truth.model.matrix[x==0,]), log=TRUE, lower.tail=FALSE)
+
+ true.truthvar <- df[[all.vars(truth_formula)[1]]]
+
+ if(!is.null(true.truthvar)){
+ # ll.truth[(!is.na(true.truthvar)) & (true.truthvar != truthvar)] <- -Inf
+ # ll.truth[(!is.na(true.truthvar)) & (true.truthvar == truthvar)] <- 0
+ }
+ ll.parts[,i] <- ll.y + ll.w + apply(coder.lls,1,sum) + ll.truth
+
+ }