X-Git-Url: https://code.communitydata.science/ml_measurement_error_public.git/blobdiff_plain/47e9367ed5c61b721bdc17cddd76bced4f8ed621..c42b94110b18264fdd66ada100ee05232b7b81bb:/simulations/01_two_covariates.R?ds=sidebyside diff --git a/simulations/01_two_covariates.R b/simulations/01_two_covariates.R index 3fd6914..b8f9317 100644 --- a/simulations/01_two_covariates.R +++ b/simulations/01_two_covariates.R @@ -32,7 +32,7 @@ source("simulation_base.R") simulate_data <- function(N, m, B0=0, Bxy=0.2, Bzy=-0.2, Bzx=0.2, y_explained_variance=0.025, prediction_accuracy=0.73, seed=1){ set.seed(seed) - z <- rbinom(N, 1, 0.5) + z <- rnorm(N,sd=0.5) # x.var.epsilon <- var(Bzx *z) * ((1-zx_explained_variance)/zx_explained_variance) xprime <- Bzx * z #+ x.var.epsilon x <- rbinom(N,1,plogis(xprime)) @@ -77,7 +77,7 @@ parser <- add_argument(parser, "--proxy_formula", help='formula for the proxy va parser <- add_argument(parser, "--truth_formula", help='formula for the true variable', default="x~z") parser <- add_argument(parser, "--Bzx", help='Effect of z on x', default=0.3) parser <- add_argument(parser, "--Bzy", help='Effect of z on y', default=-0.3) -parser <- add_argument(parser, "--Bxy", help='Effect of z on y', default=0.3) +parser <- add_argument(parser, "--Bxy", help='Effect of x on y', default=0.3) args <- parse_args(parser) B0 <- 0 @@ -85,23 +85,21 @@ Bxy <- args$Bxy Bzy <- args$Bzy Bzx <- args$Bzx -if (args$m < args$N){ +df <- simulate_data(args$N, args$m, B0, Bxy, Bzy, Bzx, seed=args$seed + 500, y_explained_variance = args$y_explained_variance, prediction_accuracy=args$prediction_accuracy) - df <- simulate_data(args$N, args$m, B0, Bxy, Bzy, Bzx, seed=args$seed + 500, y_explained_variance = args$y_explained_variance, prediction_accuracy=args$prediction_accuracy) +result <- list('N'=args$N,'m'=args$m,'B0'=B0,'Bxy'=Bxy, Bzx=Bzx, 'Bzy'=Bzy, 'seed'=args$seed, 'y_explained_variance'=args$y_explained_variance, 'prediction_accuracy'=args$prediction_accuracy, 'accuracy_imbalance_difference'=args$accuracy_imbalance_difference, 'outcome_formula'=args$outcome_formula, 'proxy_formula'=args$proxy_formula,truth_formula=args$truth_formula, error='') - result <- list('N'=args$N,'m'=args$m,'B0'=B0,'Bxy'=Bxy, Bzx=Bzx, 'Bzy'=Bzy, 'seed'=args$seed, 'y_explained_variance'=args$y_explained_variance, 'prediction_accuracy'=args$prediction_accuracy, 'accuracy_imbalance_difference'=args$accuracy_imbalance_difference, 'outcome_formula'=args$outcome_formula, 'proxy_formula'=args$proxy_formula,truth_formula=args$truth_formula, error='') - - outline <- run_simulation(df, result, outcome_formula=as.formula(args$outcome_formula), proxy_formula=as.formula(args$proxy_formula), truth_formula=as.formula(args$truth_formula)) +outline <- run_simulation(df, result, outcome_formula=as.formula(args$outcome_formula), proxy_formula=as.formula(args$proxy_formula), truth_formula=as.formula(args$truth_formula)) - outfile_lock <- lock(paste0(args$outfile, '_lock'),exclusive=TRUE) - if(file.exists(args$outfile)){ - logdata <- read_feather(args$outfile) - logdata <- rbind(logdata,as.data.table(outline),fill=TRUE) - } else { - logdata <- as.data.table(outline) - } - - print(outline) - write_feather(logdata, args$outfile) - unlock(outfile_lock) +outfile_lock <- lock(paste0(args$outfile, '_lock'),exclusive=TRUE) +if(file.exists(args$outfile)){ + logdata <- read_feather(args$outfile) + logdata <- rbind(logdata,as.data.table(outline),fill=TRUE) +} else { + logdata <- as.data.table(outline) } + +print(outline) +write_feather(logdata, args$outfile) +unlock(outfile_lock) +