Getting Started with healthyR.ai

A Quick Introduction

Steven P. Sanderson II, MPH

2022-04-29

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

suppressPackageStartupMessages(library(timetk))
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(purrr))
suppressPackageStartupMessages(library(healthyR.data))
suppressPackageStartupMessages(library(rsample))
suppressPackageStartupMessages(library(recipes))
suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(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"
    )

head(data_tbl)
#> # A tibble: 6 x 2
#>   date_col            value
#>   <dttm>              <int>
#> 1 2013-01-01 00:00:00  2082
#> 2 2013-02-01 00:00:00  1719
#> 3 2013-03-01 00:00:00  1796
#> 4 2013-04-01 00:00:00  1865
#> 5 2013-05-01 00:00:00  2028
#> 6 2013-06-01 00:00:00  1813

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
#> <Analysis/Assess/Total>
#> <76/19/95>

head(training(splits))
#> # A tibble: 6 x 2
#>   date_col            value
#>   <dttm>              <int>
#> 1 2019-10-01 00:00:00  1525
#> 2 2014-09-01 00:00:00  1749
#> 3 2015-12-01 00:00:00  1571
#> 4 2014-11-01 00:00:00  1464
#> 5 2014-06-01 00:00:00  1753
#> 6 2017-05-01 00:00:00  1647

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
#> Recipe
#> 
#> Inputs:
#> 
#>       role #variables
#>    outcome          1
#>  predictor          1
#> 
#> Operations:
#> 
#> Timeseries signature features from date_col
#> Variables removed matches("(iso$)|(xts$)|(hour)|(min)|(sec)|(am.pm)")

get_juiced_data(rec_obj) %>% glimpse()
#> Rows: 76
#> Columns: 20
#> $ date_col           <dttm> 2019-10-01, 2014-09-01, 2015-12-01, 2014-11-01, 20~
#> $ value              <int> 1525, 1749, 1571, 1464, 1753, 1647, 1164, 1512, 130~
#> $ date_col_index.num <dbl> 1569888000, 1409529600, 1448928000, 1414800000, 140~
#> $ date_col_year      <int> 2019, 2014, 2015, 2014, 2014, 2017, 2020, 2017, 201~
#> $ date_col_half      <int> 2, 2, 2, 2, 1, 1, 2, 2, 2, 1, 2, 1, 1, 2, 2, 1, 2, ~
#> $ date_col_quarter   <int> 4, 3, 4, 4, 2, 2, 4, 3, 4, 1, 3, 1, 1, 3, 4, 1, 4, ~
#> $ date_col_month     <int> 10, 9, 12, 11, 6, 5, 10, 9, 11, 3, 7, 3, 1, 8, 11, ~
#> $ date_col_month.lbl <ord> October, September, December, November, June, May, ~
#> $ date_col_day       <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ~
#> $ date_col_wday      <int> 3, 2, 3, 7, 1, 2, 5, 6, 6, 6, 4, 5, 2, 5, 5, 3, 5, ~
#> $ date_col_wday.lbl  <ord> Tuesday, Monday, Tuesday, Saturday, Sunday, Monday,~
#> $ date_col_mday      <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ~
#> $ date_col_qday      <int> 1, 63, 62, 32, 62, 31, 1, 63, 32, 60, 1, 60, 1, 32,~
#> $ date_col_yday      <int> 274, 244, 335, 305, 152, 121, 275, 244, 305, 60, 18~
#> $ date_col_mweek     <int> 5, 6, 5, 5, 5, 6, 5, 5, 5, 5, 5, 5, 6, 5, 5, 5, 5, ~
#> $ date_col_week      <int> 40, 35, 48, 44, 22, 18, 40, 35, 44, 9, 26, 9, 1, 31~
#> $ date_col_week2     <int> 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, ~
#> $ date_col_week3     <int> 1, 2, 0, 2, 1, 0, 1, 2, 2, 0, 2, 0, 1, 1, 2, 0, 1, ~
#> $ date_col_week4     <int> 0, 3, 0, 0, 2, 2, 0, 3, 0, 1, 2, 1, 1, 3, 0, 1, 0, ~
#> $ date_col_mday7     <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ~

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
#> Recipe
#> 
#> Inputs:
#> 
#>       role #variables
#>    outcome          1
#>  predictor          1
#> 
#> Operations:
#> 
#> Timeseries signature features from date_col
#> Variables removed matches("(iso$)|(xts$)|(hour)|(min)|(sec)|(am.pm)")
#> Centering for recipes::all_numeric()
#> Scaling for recipes::all_numeric()
#> Sparse, unbalanced variable filter on recipes::all_numeric()
#> PCA extraction with recipes::all_numeric_predictors()

