Created
May 29, 2020 21:41
-
-
Save MNoorFawi/2267a920caa9fff2dce0029262c00e11 to your computer and use it in GitHub Desktop.
Single Variable Model in R
This file contains hidden or 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
## How to create a single variable model in R | |
single_variable_model <- function(x, y, pos) { | |
if (class(x) %in% c("numeric", "integer")) { | |
# if numeric descretize it | |
probs <- unique(quantile(x, probs = seq(0.1, 1, 0.1), na.rm = T)) | |
x <- cut(x, breaks = probs, include.lowest = T) | |
} | |
prob_table <- table(as.factor(y), x) | |
vals <- unique(y) | |
neg <- vals[which(vals != pos)] | |
outcome_prob <- sum(y == pos, na.rm = T) / length(y) #outcome probability | |
cond_prob <- | |
(prob_table[pos,] + 0.001 * outcome_prob) / # 0.001 is laplace smoothing | |
(colSums(prob_table) + 0.001) # probability of outcome given variable | |
cond_prob_model <- cond_prob[x] | |
cond_prob_model[is.na(cond_prob_model)] <- outcome_prob # replace NA values with the most common one | |
cond_prob_model | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment