Hacer análisis correlacional
library(readr)
library(dplyr)
# Cargar las librerías
library(Hmisc) # Para correlaciones
library(corrplot) # Para visualización de correlaciones
library(GGally) # Otra opción para gráficos de correlación
library(PerformanceAnalytics) # Para coorelaciones gráficas
library(psych)
datos = read.csv("https://raw.githubusercontent.com/rpizarrog/analitica_de_datos/refs/heads/main/datos/datos_kep.csv")
datos$sexo <- factor(datos$sexo, levels = c("F", "M"), labels = c("F", "M"))
n = nrow(datos)
nvars = ncol(datos)
describe(datos[c('peso', 'estatura', 'habitantes', 'kep')])
## vars n mean sd median trimmed mad min max range skew
## peso 1 9 70.44 21.63 62.00 70.44 19.27 49.00 114.00 65.00 0.66
## estatura 2 9 1.67 0.10 1.65 1.67 0.12 1.56 1.85 0.29 0.42
## habitantes 3 9 3.44 1.01 4.00 3.44 1.48 2.00 5.00 3.00 -0.19
## kep 4 9 168.56 83.38 170.00 168.56 29.65 87.00 373.00 286.00 1.45
## kurtosis se
## peso -0.86 7.21
## estatura -1.37 0.03
## habitantes -1.42 0.34
## kep 1.18 27.79
cor_matrix <- cor(datos[c('peso', 'estatura', 'habitantes', 'kep')], method = "pearson")
print(cor_matrix)
## peso estatura habitantes kep
## peso 1.0000000 0.6530805 -0.3635884 0.2592963
## estatura 0.6530805 1.0000000 0.3012636 0.6435844
## habitantes -0.3635884 0.3012636 1.0000000 0.1712103
## kep 0.2592963 0.6435844 0.1712103 1.0000000
# Calcular la matriz de correlaciones y los p-valores
rcorr_result <- rcorr(as.matrix(datos[c('peso', 'estatura', 'habitantes', 'kep')]), type = "pearson")
# Matriz de correlaciones
cor_matrix <- rcorr_result$r
# Matriz de p-valores
p_values <- rcorr_result$P
print (p_values)
## peso estatura habitantes kep
## peso NA 0.05649631 0.3361250 0.5004739
## estatura 0.05649631 NA 0.4308139 0.0614455
## habitantes 0.33612499 0.43081388 NA 0.6596212
## kep 0.50047394 0.06144550 0.6596212 NA
# Visualizar la matriz de correlaciones con corrplot
corrplot(cor_matrix, method = "circle", type = "upper", tl.col = "black", tl.srt = 45)
chart.Correlation(datos[c('peso', 'estatura', 'habitantes', 'kep')], histogram = TRUE)
F <- filter(datos, sexo == 'F')
F
## sexo peso estatura habitantes kep
## 1 F 61 1.56 3 87
## 2 F 62 1.57 2 172
## 3 F 52 1.60 3 120
## 4 F 49 1.65 5 170
## 5 F 49 1.63 4 150
M <- filter(datos, sexo == 'M')
M
## sexo peso estatura habitantes kep
## 1 M 114 1.74 2 173
## 2 M 82 1.69 4 170
## 3 M 83 1.78 4 102
## 4 M 82 1.85 4 373
describe(F)
## vars n mean sd median trimmed mad min max range skew
## sexo* 1 5 1.0 0.00 1.0 1.0 0.00 1.00 1.00 0.00 NaN
## peso 2 5 54.6 6.43 52.0 54.6 4.45 49.00 62.00 13.00 0.22
## estatura 3 5 1.6 0.04 1.6 1.6 0.04 1.56 1.65 0.09 0.09
## habitantes 4 5 3.4 1.14 3.0 3.4 1.48 2.00 5.00 3.00 0.19
## kep 5 5 139.8 36.17 150.0 139.8 32.62 87.00 172.00 85.00 -0.39
## kurtosis se
## sexo* NaN 0.00
## peso -2.22 2.87
## estatura -2.07 0.02
## habitantes -1.75 0.51
## kep -1.85 16.18
chart.Correlation(F[c('peso', 'estatura', 'habitantes', 'kep')], histogram = TRUE)
describe(M)
## vars n mean sd median trimmed mad min max range skew
## sexo* 1 4 2.00 0.00 2.00 2.00 0.00 2.00 2.00 0.00 NaN
## peso 2 4 90.25 15.84 82.50 90.25 0.74 82.00 114.00 32.00 0.75
## estatura 3 4 1.77 0.07 1.76 1.77 0.07 1.69 1.85 0.16 0.15
## habitantes 4 4 3.50 1.00 4.00 3.50 0.00 2.00 4.00 2.00 -0.75
## kep 5 4 204.50 117.02 171.50 204.50 52.63 102.00 373.00 271.00 0.57
## kurtosis se
## sexo* NaN 0.00
## peso -1.69 7.92
## estatura -1.99 0.03
## habitantes -1.69 0.50
## kep -1.77 58.51
chart.Correlation(M[c('peso', 'estatura', 'habitantes', 'kep')], histogram = TRUE)
\[ t_{IND} = \frac{\bar{F} - \bar{M}}{S_p\sqrt{\frac{1}{n_F} + \frac{1}{n_M}}} = \]
\[ s_p = \sqrt\frac{\sum(F-\bar{F})^2+\sum(M-\bar{M})^2}{gl} \]
gl = nvars - 2
sp <- sqrt((sum((F$peso-mean(F$peso))^2) + sum((M$peso-mean(M$peso))^2)) / gl)
numerador = mean(F$peso) - mean(M$peso)
denominador = sp * sqrt(1/nrow(F) + 1 / nrow(M))
numerador
## [1] -35.65
denominador
## [1] 11.73424
tIND = numerador / denominador
tIND
## [1] -3.038116
Aplicación de la t Student para muestras dependientes
datos <- data.frame('L' = c(3,2,3,2,4,3,5,4,3,4), 'M'=c(5,3,3,2,5,2,7,5,2,7))
datos
## L M
## 1 3 5
## 2 2 3
## 3 3 3
## 4 2 2
## 5 4 5
## 6 3 2
## 7 5 7
## 8 4 5
## 9 3 2
## 10 4 7
# Realizar la prueba t para muestras dependientes
resultado <- t.test(datos$L, datos$M, paired = TRUE)
print (resultado)
##
## Paired t-test
##
## data: datos$L and datos$M
## t = -1.9215, df = 9, p-value = 0.08684
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
## -1.7418111 0.1418111
## sample estimates:
## mean difference
## -0.8
Pendiente …
Pendiente …