Variable Loadings

pca_list$variable_loadings
#> # A tibble: 169 x 4
#>    terms                  value component id       
#>    <chr>                  <dbl> <chr>     <chr>    
#>  1 date_col_index.num -0.146    PC1       pca_FtZl1
#>  2 date_col_year      -0.0931   PC1       pca_FtZl1
#>  3 date_col_half      -0.385    PC1       pca_FtZl1
#>  4 date_col_quarter   -0.425    PC1       pca_FtZl1
#>  5 date_col_month     -0.427    PC1       pca_FtZl1
#>  6 date_col_wday      -0.00548  PC1       pca_FtZl1
#>  7 date_col_qday      -0.0712   PC1       pca_FtZl1
#>  8 date_col_yday      -0.428    PC1       pca_FtZl1
#>  9 date_col_mweek     -0.000472 PC1       pca_FtZl1
#> 10 date_col_week      -0.428    PC1       pca_FtZl1
#> # ... with 159 more rows

Variable Variance

pca_list$variable_variance
#> # A tibble: 52 x 4
#>    terms       value component id       
#>    <chr>       <dbl>     <int> <chr>    
#>  1 variance 5.32             1 pca_FtZl1
#>  2 variance 1.95             2 pca_FtZl1
#>  3 variance 1.51             3 pca_FtZl1
#>  4 variance 1.24             4 pca_FtZl1
#>  5 variance 1.12             5 pca_FtZl1
#>  6 variance 0.655            6 pca_FtZl1
#>  7 variance 0.622            7 pca_FtZl1
#>  8 variance 0.515            8 pca_FtZl1
#>  9 variance 0.0690           9 pca_FtZl1
#> 10 variance 0.000268        10 pca_FtZl1
#> # ... with 42 more rows

PCA Estimates

pca_list$pca_estimates
#> Recipe
#> 
#> Inputs:
#> 
#>       role #variables
#>    outcome          1
#>  predictor          1
#> 
#> Training data contained 76 data points and no missing data.
#> 
#> Operations:
#> 
#> Timeseries signature features from date_col [trained]
#> Variables removed date_col_year.iso, date_col_month.xts, date_col_hour, d... [trained]
#> Centering for value, date_col_index.num, date_col_year, date_... [trained]
#> Scaling for value, date_col_index.num, date_col_year, date_... [trained]
#> Sparse, unbalanced variable filter removed date_col_day, date_col_mday, date_col_m... [trained]
#> PCA extraction with date_col_index.num, date_col_year, date_col_half... [trained]

Jucied and Baked Data

pca_list$pca_juiced_estimates %>% glimpse()
#> Rows: 76
#> Columns: 9
#> $ date_col           <dttm> 2019-10-01, 2014-09-01, 2015-12-01, 2014-11-01, 20~
#> $ value              <dbl> -0.170764178, 0.687290535, 0.005443486, -0.40443086~
#> $ date_col_month.lbl <ord> October, September, December, November, June, May, ~
#> $ date_col_wday.lbl  <ord> Tuesday, Monday, Tuesday, Saturday, Sunday, Monday,~
#> $ PC1                <dbl> -2.8442322, -0.9427841, -3.2377210, -2.8614458, 0.7~
#> $ PC2                <dbl> -1.58324404, 1.27159910, 1.55469486, 1.83092976, 1.~
#> $ PC3                <dbl> 0.64057382, -2.98892432, 1.01819978, 0.60152577, -0~
#> $ PC4                <dbl> -1.27262355, 1.83316259, 0.34402452, -1.10619204, 0~
#> $ PC5                <dbl> 0.4102121, -0.2180284, 1.5305877, -1.4050722, 1.175~

