---
title: "Data Analysis of Llama 3.1 70B Instruct"
author: "Carlos Javier Pérez Pérez"
date: "October 4, 2024"
output:
  html_document: default
  pdf_document: default
---

## **Libraries Used During the Analysis**

This chunk loads the necessary libraries for data analysis and visualization.

```{r}
# Load necessary libraries
library(readr)      # To read data files
library(readxl)     # To read Excel files
library(dplyr)      # For data manipulation
library(ggplot2)    # For data visualization
library(tidyr)      # For data cleaning and transformation
library(fmsb)       # For radar charts

```

## **Load and Confirm the Data**

This chunk presents the code used to access the database and begin its analysis. It is necessary to specify the location of the "1-Database" file on your computer.

```{r}
# Define the database file location
location <- "C:/Users/Carlos/Desktop/Investigación IA/2-Base de datos/1-Database.xlsx"

# Read the database sheets
internal_assessment_db <- read_xlsx(location, sheet = 1)  # Load sheet 1
external_assessment_db <- read_xlsx(location, sheet = 2)  # Load sheet 2

```

## **Data Exploration**

This chunk performs a superficial exploration of the data to understand its structure.

```{r}
# Show the first rows of the internal dataset
head(internal_assessment_db)

# Show the first rows of the external dataset
head(external_assessment_db)
```

## **Data Cleaning**

Next, we will check for empty cells, duplicated questions or answers, and verify that the data is of the correct type and within the proper range.

## **Check for NA Values**

This script checks if there are empty cells in the database.

```{r}

# Define the function to check NA values
check_na_values <- function(db) {
  # Check and print NA values in each column
  db %>% summarise(across(everything(), ~ any(is.na(.)))) %>% print()
}

# Call the function to check NA values in both datasets
check_na_values(internal_assessment_db)
check_na_values(external_assessment_db)
```

## **Check for Duplicated Questions**

This script checks if there are any duplicated questions or answers.

```{r}
# Define the function to check for duplicated questions
duplicated_aq_checker <- function(db, aq) {
  # Count the frequency of each question
  duplicated_checker <- db %>% select(!!sym(aq)) %>% count(!!sym(aq))
  
  # Identify duplicated questions
  double_aq <- duplicated_checker %>% filter(n > 1) %>% pull(!!sym(aq))
  
  return(double_aq)
}

# Call the function to check for duplicated questions in both datasets
duplicated_aq_checker(internal_assessment_db, "Pregunta")
duplicated_aq_checker(internal_assessment_db, "Respuesta")
duplicated_aq_checker(external_assessment_db, "Pregunta")
duplicated_aq_checker(external_assessment_db, "Respuesta")
```

## **Check Numeric Columns**

This script checks if the columns "Precisión," "Relevancia," and "Coherencia" are numeric and within the range of 1 to 5.

```{r}
# Define the function to check that columns are numeric and between 1 and 5
parameters_checker <- function(db) {
  # Select columns and check if all values are numeric and between 1 and 5
  result <- db %>% select(Precisión, Relevancia, Coherencia) %>% summarise(
    Precisión = all(is.numeric(Precisión) & between(Precisión, 1, 5)),
    Relevancia = all(is.numeric(Relevancia) & between(Relevancia, 1, 5)),
    Coherencia = all(is.numeric(Coherencia) & between(Coherencia, 1, 5))
  )
  
  # Print result
  if (all(result)) {
    print("All values are numeric and between 1 and 5.")
  } else {
    print("There are values that are not numeric or not between 1 and 5.")
    print(result)
  }
}

# Call the function to check numeric values and ranges in both datasets
parameters_checker(internal_assessment_db)
parameters_checker(external_assessment_db)
```

No empty columns were found in the databases; neither questions nor answers were duplicated. All data were within the normal range, so the analysis proceeded.

## **Tool Analysis**

General analysis of precision, relevance, and coherence of the tool.

```{r}
# Print summary of the internal dataset
print(summary(internal_assessment_db))

# Print summary of the external dataset
print(summary(external_assessment_db))

# Define function to calculate global mean of parameters
mean_value <- function(db) {
  mean_global_parameters <- db %>% select(Precisión, Relevancia, Coherencia) %>% summarise(
    Precisión = mean(Precisión),
    Relevancia = mean(Relevancia),
    Coherencia = mean(Coherencia)
  )
}

# Calculate means for both datasets
mean_value_internal_assessment <- mean_value(internal_assessment_db)
mean_value_external_assessment <- mean_value(external_assessment_db)

# Print calculated means
print(mean_value_internal_assessment)
print(mean_value_external_assessment)
```

## **General Evaluation Chart**

Create a chart showing the general evaluation.

