HELP
library(mdsr)
HELP
HELPr
HLPr
library(tidyverse)
library(mdsr)
url <-
"http://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data"
census <- read_csv(
url,
col_names = c(
"age", "workclass", "fnlwgt", "education",
"education_1", "marital_status", "occupation", "relationship",
"race", "sex", "capital_gain", "capital_loss", "hours_per_week",
"native_country", "income"
)
) %>%
mutate(income = factor(income))
glimpse(census)
library(tidymodels)
set.seed(364)
n <- nrow(census)
census_parts <- census %>%
initial_split(prop = 0.8)
train <- census_parts %>%
training()
test <- census_parts %>%
testing()
list(train, test) %>%
map_int(nrow)
pi_bar <- train %>%
count(income) %>%
mutate(pct = n / sum(n)) %>%
filter(income == ">50K") %>%
pull(pct)
pi_bar
train %>%
count(income) %>%
mutate(pct = n / sum(n))
mod_null <- logistic_reg(mode = "classification") %>%
set_engine("glm") %>%
fit(income ~ 1, data = train)
library(yardstick)
pred <- train %>%
select(income, capital_gain) %>%
bind_cols(
predict(mod_null, new_data = train, type = "class")
) %>%
rename(income_null = .pred_class)
accuracy(pred, income, income_null)
confusion_null <- pred %>%
conf_mat(truth = income, estimate = income_null)
confusion_null
mod_log_1 <- logistic_reg(mode = "classification") %>%
set_engine("glm") %>%
fit(income ~ capital_gain, data = train)
train_plus <- train %>%
mutate(high_earner = as.integer(income == ">50K"))
ggplot(train_plus, aes(x = capital_gain, y = high_earner)) +
geom_count(
position = position_jitter(width = 0, height = 0.05),
alpha = 0.5
) +
geom_smooth(
method = "glm", method.args = list(family = "binomial"),
color = "dodgerblue", lty = 2, se = FALSE
) +
geom_hline(aes(yintercept = 0.5), linetype = 3) +
scale_x_log10(labels = scales::dollar)
pred <- pred %>%
bind_cols(
predict(mod_log_1, new_data = train, type = "class")
) %>%
rename(income_log_1 = .pred_class)
confusion_log_1 <- pred %>%
conf_mat(truth = income, estimate = income_log_1)
confusion_log_1
accuracy(pred, income, income_log_1)
autoplot(confusion_null) +
geom_label(
aes(
x = (xmax + xmin) / 2,
y = (ymax + ymin) / 2,
label = c("TN", "FP", "FN", "TP")
)
)
autoplot(confusion_log_1) +
geom_label(
aes(
x = (xmax + xmin) / 2,
y = (ymax + ymin) / 2,
label = c("TN", "FP", "FN", "TP")
)
)
broom::tidy(mod_log_1)
income_probs <- pred %>%
select(income, income_log_1, capital_gain) %>%
bind_cols(
predict(mod_log_1, new_data = train, type = "prob")
)
income_probs %>%
rename(rich_prob = `.pred_>50K`) %>%
distinct() %>%
filter(abs(rich_prob - 0.5) < 0.02) %>%
arrange(desc(rich_prob))
mod_log_all <- logistic_reg(mode = "classification") %>%
set_engine("glm") %>%
fit(
income ~ age + workclass + education + marital_status +
occupation + relationship + race + sex +
capital_gain + capital_loss + hours_per_week,
data = train
)
pred <- pred %>%
bind_cols(
predict(mod_log_all, new_data = train, type = "class")
) %>%
rename(income_log_all = .pred_class)
pred %>%
conf_mat(truth = income, estimate = income_log_all)
accuracy(pred, income, income_log_all)
