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 2019-07-01 1474
#> 2 2013-06-01 1813
#> 3 2016-05-01 1587
#> 4 2017-02-01 1449
#> 5 2014-12-01 1757
#> 6 2015-12-01 1571
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> 2019-07-01, 2013-06-01, 2016-05-01, 2017-02-01, 20…
#> $ value <int> 1474, 1813, 1587, 1449, 1757, 1571, 1486, 1782, 153…
#> $ date_col_index.num <dbl> 1561939200, 1370044800, 1462060800, 1485907200, 141…
#> $ date_col_year <int> 2019, 2013, 2016, 2017, 2014, 2015, 2019, 2014, 201…
#> $ date_col_half <int> 2, 1, 1, 1, 2, 2, 1, 2, 2, 1, 2, 2, 2, 1, 2, 1, 2, …
#> $ date_col_quarter <int> 3, 2, 2, 1, 4, 4, 2, 3, 4, 2, 3, 4, 4, 1, 4, 2, 4, …
#> $ date_col_month <int> 7, 6, 5, 2, 12, 12, 5, 7, 12, 4, 8, 11, 10, 3, 11, …
#> $ date_col_month.lbl <ord> July, June, May, February, December, December, 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> 2, 7, 1, 4, 2, 3, 4, 3, 6, 4, 5, 1, 4, 3, 3, 2, 3, …
#> $ date_col_wday.lbl <ord> Monday, Saturday, Sunday, Wednesday, Monday, Tuesda…
#> $ 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, 62, 31, 32, 62, 62, 31, 1, 62, 1, 32, 32, 1, 61,…
#> $ date_col_yday <int> 182, 152, 122, 32, 335, 335, 121, 182, 335, 92, 213…
#> $ date_col_mweek <int> 6, 5, 5, 5, 6, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 5, …
#> $ date_col_week <int> 26, 22, 18, 5, 48, 48, 18, 26, 48, 14, 31, 44, 40, …
#> $ date_col_week2 <int> 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, …
#> $ date_col_week3 <int> 2, 1, 0, 2, 0, 0, 0, 2, 0, 2, 1, 2, 1, 0, 2, 1, 1, …
#> $ date_col_week4 <int> 2, 2, 2, 1, 0, 0, 2, 2, 0, 2, 3, 0, 0, 1, 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
)
#> Warning: ! The following columns have zero variance so scaling cannot be used:
#> date_col_day, date_col_mday, 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: 169 × 4
#> terms value component id
#> <chr> <dbl> <chr> <chr>
#> 1 date_col_index.num -0.0281 PC1 pca_kc5us
#> 2 date_col_year 0.0271 PC1 pca_kc5us
#> 3 date_col_half -0.392 PC1 pca_kc5us
#> 4 date_col_quarter -0.435 PC1 pca_kc5us
#> 5 date_col_month -0.438 PC1 pca_kc5us
#> 6 date_col_wday 0.0284 PC1 pca_kc5us
#> 7 date_col_qday -0.0532 PC1 pca_kc5us
#> 8 date_col_yday -0.439 PC1 pca_kc5us
#> 9 date_col_mweek 0.0207 PC1 pca_kc5us
#> 10 date_col_week -0.439 PC1 pca_kc5us
#> # ℹ 159 more rows
pca_list$variable_variance
#> # A tibble: 52 × 4
#> terms value component id
#> <chr> <dbl> <int> <chr>
#> 1 variance 5.13 1 pca_kc5us
#> 2 variance 2.03 2 pca_kc5us
#> 3 variance 1.54 3 pca_kc5us
#> 4 variance 1.47 4 pca_kc5us
#> 5 variance 1.05 5 pca_kc5us
#> 6 variance 0.674 6 pca_kc5us
#> 7 variance 0.574 7 pca_kc5us
#> 8 variance 0.487 8 pca_kc5us
#> 9 variance 0.0568 9 pca_kc5us
#> 10 variance 0.000229 10 pca_kc5us
#> # ℹ 42 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: 9
#> $ date_col <date> 2019-07-01, 2013-06-01, 2016-05-01, 2017-02-01, 20…
#> $ value <dbl> -0.172795334, 1.040256159, 0.231555164, -0.26225340…
#> $ date_col_month.lbl <ord> July, June, May, February, December, December, May,…
#> $ date_col_wday.lbl <ord> Monday, Saturday, Sunday, Wednesday, Monday, Tuesda…
#> $ PC1 <dbl> -0.9352009, 0.5550759, 0.9623746, 2.8585756, -3.412…
#> $ PC2 <dbl> 1.28412936, -2.11491139, -0.64577786, 0.18450604, -…
#> $ PC3 <dbl> 2.44752233, -2.01287143, 0.21326133, 0.03118474, 0.…
#> $ PC4 <dbl> 1.0053936, -0.4832961, -0.9500908, 0.1161267, -0.50…
#> $ PC5 <dbl> 0.16759026, -0.04055285, -1.34298519, 0.90531800, -…
pca_list$pca_baked_data %>% glimpse()
#> Rows: 95
#> Columns: 9
#> $ date_col <date> 2013-01-01, 2013-02-01, 2013-03-01, 2013-04-01, 20…
#> $ value <dbl> 2.0028250, 0.7038938, 0.9794247, 1.2263290, 1.80959…
#> $ 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.3915545, 2.8884096, 2.5781167, 1.8758637, 1.00990…
#> $ PC2 <dbl> -2.627292, -2.140430, -2.393728, -2.605670, -2.3088…
#> $ PC3 <dbl> 1.59995295, -0.78322532, -1.97530260, 1.80348274, -…
#> $ PC4 <dbl> -0.01755145, -0.05588605, -0.80470662, 0.13002740, …
#> $ PC5 <dbl> 0.27993391, 1.64901714, -0.54325638, 0.04775979, -0…
pca_list$pca_rotation_df %>% glimpse()
#> Rows: 13
#> Columns: 13
#> $ PC1 <dbl> -0.02813935, 0.02708072, -0.39228225, -0.43505104, -0.43847874, 0…
#> $ PC2 <dbl> 0.692388085, 0.692725314, 0.005735973, 0.012641973, -0.005336008,…
#> $ PC3 <dbl> 0.060132938, 0.067393876, 0.006956505, 0.053971475, -0.058604045,…
#> $ PC4 <dbl> 0.01379547, 0.00860295, 0.22637907, -0.02140041, 0.04011647, -0.1…
#> $ PC5 <dbl> -0.12348978, -0.11674281, 0.18395935, 0.08525657, -0.05515800, 0.…
#> $ PC6 <dbl> -0.0012501727, -0.0011720550, -0.2603258865, -0.1338404107, -0.00…
#> $ PC7 <dbl> -0.013390470, -0.012295654, -0.116583285, 0.030203360, -0.0072607…
#> $ PC8 <dbl> -0.04144025, -0.03569565, -0.13917870, -0.01230711, -0.04219994, …
#> $ PC9 <dbl> 0.004382562, -0.023455625, -0.812463574, 0.271220498, 0.221454058…
#> $ PC10 <dbl> 0.012997467, -0.011228249, 0.010297147, 0.303931126, 0.373458496,…
#> $ PC11 <dbl> -0.0249358937, 0.0257301370, -0.0012088467, 0.0151358185, 0.62540…
#> $ PC12 <dbl> 3.300013e-03, -3.012807e-03, 2.881177e-03, 7.835189e-01, -4.67024…
#> $ PC13 <dbl> 7.057138e-01, -7.059599e-01, -1.435126e-04, -2.980981e-02, -4.284…
pca_list$pca_variance_df %>% glimpse()
#> Rows: 13
#> Columns: 6
#> $ PC <chr> "PC1", "PC2", "PC3", "PC4", "PC5", "PC6", "PC7", "PC8"…
#> $ var_explained <dbl> 3.942454e-01, 1.561552e-01, 1.181296e-01, 1.130391e-01…
#> $ var_pct_txt <chr> "39.42%", "15.62%", "11.81%", "11.30%", "8.06%", "5.18…
#> $ cum_var_pct <dbl> 0.3942454, 0.5504006, 0.6685302, 0.7815693, 0.8621516,…
#> $ cum_var_pct_txt <chr> "39.42%", "55.04%", "66.85%", "78.16%", "86.22%", "91.…
#> $ ou_threshold <fct> Under, Under, Under, Under, Over, Over, Over, Over, Ov…