]> code.communitydata.science - ml_measurement_error_public.git/blob - simulations/plot_example.R
add TADA stuff
[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 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
13 summarize.estimator <- function(df, suffix='naive', coefname='x'){
14
15     part <- df[,c('N',
16                   'm',
17                   'Bxy',
18                   paste0('B',coefname,'y.est.',suffix),
19                   paste0('B',coefname,'y.ci.lower.',suffix),
20                   paste0('B',coefname,'y.ci.upper.',suffix),
21                   'y_explained_variance',
22                   'Bzx',
23                   'Bzy',
24                   'accuracy_imbalance_difference'
25                   ),
26                with=FALSE]
27     
28     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)]]))
29     zero.in.ci <- as.integer(0 >= part[[paste0('B',coefname,'y.ci.lower.',suffix)]]) & (0 <= part[[paste0('B',coefname,'y.ci.upper.',suffix)]])
30     bias <- part$Bxy - part[[paste0('B',coefname,'y.est.',suffix)]]
31     sign.correct <- as.integer(sign(part$Bxy) == sign(part[[paste0('B',coefname,'y.est.',suffix)]]))
32
33     part <- part[,':='(true.in.ci = true.in.ci,
34                        zero.in.ci = zero.in.ci,
35                        bias=bias,
36                        sign.correct =sign.correct)]
37
38     part.plot <- part[, .(p.true.in.ci = mean(true.in.ci),
39                           mean.bias = mean(bias),
40                           mean.est = mean(.SD[[paste0('B',coefname,'y.est.',suffix)]]),
41                           var.est = var(.SD[[paste0('B',coefname,'y.est.',suffix)]]),
42                           est.upper.95 = quantile(.SD[[paste0('B',coefname,'y.est.',suffix)]],0.95,na.rm=T),
43                           est.lower.95 = quantile(.SD[[paste0('B',coefname,'y.est.',suffix)]],0.05,na.rm=T),
44                           N.sims = .N,
45                           p.sign.correct = mean(as.integer(sign.correct & (! zero.in.ci))),
46                           variable=coefname,
47                           method=suffix
48                           ),
49                       by=c("N","m",'y_explained_variance','Bzx', 'Bzy', 'accuracy_imbalance_difference')
50                       ]
51     
52     return(part.plot)
53 }
54
55 build_plot_dataset <- function(df){
56     
57     x.true <-  summarize.estimator(df, 'true','x')
58
59     z.true <-  summarize.estimator(df, 'true','z')
60
61     x.naive <- summarize.estimator(df, 'naive','x')
62     
63     z.naive <- summarize.estimator(df,'naive','z')
64
65     x.feasible <- summarize.estimator(df, 'feasible', 'x')
66
67     z.feasible <- summarize.estimator(df, 'feasible', 'z')
68
69     x.amelia.full <- summarize.estimator(df, 'amelia.full', 'x')
70
71     z.amelia.full <- summarize.estimator(df, 'amelia.full', 'z')
72     
73     x.mecor <- summarize.estimator(df, 'mecor', 'x')
74
75     z.mecor <- summarize.estimator(df, 'mecor', 'z')
76
77     x.mecor <- summarize.estimator(df, 'mecor', 'x')
78
79     z.mecor <- summarize.estimator(df, 'mecor', 'z')
80
81     x.mle <- summarize.estimator(df, 'mle', 'x')
82
83     z.mle <- summarize.estimator(df, 'mle', 'z')
84     
85     x.zhang <- summarize.estimator(df, 'zhang', 'x')
86
87     z.zhang <- summarize.estimator(df, 'zhang', 'z')
88
89     x.gmm <- summarize.estimator(df, 'gmm', 'x')
90
91     z.gmm <- summarize.estimator(df, 'gmm', 'z')
92
93     accuracy <- df[,mean(accuracy)]
94     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)
95     plot.df[,accuracy := accuracy]
96     plot.df <- plot.df[,":="(sd.est=sqrt(var.est)/N.sims)]
97     return(plot.df)
98 }
99
100
101 plot.df <- read_feather(args$infile)
102 print(unique(plot.df$N))
103
104 # df <- df[apply(df,1,function(x) !any(is.na(x)))]
105
106 if(!('Bzx' %in% names(plot.df)))
107     plot.df[,Bzx:=NA]
108
109 if(!('accuracy_imbalance_difference' %in% names(plot.df)))
110     plot.df[,accuracy_imbalance_difference:=NA]
111
112 unique(plot.df[,'accuracy_imbalance_difference'])
113
114 #plot.df <- build_plot_dataset(df[accuracy_imbalance_difference==0.1][N==700])
115 plot.df <- build_plot_dataset(plot.df)
116
117 remember(plot.df,args$name)
118
119 #ggplot(df,aes(x=Bxy.est.mle)) + geom_histogram() + facet_grid(accuracy_imbalance_difference ~ Bzy)
120
121 ## ## ## df[gmm.ER_pval<0.05]
122
123 ## 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),
124 ##                                    N=factor(N),
125 ##                                    m=factor(m))]
126
127 ## plot.df.test <- plot.df.test[(variable=='x') & (method!="Multiple imputation (Classifier features unobserved)")]
128 ## p <- ggplot(plot.df.test, aes(y=mean.est, ymax=mean.est + var.est/2, ymin=mean.est-var.est/2, x=method))
129 ## p <- p + geom_hline(data=plot.df.test, mapping=aes(yintercept=0.1),linetype=2)
130
131 ## p <- p + geom_pointrange() + facet_grid(N~m,as.table=F,scales='free') + scale_x_discrete(labels=label_wrap_gen(4))
132 ## print(p)
133
134 ## 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),
135 ##                                    N=factor(N),
136 ##                                    m=factor(m))]
137
138 ## plot.df.test <- plot.df.test[(variable=='z') & (method!="Multiple imputation (Classifier features unobserved)")]
139 ## p <- ggplot(plot.df.test, aes(y=mean.est, ymax=mean.est + var.est/2, ymin=mean.est-var.est/2, x=method))
140 ## p <- p + geom_hline(data=plot.df.test, mapping=aes(yintercept=-0.1),linetype=2)
141
142 ## p <- p + geom_pointrange() + facet_grid(m~N,as.table=F,scales='free') + scale_x_discrete(labels=label_wrap_gen(4))
143 ## print(p)
144
145
146 ## x.mle <- df[,.(N,m,Bxy.est.mle,Bxy.ci.lower.mle, Bxy.ci.upper.mle, y_explained_variance, Bzx, Bzy, accuracy_imbalance_difference)]
147 ## x.mle.plot <- x.mle[,.(mean.est = mean(Bxy.est.mle),
148 ##                        var.est = var(Bxy.est.mle),
149 ##                        N.sims = .N,
150 ##                        variable='z',
151 ##                        method='Bespoke MLE'
152 ##                        ),
153 ##                     by=c("N","m",'y_explained_variance', 'Bzx', 'Bzy','accuracy_imbalance_difference')]
154
155 ## z.mle <- df[,.(N,m,Bzy.est.mle,Bzy.ci.lower.mle, Bzy.ci.upper.mle, y_explained_variance, Bzx, Bzy, accuracy_imbalance_difference)]
156
157 ## z.mle.plot <- z.mle[,.(mean.est = mean(Bzy.est.mle),
158 ##                        var.est = var(Bzy.est.mle),
159 ##                        N.sims = .N,
160 ##                        variable='z',
161 ##                        method='Bespoke MLE'
162 ##                        ),
163 ##                     by=c("N","m",'y_explained_variance','Bzx')]
164
165 ## plot.df <- z.mle.plot
166 ## 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),
167 ##                                    N=factor(N),
168 ##                                    m=factor(m))]
169
170 ## plot.df.test <- plot.df.test[(variable=='z') & (m != 1000) & (m!=500) & (method!="Multiple imputation (Classifier features unobserved)")]
171 ## p <- ggplot(plot.df.test, aes(y=mean.est, ymax=mean.est + var.est/2, ymin=mean.est-var.est/2, x=method))
172 ## p <- p + geom_hline(aes(yintercept=0.2),linetype=2)
173
174 ## p <- p + geom_pointrange() + facet_grid(m~Bzx, Bzy,as.table=F) + scale_x_discrete(labels=label_wrap_gen(4))
175 ## print(p)
176
177
178 ## ## 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))
179
180 ## ## 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") 
181
182 ## ## 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?