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 2016-10-01 1488
#> 2 2020-02-01 1363
#> 3 2014-03-01 1749
#> 4 2020-05-01 716
#> 5 2019-09-01 1364
#> 6 2020-10-01 1164
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> 2016-10-01, 2020-02-01, 2014-03-01, 2020-05-01, 20…
#> $ value <int> 1488, 1363, 1749, 716, 1364, 1164, 1917, 1628, 1757…
#> $ date_col_index.num <dbl> 1475280000, 1580515200, 1393632000, 1588291200, 156…
#> $ date_col_year <int> 2016, 2020, 2014, 2020, 2019, 2020, 2013, 2015, 201…
#> $ date_col_half <int> 2, 1, 1, 1, 2, 2, 2, 1, 2, 1, 2, 2, 2, 1, 2, 2, 1, …
#> $ date_col_quarter <int> 4, 1, 1, 2, 3, 4, 3, 1, 4, 1, 3, 3, 3, 2, 4, 4, 1, …
#> $ date_col_month <int> 10, 2, 3, 5, 9, 10, 7, 3, 12, 2, 7, 9, 8, 5, 11, 10…
#> $ date_col_month.lbl <ord> October, February, March, May, September, October, …
#> $ 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> 7, 7, 7, 6, 1, 5, 2, 1, 2, 5, 2, 1, 6, 4, 6, 1, 3, …
#> $ date_col_wday.lbl <ord> Saturday, Saturday, Saturday, Friday, Sunday, Thurs…
#> $ 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, 31, 63, 1, 1, 60, 62, 32, 1, 63, 32, 31,…
#> $ date_col_yday <int> 275, 32, 60, 122, 244, 275, 182, 60, 335, 32, 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> 40, 5, 9, 18, 35, 40, 26, 9, 48, 5, 26, 35, 31, 18,…
#> $ date_col_week2 <int> 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, …
#> $ date_col_week3 <int> 1, 2, 0, 0, 2, 1, 2, 0, 0, 2, 2, 2, 1, 0, 2, 1, 1, …
#> $ date_col_week4 <int> 0, 1, 1, 2, 3, 0, 2, 1, 0, 1, 2, 3, 3, 2, 0, 0, 1, …
#> $ 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.0152 PC1 pca_fjcNR
#> 2 date_col_year -0.0409 PC1 pca_fjcNR
#> 3 date_col_half 0.389 PC1 pca_fjcNR
#> 4 date_col_quarter 0.433 PC1 pca_fjcNR
#> 5 date_col_month 0.437 PC1 pca_fjcNR
#> 6 date_col_wday -0.0488 PC1 pca_fjcNR
#> 7 date_col_qday 0.0563 PC1 pca_fjcNR
#> 8 date_col_yday 0.437 PC1 pca_fjcNR
#> 9 date_col_week 0.437 PC1 pca_fjcNR
#> 10 date_col_week2 -0.268 PC1 pca_fjcNR
#> # ℹ 134 more rows
pca_list$variable_variance
#> # A tibble: 48 × 4
#> terms value component id
#> <chr> <dbl> <int> <chr>
#> 1 variance 5.16 1 pca_fjcNR
#> 2 variance 2.01 2 pca_fjcNR
#> 3 variance 1.31 3 pca_fjcNR
#> 4 variance 1.29 4 pca_fjcNR
#> 5 variance 0.951 5 pca_fjcNR
#> 6 variance 0.660 6 pca_fjcNR
#> 7 variance 0.557 7 pca_fjcNR
#> 8 variance 0.0591 8 pca_fjcNR
#> 9 variance 0.000225 9 pca_fjcNR
#> 10 variance 0.0000104 10 pca_fjcNR
#> # ℹ 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> 2016-10-01, 2020-02-01, 2014-03-01, 2020-05-01, 20…
#> $ value <dbl> -0.22533933, -0.68640408, 0.73736386, -3.07287521, …
#> $ date_col_month.lbl <ord> October, February, March, May, September, October, …
#> $ date_col_wday.lbl <ord> Saturday, Saturday, Saturday, Friday, Sunday, Thurs…
#> $ PC1 <dbl> 2.6621895, -2.8304824, -2.4412299, -0.9449244, 1.39…
#> $ PC2 <dbl> 0.10978969, 2.10365443, -1.75504749, 2.06635819, 1.…
#> $ PC3 <dbl> 0.1632945, -0.5329522, 1.4707696, 1.3883015, -1.535…
#> $ PC4 <dbl> -1.0603028, 0.2536896, 1.0860372, 0.5515197, 1.6807…
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.9656404, 0.6267083, 0.9107242, 1.1652319, 1.76646…
#> $ 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.1477652, -2.7277665, -2.4065922, -1.6424198, -0.…
#> $ PC2 <dbl> -2.1724058, -2.2469556, -2.3860982, -2.0717474, -2.…
#> $ PC3 <dbl> -0.47229209, -0.82153259, 1.41693886, -0.48348508, …
#> $ PC4 <dbl> -1.40707499, -0.26104912, 0.86959630, -1.40890328, …
pca_list$pca_rotation_df %>% glimpse()
#> Rows: 12
#> Columns: 12
#> $ PC1 <dbl> 0.01519448, -0.04086230, 0.38850734, 0.43321042, 0.43709259, -0.0…
#> $ PC2 <dbl> 0.700447001, 0.698488221, 0.038413902, 0.031908539, 0.001497099, …
#> $ PC3 <dbl> 0.04574084, 0.04254560, -0.27705827, -0.07211700, 0.02591212, 0.0…
#> $ PC4 <dbl> 0.06164981, 0.05052830, 0.09404945, -0.06201472, 0.08650448, 0.34…
#> $ PC5 <dbl> 0.034144280, 0.032661768, 0.045883665, -0.033127032, 0.011999589,…
#> $ PC6 <dbl> -0.0595188241, -0.0594186968, 0.2483734032, 0.1377958496, 0.00323…
#> $ PC7 <dbl> 0.025834083, 0.030834716, -0.212557965, 0.008200128, -0.035992663…
#> $ PC8 <dbl> -0.009107749, 0.019807514, 0.808030013, -0.272168869, -0.22379436…
#> $ PC9 <dbl> 0.013317017, -0.011158622, 0.008910888, 0.303709018, 0.370242380,…
#> $ PC10 <dbl> 2.212526e-02, -2.276297e-02, 1.679592e-03, 9.913246e-02, -6.89840…
#> $ PC11 <dbl> 0.0049719052, -0.0047118276, 0.0030923088, 0.7776145246, -0.36915…
#> $ PC12 <dbl> -7.050531e-01, 7.067361e-01, 5.633713e-05, 3.584546e-02, 4.773869…
pca_list$pca_variance_df %>% glimpse()
#> Rows: 12
#> Columns: 6
#> $ PC <chr> "PC1", "PC2", "PC3", "PC4", "PC5", "PC6", "PC7", "PC8"…
#> $ var_explained <dbl> 4.298959e-01, 1.676994e-01, 1.090520e-01, 1.077491e-01…
#> $ var_pct_txt <chr> "42.99%", "16.77%", "10.91%", "10.77%", "7.92%", "5.50…
#> $ cum_var_pct <dbl> 0.4298959, 0.5975953, 0.7066473, 0.8143964, 0.8936404,…
#> $ cum_var_pct_txt <chr> "42.99%", "59.76%", "70.66%", "81.44%", "89.36%", "94.…
#> $ ou_threshold <fct> Under, Under, Under, Over, Over, Over, Over, Over, Ove…