pca_list$pca_baked_data %>% glimpse()
#> Rows: 95
#> Columns: 9
#> $ date_col           <dttm> 2013-01-01, 2013-02-01, 2013-03-01, 2013-04-01, 20~
#> $ value              <dbl> 1.9628808, 0.5723725, 0.8673288, 1.1316403, 1.75602~
#> $ date_col_month.lbl <ord> January, February, March, April, May, June, July, A~
#> $ date_col_wday.lbl  <ord> Tuesday, Friday, Friday, Monday, Wednesday, Saturda~
#> $ PC1                <dbl> 3.6796711, 3.1368607, 2.8694724, 2.2188521, 1.40628~
#> $ PC2                <dbl> 1.0923760, 1.6435200, 2.1807299, 1.1999521, 2.06175~
#> $ PC3                <dbl> -1.64682300, 0.10154612, 1.30228284, -1.93603800, 0~
#> $ PC4                <dbl> -1.41307634, -0.51801196, 0.70951190, -1.27162253, ~
#> $ PC5                <dbl> 0.577868238, -1.523171561, 0.216532168, 0.905110558~

Roatation Data

pca_list$pca_rotation_df %>% glimpse()
#> Rows: 13
#> Columns: 13
#> $ PC1  <dbl> -0.1456871869, -0.0930609962, -0.3845155302, -0.4254393718, -0.42~
#> $ PC2  <dbl> -0.653395430, -0.679050406, 0.001363947, 0.030978883, 0.085375059~
#> $ PC3  <dbl> 0.144685244, 0.149422209, -0.185728363, -0.054036956, -0.01072106~
#> $ PC4  <dbl> 0.11754817, 0.10990998, 0.05945078, -0.09052308, 0.07884682, -0.0~
#> $ PC5  <dbl> 0.03055128, 0.02853956, -0.21000614, -0.02199578, 0.02299931, -0.~
#> $ PC6  <dbl> -0.01625390, -0.01348396, -0.08121371, -0.07561114, -0.02523135, ~
#> $ PC7  <dbl> 0.029918952, 0.026424425, 0.007372112, -0.059626033, 0.027140995,~
#> $ PC8  <dbl> -0.07031081, -0.07598782, 0.33587929, 0.12677723, 0.03217332, -0.~
#> $ PC9  <dbl> 0.008354176, -0.020536701, -0.806549506, 0.278820962, 0.221833858~
#> $ PC10 <dbl> 0.013565362, -0.011243532, 0.004040460, 0.305668628, 0.372975942,~
#> $ PC11 <dbl> 0.0266125180, -0.0266955088, 0.0010741225, 0.0639718463, -0.66965~
#> $ PC12 <dbl> 0.0069559723, -0.0064131636, 0.0030688432, 0.7774501034, -0.40494~
#> $ PC13 <dbl> 7.134785e-01, -6.978913e-01, -1.672857e-04, -3.745852e-02, -1.992~

Variance and Scree Plot

pca_list$pca_variance_df %>% glimpse()
#> Rows: 13
#> Columns: 6
#> $ PC              <chr> "PC1", "PC2", "PC3", "PC4", "PC5", "PC6", "PC7", "PC8"~
#> $ var_explained   <dbl> 4.094482e-01, 1.502657e-01, 1.162798e-01, 9.507939e-02~
#> $ var_pct_txt     <chr> "40.94%", "15.03%", "11.63%", "9.51%", "8.58%", "5.03%~
#> $ cum_var_pct     <dbl> 0.4094482, 0.5597139, 0.6759937, 0.7710731, 0.8568627,~
#> $ cum_var_pct_txt <chr> "40.94%", "55.97%", "67.60%", "77.11%", "85.69%", "90.~
#> $ ou_threshold    <fct> Under, Under, Under, Under, Over, Over, Over, Over, Ov~
pca_list$pca_variance_scree_plt

Variable Loading Plots

pca_list$pca_loadings_plt


pca_list$pca_top_n_loadings_plt