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.
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))
Now that we have out libraries we can go ahead and get our data set ready.
<- healthyR_data %>%
data_tbl 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 × 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.
<- initial_split(data = data_tbl, prop = 0.8)
splits
splits#> <Training/Testing/Total>
#> <76/19/95>
head(training(splits))
#> # A tibble: 6 × 2
#> date_col value
#> <dttm> <int>
#> 1 2015-01-01 00:00:00 1692
#> 2 2014-03-01 00:00:00 1749
#> 3 2013-12-01 00:00:00 1670
#> 4 2014-02-01 00:00:00 1651
#> 5 2013-06-01 00:00:00 1813
#> 6 2018-07-01 00:00:00 1651
<- recipe(value ~ ., training(splits)) %>%
rec_obj 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()
#> Warning: `terms_select()` was deprecated in recipes 0.1.17.
#> Please use `recipes_eval_select()` instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
#> Rows: 76
#> Columns: 20
#> $ date_col <dttm> 2015-01-01, 2014-03-01, 2013-12-01, 2014-02-01, 20…
#> $ value <int> 1692, 1749, 1670, 1651, 1813, 1651, 1550, 1753, 147…
#> $ date_col_index.num <dbl> 1420070400, 1393632000, 1385856000, 1391212800, 137…
#> $ date_col_year <int> 2015, 2014, 2013, 2014, 2013, 2018, 2018, 2013, 201…
#> $ date_col_half <int> 1, 1, 2, 1, 1, 2, 2, 2, 1, 2, 2, 1, 2, 2, 2, 2, 1, …
#> $ date_col_quarter <int> 1, 1, 4, 1, 2, 3, 4, 3, 1, 4, 3, 2, 4, 3, 4, 3, 1, …
#> $ date_col_month <int> 1, 3, 12, 2, 6, 7, 11, 9, 2, 10, 9, 4, 10, 7, 11, 8…
#> $ date_col_month.lbl <ord> January, March, December, February, June, July, Nov…
#> $ 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, 7, 1, 7, 7, 1, 5, 1, 2, 7, 2, 2, 4, 2, 1, 3, 6, …
#> $ date_col_wday.lbl <ord> Thursday, Saturday, Sunday, Saturday, Saturday, Sun…
#> $ 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, 60, 62, 32, 62, 1, 32, 63, 32, 1, 63, 1, 1, 1, 3…
#> $ date_col_yday <int> 1, 60, 335, 32, 152, 182, 305, 244, 32, 275, 244, 9…
#> $ date_col_mweek <int> 5, 5, 5, 5, 5, 5, 5, 5, 6, 5, 6, 6, 5, 6, 5, 6, 5, …
#> $ date_col_week <int> 1, 9, 48, 5, 22, 26, 44, 35, 5, 40, 35, 13, 40, 26,…
#> $ date_col_week2 <int> 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, …
#> $ date_col_week3 <int> 1, 0, 0, 2, 1, 2, 2, 2, 2, 1, 2, 1, 1, 2, 2, 1, 1, …
#> $ date_col_week4 <int> 1, 1, 0, 1, 2, 2, 0, 3, 1, 0, 3, 1, 0, 2, 0, 3, 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_your_recipe(
pca_list .recipe_object = rec_obj,
.data = data_tbl,
.threshold = 0.8,
.top_n = 5
)#> Warning: Column(s) have zero variance so scaling cannot be used: `date_col_day`,
#> `date_col_mday` and `date_col_mday7`. Consider using `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_list$pca_transform
pca_rec_obj
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#> # A tibble: 169 × 4
#> terms value component id
#> <chr> <dbl> <chr> <chr>
#> 1 date_col_index.num 0.0639 PC1 pca_QOlOW
#> 2 date_col_year 0.00885 PC1 pca_QOlOW
#> 3 date_col_half 0.388 PC1 pca_QOlOW
#> 4 date_col_quarter 0.435 PC1 pca_QOlOW
#> 5 date_col_month 0.439 PC1 pca_QOlOW
#> 6 date_col_wday -0.0678 PC1 pca_QOlOW
#> 7 date_col_qday 0.0589 PC1 pca_QOlOW
#> 8 date_col_yday 0.439 PC1 pca_QOlOW
#> 9 date_col_mweek -0.0160 PC1 pca_QOlOW
#> 10 date_col_week 0.439 PC1 pca_QOlOW
#> # … with 159 more rows
$variable_variance
pca_list#> # A tibble: 52 × 4
#> terms value component id
#> <chr> <dbl> <int> <chr>
#> 1 variance 5.11 1 pca_QOlOW
#> 2 variance 2.00 2 pca_QOlOW
#> 3 variance 1.45 3 pca_QOlOW
#> 4 variance 1.42 4 pca_QOlOW
#> 5 variance 1.18 5 pca_QOlOW
#> 6 variance 0.703 6 pca_QOlOW
#> 7 variance 0.583 7 pca_QOlOW
#> 8 variance 0.491 8 pca_QOlOW
#> 9 variance 0.0582 9 pca_QOlOW
#> 10 variance 0.000259 10 pca_QOlOW
#> # … with 42 more rows
$pca_estimates
pca_list#> 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_juiced_estimates %>% glimpse()
pca_list#> Rows: 76
#> Columns: 9
#> $ date_col <dttm> 2015-01-01, 2014-03-01, 2013-12-01, 2014-02-01, 20…
#> $ value <dbl> 0.5073277, 0.7731731, 0.4047207, 0.3161056, 1.07166…
#> $ date_col_month.lbl <ord> January, March, December, February, June, July, Nov…
#> $ date_col_wday.lbl <ord> Thursday, Saturday, Sunday, Saturday, Saturday, Sun…
#> $ PC1 <dbl> -3.5015699, -2.7144366, 3.5218423, -3.1325908, -0.7…
#> $ PC2 <dbl> -0.56492417, -1.35720479, -2.22971987, -1.19309077,…
#> $ PC3 <dbl> 0.6757987, -0.2788904, 1.1356306, -0.5134949, -0.54…
#> $ PC4 <dbl> -0.49692465, -2.03049911, -0.75217030, -1.01262958,…
#> $ PC5 <dbl> 0.927425645, -1.019655449, -1.689864351, 1.36428770…
$pca_baked_data %>% glimpse()
pca_list#> Rows: 95
#> Columns: 9
#> $ date_col <dttm> 2013-01-01, 2013-02-01, 2013-03-01, 2013-04-01, 20…
#> $ value <dbl> 2.3262697, 0.6332545, 0.9923789, 1.3141917, 2.07441…
#> $ 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.5358988, -3.1316825, -2.7135283, -1.9670331, -1.…
#> $ PC2 <dbl> -2.0893725, -1.8575234, -2.0216375, -2.1391137, -1.…
#> $ PC3 <dbl> 1.1722278, -0.3679846, -0.1333801, 1.1727425, 0.785…
#> $ PC4 <dbl> 1.4986669, -0.7891256, -1.8069951, 1.7468974, -1.09…
#> $ PC5 <dbl> 0.3342324, 1.2731077, -1.1108355, 0.1318650, -1.030…
$pca_rotation_df %>% glimpse()
pca_list#> Rows: 13
#> Columns: 13
#> $ PC1 <dbl> 0.063870872, 0.008847484, 0.387751113, 0.435126138, 0.438632012, …
#> $ PC2 <dbl> 0.693719864, 0.701454781, -0.012078691, -0.009039831, -0.03107277…
#> $ PC3 <dbl> -0.02223038, -0.01438755, -0.20804533, 0.03547750, -0.06234487, -…
#> $ PC4 <dbl> 0.05859515, 0.06185489, 0.12351931, 0.03023348, -0.02372845, -0.5…
#> $ PC5 <dbl> -0.08274819, -0.07731281, 0.21552327, 0.09966117, -0.04863203, 0.…
#> $ PC6 <dbl> -0.025572645, -0.024757530, 0.210950098, 0.130996641, -0.00405092…
#> $ PC7 <dbl> 0.005619409, 0.007181293, 0.182519494, -0.006027447, -0.013205816…
#> $ PC8 <dbl> -0.03600038, -0.02713941, -0.07365767, -0.03280920, -0.06848176, …
#> $ PC9 <dbl> -0.01072348, 0.01598363, 0.81316753, -0.26989395, -0.21067788, 0.…
#> $ PC10 <dbl> 0.013184769, -0.011168317, 0.004608735, 0.296163224, 0.378487343,…
#> $ PC11 <dbl> -0.0266146279, 0.0270941341, -0.0014630052, -0.0528341745, 0.6648…
#> $ PC12 <dbl> 0.0101973976, -0.0099633796, 0.0030695364, 0.7843878360, -0.40655…
#> $ PC13 <dbl> 7.076833e-01, -7.037457e-01, -1.540099e-04, -3.993046e-02, 2.2838…
$pca_variance_df %>% glimpse()
pca_list#> Rows: 13
#> Columns: 6
#> $ PC <chr> "PC1", "PC2", "PC3", "PC4", "PC5", "PC6", "PC7", "PC8"…
#> $ var_explained <dbl> 3.933388e-01, 1.541452e-01, 1.114291e-01, 1.093657e-01…
#> $ var_pct_txt <chr> "39.33%", "15.41%", "11.14%", "10.94%", "9.05%", "5.41…
#> $ cum_var_pct <dbl> 0.3933388, 0.5474840, 0.6589130, 0.7682787, 0.8587765,…
#> $ cum_var_pct_txt <chr> "39.33%", "54.75%", "65.89%", "76.83%", "85.88%", "91.…
#> $ ou_threshold <fct> Under, Under, Under, Under, Over, Over, Over, Over, Ov…
$pca_variance_scree_plt pca_list
$pca_loadings_plt pca_list
$pca_top_n_loadings_plt pca_list