```{r}
# Create the dataframe for the chart
data <- data.frame(
  Categoria = factor(rep(c("Precisión", "Relevancia", "Coherencia"), 2), levels = c("Precisión", "Relevancia", "Coherencia")),
  Valor = c(4.7, 4.9, 4.9, 4.4, 4.9, 4.7),
  Grupo = factor(rep(c("Evaluadores Internos", "Evaluadores Externos"), each = 3), levels = c("Evaluadores Internos", "Evaluadores Externos"))
)

# Create the grouped bar chart with ggplot2
ggplot(data, aes(x = Grupo, y = Valor, fill = Categoria)) +
 geom_bar(stat = "identity", position = position_dodge()) +
 geom_text(aes(label = Valor), vjust = -0.3, position = position_dodge(0.9)) +
 labs(title = "Evaluation of Precision, Relevance, and Coherence", x = "Evaluator Group", y = "Value") +
 theme_minimal() +
 theme(legend.position = "bottom", legend.title = element_blank()) +
 scale_fill_manual(values = c("Precisión" = "#16A085", "Relevancia" = "#2C3E50", "Coherencia" = "#B0B0B0"))
```

## **Subject Analysis**

Detailed analysis by subject.

```{r}
# Define function to calculate averages by subject
mean_matter <- function(dataset, asignatura) {
   mean_matter_parameters <- dataset %>% select(Asignatura, Precisión, Relevancia, Coherencia) %>%
   filter(Asignatura == asignatura) %>%
   summarise(
     Precisión = mean(Precisión),
     Relevancia = mean(Relevancia),
     Coherencia = mean(Coherencia)
   )
}

# Calculate means for Biochemistry in both datasets
biochemistry_internal_assessment_mean <- mean_matter(internal_assessment_db,"Bioquímica")
biochemistry_external_assessment_mean <- mean_matter(external_assessment_db,"Bioquímica")

# Calculate means for Metabolism in both datasets
metabolism_internal_assessment_mean <- mean_matter(internal_assessment_db,"Metabolismo\r\ny Nutrición")
metabolism_external_assessment_mean <- mean_matter(external_assessment_db,"Metabolismo\r\r\r\ny Nutrición")

# Print results for each subject
print(biochemistry_internal_assessment_mean)
print(biochemistry_external_assessment_mean)
print(metabolism_internal_assessment_mean)
print(metabolism_external_assessment_mean)
```

## **Charts by Subject**

Plotting the obtained results.

```{r}
# DataFrame for Biochemistry
biochemistry_heatmap_dataframe <- data.frame(
   parameters_to_assessment = c("Precisión", "Relevancia", "Coherencia", "Precisión", "Relevancia", "Coherencia"),
   internaL_external_assessment = c(rep("Internal assessment", 3), rep("External assessment", 3)),
   biochemistry_assessment_heatmap = c(
     biochemistry_internal_assessment_mean$Precisión,
     biochemistry_internal_assessment_mean$Relevancia,
     biochemistry_internal_assessment_mean$Coherencia,
     biochemistry_external_assessment_mean$Precisión,
     biochemistry_external_assessment_mean$Relevancia,
     biochemistry_external_assessment_mean$Coherencia 
   )
)

# Biochemistry chart using ggplot2
ggplot(biochemistry_heatmap_dataframe, aes(x = parameters_to_assessment, y = internaL_external_assessment, fill = biochemistry_assessment_heatmap)) +
 geom_tile() +
 scale_fill_gradientn(colors = c("#2C3E50", "#16A085")) +
 labs(title = "Biochemistry Evaluation Heatmap", x = "Parameters", y = "Evaluation Type", fill = "Evaluation Value") +
 theme_minimal() +
 geom_text(aes(label = round(biochemistry_assessment_heatmap,2)), color="white", size = 4)

# DataFrame for Metabolism
metabolism_heatmap_dataframe <- data.frame(
   parameters_to_assessment = c("Precisión", "Relevancia", "Coherencia", "Precisión", "Relevancia", "Coherencia"),
   internaL_external_assessment = c(rep("Internal assessment", 3), rep("External assessment", 3)),
   metabolism_assessment_heatmap = c(
     metabolism_internal_assessment_mean$Precisión,
     metabolism_internal_assessment_mean$Relevancia,
     metabolism_internal_assessment_mean$Coherencia,
     metabolism_external_assessment_mean$Precisión,
     metabolism_external_assessment_mean$Relevancia,
     metabolism_external_assessment_mean$Coherencia 
   )
)

# Metabolism chart using ggplot2
ggplot(metabolism_heatmap_dataframe, aes(x = parameters_to_assessment, y = internaL_external_assessment, fill = metabolism_assessment_heatmap)) +
 geom_tile() +
 scale_fill_gradientn(colors = c("#2C3E50", "#16A085")) +
 labs(title = "Metabolism Evaluation Heatmap", x = "Parameters", y = "Evaluation Type", fill = "Evaluation Value") +
 theme_minimal() +
 geom_text(aes(label = round(metabolism_assessment_heatmap, 2)), color="white", size = 4)
```

## **Análisis por temas**

Análisis detallado por temas dentro de cada asignatura acorde a evaluadores externos e internos.

