]> code.communitydata.science - ml_measurement_error_public.git/blob - simulations/plot_dv_example.R
4052c38f26c8e388c37b358be24c5b93197972b4
[ml_measurement_error_public.git] / simulations / plot_dv_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                   'Bzy'
23                   ),
24                with=FALSE]
25     
26     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)]]))
27     zero.in.ci <- as.integer(0 >= part[[paste0('B',coefname,'y.ci.lower.',suffix)]]) & (0 <= part[[paste0('B',coefname,'y.ci.upper.',suffix)]])
28     bias <- part$Bxy - part[[paste0('B',coefname,'y.est.',suffix)]]
29     sign.correct <- as.integer(sign(part$Bxy) == sign(part[[paste0('B',coefname,'y.est.',suffix)]]))
30
31     part <- part[,':='(true.in.ci = true.in.ci,
32                        zero.in.ci = zero.in.ci,
33                        bias=bias,
34                        sign.correct =sign.correct)]
35
36     part.plot <- part[, .(p.true.in.ci = mean(true.in.ci),
37                           mean.bias = mean(bias),
38                           mean.est = mean(.SD[[paste0('B',coefname,'y.est.',suffix)]]),
39                           var.est = var(.SD[[paste0('B',coefname,'y.est.',suffix)]]),
40                           est.upper.95 = quantile(.SD[[paste0('B',coefname,'y.est.',suffix)]],0.95),
41                           est.lower.95 = quantile(.SD[[paste0('B',coefname,'y.est.',suffix)]],0.05),
42                           N.sims = .N,
43                           p.sign.correct = mean(as.integer(sign.correct & (! zero.in.ci))),
44                           variable=coefname,
45                           method=suffix
46                           ),
47                       by=c("N","m",'Bzy','y_explained_variance')
48                       ]
49     
50     return(part.plot)
51 }
52
53
54 build_plot_dataset <- function(df){
55
56     x.true <- summarize.estimator(df, 'true','x')
57     z.true <- summarize.estimator(df, 'true','z')
58
59     x.naive <- summarize.estimator(df, 'naive','x')
60     z.naive <- summarize.estimator(df, 'naive','z')
61     
62     x.feasible <- summarize.estimator(df, 'feasible','x')
63     z.feasible <- summarize.estimator(df, 'feasible','z')
64     
65     x.amelia.full <- summarize.estimator(df, 'amelia.full','x')
66     z.amelia.full <- summarize.estimator(df, 'amelia.full','z')
67
68     x.mle <- summarize.estimator(df, 'mle','x')
69     z.mle <- summarize.estimator(df, 'mle','z')
70
71     x.zhang <- summarize.estimator(df, 'zhang','x')
72     z.zhang <- summarize.estimator(df, 'zhang','z')
73     
74     accuracy <- df[,mean(accuracy)]
75
76     plot.df <- rbindlist(list(x.true, z.true, x.naive,z.naive,x.amelia.full,z.amelia.full,x.mle, z.mle, x.zhang, z.zhang, x.feasible, z.feasible),use.names=T)
77
78     plot.df[,accuracy := accuracy]
79
80     plot.df <- plot.df[,":="(sd.est=sqrt(var.est)/N.sims)]
81
82     return(plot.df)
83 }
84
85
86 df <- read_feather(args$infile)
87 plot.df <- build_plot_dataset(df)
88
89 remember(plot.df,args$name)
90
91
92 ## 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),
94 ##                                    N=factor(N),
95 ##                                    m=factor(m))]
96
97
98 ## plot.df.test <- plot.df.test[(variable=='z') & (m != 1000) & (m!=500) & !is.na(p.true.in.ci) & (method!="Multiple imputation (Classifier features unobserved)")]
99 ## p <- ggplot(plot.df.test, aes(y=mean.est, ymax=mean.est + var.est/2, ymin=mean.est-var.est/2, x=method))
100 ## p <- p + geom_hline(aes(yintercept=-0.05),linetype=2)
101
102 ## p <- p + geom_pointrange() + facet_grid(m~N,as.table=F) + scale_x_discrete(labels=label_wrap_gen(4))
103 ## print(p)
104 ## 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))
105
106 ## 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") 
107
108 ## 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?