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
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.
Now that we have out libraries we can go ahead and get our data set ready.
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)
#> # A tibble: 6 × 2
#> date_col value
#> <date> <int>
#> 1 2013-01-01 2082
#> 2 2013-02-01 1719
#> 3 2013-03-01 1796
#> 4 2013-04-01 1865
#> 5 2013-05-01 2028
#> 6 2013-06-01 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 <- initial_split(data = data_tbl, prop = 0.8)
splits
#> <Training/Testing/Total>
#> <76/19/95>
head(training(splits))
#> # A tibble: 6 × 2
#> date_col value
#> <date> <int>
#> 1 2015-01-01 1692
#> 2 2017-08-01 1607
#> 3 2018-03-01 1618
#> 4 2020-03-01 1241
#> 5 2014-08-01 1662
#> 6 2020-11-01 313
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
#> Number of variables by role
#> 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 <date> 2015-01-01, 2017-08-01, 2018-03-01, 2020-03-01, 20…
#> $ value <int> 1692, 1607, 1618, 1241, 1662, 313, 1844, 1813, 716,…
#> $ date_col_index.num <dbl> 1420070400, 1501545600, 1519862400, 1583020800, 140…
#> $ date_col_year <int> 2015, 2017, 2018, 2020, 2014, 2020, 2014, 2013, 202…
#> $ date_col_half <int> 1, 2, 1, 1, 2, 2, 1, 1, 1, 1, 1, 2, 2, 2, 1, 2, 2, …
#> $ date_col_quarter <int> 1, 3, 1, 1, 3, 4, 1, 2, 2, 1, 2, 3, 4, 4, 2, 3, 3, …
#> $ date_col_month <int> 1, 8, 3, 3, 8, 11, 1, 6, 5, 1, 4, 7, 11, 11, 4, 8, …
#> $ date_col_month.lbl <ord> January, August, March, March, August, November, Ja…
#> $ 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> 5, 3, 5, 1, 6, 1, 4, 7, 6, 1, 3, 4, 6, 7, 2, 7, 3, …
#> $ date_col_wday.lbl <ord> Thursday, Tuesday, Thursday, Sunday, Friday, Sunday…
#> $ 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, 32, 60, 61, 32, 32, 1, 62, 31, 1, 1, 1, 32, 32, …
#> $ date_col_yday <int> 1, 213, 60, 61, 213, 306, 1, 152, 122, 1, 91, 182, …
#> $ date_col_mweek <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
#> $ date_col_week <int> 1, 31, 9, 9, 31, 44, 1, 22, 18, 1, 13, 26, 44, 44, …
#> $ date_col_week2 <int> 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, …
#> $ date_col_week3 <int> 1, 1, 0, 0, 1, 2, 1, 1, 0, 1, 1, 2, 2, 2, 1, 1, 2, …
#> $ date_col_week4 <int> 1, 3, 1, 1, 3, 0, 1, 2, 2, 1, 1, 2, 0, 0, 1, 3, 2, …
#> $ 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
)
#> Warning: ! The following columns have zero variance so scaling cannot be used:
#> date_col_day, date_col_mday, date_col_mweek, and date_col_mday7.
#> ℹ Consider using ?step_zv (`?recipes::step_zv()`) to remove those columns
#> before normalizing.
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:
Lets start going down the list of items.
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
#> Number of variables by role
#> 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()
pca_list$variable_loadings
#> # A tibble: 144 × 4
#> terms value component id
#> <chr> <dbl> <chr> <chr>
#> 1 date_col_index.num 0.128 PC1 pca_T5Vgs
#> 2 date_col_year 0.0758 PC1 pca_T5Vgs
#> 3 date_col_half 0.386 PC1 pca_T5Vgs
#> 4 date_col_quarter 0.431 PC1 pca_T5Vgs
#> 5 date_col_month 0.433 PC1 pca_T5Vgs
#> 6 date_col_wday -0.0323 PC1 pca_T5Vgs
#> 7 date_col_qday 0.0466 PC1 pca_T5Vgs
#> 8 date_col_yday 0.433 PC1 pca_T5Vgs
#> 9 date_col_week 0.433 PC1 pca_T5Vgs
#> 10 date_col_week2 -0.255 PC1 pca_T5Vgs
#> # ℹ 134 more rows
pca_list$variable_variance
#> # A tibble: 48 × 4
#> terms value component id
#> <chr> <dbl> <int> <chr>
#> 1 variance 5.21 1 pca_T5Vgs
#> 2 variance 2.00 2 pca_T5Vgs
#> 3 variance 1.44 3 pca_T5Vgs
#> 4 variance 1.35 4 pca_T5Vgs
#> 5 variance 0.945 5 pca_T5Vgs
#> 6 variance 0.551 6 pca_T5Vgs
#> 7 variance 0.460 7 pca_T5Vgs
#> 8 variance 0.0507 8 pca_T5Vgs
#> 9 variance 0.000201 9 pca_T5Vgs
#> 10 variance 0.0000103 10 pca_T5Vgs
#> # ℹ 38 more rows
pca_list$pca_estimates
#>
#> ── Recipe ──────────────────────────────────────────────────────────────────────
#>
#> ── Inputs
#> Number of variables by role
#> outcome: 1
#> predictor: 1
#>
#> ── Training information
#> Training data contained 76 data points and no incomplete rows.
#>
#> ── Operations
#> • Timeseries signature features from: date_col | Trained
#> • Variables removed: date_col_year.iso and date_col_month.xts, ... | Trained
#> • Centering for: value, date_col_index.num, date_col_year, ... | Trained
#> • Scaling for: value, date_col_index.num, date_col_year, ... | Trained
#> • Sparse, unbalanced variable filter removed: date_col_day, ... | Trained
#> • PCA extraction with: date_col_index.num and date_col_year, ... | Trained
pca_list$pca_juiced_estimates %>% glimpse()
#> Rows: 76
#> Columns: 8
#> $ date_col <date> 2015-01-01, 2017-08-01, 2018-03-01, 2020-03-01, 20…
#> $ value <dbl> 0.49376798, 0.19009879, 0.22939716, -1.11746502, 0.…
#> $ date_col_month.lbl <ord> January, August, March, March, August, November, Ja…
#> $ date_col_wday.lbl <ord> Thursday, Tuesday, Thursday, Sunday, Friday, Sunday…
#> $ PC1 <dbl> -3.6227024, 0.6229036, -2.5724684, -2.3236551, 0.30…
#> $ PC2 <dbl> 0.7761488, -0.7978221, -1.2697926, -2.5871143, 1.06…
#> $ PC3 <dbl> -0.1265658, 1.3967737, -1.1315692, -1.6935348, 1.97…
#> $ PC4 <dbl> 0.8835260, -0.3152795, -1.3723383, -0.6425643, -1.0…
pca_list$pca_baked_data %>% glimpse()
#> Rows: 95
#> Columns: 8
#> $ date_col <date> 2013-01-01, 2013-02-01, 2013-03-01, 2013-04-01, 20…
#> $ value <dbl> 1.8870737, 0.5902276, 0.8653162, 1.1118241, 1.69415…
#> $ 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.7680216, -3.3299097, -3.0333142, -2.2679560, -1.…
#> $ PC2 <dbl> 1.8716202, 1.7795317, 1.6852072, 1.9478439, 1.81346…
#> $ PC3 <dbl> -0.09385205, 0.96137590, -0.51991004, -0.07568720, …
#> $ PC4 <dbl> 0.9894993, 0.1599215, -1.9738512, 0.9456281, -0.799…
pca_list$pca_rotation_df %>% glimpse()
#> Rows: 12
#> Columns: 12
#> $ PC1 <dbl> 0.12844119, 0.07579586, 0.38578482, 0.43050549, 0.43282951, -0.03…
#> $ PC2 <dbl> -0.65970353, -0.67777771, 0.02514657, 0.08115234, 0.04382788, 0.0…
#> $ PC3 <dbl> -0.11649978, -0.12326794, 0.28890090, 0.03689042, 0.03455435, 0.1…
#> $ PC4 <dbl> 0.09700162, 0.11232896, 0.06012248, 0.07469386, -0.10746293, -0.2…
#> $ PC5 <dbl> -0.09892536, -0.10460431, 0.05373077, -0.01802983, 0.02979044, -0…
#> $ PC6 <dbl> -0.071166841, -0.071492253, -0.330788415, -0.075921480, -0.007051…
#> $ PC7 <dbl> 0.037958295, 0.037054252, -0.081065302, -0.103997291, 0.006912891…
#> $ PC8 <dbl> -0.025098341, 0.001891590, 0.802819328, -0.268874791, -0.21164690…
#> $ PC9 <dbl> -0.013503673, 0.011087223, 0.007037015, -0.302535238, -0.36467427…
#> $ PC10 <dbl> -0.0248788347, 0.0250095134, -0.0021199363, -0.0392393598, 0.6707…
#> $ PC11 <dbl> 0.0053043510, -0.0045361044, 0.0026097447, 0.7859682297, -0.41146…
#> $ PC12 <dbl> 7.124335e-01, -6.991858e-01, -1.506388e-04, -3.358056e-02, -1.983…
pca_list$pca_variance_df %>% glimpse()
#> Rows: 12
#> Columns: 6
#> $ PC <chr> "PC1", "PC2", "PC3", "PC4", "PC5", "PC6", "PC7", "PC8"…
#> $ var_explained <dbl> 4.339957e-01, 1.664199e-01, 1.198460e-01, 1.125080e-01…
#> $ var_pct_txt <chr> "43.40%", "16.64%", "11.98%", "11.25%", "7.87%", "4.59…
#> $ cum_var_pct <dbl> 0.4339957, 0.6004156, 0.7202616, 0.8327696, 0.9115129,…
#> $ cum_var_pct_txt <chr> "43.40%", "60.04%", "72.03%", "83.28%", "91.15%", "95.…
#> $ ou_threshold <fct> Under, Under, Under, Over, Over, Over, Over, Over, Ove…