#========================================================
# LIMPIAR ENTORNO
#========================================================

rm(list = ls())

#========================================================
# CARGAR LIBRERÍAS
#========================================================

if (!require("readr")) install.packages("readr")
## Loading required package: readr
if (!require("dplyr")) install.packages("dplyr")
## Loading required package: dplyr
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
if (!require("knitr")) install.packages("knitr")
## Loading required package: knitr
if (!require("moments")) install.packages("moments")
## Loading required package: moments
if (!require("e1071")) install.packages("e1071")
## Loading required package: e1071
## 
## Attaching package: 'e1071'
## The following objects are masked from 'package:moments':
## 
##     kurtosis, moment, skewness
if (!require("gt")) install.packages("gt")
## Loading required package: gt
library(readr)
library(dplyr)
library(knitr)
library(moments)
library(e1071)
library(gt)

#========================================================
# CARGAR DATOS
#========================================================

ruta <- "D:/PUNTOS LA LO.csv"

datos <- read_csv(ruta)
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
##   dat <- vroom(...)
##   problems(dat)
## Rows: 2500 Columns: 341
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (116): FID, DEPOSIT_AN, DEPOSIT_UI, DEPOSIT_NA, DEPOSIT_LO, DEPOSIT_EN,...
## dbl  (164): FE2O3_WT_P, FEO_WT_PER, H2OTOTAL_W, CO2_WT_PER, CO2_DETECT, LOIT...
## lgl   (59): TOP_DEPTH_, BASE_DEPTH, PROVINCE, STRAT_UNIT, STRAT_UN_1, STRAT_...
## date   (2): ANALYSIS_D, LAST_UPDAT
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
cat("✓ Datos cargados exitosamente\n")
## ✓ Datos cargados exitosamente
cat("✓ Dimensiones:", nrow(datos), "observaciones\n")
## ✓ Dimensiones: 2500 observaciones
#========================================================
# EXTRAER Y LIMPIAR VARIABLE
#========================================================

LONGITUDE_raw <- datos$DEPOSIT__1

LONGITUDE <- as.numeric(LONGITUDE_raw)

LONGITUDE <- LONGITUDE[!is.na(LONGITUDE)]

LONGITUDE <- LONGITUDE[LONGITUDE != 0]

n <- length(LONGITUDE)

n
## [1] 2500
#========================================================
# MEDIDAS DESCRIPTIVAS
#========================================================

media_longitud <- mean(LONGITUDE)
media_longitud
## [1] -128.669
mediana_longitud <- median(LONGITUDE)
mediana_longitud
## [1] -115.783
desviacion_longitud <- sd(LONGITUDE)
desviacion_longitud
## [1] 20.48554
varianza_longitud <- var(LONGITUDE)
varianza_longitud
## [1] 419.6575
minimo_longitud <- min(LONGITUDE)
minimo_longitud
## [1] -162.8379
maximo_longitud <- max(LONGITUDE)
maximo_longitud
## [1] -68.7448
rango_longitud <- maximo_longitud - minimo_longitud
rango_longitud
## [1] 94.0931
asimetria_longitud <- skewness(LONGITUDE)
asimetria_longitud
## [1] -0.3002175
curtosis_longitud <- kurtosis(LONGITUDE)
curtosis_longitud
## [1] -1.224926
#========================================================
# GRÁFICA Nº1
# DISTRIBUCIÓN DE LA LONGITUD
#========================================================

hist(LONGITUDE,
     probability = TRUE,
     breaks = 20,
     main = "Gráfica Nº1: Distribución de la longitud
     en análisis geoquímicos y geológicos de depósitos minerales",
     xlab = "Longitud (grados)",
     ylab = "Densidad",
     col = "gray",
     border = "black")

lines(density(LONGITUDE),
      col = "blue",
      lwd = 3)

abline(v = media_longitud,
       col = "red",
       lwd = 2,
       lty = 2)

abline(v = mediana_longitud,
       col = "darkgreen",
       lwd = 2,
       lty = 2)

legend("topright",
       legend = c(
         paste("Media =", round(media_longitud,2)),
         paste("Mediana =", round(mediana_longitud,2))
       ),
       col = c("red","darkgreen"),
       lty = 2,
       cex = 0.8)

#========================================================
# TEST DE NORMALIDAD
#========================================================

# Debido al gran tamaño muestral se toma una muestra

set.seed(123)

muestra_longitud <- sample(LONGITUDE,
                           size = 500)

normalidad_longitud <- shapiro.test(muestra_longitud)

normalidad_longitud
## 
##  Shapiro-Wilk normality test
## 
## data:  muestra_longitud
## W = 0.74694, p-value < 2.2e-16
#========================================================
# GRÁFICA Nº2
# DENSIDAD REAL DE LA DISTRIBUCIÓN
#========================================================

plot(density(LONGITUDE),
     lwd = 3,
     col = "blue",
     main = "Gráfica Nº2: Densidad real de la longitud
     en depósitos minerales",
     xlab = "Longitud",
     ylab = "Densidad")

polygon(density(LONGITUDE),
        col = rgb(0,0,1,0.3),
        border = "blue")

abline(v = mediana_longitud,
       col = "red",
       lwd = 2,
       lty = 2)

#========================================================
# GRÁFICA Nº3
# DIAGRAMA DE CAJA
#========================================================

boxplot(LONGITUDE,
        horizontal = TRUE,
        col = "skyblue",
        main = "Gráfica Nº3: Diagrama de caja de la longitud
        en depósitos minerales",
        xlab = "Longitud")

abline(v = mediana_longitud,
       col = "red",
       lwd = 2,
       lty = 2)

#========================================================
# ÍNDICE DE ASIMETRÍA
#========================================================

diferencia_central <- abs(media_longitud - mediana_longitud)

indice_asimetria <- (diferencia_central/abs(media_longitud))*100

indice_asimetria
## [1] 10.01488
#========================================================
# TABLA RESUMEN
#========================================================

Variable <- c("Longitud")

tabla_resumen <- data.frame(
  Variable,
  round(media_longitud,2),
  round(mediana_longitud,2),
  round(desviacion_longitud,2),
  round(varianza_longitud,2),
  round(asimetria_longitud,2),
  round(curtosis_longitud,2),
  round(indice_asimetria,2),
  round(normalidad_longitud$p.value,5)
)

colnames(tabla_resumen) <- c(
  "Variable",
  "Media",
  "Mediana",
  "Desviación estándar",
  "Varianza",
  "Asimetría",
  "Curtosis",
  "Índice de asimetría (%)",
  "p-valor Shapiro"
)

kable(tabla_resumen,
      format = "markdown",
      caption = "Tabla Nº1: Resumen estadístico de la longitud")
Tabla Nº1: Resumen estadístico de la longitud
Variable Media Mediana Desviación estándar Varianza Asimetría Curtosis Índice de asimetría (%) p-valor Shapiro
Longitud -128.67 -115.78 20.49 419.66 -0.3 -1.22 10.01 0
#========================================================
# PROBABILIDAD EMPÍRICA
#========================================================

# ¿Cuál es la probabilidad de que una nueva observación
# de longitud se encuentre entre -130 y -110?

longitud_rango <- LONGITUDE[
  LONGITUDE >= -130 &
  LONGITUDE <= -110
]

Probabilidad_1 <- (length(longitud_rango)/length(LONGITUDE))*100

Probabilidad_1
## [1] 57.72