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)
I'm sorry, but it is not clear what you are asking for help with. Could you please provide more information on what you are trying to achieve and where exactly you need help?
User
yes, I need to do an analysis of an elastic net with pca. but as you can see, the "$" are becoming and have a different format from some reason.. can you generate an answer that will take this into account, and give me a simple R text?
Assistant
Sure! Here's an attempt to provide a simple R code that performs an elastic net analysis with PCA:
R
User
looks good, but there are stil some <br/
Assistant
I apologize for the confusion. It seems that "" tags have been added to the code, possibly due to copy-pasting from another source. Here's the same code without the "" tags:
R
User
tags agian..
Assistant
I apologize for the issue with tags. Here is the code again, without any tags:
R
WADDPCsdf=data.frame(PCs)PCsdf
1) Split to train and test. use 0.7 for the train data
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.
# Load the data
library(readxl)
library(dplyr)
# loading the raw data
raw_data = read_excel('dataFA.xlsx')
# feature engineering
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()
# Perform PCA
library(psych)
## How many components?
n <- n_factors(my_data)
### Scree plot
scree(my_data, factors = FALSE, pc = TRUE)
num_of_factors <- mean(n$n_Factors)
### %Variance accounted for
PCA_params <- principal_components(my_data, n = num_of_factors)
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
# Split the data into training and testing sets
library(caret)
set.seed(1234)
splits <- initial_split(PCs_df, prop = 0.7)
train_data <- training(splits)
test_data <- testing(splits)
# Prepare the recipe
library(recipes)
rec <- recipe(WADD ~ ., data = train_data) %>%
step_range(all_numeric_predictors())
# Perform elastic net with cross-validation
library(caret)
library(glmnet)
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 <- train(x = rec,
data = train_data,
method = "glmnet",
tuneGrid = tg,
trControl = tc)
# Select the best model
best_alpha <- mod$bestTune$alpha
best_lambda <- mod$bestTune$lambda
# Generate predictions
test_data$pred <- predict(mod, newdata = test_data)
# Evaluate performance
library(yardstick)
rmse(test_data, truth = WADD, estimate = pred)
rsq(test_data, truth = WADD, estimate = pred)
# Extract coefficients
coefficients <- coef(mod$finalModel, s = best_lambda)
I hope this helps! Let me know if you have any questions or if there's anything else I can assist you with.
# Load the data
library(readxl)
library(dplyr)
# loading the raw data
raw_data = read_excel('dataFA.xlsx')
# feature engineering
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()
# Perform PCA
library(psych)
## How many components?
n <- n_factors(my_data)
### Scree plot
scree(my_data, factors = FALSE, pc = TRUE)
num_of_factors <- mean(n$n_Factors)
### %Variance accounted for
PCA_params <- principal_components(my_data, n = num_of_factors)
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
# Split the data into training and testing sets
library(caret)
set.seed(1234)
splits <- initial_split(PCs_df, prop = 0.7)
train_data <- training(splits)
test_data <- testing(splits)
# Prepare the recipe
library(recipes)
rec <- recipe(WADD ~ ., data = train_data) %>%
step_range(all_numeric_predictors())
# Perform elastic net with cross-validation
library(caret)
library(glmnet)
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 <- train(x = rec,
data = train_data,
method = "glmnet",
tuneGrid = tg,
trControl = tc)
# Select the best model
best_alpha <- mod$bestTune$alpha
best_lambda <- mod$bestTune$lambda
# Generate predictions
test_data$pred <- predict(mod, newdata = test_data)
# Evaluate performance
library(yardstick)
rmse(test_data, truth = WADD, estimate = pred)
rsq(test_data, truth = WADD, estimate = pred)
# Extract coefficients
coefficients <- coef(mod$finalModel, s = best_lambda)
I hope this helps! Please let me know if you have any further questions or issues.
# Load the data
library(readxl)
library(dplyr)
# loading the raw data
raw_data = read_excel('dataFA.xlsx')
# feature engineering
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()
# Perform PCA
library(psych)
## How many components?
n <- n_factors(my_data)
### Scree plot
scree(my_data, factors = FALSE, pc = TRUE)
num_of_factors <- mean(n$n_Factors)
### %Variance accounted for
PCA_params <- principal_components(my_data, n = num_of_factors)
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
# Split the data into training and testing sets
library(caret)
set.seed(1234)
splits <- initial_split(PCs_df, prop = 0.7)
train_data <- training(splits)
test_data <- testing(splits)
# Prepare the recipe
library(recipes)
rec <- recipe(WADD ~ ., data = train_data) %>%
step_range(all_numeric_predictors())
# Perform elastic net with cross-validation
library(caret)
library(glmnet)
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 <- train(x = rec,
data = train_data,
method = "glmnet",
tuneGrid = tg,
trControl = tc)
# Select the best model
best_alpha <- mod$bestTune$alpha
best_lambda <- mod$bestTune$lambda
# Generate predictions
test_data$pred <- predict(mod, newdata = test_data)
# Evaluate performance
library(yardstick)
rmse(test_data, truth = WADD, estimate = pred)
rsq(test_data, truth = WADD, estimate = pred)
# Extract coefficients
coefficients <- coef(mod$finalModel, s = best_lambda)
I hope this helps! If you have any further questions or issues, please let me know.