library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(fBasics)
library(corrplot)
## corrplot 0.94 loaded
ts <- read.csv(file = "Targeted_Sensory.csv") %>% 
  dplyr::select(-X.)
# Función para calcular la desviación estándar de cada columna
col_sd <- function(data) {
  apply(data, 2, sd)
}

# Función para realizar el escalado de Pareto
pareto_scale <- function(data) {
  # Centrar los datos (restar la media de cada columna)
  centered_data <- scale(data, center = TRUE, scale = FALSE)
  
  # Calcular la desviación estándar de cada columna
  std_devs <- col_sd(centered_data)
  
  # Escalar cada columna por la raíz cuadrada de su desviación estándar
  pareto_scaled_data <- sweep(centered_data, 2, sqrt(std_devs), FUN = "/")
  
  return(pareto_scaled_data)
}
scaled_data <- pareto_scale(ts) %>% 
  as.data.frame()
write.csv(scaled_data, file = "scaled_sensory_targeted.csv")
ts_1r <- ts[, c(1:15,17:18)]# catechins
ts_2r <- ts[, c(1:15,19,20)]#procyanidins
ts_3r <- ts[, c(1:15,21,22,27)]# acids
ts_4r <- ts[, c(1:15,23,24)]# sugars
ts_5r <- ts[, c(1:15,25,26)]# acids
ts_1 <- scaled_data[, c(1:15,17:18)]# catechins
ts_2 <- scaled_data[, c(1:15,19,20)]#procyanidins
ts_3 <- scaled_data[, c(1:15,21,22,27)]# acids
ts_4 <- scaled_data[, c(1:15,23,24)]# sugars
ts_5 <- scaled_data[, c(1:15,25,26)]# Cafeina_theo
pairs(ts_1)

pairs(ts_2)

pairs(ts_3)

#Lineal correlation plots raw data

corrplot(corr=cor(ts_1r),
         method = "number",
         type = "full",
         tl.pos = "tl",
         order = "original")

corrplot(corr=cor(ts_2r),
         method = "number",
         type = "full",
         tl.pos = "tl",
         order = "original")

corrplot(corr=cor(ts_3r),
         method = "number",
         type = "full",
         tl.pos = "tl",
         order = "original")

corrplot(corr=cor(ts_4r),
         method = "number",
         type = "full",
         tl.pos = "tl",
         order = "original")

#Lineal Correlation plots scaled data

corrplot(corr=cor(ts_1),
         method = "number",
         type = "full",
         tl.pos = "tl",
         order = "original")

corrplot(corr=cor(ts_2),
         method = "number",
         type = "full",
         tl.pos = "tl",
         order = "original")

corrplot(corr=cor(ts_3),
         method = "number",
         type = "full",
         tl.pos = "tl",
         order = "original")

corrplot(corr=cor(ts_4),
         method = "number",
         type = "full",
         tl.pos = "tl",
         order = "original")

corrplot(corr=cor(ts[,-16]),
         method = "number",
         type = "full",
         tl.pos = "tl",
         order = "original")