### EXAMPLE 2: demonstrates how measurement error can lead to a type sign error in a covariate
### Even when you have a good predictor, if it's biased against a covariate you can get the wrong sign.
### Even when you include the proxy variable in the regression.
### But with some ground truth and multiple imputation, you can fix it.

library(argparser)
library(mecor)
library(ggplot2)
library(data.table)
library(filelock)
library(arrow)
library(Amelia)
library(Zelig)
library(predictionError)

source("simulation_base.R")

## SETUP:
### we want to estimate x -> y; x is MAR
### we have x -> k; k -> w; x -> w is used to predict x via the model w.
### A realistic scenario is that we have an NLP model predicting something like "racial harassment" in social media comments
### The labels x are binary, but the model provides a continuous predictor

### simulation:
#### how much power do we get from the model in the first place? (sweeping N and m)
#### 
logistic <- function(x) {1/(1+exp(-1*x))}

simulate_latent_cocause <- function(N, m, B0, Bxy, Bgy, Bkx, Bgx, seed){
    set.seed(seed)

    ## the true value of x

    g <- rbinom(N, 1, 0.5)
    xprime <- Bgx * g + rnorm(N,0,1)

    k <- Bkx*xprime + rnorm(N,0,3)

    x <- as.integer(logistic(scale(xprime)) > 0.5)

    y <-  Bxy * x  + Bgy * g + rnorm(N, 0, 2) + B0
    df <- data.table(x=x,k=k,y=y,g=g)

    if( m < N){
        df <- df[sample(nrow(df), m), x.obs := x]
    } else {
        df <- df[, x.obs := x]
    }

    w.model <- glm(x ~ k,df, family=binomial(link='logit'))
    w <- predict(w.model,data.frame(k=k)) + rnorm(N,0,1)
    ## y = B0 + B1x + e

    df[,':='(w=w, w_pred = as.integer(w>0.5))]
    return(df)
}


parser <- arg_parser("Simulate data and fit corrected models")
parser <- add_argument(parser, "--N", default=1000, help="number of observations of w")
parser <- add_argument(parser, "--m", default=100, help="m the number of ground truth observations")
parser <- add_argument(parser, "--seed", default=432, help='seed for the rng')
parser <- add_argument(parser, "--outfile", help='output file', default='example_2_B.feather')
args <- parse_args(parser)


B0 <- 0
Bxy <- 0.2
Bgy <- 0
Bkx <- 3.2
Bgx <- 0

df <- simulate_latent_cocause(args$N, args$m, B0, Bxy, Bgy, Bkx, Bgx, args$seed)

outline <- run_simulation(df
                         ,list('N'=args$N,'m'=args$m,'B0'=B0,'Bxy'=Bxy,'Bgy'=0, 'Bkx'=Bkx, 'Bgx'=0, 'seed'=args$seed))

outfile_lock <- lock(paste0(args$outfile, '_lock'))
if(file.exists(args$outfile)){
    logdata <- read_feather(args$outfile)
    logdata <- rbind(logdata,as.data.table(outline))
} else {
    logdata <- as.data.table(outline)
}

print(outline)
write_feather(logdata, args$outfile)
unlock(outfile_lock)

## for(N in Ns){
##     print(N)
##     for(m in ms){
##         if(N>m){
##             for(seed in seeds){
##                 rows <- append(rows, list(run_simulation(N, m, B0, Bxy, Bkx,  seed)))
##             }
##         }
##     }
## }


## run_simulation <-  function(N, m, B0, Bxy, Bkx, seed){
##     result <- list()
##     df <- simulate_latent_cocause(N, m, B0, Bxy, Bkx, seed)

##     result <- append(result, list(N=N,
##                                   m=m,
##                                   B0=B0,
##                                   Bxy=Bxy,
##                                   Bkx=Bkx,
##                                   seed=seed))

##     (correlation <- cor(df$w,df$x,method='spearman'))
##     result <- append(result, list(correlation=correlation))

##     (accuracy <- mean(df$x == df$w_pred))

##     result <- append(result, list(accuracy=accuracy))
    
##     (model.true <- lm(y ~ x, data=df))

