Last active
May 26, 2018 10:36
-
-
Save eddjberry/17648ed8bc0b29880db13c07d80f40d9 to your computer and use it in GitHub Desktop.
Simulate a binomial target and some features
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sim_binom <- function(n_samples = 1000, n_features = 2, | |
true_target_prob = 0.5, beta = NULL, seed = NULL) { | |
if(!is.null(seed)) { | |
set.seed(seed) | |
} | |
x = matrix(rnorm(n_samples * n_features), | |
nrow = n_samples, ncol = n_features) | |
b0 = log(true_target_prob / (1 - true_target_prob)) | |
if(is.null(beta)) { | |
beta = rnorm(n_features, mean = 0, sd = 0.5) | |
} else { | |
stopifnot(length(beta) == n_features, is.vector(beta), is.numeric(beta)) | |
} | |
z = b0 + beta %*% t(x) | |
prob = as.vector(1 / (1 + exp(-z))) | |
y = rbinom(n_samples, 1, prob = prob) | |
df = as.data.frame(x) | |
df$y = y | |
df$prob = prob | |
list(df = df, | |
beta_0 = b0, | |
beta = beta) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment