X-Git-Url: https://code.communitydata.science/ml_measurement_error_public.git/blobdiff_plain/5c931a7198452ff3ce0ace5b1f68046bfb33d352..214551f74cc94ef1fa2f24aa317265c25bb03757:/simulations/summarize_estimator.R diff --git a/simulations/summarize_estimator.R b/simulations/summarize_estimator.R index 3e4209f..8ddeb7c 100644 --- a/simulations/summarize_estimator.R +++ b/simulations/summarize_estimator.R @@ -1,18 +1,23 @@ +library(ggdist) -summarize.estimator <- function(df, suffix='naive', coefname='x'){ +summarize.estimator <- function(sims.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', - 'Bzx', - 'Bzy', - 'accuracy_imbalance_difference' - ), - with=FALSE] + reported_vars <- c( + 'Bxy', + paste0('B',coefname,'y.est.',suffix), + paste0('B',coefname,'y.ci.lower.',suffix), + paste0('B',coefname,'y.ci.upper.',suffix) + ) + + + grouping_vars <- c('N','m','B0', 'Bxy', 'Bzy', 'Bzx', 'Px', 'Py','y_explained_variance', 'prediction_accuracy','outcome_formula','proxy_formula','truth_formula','z_bias','y_bias') + + grouping_vars <- grouping_vars[grouping_vars %in% names(df)] + + part <- sims.df[, + unique(c(reported_vars, + grouping_vars)), + 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)]])) @@ -25,14 +30,17 @@ summarize.estimator <- function(df, suffix='naive', coefname='x'){ 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)]]), + mean.est = mean(.SD[[paste0('B',coefname,'y.est.',suffix)]],na.rm=T), + var.est = var(.SD[[paste0('B',coefname,'y.est.',suffix)]],na.rm=T), est.upper.95 = quantile(.SD[[paste0('B',coefname,'y.est.',suffix)]],0.975,na.rm=T), est.lower.95 = quantile(.SD[[paste0('B',coefname,'y.est.',suffix)]],0.025,na.rm=T), mean.ci.upper = mean(.SD[[paste0('B',coefname,'y.ci.upper.',suffix)]],na.rm=T), mean.ci.lower = mean(.SD[[paste0('B',coefname,'y.ci.lower.',suffix)]],na.rm=T), + median.ci.upper = median(.SD[[paste0('B',coefname,'y.ci.upper.',suffix)]],na.rm=T), + median.ci.lower = median(.SD[[paste0('B',coefname,'y.ci.lower.',suffix)]],na.rm=T), ci.upper.975 = quantile(.SD[[paste0('B',coefname,'y.ci.upper.',suffix)]],0.975,na.rm=T), ci.upper.025 = quantile(.SD[[paste0('B',coefname,'y.ci.upper.',suffix)]],0.025,na.rm=T), ci.lower.975 = quantile(.SD[[paste0('B',coefname,'y.ci.lower.',suffix)]],0.975,na.rm=T), @@ -43,7 +51,7 @@ summarize.estimator <- function(df, suffix='naive', coefname='x'){ variable=coefname, method=suffix ), - by=c("N","m",'y_explained_variance','Bzx', 'Bzy', 'accuracy_imbalance_difference') + by=grouping_vars, ] return(part.plot)