##     (cor(resid(model.true),df$w))

##     true.ci.Bxy <- confint(model.true)['x',]

##     result <- append(result, list(Bxy.est.true=coef(model.true)['x'],
##                                   Bxy.ci.upper.true = true.ci.Bxy[2],
##                                   Bxy.ci.lower.true = true.ci.Bxy[1]))

##     (model.naive <- lm(y~w, data=df))

##     (model.feasible <- lm(y~x.obs,data=df))

##     feasible.ci.Bxy <- confint(model.feasible)['x.obs',]
##     result <- append(result, list(Bxy.est.feasible=coef(model.feasible)['x.obs'],
##                                   Bxy.ci.upper.feasible = feasible.ci.Bxy[2],
##                                   Bxy.ci.lower.feasible = feasible.ci.Bxy[1]))


##     naive.ci.Bxy <- confint(model.naive)['w',]

##     result <- append(result, list(Bxy.est.naive=coef(model.naive)['w'],
##                                   Bxy.ci.upper.naive = naive.ci.Bxy[2],
##                                   Bxy.ci.lower.naive = naive.ci.Bxy[1]))


##     ## multiple imputation when k is observed
    
##     amelia.out.k <- amelia(df, m=200, p2s=0, idvars=c('x','w_pred'), noms=c("x.obs"),lgstc=c('w'))
##     mod.amelia.k <- zelig(y~x.obs, model='ls', data=amelia.out.k$imputations, cite=FALSE)
##     (coefse <- combine_coef_se(mod.amelia.k, messages=FALSE))

##     est.x.mi <- coefse['x.obs','Estimate']
##     est.x.se <- coefse['x.obs','Std.Error']
##     result <- append(result,
##                      list(Bxy.est.amelia.full = est.x.mi,
##                           Bxy.ci.upper.amelia.full = est.x.mi + 1.96 * est.x.se,
##                           Bxy.ci.lower.amelia.full = est.x.mi - 1.96 * est.x.se
##                           ))


##     ## What if we can't observe k -- most realistic scenario. We can't include all the ML features in a model.
##     amelia.out.nok <- amelia(df, m=200, p2s=0, idvars=c("x","k","w_pred"),noms=c("x.obs"),lgstc=c('w'))
##     mod.amelia.nok <- zelig(y~x.obs, model='ls', data=amelia.out.nok$imputations, cite=FALSE)
##     (coefse <- combine_coef_se(mod.amelia.nok, messages=FALSE))

##     est.x.mi <- coefse['x.obs','Estimate']
##     est.x.se <- coefse['x.obs','Std.Error']
##     result <- append(result,
##                      list(Bxy.est.amelia.nok = est.x.mi,
##                           Bxy.ci.upper.amelia.nok = est.x.mi + 1.96 * est.x.se,
##                           Bxy.ci.lower.amelia.nok = est.x.mi - 1.96 * est.x.se
##                           ))

##     p <- v <- train <- rep(0,N)
##     M <- m
##     p[(M+1):(N)] <- 1
##     v[1:(M)] <- 1
##     df <- df[order(x.obs)]
##     y <- df[,y]
##     x <- df[,x.obs]
##     w <- df[,w]
##     (gmm.res <- predicted_covariates(y, x, g, w, v, train, p, max_iter=100, verbose=FALSE))

##     result <- append(result,
##                      list(Bxy.est.gmm = gmm.res$beta[1,1],
##                           Bxy.ci.upper.gmm = gmm.res$confint[1,2],
##                           Bxy.ci.lower.gmm = gmm.res$confint[1,1]))

##     mod.calibrated.mle <- mecor(y ~ MeasError(w, reference = x.obs), df, B=400, method='efficient')

##     (mecor.ci <- summary(mod.calibrated.mle)$c$ci['x.obs',])

##     result <- append(result, list(
##                                  Bxy.est.mecor = mecor.ci['Estimate'],
##                                  Bxy.upper.mecor = mecor.ci['UCI'],
##                                  Bxy.lower.mecor = mecor.ci['LCI'])
##                      )
    

##     return(result)
## }

