Skip to content

Instantly share code, notes, and snippets.

@topepo
Created April 14, 2020 13:27
Show Gist options
  • Save topepo/7593de184bfe91f241a044befe7182bb to your computer and use it in GitHub Desktop.
Save topepo/7593de184bfe91f241a044befe7182bb to your computer and use it in GitHub Desktop.
library(tidyverse)
library(janitor)
library(tidymodels)
library(plotly)
theme_set(theme_bw())
body <-
read_csv("http://staff.pubhealth.ku.dk/~tag/Teaching/share/data/Bodyfat.csv") %>%
janitor::clean_names() %>%
mutate(subject = format(row_number()))
basic_rec <-
recipe(density + bodyfat ~ ., data = body) %>%
update_role(subject, new_role = "id") %>%
step_normalize(all_predictors())
pca_rec <-
basic_rec %>%
step_pca(all_predictors(), threshold = 0.95)
make_interactive <- function(p) {
girafe(ggobj = p)
}
pca_data <-
pca_rec %>%
prep() %>%
juice()
pca_range <- extendrange(c(pca_data$PC1, pca_data$PC2))
pca_plot <-
ggplot(pca_data, aes(x = PC1, y = PC2)) +
geom_point(aes(text = subject), alpha = .3) +
lims(x = pca_range, y = pca_range)
ggplotly(pca_plot)
ica_rec <-
basic_rec %>%
step_ica(all_predictors(), num_comp = 2)
ica_data <-
ica_rec %>%
prep() %>%
juice()
ica_range <- extendrange(c(ica_data$IC1, ica_data$IC2))
ica_plot <-
ggplot(ica_data, aes(x = IC1, y = IC2)) +
geom_point(aes(text = subject), alpha = .3) +
lims(x = ica_range, y = ica_range)
ggplotly(ica_plot)
pls_rec <-
basic_rec %>%
step_pls(all_predictors(), outcome = "density", num_comp = 2)
pls_data <-
pls_rec %>%
prep() %>%
juice()
pls_range <- extendrange(c(pls_data$IC1, pls_data$IC2))
pls_plot <-
ggplot(pls_data, aes(x = PLS1, y = PLS2)) +
geom_point(aes(text = subject), alpha = .3) +
lims(x = ica_range, y = ica_range)
ggplotly(pls_plot)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment