library(arrow)
library(data.table)
library(ggplot2)

df <- data.table(read_feather("example_2_simulation.feather"))

x.naive <- df[,.(N, m, Bxy, Bxy.est.naive, Bxy.ci.lower.naive, Bxy.ci.upper.naive)]
x.naive <- x.naive[,':='(true.in.ci = as.integer((Bxy >= Bxy.ci.lower.naive) & (Bxy <= Bxy.ci.upper.naive)),
                         zero.in.ci = (0 >= Bxy.ci.lower.naive) & (0 <= Bxy.ci.upper.naive),
                         bias = Bxy - Bxy.est.naive,
                         sign.correct = as.integer(sign(Bxy) == sign(Bxy.est.naive)))]

x.naive.plot <- x.naive[,.(p.true.in.ci = mean(true.in.ci),
                           mean.bias = mean(bias),
                           p.sign.correct = mean(as.integer(sign.correct & (! zero.in.ci))),
                           variable='x',
                           method='Naive'
                           ),
                        by=c('N','m')]
    
g.naive <- df[,.(N, m, Bgy, Bgy.est.naive, Bgy.ci.lower.naive, Bgy.ci.upper.naive)]
g.naive <- g.naive[,':='(true.in.ci = as.integer((Bgy >= Bgy.ci.lower.naive) & (Bgy <= Bgy.ci.upper.naive)),
                         zero.in.ci = (0 >= Bgy.ci.lower.naive) & (0 <= Bgy.ci.upper.naive),
                         bias = Bgy - Bgy.est.naive,
                         sign.correct = as.integer(sign(Bgy) == sign(Bgy.est.naive)))]

g.naive.plot <- g.naive[,.(p.true.in.ci = mean(true.in.ci),
                           mean.bias = mean(bias),
                           p.sign.correct = mean(as.integer(sign.correct & (! zero.in.ci))),
                           variable='g',
                           method='Naive'
                           ),
                        by=c('N','m')]
    


x.amelia.full <- x.amelia.full[,':='(true.in.ci = (Bxy.est.true >= Bxy.ci.lower.amelia.full) & (Bxy.est.true <= Bxy.ci.upper.amelia.full),
                                     zero.in.ci = (0 >= Bxy.ci.lower.amelia.full) & (0 <= Bxy.ci.upper.amelia.full),
                                     bias = Bxy.est.true - Bxy.est.amelia.full,
                                     sign.correct = sign(Bxy.est.true) == sign(Bxy.est.amelia.full))]

x.amelia.full.plot <- x.amelia.full[,.(p.true.in.ci = mean(as.integer(true.in.ci)),
                                       mean.bias = mean(bias),
                                       p.sign.correct = mean(as.integer(sign.correct & (! zero.in.ci))),
                                       variable='x',
                                       method='Multiple imputation'
                                       ),
                                    by=c('N','m')]



g.amelia.full <- df[,.(N, m, Bgy.est.true, Bgy.est.amelia.full, Bgy.ci.lower.amelia.full, Bgy.ci.upper.amelia.full)]
g.amelia.full <- g.amelia.full[,':='(true.in.ci = (Bgy.est.true >= Bgy.ci.lower.amelia.full) & (Bgy.est.true <= Bgy.ci.upper.amelia.full),
                                     zero.in.ci = (0 >= Bgy.ci.lower.amelia.full) & (0 <= Bgy.ci.upper.amelia.full),
                                     bias =  Bgy.est.amelia.full - Bgy.est.true,
                                     sign.correct = sign(Bgy.est.true) == sign(Bgy.est.amelia.full))]

g.amelia.full.plot <- g.amelia.full[,.(p.true.in.ci = mean(as.integer(true.in.ci)),
                                       mean.bias = mean(bias),
                                       p.sign.correct = mean(as.integer(sign.correct & (! zero.in.ci))),
                                       variable='g',
                                       method='Multiple imputation'
                                       ),
                                    by=c('N','m')]




x.amelia.nok <- df[,.(N, m, Bxy.est.true, Bxy.est.amelia.nok, Bxy.ci.lower.amelia.nok, Bxy.ci.upper.amelia.nok)]
x.amelia.nok <- x.amelia.nok[,':='(true.in.ci = (Bxy.est.true >= Bxy.ci.lower.amelia.nok) & (Bxy.est.true <= Bxy.ci.upper.amelia.nok),
                                     zero.in.ci = (0 >= Bxy.ci.lower.amelia.nok) & (0 <= Bxy.ci.upper.amelia.nok),
                                     bias =  Bxy.est.amelia.nok - Bxy.est.true,
                                     sign.correct = sign(Bxy.est.true) == sign(Bxy.est.amelia.nok))]

 x.amelia.nok.plot <- x.amelia.nok[,.(p.true.in.ci = mean(as.integer(true.in.ci)),
                                       mean.bias = mean(bias),
                                     p.sign.correct = mean(as.integer(sign.correct & (! zero.in.ci))),
                                     variable='x',
                                     method='Multiple imputation (Classifier features unobserved)'
                                     ),
                                    by=c('N','m')]

g.amelia.nok <- df[,.(N, m, Bgy.est.true, Bgy.est.amelia.nok, Bgy.ci.lower.amelia.nok, Bgy.ci.upper.amelia.nok)]
g.amelia.nok <- g.amelia.nok[,':='(true.in.ci = (Bgy.est.true >= Bgy.ci.lower.amelia.nok) & (Bgy.est.true <= Bgy.ci.upper.amelia.nok),
                                     zero.in.ci = (0 >= Bgy.ci.lower.amelia.nok) & (0 <= Bgy.ci.upper.amelia.nok),
                                     bias =  Bgy.est.amelia.nok - Bgy.est.true,
                                     sign.correct = sign(Bgy.est.true) == sign(Bgy.est.amelia.nok))]

g.amelia.nok.plot <- g.amelia.nok[,.(p.true.in.ci = mean(as.integer(true.in.ci)),
                                       mean.bias = mean(bias),
                                     p.sign.correct = mean(as.integer(sign.correct & (! zero.in.ci))),
                                     variable='g',
                                     method='Multiple imputation (Classifier features unobserved)'
                                     ),
                                    by=c('N','m')]


plot.df <- rbindlist(list(x.naive.plot,g.naive.plot,x.amelia.full.plot,g.amelia.full.plot,x.amelia.nok.plot,g.amelia.nok.plot))

ggplot(plot.df,aes(y=N,x=m,color=p.sign.correct)) + geom_point() + facet_grid(variable ~ method) + scale_color_viridis_c(option='C') + theme_minimal() + xlab("Number of gold standard labels") + ylab("Total sample size") 

kggplot(plot.df,aes(y=N,x=m,color=abs(mean.bias))) + geom_point() + facet_grid(variable ~ method) + scale_color_viridis_c(option='C') + theme_minimal() + xlab("Number of gold standard labels") + ylab("Total sample size") 
