]> code.communitydata.science - ml_measurement_error_public.git/blob - simulations/plot_irr_example.R
update simulation and mle code
[ml_measurement_error_public.git] / simulations / plot_irr_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 source("summarize_estimator.R")
13
14 build_plot_dataset <- function(df){
15     
16     x.true <-  summarize.estimator(df, 'true','x')
17
18     z.true <-  summarize.estimator(df, 'true','z')
19
20     x.loa0.feasible <- summarize.estimator(df, 'loa0.feasible','x')
21     
22     z.loa0.feasible <- summarize.estimator(df,'loa0.feasible','z')
23
24     x.loa0.mle <- summarize.estimator(df, 'loa0.mle', 'x')
25
26     z.loa0.mle <- summarize.estimator(df, 'loa0.mle', 'z')
27
28     x.loco.feasible <- summarize.estimator(df, 'loco.feasible', 'x')
29
30     z.loco.feasible <- summarize.estimator(df, 'loco.feasible', 'z')
31
32     x.loco.mle <- summarize.estimator(df, 'loco.mle', 'x')
33
34     z.loco.mle <- summarize.estimator(df, 'loco.mle', 'z')
35
36     ## x.mle <- summarize.estimator(df, 'mle', 'x')
37
38     ## z.mle <- summarize.estimator(df, 'mle', 'z')
39
40     accuracy <- df[,mean(accuracy)]
41     plot.df <- rbindlist(list(x.true,z.true,x.loa0.feasible,z.loa0.feasible,x.loa0.mle,z.loa0.mle,x.loco.feasible, z.loco.feasible, x.loco.mle, z.loco.mle),use.names=T)
42     plot.df[,accuracy := accuracy]
43     plot.df <- plot.df[,":="(sd.est=sqrt(var.est)/N.sims)]
44     return(plot.df)
45 }
46
47
48 plot.df <- read_feather(args$infile)
49 print(unique(plot.df$N))
50
51 # df <- df[apply(df,1,function(x) !any(is.na(x)))]
52
53 if(!('Bzx' %in% names(plot.df)))
54     plot.df[,Bzx:=NA]
55
56 if(!('accuracy_imbalance_difference' %in% names(plot.df)))
57     plot.df[,accuracy_imbalance_difference:=NA]
58
59 unique(plot.df[,'accuracy_imbalance_difference'])
60
61 #plot.df <- build_plot_dataset(df[accuracy_imbalance_difference==0.1][N==700])
62 plot.df <- build_plot_dataset(plot.df)
63 change.remember.file("remember_irr.RDS",clear=TRUE)
64 remember(plot.df,args$name)
65
66 #ggplot(df,aes(x=Bxy.est.mle)) + geom_histogram() + facet_grid(accuracy_imbalance_difference ~ Bzy)
67
68 ## ## ## df[gmm.ER_pval<0.05]
69
70 ## 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),
71 ##                                    N=factor(N),
72 ##                                    m=factor(m))]
73
74 ## plot.df.test <- plot.df.test[(variable=='x') & (method!="Multiple imputation (Classifier features unobserved)")]
75 ## p <- ggplot(plot.df.test, aes(y=mean.est, ymax=mean.est + var.est/2, ymin=mean.est-var.est/2, x=method))
76 ## p <- p + geom_hline(data=plot.df.test, mapping=aes(yintercept=0.1),linetype=2)
77
78 ## p <- p + geom_pointrange() + facet_grid(N~m,as.table=F,scales='free') + scale_x_discrete(labels=label_wrap_gen(4))
79 ## print(p)
80
81 ## 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),
82 ##                                    N=factor(N),
83 ##                                    m=factor(m))]
84
85 ## plot.df.test <- plot.df.test[(variable=='z') & (method!="Multiple imputation (Classifier features unobserved)")]
86 ## p <- ggplot(plot.df.test, aes(y=mean.est, ymax=mean.est + var.est/2, ymin=mean.est-var.est/2, x=method))
87 ## p <- p + geom_hline(data=plot.df.test, mapping=aes(yintercept=-0.1),linetype=2)
88
89 ## p <- p + geom_pointrange() + facet_grid(m~N,as.table=F,scales='free') + scale_x_discrete(labels=label_wrap_gen(4))
90 ## print(p)
91
92
93 ## x.mle <- df[,.(N,m,Bxy.est.mle,Bxy.ci.lower.mle, Bxy.ci.upper.mle, y_explained_variance, Bzx, Bzy, accuracy_imbalance_difference)]
94 ## x.mle.plot <- x.mle[,.(mean.est = mean(Bxy.est.mle),
95 ##                        var.est = var(Bxy.est.mle),
96 ##                        N.sims = .N,
97 ##                        variable='z',
98 ##                        method='Bespoke MLE'
99 ##                        ),
100 ##                     by=c("N","m",'y_explained_variance', 'Bzx', 'Bzy','accuracy_imbalance_difference')]
101
102 ## z.mle <- df[,.(N,m,Bzy.est.mle,Bzy.ci.lower.mle, Bzy.ci.upper.mle, y_explained_variance, Bzx, Bzy, accuracy_imbalance_difference)]
103
104 ## z.mle.plot <- z.mle[,.(mean.est = mean(Bzy.est.mle),
105 ##                        var.est = var(Bzy.est.mle),
106 ##                        N.sims = .N,
107 ##                        variable='z',
108 ##                        method='Bespoke MLE'
109 ##                        ),
110 ##                     by=c("N","m",'y_explained_variance','Bzx')]
111
112 ## plot.df <- z.mle.plot
113 ## 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),
114 ##                                    N=factor(N),
115 ##                                    m=factor(m))]
116
117 ## plot.df.test <- plot.df.test[(variable=='z') & (m != 1000) & (m!=500) & (method!="Multiple imputation (Classifier features unobserved)")]
118 ## p <- ggplot(plot.df.test, aes(y=mean.est, ymax=mean.est + var.est/2, ymin=mean.est-var.est/2, x=method))
119 ## p <- p + geom_hline(aes(yintercept=0.2),linetype=2)
120
121 ## p <- p + geom_pointrange() + facet_grid(m~Bzx, Bzy,as.table=F) + scale_x_discrete(labels=label_wrap_gen(4))
122 ## print(p)
123
124
125 ## ## 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))
126
127 ## ## 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") 
128
129 ## ## 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?