]> code.communitydata.science - ml_measurement_error_public.git/blob - simulations/plot_example_2.R
Merge branch 'master' of code:ml_measurement_error_public
[ml_measurement_error_public.git] / simulations / plot_example_2.R
1 library(arrow)
2 library(data.table)
3 library(ggplot2)
4
5 df <- data.table(read_feather("example_2_simulation.feather"))
6
7 x.naive <- df[,.(N, m, Bxy, Bxy.est.naive, Bxy.ci.lower.naive, Bxy.ci.upper.naive)]
8 x.naive <- x.naive[,':='(true.in.ci = as.integer((Bxy >= Bxy.ci.lower.naive) & (Bxy <= Bxy.ci.upper.naive)),
9                          zero.in.ci = (0 >= Bxy.ci.lower.naive) & (0 <= Bxy.ci.upper.naive),
10                          bias = Bxy - Bxy.est.naive,
11                          sign.correct = as.integer(sign(Bxy) == sign(Bxy.est.naive)))]
12
13 x.naive.plot <- x.naive[,.(p.true.in.ci = mean(true.in.ci),
14                            mean.bias = mean(bias),
15                            p.sign.correct = mean(as.integer(sign.correct & (! zero.in.ci))),
16                            variable='x',
17                            method='Naive'
18                            ),
19                         by=c('N','m')]
20     
21 g.naive <- df[,.(N, m, Bgy, Bgy.est.naive, Bgy.ci.lower.naive, Bgy.ci.upper.naive)]
22 g.naive <- g.naive[,':='(true.in.ci = as.integer((Bgy >= Bgy.ci.lower.naive) & (Bgy <= Bgy.ci.upper.naive)),
23                          zero.in.ci = (0 >= Bgy.ci.lower.naive) & (0 <= Bgy.ci.upper.naive),
24                          bias = Bgy - Bgy.est.naive,
25                          sign.correct = as.integer(sign(Bgy) == sign(Bgy.est.naive)))]
26
27 g.naive.plot <- g.naive[,.(p.true.in.ci = mean(true.in.ci),
28                            mean.bias = mean(bias),
29                            p.sign.correct = mean(as.integer(sign.correct & (! zero.in.ci))),
30                            variable='g',
31                            method='Naive'
32                            ),
33                         by=c('N','m')]
34     
35
36
37 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),
38                                      zero.in.ci = (0 >= Bxy.ci.lower.amelia.full) & (0 <= Bxy.ci.upper.amelia.full),
39                                      bias = Bxy.est.true - Bxy.est.amelia.full,
40                                      sign.correct = sign(Bxy.est.true) == sign(Bxy.est.amelia.full))]
41
42 x.amelia.full.plot <- x.amelia.full[,.(p.true.in.ci = mean(as.integer(true.in.ci)),
43                                        mean.bias = mean(bias),
44                                        p.sign.correct = mean(as.integer(sign.correct & (! zero.in.ci))),
45                                        variable='x',
46                                        method='Multiple imputation'
47                                        ),
48                                     by=c('N','m')]
49
50
51
52 g.amelia.full <- df[,.(N, m, Bgy.est.true, Bgy.est.amelia.full, Bgy.ci.lower.amelia.full, Bgy.ci.upper.amelia.full)]
53 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),
54                                      zero.in.ci = (0 >= Bgy.ci.lower.amelia.full) & (0 <= Bgy.ci.upper.amelia.full),
55                                      bias =  Bgy.est.amelia.full - Bgy.est.true,
56                                      sign.correct = sign(Bgy.est.true) == sign(Bgy.est.amelia.full))]
57
58 g.amelia.full.plot <- g.amelia.full[,.(p.true.in.ci = mean(as.integer(true.in.ci)),
59                                        mean.bias = mean(bias),
60                                        p.sign.correct = mean(as.integer(sign.correct & (! zero.in.ci))),
61                                        variable='g',
62                                        method='Multiple imputation'
63                                        ),
64                                     by=c('N','m')]
65
66
67
68
69 x.amelia.nok <- df[,.(N, m, Bxy.est.true, Bxy.est.amelia.nok, Bxy.ci.lower.amelia.nok, Bxy.ci.upper.amelia.nok)]
70 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),
71                                      zero.in.ci = (0 >= Bxy.ci.lower.amelia.nok) & (0 <= Bxy.ci.upper.amelia.nok),
72                                      bias =  Bxy.est.amelia.nok - Bxy.est.true,
73                                      sign.correct = sign(Bxy.est.true) == sign(Bxy.est.amelia.nok))]
74
75  x.amelia.nok.plot <- x.amelia.nok[,.(p.true.in.ci = mean(as.integer(true.in.ci)),
76                                        mean.bias = mean(bias),
77                                      p.sign.correct = mean(as.integer(sign.correct & (! zero.in.ci))),
78                                      variable='x',
79                                      method='Multiple imputation (Classifier features unobserved)'
80                                      ),
81                                     by=c('N','m')]
82
83 g.amelia.nok <- df[,.(N, m, Bgy.est.true, Bgy.est.amelia.nok, Bgy.ci.lower.amelia.nok, Bgy.ci.upper.amelia.nok)]
84 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),
85                                      zero.in.ci = (0 >= Bgy.ci.lower.amelia.nok) & (0 <= Bgy.ci.upper.amelia.nok),
86                                      bias =  Bgy.est.amelia.nok - Bgy.est.true,
87                                      sign.correct = sign(Bgy.est.true) == sign(Bgy.est.amelia.nok))]
88
89 g.amelia.nok.plot <- g.amelia.nok[,.(p.true.in.ci = mean(as.integer(true.in.ci)),
90                                        mean.bias = mean(bias),
91                                      p.sign.correct = mean(as.integer(sign.correct & (! zero.in.ci))),
92                                      variable='g',
93                                      method='Multiple imputation (Classifier features unobserved)'
94                                      ),
95                                     by=c('N','m')]
96
97
98 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))
99
100 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") 
101
102 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") 

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