]> code.communitydata.science - ml_measurement_error_public.git/blob - simulations/plot_dv_example.R
71963b1f67cf2cfca3afb92db49f757a7005099f
[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, "--remember-file", default="remembr.RDS", help="name of the remember file.")
11 parser <- add_argument(parser, "--name", default="", help="The name to safe the data to in the remember file.")
12 args <- parse_args(parser)
13
14 ## summarize.estimator <- function(df, suffix='naive', coefname='x'){
15
16 ##     part <- df[,c('N',
17 ##                   'm',
18 ##                   'Bxy',
19 ##                   paste0('B',coefname,'y.est.',suffix),
20 ##                   paste0('B',coefname,'y.ci.lower.',suffix),
21 ##                   paste0('B',coefname,'y.ci.upper.',suffix),
22 ##                   'y_explained_variance',
23 ##                   'Bzy'
24 ##                   ),
25 ##                with=FALSE]
26     
27 ##     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)]]))
28 ##     zero.in.ci <- as.integer(0 >= part[[paste0('B',coefname,'y.ci.lower.',suffix)]]) & (0 <= part[[paste0('B',coefname,'y.ci.upper.',suffix)]])
29 ##     bias <- part$Bxy - part[[paste0('B',coefname,'y.est.',suffix)]]
30 ##     sign.correct <- as.integer(sign(part$Bxy) == sign(part[[paste0('B',coefname,'y.est.',suffix)]]))
31
32 ##     part <- part[,':='(true.in.ci = true.in.ci,
33 ##                        zero.in.ci = zero.in.ci,
34 ##                        bias=bias,
35 ##                        sign.correct =sign.correct)]
36
37 ##     part.plot <- part[, .(p.true.in.ci = mean(true.in.ci),
38 ##                           mean.bias = mean(bias),
39 ##                           mean.est = mean(.SD[[paste0('B',coefname,'y.est.',suffix)]]),
40 ##                           var.est = var(.SD[[paste0('B',coefname,'y.est.',suffix)]]),
41 ##                           est.upper.95 = quantile(.SD[[paste0('B',coefname,'y.est.',suffix)]],0.95),
42 ##                           est.lower.95 = quantile(.SD[[paste0('B',coefname,'y.est.',suffix)]],0.05),
43 ##                           N.sims = .N,
44 ##                           p.sign.correct = mean(as.integer(sign.correct & (! zero.in.ci))),
45 ##                           variable=coefname,
46 ##                           method=suffix
47 ##                           ),
48 ##                       by=c("N","m",'Bzy','y_explained_variance')
49 ##                       ]
50     
51 ##     return(part.plot)
52 ## }
53
54 source("summarize_estimator.R")
55
56 build_plot_dataset <- function(df){
57
58     x.true <- summarize.estimator(df, 'true','x')
59     z.true <- summarize.estimator(df, 'true','z')
60
61     x.naive <- summarize.estimator(df, 'naive','x')
62     z.naive <- summarize.estimator(df, 'naive','z')
63     
64     x.feasible <- summarize.estimator(df, 'feasible','x')
65     z.feasible <- summarize.estimator(df, 'feasible','z')
66     
67     x.amelia.full <- summarize.estimator(df, 'amelia.full','x')
68     z.amelia.full <- summarize.estimator(df, 'amelia.full','z')
69
70     x.mle <- summarize.estimator(df, 'mle','x')
71     z.mle <- summarize.estimator(df, 'mle','z')
72
73     x.zhang <- summarize.estimator(df, 'zhang','x')
74     z.zhang <- summarize.estimator(df, 'zhang','z')
75     
76     accuracy <- df[,mean(accuracy)]
77
78     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)
79
80     plot.df[,accuracy := accuracy]
81
82     plot.df <- plot.df[,":="(sd.est=sqrt(var.est)/N.sims)]
83
84     return(plot.df)
85 }
86
87 change.remember.file(args$remember_file, clear=TRUE)
88 sims.df <- read_feather(args$infile)
89 sims.df[,Bzx:=NA]
90 sims.df[,accuracy_imbalance_difference:=NA]
91 plot.df <- build_plot_dataset(sims.df)
92
93 remember(plot.df,args$name)
94
95 set.remember.prefix(gsub("plot.df.","",args$name))
96
97 remember(median(sims.df$cor.xz),'med.cor.xz')
98 remember(median(sims.df$accuracy),'med.accuracy')
99 remember(median(sims.df$error.cor.x),'med.error.cor.x')
100 remember(median(sims.df$lik.ratio),'med.lik.ratio')
101
102
103
104
105 ## df[gmm.ER_pval<0.05]
106 ## 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),
107 ##                                    N=factor(N),
108 ##                                    m=factor(m))]
109
110
111 ## plot.df.test <- plot.df.test[(variable=='z') & (m != 1000) & (m!=500) & !is.na(p.true.in.ci) & (method!="Multiple imputation (Classifier features unobserved)")]
112 ## p <- ggplot(plot.df.test, aes(y=mean.est, ymax=mean.est + var.est/2, ymin=mean.est-var.est/2, x=method))
113 ## p <- p + geom_hline(aes(yintercept=-0.05),linetype=2)
114
115 ## p <- p + geom_pointrange() + facet_grid(m~N,as.table=F) + scale_x_discrete(labels=label_wrap_gen(4))
116 ## print(p)
117 ## 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))
118
119 ## 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") 
120
121 ## 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?