X-Git-Url: https://code.communitydata.science/ml_measurement_error_public.git/blobdiff_plain/d0c5766bdf867a81a2477d2cac1d40812110af90..refs/heads/main:/simulations/05_irr_indep.R diff --git a/simulations/05_irr_indep.R b/simulations/05_irr_indep.R index 4c3a109..ebee715 100644 --- a/simulations/05_irr_indep.R +++ b/simulations/05_irr_indep.R @@ -39,7 +39,7 @@ simulate_data <- function(N, m, B0=0, Bxy=0.2, Bzy=-0.2, Bzx=0.2, y_explained_va y.var.epsilon <- (var(Bzy * z) + var(Bxy *x) + 2*cov(Bxy*x,Bzy*z)) * ((1-y_explained_variance)/y_explained_variance) y.epsilon <- rnorm(N, sd = sqrt(y.var.epsilon)) - y <- Bzy * z + Bxy * x + y.epsilon + y <- Bzy * z + Bxy * x + y.epsilon + B0 df <- data.table(x=x,y=y,z=z) @@ -49,9 +49,12 @@ simulate_data <- function(N, m, B0=0, Bxy=0.2, Bzy=-0.2, Bzx=0.2, y_explained_va df <- df[, x.obs := x] } - df[ (!is.na(x.obs)) ,x.obs.0 := abs(x.obs - rbinom(.N, 1, 1-coder_accuracy))] - df[ (!is.na(x.obs)) ,x.obs.1 := abs(x.obs - rbinom(.N, 1, 1-coder_accuracy))] - + coder.0.correct <- rbinom(m, 1, coder_accuracy) + coder.1.correct <- rbinom(m, 1, coder_accuracy) + + df[!is.na(x.obs),x.obs.0 := as.numeric((x.obs & coder.0.correct) | (!x.obs & !coder.0.correct))] + df[!is.na(x.obs),x.obs.1 := as.numeric((x.obs & coder.1.correct) | (!x.obs & !coder.1.correct))] + ## how can you make a model with a specific accuracy? w0 =(1-x)**2 + (-1)**(1-x) * prediction_accuracy @@ -69,21 +72,21 @@ simulate_data <- function(N, m, B0=0, Bxy=0.2, Bzy=-0.2, Bzx=0.2, y_explained_va parser <- arg_parser("Simulate data and fit corrected models") parser <- add_argument(parser, "--N", default=1000, help="number of observations of w") -parser <- add_argument(parser, "--m", default=500, help="m the number of ground truth observations") -parser <- add_argument(parser, "--seed", default=57, help='seed for the rng') +parser <- add_argument(parser, "--m", default=150, help="m the number of ground truth observations") +parser <- add_argument(parser, "--seed", default=1, help='seed for the rng') parser <- add_argument(parser, "--outfile", help='output file', default='example_1.feather') -parser <- add_argument(parser, "--y_explained_variance", help='what proportion of the variance of y can be explained?', default=0.05) +parser <- add_argument(parser, "--y_explained_variance", help='what proportion of the variance of y can be explained?', default=0.1) # parser <- add_argument(parser, "--zx_explained_variance", help='what proportion of the variance of x can be explained by z?', default=0.3) parser <- add_argument(parser, "--prediction_accuracy", help='how accurate is the predictive model?', default=0.73) -parser <- add_argument(parser, "--coder_accuracy", help='how accurate is the predictive model?', default=0.8) +parser <- add_argument(parser, "--coder_accuracy", help='how accurate are the human coders?', default=0.85) parser <- add_argument(parser, "--outcome_formula", help='formula for the outcome variable', default="y~x+z") parser <- add_argument(parser, "--proxy_formula", help='formula for the proxy variable', default="w_pred~x") # parser <- add_argument(parser, "--rater_formula", help='formula for the true variable', default="x.obs~x") 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, "--Bzy", help='Effect of z on y', default=0.27) +parser <- add_argument(parser, "--Bxy", help='Effect of x on y', default=-0.33) args <- parse_args(parser) B0 <- 0 @@ -93,7 +96,7 @@ 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, coder_accuracy=args$coder_accuracy) + df <- simulate_data(args$N, args$m, B0, Bxy, Bzy, Bzx, seed=args$seed, y_explained_variance = args$y_explained_variance, prediction_accuracy=args$prediction_accuracy, coder_accuracy=args$coder_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, 'truth_formula'=args$truth_formula, 'proxy_formula'=args$proxy_formula,truth_formula=args$truth_formula, 'coder_accuracy'=args$coder_accuracy, error='')