Getting Started with healthyR.ai

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

First of all, thank you for using healthyR.ai. If you encounter issues or want to make a feature request, please visit https://github.com/spsanderson/healthyR.ai/issues

library(healthyR.ai)

In this should example we will showcase the pca_your_recipe() function. This function takes only a few arguments. The arguments are currently .data which is the full data set that gets passed internally to the recipes::bake() function, .recipe_object which is a recipe you have already made and want to pass to the function in order to perform the pca, and finally .threshold which is the fraction of the variance that should be captured by the components.

To start this walk through we will first load in a few libraries.

Libraries

library(timetk)
library(dplyr)
library(purrr)
library(healthyR.data)
library(rsample)
library(recipes)
library(ggplot2)
library(plotly)

Data

Now that we have out libraries we can go ahead and get our data set ready.

Data Set

data_tbl <- healthyR_data %>%
    select(visit_end_date_time) %>%
    summarise_by_time(
        .date_var = visit_end_date_time,
        .by       = "month",
        value     = n()
    ) %>%
    set_names("date_col","value") %>%
    filter_by_time(
        .date_var = date_col,
        .start_date = "2013",
        .end_date = "2020"
    ) %>%
    mutate(date_col = as.Date(date_col))

head(data_tbl)

The data set is simple and by itself would not be at all useful for a pca analysis since there is only one predictor, being time. In order to facilitate the use of the function and this example, we will create a splits object and a recipe object.

Splits

splits <- initial_split(data = data_tbl, prop = 0.8)

splits

head(training(splits))

Initial Recipe

rec_obj <- recipe(value ~ ., training(splits)) %>%
    step_timeseries_signature(date_col) %>%
    step_rm(matches("(iso$)|(xts$)|(hour)|(min)|(sec)|(am.pm)"))

rec_obj

get_juiced_data(rec_obj) %>% glimpse()

Now that we have out initial recipe we can use the pca_your_recipe() function.

pca_list <- pca_your_recipe(
  .recipe_object = rec_obj,
  .data          = data_tbl,
  .threshold     = 0.8,
  .top_n         = 5
)

Inspect PCA Output

The function returns a list object and does so insvisible so you must assign the output to a variable, you can then access the items of the list in the usual manner.

The following items are included in the output of the function:

  1. pca_transform - This is the pca recipe.
  2. variable_loadings
  3. variable_variance
  4. pca_estimates
  5. pca_juiced_estimates
  6. pca_baked_data
  7. pca_variance_df
  8. pca_variance_scree_plt
  9. pca_rotation_df

Lets start going down the list of items.

PCA Transform

This is the portion you will want to output to a variable as this is the recipe object itself that you will use further down the line of your work.

pca_rec_obj <- pca_list$pca_transform

pca_rec_obj

Variable Loadings

pca_list$variable_loadings

Variable Variance

pca_list$variable_variance

PCA Estimates

pca_list$pca_estimates

Jucied and Baked Data

pca_list$pca_juiced_estimates %>% glimpse()

pca_list$pca_baked_data %>% glimpse()

Roatation Data

pca_list$pca_rotation_df %>% glimpse()

Variance and Scree Plot

pca_list$pca_variance_df %>% glimpse()
pca_list$pca_variance_scree_plt

Variable Loading Plots

pca_list$pca_loadings_plt

pca_list$pca_top_n_loadings_plt


Try the healthyR.ai package in your browser

Any scripts or data that you put into this service are public.

healthyR.ai documentation built on June 8, 2025, 11:09 a.m.