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)
#> Registered S3 method overwritten by 'tune':
#> method from
#> required_pkgs.model_spec parsnip
#> == Welcome to healthyR.ai ======================================================
#> If you find this package useful, please leave a star: https://github.com/spsanderson/healthyR.ai
#> If you encounter a bug or want to request an enhancement please file an issue at:
#> https://github.com/spsanderson/healthyR.ai/issues
#> Thank you for using 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.
suppressPackageStartupMessages(library(timetk))
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(purrr))
suppressPackageStartupMessages(library(healthyR.data))
suppressPackageStartupMessages(library(rsample))
suppressPackageStartupMessages(library(recipes))
suppressPackageStartupMessages(library(ggplot2))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"
)
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 1813The 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
#> <Analysis/Assess/Total>
#> <76/19/95>
head(training(splits))
#> # A tibble: 6 x 2
#> date_col value
#> <dttm> <int>
#> 1 2014-03-01 00:00:00 1749
#> 2 2013-05-01 00:00:00 2028
#> 3 2018-10-01 00:00:00 1645
#> 4 2015-01-01 00:00:00 1692
#> 5 2017-05-01 00:00:00 1647
#> 6 2015-05-01 00:00:00 1544rec_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
#> Delete terms matches("(iso$)|(xts$)|(hour)|(min)|(sec)|(am.pm)")
get_juiced_data(rec_obj) %>% glimpse()
#> Rows: 76
#> Columns: 20
#> $ date_col <dttm> 2014-03-01, 2013-05-01, 2018-10-01, 2015-01-01, 20~
#> $ value <int> 1749, 2028, 1645, 1692, 1647, 1544, 1537, 1796, 169~
#> $ date_col_index.num <dbl> 1393632000, 1367366400, 1538352000, 1420070400, 149~
#> $ date_col_year <int> 2014, 2013, 2018, 2015, 2017, 2015, 2016, 2013, 201~
#> $ date_col_half <int> 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 2, 2, 1, 2, 1, 2, 2, ~
#> $ date_col_quarter <int> 1, 2, 4, 1, 2, 2, 3, 1, 1, 1, 3, 3, 2, 4, 2, 3, 4, ~
#> $ date_col_month <int> 3, 5, 10, 1, 5, 5, 8, 3, 3, 2, 9, 7, 6, 10, 4, 9, 1~
#> $ date_col_month.lbl <ord> March, May, October, January, May, May, August, Mar~
#> $ 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, 4, 2, 5, 2, 6, 2, 6, 3, 4, 1, 4, 6, 3, 2, 7, 1, ~
#> $ date_col_wday.lbl <ord> Saturday, Wednesday, Monday, Thursday, Monday, Frid~
#> $ 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> 60, 31, 1, 1, 31, 31, 32, 60, 61, 32, 63, 1, 62, 1,~
#> $ date_col_yday <int> 60, 121, 274, 1, 121, 121, 214, 60, 61, 32, 244, 18~
#> $ date_col_mweek <int> 5, 5, 6, 5, 6, 5, 6, 5, 5, 5, 5, 5, 5, 5, 6, 5, 5, ~
#> $ date_col_week <int> 9, 18, 40, 1, 18, 18, 31, 9, 9, 5, 35, 26, 22, 40, ~
#> $ date_col_week2 <int> 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, ~
#> $ date_col_week3 <int> 0, 0, 1, 1, 0, 0, 1, 0, 0, 2, 2, 2, 1, 1, 1, 2, 1, ~
#> $ date_col_week4 <int> 1, 2, 0, 1, 2, 2, 3, 1, 1, 1, 3, 2, 2, 0, 1, 3, 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
)
#> Registered S3 method overwritten by 'quantmod':
#> method from
#> as.zoo.data.frame zooThe 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:
#>
#> role #variables
#> outcome 1
#> predictor 1
#>
#> Operations:
#>
#> Timeseries signature features from date_col
#> Delete terms 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()
#> No PCA components were extracted.pca_list$variable_loadings
#> # A tibble: 169 x 4
#> terms value component id
#> <chr> <dbl> <chr> <chr>
#> 1 date_col_index.num 0.0591 PC1 pca_TBD0j
#> 2 date_col_year 0.00335 PC1 pca_TBD0j
#> 3 date_col_half 0.397 PC1 pca_TBD0j
#> 4 date_col_quarter 0.436 PC1 pca_TBD0j
#> 5 date_col_month 0.439 PC1 pca_TBD0j
#> 6 date_col_wday -0.00783 PC1 pca_TBD0j
#> 7 date_col_qday 0.0385 PC1 pca_TBD0j
#> 8 date_col_yday 0.439 PC1 pca_TBD0j
#> 9 date_col_mweek -0.0131 PC1 pca_TBD0j
#> 10 date_col_week 0.439 PC1 pca_TBD0j
#> # ... with 159 more rowspca_list$variable_variance
#> # A tibble: 52 x 4
#> terms value component id
#> <chr> <dbl> <int> <chr>
#> 1 variance 5.12 1 pca_TBD0j
#> 2 variance 2.03 2 pca_TBD0j
#> 3 variance 1.54 3 pca_TBD0j
#> 4 variance 1.35 4 pca_TBD0j
#> 5 variance 1.26 5 pca_TBD0j
#> 6 variance 0.604 6 pca_TBD0j
#> 7 variance 0.550 7 pca_TBD0j
#> 8 variance 0.514 8 pca_TBD0j
#> 9 variance 0.0417 9 pca_TBD0j
#> 10 variance 0.000200 10 pca_TBD0j
#> # ... with 42 more rowspca_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]pca_list$pca_juiced_estimates %>% glimpse()
#> Rows: 76
#> Columns: 9
#> $ date_col <dttm> 2014-03-01, 2013-05-01, 2018-10-01, 2015-01-01, 20~
#> $ value <dbl> 0.77484309, 1.93325617, 0.34303319, 0.53817805, 0.3~
#> $ date_col_month.lbl <ord> March, May, October, January, May, May, August, Mar~
#> $ date_col_wday.lbl <ord> Saturday, Wednesday, Monday, Thursday, Monday, Frid~
#> $ PC1 <dbl> -2.5344701, -1.0098490, 2.7632063, -3.2536592, -0.9~
#> $ PC2 <dbl> -1.58631231, -2.21638212, 0.60227053, -1.14297202, ~
#> $ PC3 <dbl> -0.53109553, -0.61820073, -0.31622672, -0.41339998,~
#> $ PC4 <dbl> 1.84402335, 0.42880476, -2.78551350, -0.22996275, -~
#> $ PC5 <dbl> -0.43936902, -0.95116452, -0.18996866, 1.11496100, ~
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> 2.1574652, 0.6502825, 0.9699879, 1.2564772, 1.93325~
#> $ 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.3312814, -2.8096725, -2.5581038, -1.8247784, -1.~
#> $ PC2 <dbl> -2.1893567, -2.1175114, -2.1887142, -2.1620590, -2.~
#> $ PC3 <dbl> 1.0676929, 0.5179360, -0.2824867, 1.2413339, -0.618~
#> $ PC4 <dbl> -1.87851814, 0.77783132, 1.65239622, -2.07523957, 0~
#> $ PC5 <dbl> 0.16758628, 1.45018162, -0.72850695, -0.21423239, -~pca_list$pca_rotation_df %>% glimpse()
#> Rows: 13
#> Columns: 13
#> $ PC1 <dbl> 0.059084397, 0.003349391, 0.396783141, 0.436143494, 0.438849183, ~
#> $ PC2 <dbl> 0.678376807, 0.683282383, 0.015513163, -0.030506780, -0.008329074~
#> $ PC3 <dbl> -0.1623272413, -0.1680681875, 0.2129605762, -0.0003228956, 0.0361~
#> $ PC4 <dbl> -0.05900012, -0.06600437, 0.03971442, -0.08226077, 0.05284108, 0.~
#> $ PC5 <dbl> 0.01120683, 0.01925682, 0.13596193, 0.05820215, -0.06521034, 0.54~
#> $ PC6 <dbl> -0.045639288, -0.047119435, -0.144895059, 0.051521586, 0.01273738~
#> $ PC7 <dbl> 0.02335477, 0.01780606, -0.14014484, -0.06518703, 0.04151796, 0.3~
#> $ PC8 <dbl> 0.03982371, 0.03698347, 0.30742275, 0.09477635, 0.02503223, 0.180~
#> $ PC9 <dbl> -0.01775394, 0.01026116, 0.80105558, -0.26561820, -0.21946954, 0.~
#> $ PC10 <dbl> 0.0130030970, -0.0123130820, 0.0079228639, 0.3000006331, 0.368700~
#> $ PC11 <dbl> -2.483705e-02, 2.527533e-02, -1.219713e-03, -3.564736e-02, 6.6141~
#> $ PC12 <dbl> 9.455747e-03, -9.068458e-03, 3.028359e-03, 7.870681e-01, -4.18217~
#> $ PC13 <dbl> 7.077798e-01, -7.037447e-01, -1.431731e-04, -3.882162e-02, -3.637~pca_list$pca_variance_df %>% glimpse()
#> Rows: 13
#> Columns: 6
#> $ PC <chr> "PC1", "PC2", "PC3", "PC4", "PC5", "PC6", "PC7", "PC8"~
#> $ var_explained <dbl> 3.935552e-01, 1.561854e-01, 1.183042e-01, 1.035031e-01~
#> $ var_pct_txt <chr> "39.36%", "15.62%", "11.83%", "10.35%", "9.69%", "4.65~
#> $ cum_var_pct <dbl> 0.3935552, 0.5497406, 0.6680448, 0.7715479, 0.8684376,~
#> $ cum_var_pct_txt <chr> "39.36%", "54.97%", "66.80%", "77.15%", "86.84%", "91.~
#> $ ou_threshold <fct> Under, Under, Under, Under, Over, Over, Over, Over, Ov~pca_list$pca_variance_scree_plt