]> code.communitydata.science - ml_measurement_error_public.git/blob - simulations/plot_example.R
Added, but didn't test the remaining robustness checks.
[ml_measurement_error_public.git] / simulations / plot_example.R
1 source("RemembR/R/RemembeR.R")
2 library(arrow)
3 library(data.table)
4 library(ggplot2)
5 library(filelock)
6 library(argparser)
7
8 source("summarize_estimator.R")
9
10
11 parser <- arg_parser("Simulate data and fit corrected models.")
12 parser <- add_argument(parser, "--infile", default="example_2.feather", help="name of the file to read.")
13 parser <- add_argument(parser, "--remember-file", default="remembr.RDS", help="name of the remember file.")
14 parser <- add_argument(parser, "--name", default="", help="The name to safe the data to in the remember file.")
15 args <- parse_args(parser)
16
17
18
19 ## summarize.estimator <- function(df, suffix='naive', coefname='x'){
20
21 ##     part <- df[,c('N',
22 ##                   'm',
23 ##                   'Bxy',
24 ##                   paste0('B',coefname,'y.est.',suffix),
25 ##                   paste0('B',coefname,'y.ci.lower.',suffix),
26 ##                   paste0('B',coefname,'y.ci.upper.',suffix),
27 ##                   'y_explained_variance',
28 ##                   'Bzx',
29 ##                   'Bzy',
30 ##                   'accuracy_imbalance_difference'
31 ##                   ),
32 ##                with=FALSE]
33     
34 ##     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)]]))
35 ##     zero.in.ci <- as.integer(0 >= part[[paste0('B',coefname,'y.ci.lower.',suffix)]]) & (0 <= part[[paste0('B',coefname,'y.ci.upper.',suffix)]])
36 ##     bias <- part$Bxy - part[[paste0('B',coefname,'y.est.',suffix)]]
37 ##     sign.correct <- as.integer(sign(part$Bxy) == sign(part[[paste0('B',coefname,'y.est.',suffix)]]))
38
39 ##     part <- part[,':='(true.in.ci = true.in.ci,
40 ##                        zero.in.ci = zero.in.ci,
41 ##                        bias=bias,
42 ##                        sign.correct =sign.correct)]
43
44 ##     part.plot <- part[, .(p.true.in.ci = mean(true.in.ci),
45 ##                           mean.bias = mean(bias),
46 ##                           mean.est = mean(.SD[[paste0('B',coefname,'y.est.',suffix)]]),
47 ##                           var.est = var(.SD[[paste0('B',coefname,'y.est.',suffix)]]),
48 ##                           est.upper.95 = quantile(.SD[[paste0('B',coefname,'y.est.',suffix)]],0.95,na.rm=T),
49 ##                           est.lower.95 = quantile(.SD[[paste0('B',coefname,'y.est.',suffix)]],0.05,na.rm=T),
50 ##                           N.sims = .N,
51 ##                           p.sign.correct = mean(as.integer(sign.correct & (! zero.in.ci))),
52 ##                           variable=coefname,
53 ##                           method=suffix
54 ##                           ),
55 ##                       by=c("N","m",'y_explained_variance','Bzx', 'Bzy', 'accuracy_imbalance_difference')
56 ##                       ]
57     
58 ##     return(part.plot)
59 ## }
60
61 build_plot_dataset <- function(df){
62     
63     x.true <-  summarize.estimator(df, 'true','x')
64
65     z.true <-  summarize.estimator(df, 'true','z')
66
67     x.naive <- summarize.estimator(df, 'naive','x')
68     
69     z.naive <- summarize.estimator(df,'naive','z')
70
71     x.feasible <- summarize.estimator(df, 'feasible', 'x')
72
73     z.feasible <- summarize.estimator(df, 'feasible', 'z')
74
75     x.amelia.full <- summarize.estimator(df, 'amelia.full', 'x')
76
77     z.amelia.full <- summarize.estimator(df, 'amelia.full', 'z')
78     
79     ## x.mecor <- summarize.estimator(df, 'mecor', 'x')
80
81     ## z.mecor <- summarize.estimator(df, 'mecor', 'z')
82
83     ## x.mecor <- summarize.estimator(df, 'mecor', 'x')
84
85     ## z.mecor <- summarize.estimator(df, 'mecor', 'z')
86
87     x.mle <- summarize.estimator(df, 'mle', 'x')
88
89     z.mle <- summarize.estimator(df, 'mle', 'z')
90     
91     x.zhang <- summarize.estimator(df, 'zhang', 'x')
92
93     z.zhang <- summarize.estimator(df, 'zhang', 'z')
94
95     x.gmm <- summarize.estimator(df, 'gmm', 'x')
96
97     z.gmm <- summarize.estimator(df, 'gmm', 'z')
98
99     accuracy <- df[,mean(accuracy)]
100     plot.df <- rbindlist(list(x.true,z.true,x.naive,z.naive,x.amelia.full,z.amelia.full,x.gmm, z.gmm, x.feasible, z.feasible,z.mle, x.mle, x.zhang, z.zhang, x.gmm, z.gmm),use.names=T)
101     plot.df[,accuracy := accuracy]
102     plot.df <- plot.df[,":="(sd.est=sqrt(var.est)/N.sims)]
103     return(plot.df)
104 }
105
106
107 sims.df <- read_feather(args$infile)
108 unique(sims.df[,.N,by=.(N,m)])
109 print(unique(sims.df$N))
110
111 # df <- df[apply(df,1,function(x) !any(is.na(x)))]
112
113 if(!('Bzx' %in% names(sims.df)))
114     sims.df[,Bzx:=NA]
115
116 if(!('accuracy_imbalance_difference' %in% names(sims.df)))
117     sims.df[,accuracy_imbalance_difference:=NA]
118
119 unique(sims.df[,'accuracy_imbalance_difference'])
120
121 change.remember.file(args$remember_file, clear=TRUE)
122 #plot.df <- build_plot_dataset(df[accuracy_imbalance_difference==0.1][N==700])
123 plot.df <- build_plot_dataset(sims.df)
124
125 remember(plot.df,args$name)
126
127 set.remember.prefix(gsub("plot.df.","",args$name))
128
129 remember(median(sims.df$cor.xz),'med.cor.xz')
130 remember(median(sims.df$accuracy),'med.accuracy')
131 remember(median(sims.df$accuracy.y0),'med.accuracy.y0')
132 remember(median(sims.df$accuracy.y1),'med.accuracy.y1')
133 remember(median(sims.df$fpr),'med.fpr')
134 remember(median(sims.df$fpr.y0),'med.fpr.y0')
135 remember(median(sims.df$fpr.y1),'med.fpr.y1')
136 remember(median(sims.df$fnr),'med.fnr')
137 remember(median(sims.df$fnr.y0),'med.fnr.y0')
138 remember(median(sims.df$fnr.y1),'med.fnr.y1')
139
140 remember(median(sims.df$cor.resid.w_pred),'cor.resid.w_pred')
141
142 #ggplot(df,aes(x=Bxy.est.mle)) + geom_histogram() + facet_grid(accuracy_imbalance_difference ~ Bzy)
143
144 ## ## ## df[gmm.ER_pval<0.05]
145
146 ## 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),
147 ##                                    N=factor(N),
148 ##                                    m=factor(m))]
149
150 ## plot.df.test <- plot.df.test[(variable=='x') & (method!="Multiple imputation (Classifier features unobserved)")]
151 ## p <- ggplot(plot.df.test, aes(y=mean.est, ymax=mean.est + var.est/2, ymin=mean.est-var.est/2, x=method))
152 ## p <- p + geom_hline(data=plot.df.test, mapping=aes(yintercept=0.1),linetype=2)
153
154 ## p <- p + geom_pointrange() + facet_grid(N~m,as.table=F,scales='free') + scale_x_discrete(labels=label_wrap_gen(4))
155 ## print(p)
156
157 ## 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),
158 ##                                    N=factor(N),
159 ##                                    m=factor(m))]
160
161 ## plot.df.test <- plot.df.test[(variable=='z') & (method!="Multiple imputation (Classifier features unobserved)")]
162 ## p <- ggplot(plot.df.test, aes(y=mean.est, ymax=mean.est + var.est/2, ymin=mean.est-var.est/2, x=method))
163 ## p <- p + geom_hline(data=plot.df.test, mapping=aes(yintercept=-0.1),linetype=2)
164
165 ## p <- p + geom_pointrange() + facet_grid(m~N,as.table=F,scales='free') + scale_x_discrete(labels=label_wrap_gen(4))
166 ## print(p)
167
168
169 ## x.mle <- df[,.(N,m,Bxy.est.mle,Bxy.ci.lower.mle, Bxy.ci.upper.mle, y_explained_variance, Bzx, Bzy, accuracy_imbalance_difference)]
170 ## x.mle.plot <- x.mle[,.(mean.est = mean(Bxy.est.mle),
171 ##                        var.est = var(Bxy.est.mle),
172 ##                        N.sims = .N,
173 ##                        variable='z',
174 ##                        method='Bespoke MLE'
175 ##                        ),
176 ##                     by=c("N","m",'y_explained_variance', 'Bzx', 'Bzy','accuracy_imbalance_difference')]
177
178 ## z.mle <- df[,.(N,m,Bzy.est.mle,Bzy.ci.lower.mle, Bzy.ci.upper.mle, y_explained_variance, Bzx, Bzy, accuracy_imbalance_difference)]
179
180 ## z.mle.plot <- z.mle[,.(mean.est = mean(Bzy.est.mle),
181 ##                        var.est = var(Bzy.est.mle),
182 ##                        N.sims = .N,
183 ##                        variable='z',
184 ##                        method='Bespoke MLE'
185 ##                        ),
186 ##                     by=c("N","m",'y_explained_variance','Bzx')]
187
188 ## plot.df <- z.mle.plot
189 ## 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),
190 ##                                    N=factor(N),
191 ##                                    m=factor(m))]
192
193 ## plot.df.test <- plot.df.test[(variable=='z') & (m != 1000) & (m!=500) & (method!="Multiple imputation (Classifier features unobserved)")]
194 ## p <- ggplot(plot.df.test, aes(y=mean.est, ymax=mean.est + var.est/2, ymin=mean.est-var.est/2, x=method))
195 ## p <- p + geom_hline(aes(yintercept=0.2),linetype=2)
196
197 ## p <- p + geom_pointrange() + facet_grid(m~Bzx, Bzy,as.table=F) + scale_x_discrete(labels=label_wrap_gen(4))
198 ## print(p)
199
200
201 ## ## 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))
202
203 ## ## 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") 
204
205 ## ## 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") 

Community Data Science Collective || Want to submit a patch?