Skip to content

Commit

Permalink
Added functions to merge factor vairables. Tested funModeling package.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Cierocki committed Apr 17, 2020
1 parent 52ba08d commit 5e880d2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
12 changes: 12 additions & 0 deletions funs.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,16 @@ plot_freq <- function(df, fac_var, target = "Exited") {
geom_line(aes(x = 1:nrow(count_frame), y = ExitedFreq * multi), color = "red", lwd = 2) +
scale_y_continuous(sec.axis = sec_axis(trans = ~ . / multi)) +
xlab("Country")
}

merge_factor_vars <- function(var1, ...) {
UseMethod("merge_factor_vars", var1)
}

merge_factor_vars.factor <- function(var1, ...) {
factor(str_c(var1, ...))
}

merge_factor_vars.tbl <- function(var1, ...) {
do.call(function(...) factor(str_c(...)), as.list(var1))
}
16 changes: 15 additions & 1 deletion work1.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ library(stringr)
library(ggplot2)
library(GGally)
library(scorecard)
library(funModeling)
# library(caret)
# library(ranger)

Expand Down Expand Up @@ -40,4 +41,17 @@ iv(data1, "Exited", "NotSpain", positive = "No")

opt_bin <- woebin(data1, "Exited", "Age", positive = "No")#[[1]]$breaks

data2 <- data1 %>% woebin_ply(opt_bin, to = "bin") %>% mutate(Age_bin = as.factor(Age_bin))
data2 <- data1 %>% mutate(Age_bin2 = discretize_rgr(Age, Exited, max_n_bins = 6))
data2 <- data2 %>% woebin_ply(opt_bin, to = "bin") %>% mutate(Age_bin = as.factor(Age_bin))

# opt_bin2 <- discretize_get_bins(data1, 4, input = "Age")

head(data2$Age_bin2)
plot(data2$Age_bin)
plot(data2$Age_bin2)

iv(data2, y = "Exited", positive = "No")

#### łączenie zmiennych w celu uzyskania zbliżonej liczby kategorii (nieobciążone oceny ważności parametrów i IV)

# dodałem funkcję w pliku funs.R
4 changes: 4 additions & 0 deletions work2.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
##### work file 2 - tidymodels + ranger

library(tidyverse)
library(stringr)

0 comments on commit 5e880d2

Please sign in to comment.