X-Git-Url: https://code.communitydata.science/ml_measurement_error_public.git/blobdiff_plain/cb1e895ff1e3359db17d918caa67b758c0d7e901..refs/heads/irr:/simulations/plot_example.R diff --git a/simulations/plot_example.R b/simulations/plot_example.R index 1a4be9b..ebfd3a9 100644 --- a/simulations/plot_example.R +++ b/simulations/plot_example.R @@ -10,267 +10,172 @@ parser <- add_argument(parser, "--infile", default="", help="name of the file to parser <- add_argument(parser, "--name", default="", help="The name to safe the data to in the remember file.") args <- parse_args(parser) -build_plot_dataset <- function(df){ - 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, - Bxy.est.naive = 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), - mean.est = mean(Bxy.est.naive), - var.est = var(Bxy.est.naive), - N.sims = .N, - p.sign.correct = mean(as.integer(sign.correct & (! zero.in.ci))), - variable='x', - method='Naive' - ), - by=c('N','m')] +summarize.estimator <- function(df, suffix='naive', coefname='x'){ + + part <- df[,c('N', + 'm', + 'Bxy', + paste0('B',coefname,'y.est.',suffix), + paste0('B',coefname,'y.ci.lower.',suffix), + paste0('B',coefname,'y.ci.upper.',suffix), + 'y_explained_variance', + 'Bzx', + 'Bzy', + 'accuracy_imbalance_difference' + ), + with=FALSE] + + true.in.ci <- as.integer((part$Bxy >= part[[paste0('B',coefname,'y.ci.lower.',suffix)]]) & (part$Bxy <= part[[paste0('B',coefname,'y.ci.upper.',suffix)]])) + zero.in.ci <- as.integer(0 >= part[[paste0('B',coefname,'y.ci.lower.',suffix)]]) & (0 <= part[[paste0('B',coefname,'y.ci.upper.',suffix)]]) + bias <- part$Bxy - part[[paste0('B',coefname,'y.est.',suffix)]] + sign.correct <- as.integer(sign(part$Bxy) == sign(part[[paste0('B',coefname,'y.est.',suffix)]])) + + part <- part[,':='(true.in.ci = true.in.ci, + zero.in.ci = zero.in.ci, + bias=bias, + sign.correct =sign.correct)] + + part.plot <- part[, .(p.true.in.ci = mean(true.in.ci), + mean.bias = mean(bias), + mean.est = mean(.SD[[paste0('B',coefname,'y.est.',suffix)]]), + var.est = var(.SD[[paste0('B',coefname,'y.est.',suffix)]]), + est.upper.95 = quantile(.SD[[paste0('B',coefname,'y.est.',suffix)]],0.95,na.rm=T), + est.lower.95 = quantile(.SD[[paste0('B',coefname,'y.est.',suffix)]],0.05,na.rm=T), + N.sims = .N, + p.sign.correct = mean(as.integer(sign.correct & (! zero.in.ci))), + variable=coefname, + method=suffix + ), + by=c("N","m",'y_explained_variance','Bzx', 'Bzy', 'accuracy_imbalance_difference') + ] + return(part.plot) +} - 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, - Bgy.est.naive = 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), - mean.est = mean(Bgy.est.naive), - var.est = var(Bgy.est.naive), - N.sims = .N, - p.sign.correct = mean(as.integer(sign.correct & (! zero.in.ci))), - variable='g', - method='Naive' - ), - by=c('N','m')] +build_plot_dataset <- function(df){ + x.true <- summarize.estimator(df, 'true','x') + + z.true <- summarize.estimator(df, 'true','z') - x.feasible <- df[,.(N, m, Bxy, Bxy.est.feasible, Bxy.ci.lower.feasible, Bxy.ci.upper.feasible)] - x.feasible <- x.feasible[,':='(true.in.ci = as.integer((Bxy >= Bxy.ci.lower.feasible) & (Bxy <= Bxy.ci.upper.feasible)), - zero.in.ci = (0 >= Bxy.ci.lower.feasible) & (0 <= Bxy.ci.upper.feasible), - bias = Bxy - Bxy.est.feasible, - Bxy.est.feasible = Bxy.est.feasible, - sign.correct = as.integer(sign(Bxy) == sign(Bxy.est.feasible)))] - - x.feasible.plot <- x.feasible[,.(p.true.in.ci = mean(true.in.ci), - mean.bias = mean(bias), - mean.est = mean(Bxy.est.feasible), - var.est = var(Bxy.est.feasible), - N.sims = .N, - p.sign.correct = mean(as.integer(sign.correct & (! zero.in.ci))), - variable='x', - method='Feasible' - ), - by=c('N','m')] + x.naive <- summarize.estimator(df, 'naive','x') + z.naive <- summarize.estimator(df,'naive','z') - g.feasible <- df[,.(N, m, Bgy, Bgy.est.feasible, Bgy.ci.lower.feasible, Bgy.ci.upper.feasible)] - g.feasible <- g.feasible[,':='(true.in.ci = as.integer((Bgy >= Bgy.ci.lower.feasible) & (Bgy <= Bgy.ci.upper.feasible)), - zero.in.ci = (0 >= Bgy.ci.lower.feasible) & (0 <= Bgy.ci.upper.feasible), - bias = Bgy - Bgy.est.feasible, - Bgy.est.feasible = Bgy.est.feasible, - sign.correct = as.integer(sign(Bgy) == sign(Bgy.est.feasible)))] - - g.feasible.plot <- g.feasible[,.(p.true.in.ci = mean(true.in.ci), - mean.bias = mean(bias), - mean.est = mean(Bgy.est.feasible), - var.est = var(Bgy.est.feasible), - N.sims = .N, - p.sign.correct = mean(as.integer(sign.correct & (! zero.in.ci))), - variable='g', - method='Feasible' - ), - by=c('N','m')] + x.feasible <- summarize.estimator(df, 'feasible', 'x') + + z.feasible <- summarize.estimator(df, 'feasible', 'z') + + x.amelia.full <- summarize.estimator(df, 'amelia.full', 'x') + + z.amelia.full <- summarize.estimator(df, 'amelia.full', 'z') + x.mecor <- summarize.estimator(df, 'mecor', 'x') + z.mecor <- summarize.estimator(df, 'mecor', 'z') - x.amelia.full <- df[,.(N, m, Bxy, Bxy.est.true, Bxy.ci.lower.amelia.full, Bxy.ci.upper.amelia.full, Bxy.est.amelia.full)] - - 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))), - mean.est = mean(Bxy.est.amelia.full), - var.est = var(Bxy.est.amelia.full), - N.sims = .N, - 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))), - mean.est = mean(Bgy.est.amelia.full), - var.est = var(Bgy.est.amelia.full), - N.sims = .N, - 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))), - ## mean.est = mean(Bxy.est.amelia.nok), - ## var.est = var(Bxy.est.amelia.nok), - ## N.sims = .N, - ## 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))), - ## mean.est = mean(Bgy.est.amelia.nok), - ## var.est = var(Bgy.est.amelia.nok), - ## N.sims = .N, - ## variable='g', - ## method="Multiple imputation (Classifier features unobserved)" - ## ), - ## by=c('N','m')] - - - x.mecor <- df[,.(N,m,Bxy.est.true, Bxy.est.mecor,Bxy.lower.mecor, Bxy.upper.mecor)] - - x.mecor <- x.mecor[,':='(true.in.ci = (Bxy.est.true >= Bxy.lower.mecor) & (Bxy.est.true <= Bxy.upper.mecor), - zero.in.ci = (0 >= Bxy.lower.mecor) & (0 <= Bxy.upper.mecor), - bias = Bxy.est.mecor - Bxy.est.true, - sign.correct = sign(Bxy.est.true) == sign(Bxy.est.mecor))] - - x.mecor.plot <- x.mecor[,.(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))), - mean.est = mean(Bxy.est.mecor), - var.est = var(Bxy.est.mecor), - N.sims = .N, - variable='x', - method='Regression Calibration' - ), - by=c('N','m')] - - g.mecor <- df[,.(N,m,Bgy.est.true, Bgy.est.mecor,Bgy.lower.mecor, Bgy.upper.mecor)] - - g.mecor <- g.mecor[,':='(true.in.ci = (Bgy.est.true >= Bgy.lower.mecor) & (Bgy.est.true <= Bgy.upper.mecor), - zero.in.ci = (0 >= Bgy.lower.mecor) & (0 <= Bgy.upper.mecor), - bias = Bgy.est.mecor - Bgy.est.true, - sign.correct = sign(Bgy.est.true) == sign(Bgy.est.mecor))] - - g.mecor.plot <- g.mecor[,.(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))), - mean.est = mean(Bgy.est.mecor), - var.est = var(Bgy.est.mecor), - N.sims = .N, - variable='g', - method='Regression Calibration' - ), - by=c('N','m')] - - ## x.mecor <- df[,.(N,m,Bgy.est.true, Bgy.est.mecor,Bgy.ci.lower.mecor, Bgy.ci.upper.mecor)] - - ## x.mecor <- x.mecor[,':='(true.in.ci = (Bgy.est.true >= Bgy.ci.lower.mecor) & (Bgy.est.true <= Bgy.ci.upper.mecor), - ## zero.in.ci = (0 >= Bgy.ci.lower.mecor) & (0 <= Bgy.ci.upper.mecor), - ## bias = Bgy.est.mecor - Bgy.est.true, - ## sign.correct = sign(Bgy.est.true) == sign(Bgy.est.mecor))] - - ## x.mecor.plot <- x.mecor[,.(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='Regression Calibration' - ## ), - ## by=c('N','m')] - - - x.gmm <- df[,.(N,m,Bxy.est.true, Bxy.est.gmm,Bxy.ci.lower.gmm, Bxy.ci.upper.gmm)] - x.gmm <- x.gmm[,':='(true.in.ci = (Bxy.est.true >= Bxy.ci.lower.gmm) & (Bxy.est.true <= Bxy.ci.upper.gmm), - zero.in.ci = (0 >= Bxy.ci.lower.gmm) & (0 <= Bxy.ci.upper.gmm), - bias = Bxy.est.gmm - Bxy.est.true, - sign.correct = sign(Bxy.est.true) == sign(Bxy.est.gmm))] - - x.gmm.plot <- x.gmm[,.(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))), - mean.est = mean(Bxy.est.gmm), - - var.est = var(Bxy.est.gmm), - N.sims = .N, - variable='x', - method='2SLS+gmm' - ), - by=c('N','m')] - - g.gmm <- df[,.(N,m,Bgy.est.true, Bgy.est.gmm,Bgy.ci.lower.gmm, Bgy.ci.upper.gmm)] - g.gmm <- g.gmm[,':='(true.in.ci = (Bgy.est.true >= Bgy.ci.lower.gmm) & (Bgy.est.true <= Bgy.ci.upper.gmm), - zero.in.ci = (0 >= Bgy.ci.lower.gmm) & (0 <= Bgy.ci.upper.gmm), - bias = Bgy.est.gmm - Bgy.est.true, - sign.correct = sign(Bgy.est.true) == sign(Bgy.est.gmm))] - - g.gmm.plot <- g.gmm[,.(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))), - mean.est = mean(Bgy.est.gmm), - var.est = var(Bgy.est.gmm), - N.sims = .N, - variable='g', - method='2SLS+gmm' - ), - by=c('N','m')] + x.mecor <- summarize.estimator(df, 'mecor', 'x') - accuracy <- df[,mean(accuracy)] + z.mecor <- summarize.estimator(df, 'mecor', 'z') - plot.df <- rbindlist(list(x.naive.plot,g.naive.plot,x.amelia.full.plot,g.amelia.full.plot,x.mecor.plot, g.mecor.plot, x.gmm.plot, g.gmm.plot, x.feasible.plot, g.feasible.plot),use.names=T) + x.mle <- summarize.estimator(df, 'mle', 'x') - plot.df[,accuracy := accuracy] + z.mle <- summarize.estimator(df, 'mle', 'z') + + x.zhang <- summarize.estimator(df, 'zhang', 'x') - plot.df <- plot.df[,":="(sd.est=sqrt(var.est)/N.sims)] + z.zhang <- summarize.estimator(df, 'zhang', 'z') + + x.gmm <- summarize.estimator(df, 'gmm', 'x') + z.gmm <- summarize.estimator(df, 'gmm', 'z') + + accuracy <- df[,mean(accuracy)] + plot.df <- rbindlist(list(x.true,z.true,x.naive,z.naive,x.amelia.full,z.amelia.full,x.mecor, z.mecor, x.gmm, z.gmm, x.feasible, z.feasible,z.mle, x.mle, x.zhang, z.zhang, x.gmm, z.gmm),use.names=T) + plot.df[,accuracy := accuracy] + plot.df <- plot.df[,":="(sd.est=sqrt(var.est)/N.sims)] return(plot.df) } -df <- read_feather(args$infile) -plot.df <- build_plot_dataset(df) +plot.df <- read_feather(args$infile) + +# df <- df[apply(df,1,function(x) !any(is.na(x)))] + +if(!('Bzx' %in% names(plot.df))) + plot.df[,Bzx:=NA] + +if(!('accuracy_imbalance_difference' %in% names(plot.df))) + plot.df[,accuracy_imbalance_difference:=NA] + +unique(plot.df[,'accuracy_imbalance_difference']) + +#plot.df <- build_plot_dataset(df[accuracy_imbalance_difference==0.1][N==700]) +plot.df <- build_plot_dataset(plot.df) + remember(plot.df,args$name) +#ggplot(df,aes(x=Bxy.est.mle)) + geom_histogram() + facet_grid(accuracy_imbalance_difference ~ Bzy) + +## ## ## df[gmm.ER_pval<0.05] + +## plot.df.test <- plot.df[,':='(method=factor(method,levels=c("Naive","Multiple imputation", "Multiple imputation (Classifier features unobserved)","Regression Calibration","2SLS+gmm","Bespoke MLE", "Feasible"),ordered=T), +## N=factor(N), +## m=factor(m))] + +## plot.df.test <- plot.df.test[(variable=='x') & (method!="Multiple imputation (Classifier features unobserved)")] +## p <- ggplot(plot.df.test, aes(y=mean.est, ymax=mean.est + var.est/2, ymin=mean.est-var.est/2, x=method)) +## p <- p + geom_hline(data=plot.df.test, mapping=aes(yintercept=0.1),linetype=2) + +## p <- p + geom_pointrange() + facet_grid(N~m,as.table=F,scales='free') + scale_x_discrete(labels=label_wrap_gen(4)) +## print(p) + +## plot.df.test <- plot.df[,':='(method=factor(method,levels=c("Naive","Multiple imputation", "Multiple imputation (Classifier features unobserved)","Regression Calibration","2SLS+gmm","Bespoke MLE", "Feasible"),ordered=T), +## N=factor(N), +## m=factor(m))] + +## plot.df.test <- plot.df.test[(variable=='z') & (method!="Multiple imputation (Classifier features unobserved)")] +## p <- ggplot(plot.df.test, aes(y=mean.est, ymax=mean.est + var.est/2, ymin=mean.est-var.est/2, x=method)) +## p <- p + geom_hline(data=plot.df.test, mapping=aes(yintercept=-0.1),linetype=2) + +## p <- p + geom_pointrange() + facet_grid(m~N,as.table=F,scales='free') + scale_x_discrete(labels=label_wrap_gen(4)) +## print(p) + + +## x.mle <- df[,.(N,m,Bxy.est.mle,Bxy.ci.lower.mle, Bxy.ci.upper.mle, y_explained_variance, Bzx, Bzy, accuracy_imbalance_difference)] +## x.mle.plot <- x.mle[,.(mean.est = mean(Bxy.est.mle), +## var.est = var(Bxy.est.mle), +## N.sims = .N, +## variable='z', +## method='Bespoke MLE' +## ), +## by=c("N","m",'y_explained_variance', 'Bzx', 'Bzy','accuracy_imbalance_difference')] + +## z.mle <- df[,.(N,m,Bzy.est.mle,Bzy.ci.lower.mle, Bzy.ci.upper.mle, y_explained_variance, Bzx, Bzy, accuracy_imbalance_difference)] -## df[gmm.ER_pval<0.05] +## z.mle.plot <- z.mle[,.(mean.est = mean(Bzy.est.mle), +## var.est = var(Bzy.est.mle), +## N.sims = .N, +## variable='z', +## method='Bespoke MLE' +## ), +## by=c("N","m",'y_explained_variance','Bzx')] +## plot.df <- z.mle.plot +## plot.df.test <- plot.df[,':='(method=factor(method,levels=c("Naive","Multiple imputation", "Multiple imputation (Classifier features unobserved)","Regression Calibration","2SLS+gmm","Bespoke MLE", "Feasible"),ordered=T), +## N=factor(N), +## m=factor(m))] +## plot.df.test <- plot.df.test[(variable=='z') & (m != 1000) & (m!=500) & (method!="Multiple imputation (Classifier features unobserved)")] +## p <- ggplot(plot.df.test, aes(y=mean.est, ymax=mean.est + var.est/2, ymin=mean.est-var.est/2, x=method)) +## p <- p + geom_hline(aes(yintercept=0.2),linetype=2) +## p <- p + geom_pointrange() + facet_grid(m~Bzx, Bzy,as.table=F) + scale_x_discrete(labels=label_wrap_gen(4)) +## print(p) -## ggplot(plot.df[variable=='x'], aes(y=mean.est, ymax=mean.est + var.est/2, ymin=mean.est-var.est/2, x=method)) + geom_pointrange() + facet_grid(-m~N) + scale_x_discrete(labels=label_wrap_gen(10)) +## ## ggplot(plot.df[variable=='x'], aes(y=mean.est, ymax=mean.est + var.est/2, ymin=mean.est-var.est/2, x=method)) + geom_pointrange() + facet_grid(-m~N) + scale_x_discrete(labels=label_wrap_gen(10)) -## ggplot(plot.df,aes(y=N,x=m,color=p.sign.correct)) + geom_point() + facet_grid(variable ~ method) + scale_color_viridis_c(option='D') + theme_minimal() + xlab("Number of gold standard labels") + ylab("Total sample size") +## ## ggplot(plot.df,aes(y=N,x=m,color=p.sign.correct)) + geom_point() + facet_grid(variable ~ method) + scale_color_viridis_c(option='D') + theme_minimal() + xlab("Number of gold standard labels") + ylab("Total sample size") -## ggplot(plot.df,aes(y=N,x=m,color=abs(mean.bias))) + geom_point() + facet_grid(variable ~ method) + scale_color_viridis_c(option='D') + theme_minimal() + xlab("Number of gold standard labels") + ylab("Total sample size") +## ## ggplot(plot.df,aes(y=N,x=m,color=abs(mean.bias))) + geom_point() + facet_grid(variable ~ method) + scale_color_viridis_c(option='D') + theme_minimal() + xlab("Number of gold standard labels") + ylab("Total sample size")