]> code.communitydata.science - ml_measurement_error_public.git/blobdiff - simulations/plot_dv_example.R
check in some old simulation updates and a dv examples with real data
[ml_measurement_error_public.git] / simulations / plot_dv_example.R
index f69ed6c012f9f1c8576bbc0a75d92e522293ffa1..45a5d51827cf2cda733c48ddbd63876bc305feaf 100644 (file)
@@ -6,53 +6,52 @@ library(filelock)
 library(argparser)
 
 parser <- arg_parser("Simulate data and fit corrected models.")
-parser <- add_argument(parser, "--infile", default="", help="name of the file to read.")
+parser <- add_argument(parser, "--infile", default="example_4.feather", help="name of the file to read.")
+parser <- add_argument(parser, "--remember-file", default="remembr.RDS", help="name of the remember file.")
 parser <- add_argument(parser, "--name", default="", help="The name to safe the data to in the remember file.")
 args <- parse_args(parser)
 
-
-
-summarize.estimator <- function(df, suffix='naive', coefname='x'){
-
-    part <- df[,c('N',
-                  'm',
-                  'Bxy',
-                  paste0('B',coefname,'y.est.',suffix),
-                  paste0('B',coefname,'y.ci.lower.',suffix),
-                  paste0('B',coefname,'y.ci.upper.',suffix),
-                  'y_explained_variance',
-                  'Bzy',
-                  'accuracy_imbalance_difference'
-                  ),
-               with=FALSE]
+## summarize.estimator <- function(df, suffix='naive', coefname='x'){
+
+##     part <- df[,c('N',
+##                   'm',
+##                   'Bxy',
+##                   paste0('B',coefname,'y.est.',suffix),
+##                   paste0('B',coefname,'y.ci.lower.',suffix),
+##                   paste0('B',coefname,'y.ci.upper.',suffix),
+##                   'y_explained_variance',
+##                   'Bzy'
+##                   ),
+##                with=FALSE]
     
-    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)]]))
-    zero.in.ci <- as.integer(0 >= part[[paste0('B',coefname,'y.ci.lower.',suffix)]]) & (0 <= part[[paste0('B',coefname,'y.ci.upper.',suffix)]])
-    bias <- part$Bxy - part[[paste0('B',coefname,'y.est.',suffix)]]
-    sign.correct <- as.integer(sign(part$Bxy) == sign(part[[paste0('B',coefname,'y.est.',suffix)]]))
-
-    part <- part[,':='(true.in.ci = true.in.ci,
-                       zero.in.ci = zero.in.ci,
-                       bias=bias,
-                       sign.correct =sign.correct)]
-
-    part.plot <- part[, .(p.true.in.ci = mean(true.in.ci),
-                          mean.bias = mean(bias),
-                          mean.est = mean(.SD[[paste0('B',coefname,'y.est.',suffix)]]),
-                          var.est = var(.SD[[paste0('B',coefname,'y.est.',suffix)]]),
-                          est.upper.95 = quantile(.SD[[paste0('B',coefname,'y.est.',suffix)]],0.95),
-                          est.lower.95 = quantile(.SD[[paste0('B',coefname,'y.est.',suffix)]],0.05),
-                          N.sims = .N,
-                          p.sign.correct = mean(as.integer(sign.correct & (! zero.in.ci))),
-                          variable=coefname,
-                          method=suffix
-                          ),
-                      by=c("N","m",'Bzy','accuracy_imbalance_difference','y_explained_variance')
-                      ]
+##     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)]]))
+##     zero.in.ci <- as.integer(0 >= part[[paste0('B',coefname,'y.ci.lower.',suffix)]]) & (0 <= part[[paste0('B',coefname,'y.ci.upper.',suffix)]])
+##     bias <- part$Bxy - part[[paste0('B',coefname,'y.est.',suffix)]]
+##     sign.correct <- as.integer(sign(part$Bxy) == sign(part[[paste0('B',coefname,'y.est.',suffix)]]))
+
+##     part <- part[,':='(true.in.ci = true.in.ci,
+##                        zero.in.ci = zero.in.ci,
+##                        bias=bias,
+##                        sign.correct =sign.correct)]
+
+##     part.plot <- part[, .(p.true.in.ci = mean(true.in.ci),
+##                           mean.bias = mean(bias),
+##                           mean.est = mean(.SD[[paste0('B',coefname,'y.est.',suffix)]]),
+##                           var.est = var(.SD[[paste0('B',coefname,'y.est.',suffix)]]),
+##                           est.upper.95 = quantile(.SD[[paste0('B',coefname,'y.est.',suffix)]],0.95),
+##                           est.lower.95 = quantile(.SD[[paste0('B',coefname,'y.est.',suffix)]],0.05),
+##                           N.sims = .N,
+##                           p.sign.correct = mean(as.integer(sign.correct & (! zero.in.ci))),
+##                           variable=coefname,
+##                           method=suffix
+##                           ),
+##                       by=c("N","m",'Bzy','y_explained_variance')
+##                       ]
     
-    return(part.plot)
-}
+##     return(part.plot)
+## }
 
+source("summarize_estimator.R")
 
 build_plot_dataset <- function(df){
 
@@ -85,12 +84,25 @@ build_plot_dataset <- function(df){
     return(plot.df)
 }
 
-
-df <- read_feather(args$infile)
-plot.df <- build_plot_dataset(df)
+change.remember.file(args$remember_file, clear=TRUE)
+sims.df <- read_feather(args$infile)
+sims.df[,Bzx:=NA]
+sims.df[,y_explained_variance:=NA]
+sims.df[,accuracy_imbalance_difference:=NA]
+plot.df <- build_plot_dataset(sims.df)
 
 remember(plot.df,args$name)
 
+set.remember.prefix(gsub("plot.df.","",args$name))
+
+remember(median(sims.df$cor.xz),'med.cor.xz')
+remember(median(sims.df$accuracy),'med.accuracy')
+remember(median(sims.df$error.cor.x),'med.error.cor.x')
+remember(median(sims.df$error.cor.z),'med.error.cor.z')
+remember(median(sims.df$lik.ratio),'med.lik.ratio')
+
+
+
 
 ## df[gmm.ER_pval<0.05]
 ## 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),

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