1 source("RemembR/R/RemembeR.R")
8 parser <- arg_parser("Simulate data and fit corrected models.")
9 parser <- add_argument(parser, "--infile", default="", help="name of the file to read.")
10 parser <- add_argument(parser, "--name", default="", help="The name to safe the data to in the remember file.")
11 args <- parse_args(parser)
12 source("summarize_estimator.R")
14 build_plot_dataset <- function(df){
16 x.true <- summarize.estimator(df, 'true','x')
18 z.true <- summarize.estimator(df, 'true','z')
20 x.naive <- summarize.estimator(df, 'naive','x')
22 z.naive <- summarize.estimator(df, 'naive','z')
24 x.loa0.feasible <- summarize.estimator(df, 'loa0.feasible','x')
26 z.loa0.feasible <- summarize.estimator(df,'loa0.feasible','z')
28 x.loa0.mle <- summarize.estimator(df, 'loa0.mle', 'x')
30 z.loa0.mle <- summarize.estimator(df, 'loa0.mle', 'z')
32 x.loco.feasible <- summarize.estimator(df, 'loco.feasible', 'x')
34 z.loco.feasible <- summarize.estimator(df, 'loco.feasible', 'z')
36 x.loco.mle <- summarize.estimator(df, 'loco.mle', 'x')
38 z.loco.mle <- summarize.estimator(df, 'loco.mle', 'z')
40 x.loco.mle <- summarize.estimator(df, 'loco.mle', 'x')
42 z.loco.amelia <- summarize.estimator(df, 'amelia.full', 'z')
43 x.loco.amelia <- summarize.estimator(df, 'amelia.full', 'x')
45 z.loco.zhang <- summarize.estimator(df, 'zhang', 'z')
46 x.loco.zhang <- summarize.estimator(df, 'zhang', 'x')
49 z.loco.gmm <- summarize.estimator(df, 'gmm', 'z')
50 x.loco.gmm <- summarize.estimator(df, 'gmm', 'x')
55 ## x.mle <- summarize.estimator(df, 'mle', 'x')
57 ## z.mle <- summarize.estimator(df, 'mle', 'z')
59 accuracy <- df[,mean(accuracy)]
60 plot.df <- rbindlist(list(x.true,z.true,x.loa0.feasible,z.loa0.feasible,x.loa0.mle,z.loa0.mle,x.loco.feasible, z.loco.feasible, x.loco.mle, z.loco.mle, x.loco.amelia, z.loco.amelia,x.loco.zhang, z.loco.zhang,x.loco.gmm, z.loco.gmm,x.naive,z.naive),use.names=T)
61 plot.df[,accuracy := accuracy]
62 plot.df <- plot.df[,":="(sd.est=sqrt(var.est)/N.sims)]
67 sims.df <- read_feather(args$infile)
68 print(unique(sims.df$N))
70 # df <- df[apply(df,1,function(x) !any(is.na(x)))]
72 if(!('Bzx' %in% names(sims.df)))
75 if(!('accuracy_imbalance_difference' %in% names(sims.df)))
76 sims.df[,accuracy_imbalance_difference:=NA]
78 unique(sims.df[,'accuracy_imbalance_difference'])
80 #plot.df <- build_plot_dataset(df[accuracy_imbalance_difference==0.1][N==700])
81 plot.df <- build_plot_dataset(sims.df)
82 change.remember.file("remember_irr.RDS",clear=TRUE)
83 remember(plot.df,args$name)
86 set.remember.prefix(gsub("plot.df.","",args$name))
87 remember(median(sims.df$loco.accuracy),'med.loco.acc')
89 #ggplot(df,aes(x=Bxy.est.mle)) + geom_histogram() + facet_grid(accuracy_imbalance_difference ~ Bzy)
91 ## ## ## df[gmm.ER_pval<0.05]
93 ## 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),
97 ## plot.df.test <- plot.df.test[(variable=='x') & (method!="Multiple imputation (Classifier features unobserved)")]
98 ## p <- ggplot(plot.df.test, aes(y=mean.est, ymax=mean.est + var.est/2, ymin=mean.est-var.est/2, x=method))
99 ## p <- p + geom_hline(data=plot.df.test, mapping=aes(yintercept=0.1),linetype=2)
101 ## p <- p + geom_pointrange() + facet_grid(N~m,as.table=F,scales='free') + scale_x_discrete(labels=label_wrap_gen(4))
104 ## 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),
108 ## plot.df.test <- plot.df.test[(variable=='z') & (method!="Multiple imputation (Classifier features unobserved)")]
109 ## p <- ggplot(plot.df.test, aes(y=mean.est, ymax=mean.est + var.est/2, ymin=mean.est-var.est/2, x=method))
110 ## p <- p + geom_hline(data=plot.df.test, mapping=aes(yintercept=-0.1),linetype=2)
112 ## p <- p + geom_pointrange() + facet_grid(m~N,as.table=F,scales='free') + scale_x_discrete(labels=label_wrap_gen(4))
116 ## x.mle <- df[,.(N,m,Bxy.est.mle,Bxy.ci.lower.mle, Bxy.ci.upper.mle, y_explained_variance, Bzx, Bzy, accuracy_imbalance_difference)]
117 ## x.mle.plot <- x.mle[,.(mean.est = mean(Bxy.est.mle),
118 ## var.est = var(Bxy.est.mle),
121 ## method='Bespoke MLE'
123 ## by=c("N","m",'y_explained_variance', 'Bzx', 'Bzy','accuracy_imbalance_difference')]
125 ## z.mle <- df[,.(N,m,Bzy.est.mle,Bzy.ci.lower.mle, Bzy.ci.upper.mle, y_explained_variance, Bzx, Bzy, accuracy_imbalance_difference)]
127 ## z.mle.plot <- z.mle[,.(mean.est = mean(Bzy.est.mle),
128 ## var.est = var(Bzy.est.mle),
131 ## method='Bespoke MLE'
133 ## by=c("N","m",'y_explained_variance','Bzx')]
135 ## plot.df <- z.mle.plot
136 ## 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),
140 ## plot.df.test <- plot.df.test[(variable=='z') & (m != 1000) & (m!=500) & (method!="Multiple imputation (Classifier features unobserved)")]
141 ## p <- ggplot(plot.df.test, aes(y=mean.est, ymax=mean.est + var.est/2, ymin=mean.est-var.est/2, x=method))
142 ## p <- p + geom_hline(aes(yintercept=0.2),linetype=2)
144 ## p <- p + geom_pointrange() + facet_grid(m~Bzx, Bzy,as.table=F) + scale_x_discrete(labels=label_wrap_gen(4))
148 ## ## 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))
150 ## ## 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")
152 ## ## 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")