I have the following code #load the data
library(readxl)
library(dplyr)
#loading the raw data
#setwd("C:/Users/defaultuser0.LAPTOP-10STVJBB/Desktop/R")
raw_data = read_excel('dataFA.xlsx')
#feature engineering. The SS variable are the correlations between the real
#averages (faces/numbers/weighted average) and subject's estimation. We defined summary statistics
#(SS) performance with correlation (and nor RMSD for example) to avoid constant biases in
#subject's estimations(that are observed in RMSD, but avoided in correlations)
my_data = raw_data %>%
  group_by(as.factor(sub_num))%>%
  summarise(WADD = mean(WADD_Acc),WADD_rule = mean(WADDrule_Acc),
            SS_faces = cor(real_Average_faces,sub_response_faces),
            SS_AA = cor(real_Average_AA,sub_response_AA),
            SS_WA3 = cor(real_Weighted_WA3,sub_response_WA3),
            SS_WA4 = cor(real_Weighted_WA4,sub_response_WA4))%>%
  ungroup()

my_data = my_data[2:7]
#ridge regression to predict job candidate performance from the averaging tasks perfomances
#pca 
library(caret)
library(leaps)
library(rsample)
library(recipes)
library(yardstick)
library(tidyverse)
library(recipes)

library(psych)
library(parameters)
library(performance)

# PCA ---------------------------------------------------------------------


## How many components? ---------------------
pca_my_data = my_data
n <- n_factors(pca_my_data)
# This function calls many methods, e.g., nFactors::nScree... Read the doc!

as.data.frame(n) # Different methods...

### Scree plot --------------------
# Visually:
scree(pca_my_data, factors = FALSE, pc = TRUE) # = 2
# 2/5 seems to be supported by the elbow method,
# and 5 by the Kaiser criterion.

num_of_factore = mean(n$n_Factors)
### %Variance accounted for --------------------

PCA_params <- principal_components(pca_my_data, n = num_of_factore)

PCA_params_summary <- summary(PCA_params) |>
  # Clean up...
  column_to_rownames("Parameter") |> 
  datawizard::data_rotate() |> 
  rownames_to_column("Component") |> 
  mutate(
    Component = factor(Component, levels = Component)
  )

ggplot(PCA_params_summary, aes(Component, Variance_Cumulative)) + 
  geom_point() + 
  geom_line(aes(group = NA)) + 
  geom_hline(yintercept = 0.90)
# Or 16....

## Extract component scores ----------------------



### With parameters ---------------------

PCA_model <- attr(PCA_params, "model")
PCs <- predict(PCA_model, newdata = my_data) # returns all PCs
WADD_C = my_data$WADD
PCs_df = data.frame(PCs)
PCs_df$WADD=WADD_C


# 1) Split to train and test. use 0.7 for the train data
set.seed(1234) 
splits_PCA <- initial_split(PCs_df, prop = 0.7)
my_data.train_PCA <- training(splits_PCA)
my_data.test_PCA <- testing(splits_PCA)

rec <- recipe(WADD ~ ., data = my_data.train_PCA) |> 
  step_range(all_numeric_predictors())


tg <- expand.grid(alpha = c(seq(0, 1, length.out = 25)),
                  lambda = exp(seq(-8, -4, length.out = 40)))

tc <- trainControl(method = "cv", number = 5)




mod.elastic <- train(
  x = rec,
  data = my_data.train_PCA,
  method = "glmnet",
  tuneGrid = tg,
  trControl =  tc
)

plot(mod.elastic, xTrans = log)
# The best tuning are: alpha - 1, lamda - 0.0074
mod.elastic$results$RMSE 

my_data.test$pred.elastic <- 
  predict(mod.elastic, newdata = my_data.test)



mod.elastic$results$RMSE



my_metrics <- metric_set(rsq, rmse)
my_metrics(my_data.test, truth = WADD, estimate = pred.elastic) # rsq = 0.138, RMSE = 0.0417


mod.elastic$pred.elastic <- ifelse(is.nan(mod.elastic$pred.elastic), NA, mod.elastic$pred.elastic)
mod.elastic <- na.omit(mod.elastic)

lambda =mod.elastic$bestTune$lambda # = 0.0024
alpha =mod.elastic$bestTune$alpha # = 0


coefficients <- coef(mod.elastic$finalModel, s = lambda)
coefficients - and I'm not 100% sure what to do next. I  geuss I need to normelize my data and then rotated the PCA components, But I have no clue how to do it or what to do next. please generate this code for me.