Hi!
I would like a variable within a loop to be evaluated straight away. That is, I would like to create a recipe in tidymodels that gets PCAs added to it in a loop; however, the problem seems to be that it doesn't evaluate within the loop. (I'm open for other solutions if there is a better one than a loop here.)
My code so far:
#Example data
set.seed(42)
df_x1 <- tibble(V_set1.V1=runif(10, 1, 10), V_set1.V2=runif(10, 1, 10))
df_x2 <- tibble(V_set2.V3=runif(10, 1, 10), V_set2.V4=runif(10, 1, 10))
y <- runif(10, 1, 10)
My_data <- cbind(df_x1, df_x2, y)
# Example recipe
My_recipe <-
recipe(y ~ .,
data = My_data) %>%
step_scale(all_predictors())
My_recipe
# Variables starting with these strings will be selected in the for loop
variable_index_vec <- c("V_set1", "V_set2")
# Loop that should add a PCA in each loop; first selecting all variables starting with V_set1; and then V_set2.
for (i in 1:length(variable_index_vec)){
variable_index <- variable_index_vec[i]
prefix_index <- paste("group_", i, sep = "")
My_recipe <- step_pca(My_recipe, starts_with(eval(parse(text=variable_index_vec[i]))), threshold = .95, prefix = prefix_index)
}
# However, here it does not say Vset1 so it does not work later!
My_recipe[3]$steps[[2]][1]
My_recipe[3]$steps[[3]][1]
# FYI, adding them like below works fine:
My_recipe <- step_pca(My_recipe, starts_with(V_set1), threshold = .95, prefix = prefix_index)
My_recipe <- step_pca(My_recipe, starts_with(V_set2), threshold = .95, prefix = prefix_index)
My_recipe[3]$steps[[4]][1]
My_recipe[3]$steps[[5]][1]