```{r}
# Definir función para calcular media por tema en cada asignatura 
calculating_mean_by_topic_matter_function<-function (db ,asignatura){
 calculating_mean_by_topic_matter<-db %>%
 select(Asignatura,Tema ,Precisión ,Relevancia ,Coherencia )%>%
 filter (Asignatura ==asignatura)
 calculating_mean_by_topic_matter_grouped<-calculating_mean_by_topic_matter%>%
 group_by(Tema)%>%
 summarise (Precisión=mean(Precisión),Relevancia=mean(Relevancia),Coherencia=mean(Coherencia))
}

# Calcular medias por tema en Bioquímica 
mean_in_biochemistry_by_parameter_internal_assessment<-calculating_mean_by_topic_matter_function(internal_assessment_db,"Bioquímica")
mean_in_biochemistry_by_parameter_external_assessment<-calculating_mean_by_topic_matter_function(external_assessment_db,"Bioquímica")

# Calcular medias por tema en Metabolismo 
mean_in_metabolism_by_parameter_internal_assessment<-calculating_mean_by_topic_matter_function(internal_assessment_db,"Metabolismo\r\ny Nutrición")
mean_in_metabolism_by_parameter_external_assessment<-calculating_mean_by_topic_matter_function(external_assessment_db,"Metabolismo\r\r\r\ny Nutrición")

# Imprimir resultados finales 
print("Medias en Bioquímica (Evaluación Interna):")
print(mean_in_biochemistry_by_parameter_internal_assessment)

print("Medias en Bioquímica (Evaluación Externa):")
print(mean_in_biochemistry_by_parameter_external_assessment)

print("Medias en Metabolismo y Nutrición (Evaluación Interna):")
print(mean_in_metabolism_by_parameter_internal_assessment)

print("Medias en Metabolismo y Nutrición (Evaluación Externa):")
print(mean_in_metabolism_by_parameter_external_assessment)
```

## **Radar Chart for Precision, Relevance, and Coherence**

Here, we generate a radar chart to visualize the comparison of precision, relevance, and coherence for both internal and external evaluators.

```{r}
# Reemplazar saltos de línea por espacios
internal_assessment_db$Asignatura <- gsub("\r\n", " ", internal_assessment_db$Asignatura)
external_assessment_db$Asignatura <- gsub("\r\r\r\n", " ", external_assessment_db$Asignatura)

# Definir la función para crear el gráfico radar
create_radar_chart <- function(internal_db, external_db, matter, parameter, chart_title) {
  
  # Extraer datos: paso uno
  extracting_data_step_one <- function(db, matter) {
    db %>% 
      select(Asignatura, Tema, Precisión, Relevancia, Coherencia) %>% 
      filter(Asignatura == matter)
  }

  # Extraer datos: paso dos
  extracting_data_step_two <- function(data, parameter) {
    data %>% 
      group_by(Tema) %>% 
      summarise(Value = mean(!!sym(parameter)))
  }

  # Extraer los datos de la evaluación interna y externa
  internal_data <- extracting_data_step_one(internal_db, matter)
  external_data <- extracting_data_step_one(external_db, matter)

  internal_values <- extracting_data_step_two(internal_data, parameter)
  external_values <- extracting_data_step_two(external_data, parameter)

  # Combinar los datos
  combined_data <- mutate(internal_values, external_value = external_values$Value)
  combined_data <- data.frame(t(combined_data[-1]))
  colnames(combined_data) <- internal_values$Tema

  # Agregar límites
  value_max <- c(rep(5, ncol(combined_data)))
  value_min <- c(rep(0, ncol(combined_data)))
  combined_data <- rbind(value_max, value_min, combined_data)
  row.names(combined_data) <- c("Valor Máximo", "Valor Mínimo", "Evaluación Interna", "Evaluación Externa")

  # Definir colores
  color <- c(
    rgb(0.0863, 0.6275, 0.5216, alpha = 1.0),
    rgb(0.1725, 0.2431, 0.3137, alpha = 1.0),
    rgb(0.7412, 0.7647, 0.7804, alpha = 0.0)
  )

  # Crear el gráfico de radar
  radarchart(combined_data,
             seg = 4,
             title = chart_title, # Utiliza el título proporcionado
             axislabcol = "black",
             plwd = 4,
             cglty = 1.8,
             cglcol = "black",
             vlcex = 0.6,
             cglwd = 1.5,
             plty = 1,
             pcol = color) # Utilizar el mismo vector de colores para las líneas
}

# Ejemplo de uso
Biochemistry_pres <- create_radar_chart(internal_assessment_db, external_assessment_db, "Bioquímica", "Precisión", "Accuracy")
b <- create_radar_chart(internal_assessment_db, external_assessment_db, "Bioquímica", "Relevancia", "Relevance")
c <- create_radar_chart(internal_assessment_db, external_assessment_db, "Bioquímica", "Coherencia", "Coherence")
d <- create_radar_chart(internal_assessment_db, external_assessment_db, "Metabolismo y Nutrición", "Precisión", "Accuracy")
e <- create_radar_chart(internal_assessment_db, external_assessment_db, "Metabolismo y Nutrición", "Relevancia", "Relevance")
f <- create_radar_chart(internal_assessment_db, external_assessment_db, "Metabolismo y Nutrición", "Coherencia", "Coherence")

```

#### End of Analysis
