library('lme4')
Loading required package: Matrix
library('multilevelmod')
Loading required package: parsnip
library('workflowsets')
library('tidymodels')
ββ Attaching packages ββββββββββββββββββββββββββββββββββββββ tidymodels 1.2.0 ββ
β broom 1.0.7 β recipes 1.1.1
β dials 1.3.0 β rsample 1.2.1
β dplyr 1.1.4 β tibble 3.2.1
β ggplot2 3.5.1 β tidyr 1.3.1
β infer 1.0.7 β tune 1.2.1
β modeldata 1.4.0 β workflows 1.1.4
β purrr 1.0.4 β yardstick 1.3.2
ββ Conflicts βββββββββββββββββββββββββββββββββββββββββ tidymodels_conflicts() ββ
β purrr::discard() masks scales::discard()
β tidyr::expand() masks Matrix::expand()
β dplyr::filter() masks stats::filter()
β dplyr::lag() masks stats::lag()
β tidyr::pack() masks Matrix::pack()
β recipes::step() masks stats::step()
β tidyr::unpack() masks Matrix::unpack()
β recipes::update() masks Matrix::update(), stats::update()
β’ Learn how to get started at https://www.tidymodels.org/start/
tidymodels_prefer()
riesby |> head()
# A tibble: 6 Γ 7
subject depr_score week male endogenous imipramine desipramine
<fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 101 -8 0 0 0 4.04 4.20
2 101 -19 1 0 0 3.93 4.81
3 101 -22 2 0 0 4.33 4.96
4 101 -23 3 0 0 4.37 4.96
5 103 -18 0 1 0 2.77 5.24
6 103 -9 1 1 0 3.47 5.21
splits <- group_initial_split(riesby, group = subject)
folds <- group_vfold_cv(training(splits), v = 5, group = subject)
rec <- recipe(depr_score ~ ., data = training(splits) %>% filter(week == 0)) %>%
add_role(subject, new_role = "exp_unit") %>%
update_role(week, new_role = "id variable") %>%
step_normalize(all_numeric_predictors(), -all_outcomes(), -has_role("exp_unit") ) %>%
step_pca(all_numeric_predictors(), -all_outcomes(), -has_role("exp_unit"), threshold = 1)
prep_rec <- prep(rec, training = training(splits) %>% filter(week == 0))
n_comp <- tidy(prep_rec, 2, type ='variance') %>%
select(component) %>%
filter(component == max(component)) %>%
pull()
tidy(prep_rec, 2, type ='variance') %>%
filter(terms == 'cumulative percent variance' | terms == 'percent variance') %>%
mutate(variance = factor(terms, levels = c('cumulative percent variance', 'percent variance'), labels = c("cumulative","percent"))) %>%
mutate(PC = component) %>%
mutate(percent = value/100) %>%
ggplot(aes(x=PC, y=percent, fill=variance, alpha = variance)) +
geom_bar(stat = "identity",
position = "identity") +
scale_fill_manual(values =
c("cumulative" = "#56B4E9",
"percent" = "black")) +
scale_x_continuous(breaks = 1:n_comp) +
scale_y_continuous(
breaks = seq(0, 1, 0.1),
labels = scales::percent_format()
) +
scale_alpha_manual(values = c(.75,1)) +
labs(
x = "Principal Component",
y = "Percent of Variance Explained",
title = "Variance Explained by Principal Component"
) +
geom_text(aes(label = scales::percent(percent)),
#position = position_stack(),
size = 3)
Warning in 1:n_comp: numerical expression has 4 elements: only the first used

rec <- recipe(depr_score ~ ., data = training(splits) %>% filter(week == 0)) %>%
add_role(subject, new_role = "exp_unit") %>%
update_role(week, new_role = "id variable") %>%
step_normalize(all_numeric_predictors(), -all_outcomes(), -has_role("exp_unit") ) %>%
step_pca(all_numeric_predictors(), -all_outcomes(), -has_role("exp_unit"), num_comp = 3)
lmer_spec <- linear_reg() %>%
set_engine("lmer")
lmer_wflow <- workflow() %>%
add_variables(outcomes = depr_score, predictors = c(starts_with("PC"), c(subject))) %>%
add_model(lmer_spec, formula = depr_score ~ PC1 + PC2 + PC3 + (1|subject))
lmer_wflow2 <- lmer_wflow %>%
remove_variables() %>%
add_recipe(rec)
lmer_fits <- tune_grid(
lmer_wflow2,
resamples = folds,
grid = 10,
control = control_grid(save_pred = TRUE)
)
Warning: No tuning parameters have been detected, performance will be evaluated
using the resamples with no tuning. Did you want to [tune()] parameters?
collect_predictions(lmer_fits)
# A tibble: 188 Γ 5
.pred id .row depr_score .config
<dbl> <chr> <int> <dbl> <chr>
1 -8.06 Resample1 23 -6 Preprocessor1_Model1
2 -8.85 Resample1 24 -7 Preprocessor1_Model1
3 -9.94 Resample1 25 -12 Preprocessor1_Model1
4 -10.3 Resample1 26 -13 Preprocessor1_Model1
5 -10.0 Resample1 35 -1 Preprocessor1_Model1
6 -8.44 Resample1 36 -1 Preprocessor1_Model1
7 -10.1 Resample1 37 -8 Preprocessor1_Model1
8 -11.2 Resample1 38 -6 Preprocessor1_Model1
9 -12.3 Resample1 46 -8 Preprocessor1_Model1
10 -13.0 Resample1 47 -10 Preprocessor1_Model1
# βΉ 178 more rows