# install.packages("faux")
# install.packages("psych")
library(faux)
library(psych)
set.seed(1014979601)
peach = rnorm_multi(
n = 120,
vars = 2,
mu = c(4, 4.5),
sd = c(0.4, 0.5),
varnames = c("DE", "DL"),
r = 0.7
)
head(peach)
## DE DL
## 1 3.842986 4.494950
## 2 4.394231 4.786908
## 3 3.670177 4.618484
## 4 4.037536 4.520406
## 5 4.186921 4.273256
## 6 4.161558 3.907679
xbar <- colMeans(peach)
s <- apply(peach, 2, sd)
rango <- apply(peach, 2, function(x) max(x) - min(x))
minimo <- apply(peach, 2, min)
maximo <- apply(peach, 2, max)
suma <- colSums(peach)
norma <- apply(peach, 2, function(x) sqrt(sum(x^2)))
# 1 Z-score
z1 <- scale(peach, center = TRUE, scale = TRUE)
# 2 (x - media)/rango
z2 <- sweep(peach, 2, xbar, "-")
z2 <- sweep(z2, 2, rango, "/")
# 3 (x - min)/rango (Min-Max)
z3 <- sweep(peach, 2, minimo, "-")
z3 <- sweep(z3, 2, rango, "/")
# 4 x / sd
z4 <- sweep(peach, 2, s, "/")
# 5 x / rango
z5 <- sweep(peach, 2, rango, "/")
# 6 x / max
z6 <- sweep(peach, 2, maximo, "/")
# 7 x / media
z7 <- sweep(peach, 2, xbar, "/")
# 8 x / suma
z8 <- sweep(peach, 2, suma, "/")
# 9 x / norma L2
z9 <- sweep(peach, 2, norma, "/")
resumen <- function(x){
data.frame(
Media = colMeans(x),
Desviacion = apply(x, 2, sd),
Rango = apply(x, 2, function(y) max(y)-min(y))
)
}
datos_lista <- list(
Original = peach,
Z_score = z1,
Media_Rango = z2,
Min_Max = z3,
Cociente_SD = z4,
Cociente_Rango = z5,
Cociente_Max = z6,
Cociente_Media = z7,
Cociente_Suma = z8,
Cociente_NormaL2 = z9
)
resultados <- lapply(datos_lista, resumen)
resultados
## $Original
## Media Desviacion Rango
## DE 4.001881 0.3880766 1.828890
## DL 4.491905 0.4514219 2.293975
##
## $Z_score
## Media Desviacion Rango
## DE 7.758551e-16 1 4.712702
## DL -5.302038e-16 1 5.081665
##
## $Media_Rango
## Media Desviacion Rango
## DE 1.637434e-16 0.2121925 1
## DL -1.039136e-16 0.1967859 1
##
## $Min_Max
## Media Desviacion Rango
## DE 0.4492247 0.2121925 1
## DL 0.5278036 0.1967859 1
##
## $Cociente_SD
## Media Desviacion Rango
## DE 10.312089 1 4.712702
## DL 9.950569 1 5.081665
##
## $Cociente_Rango
## Media Desviacion Rango
## DE 2.188148 0.2121925 1
## DL 1.958132 0.1967859 1
##
## $Cociente_Max
## Media Desviacion Rango
## DE 0.7989081 0.07747296 0.3651070
## DL 0.8057067 0.08097092 0.4114671
##
## $Cociente_Media
## Media Desviacion Rango
## DE 1 0.09697356 0.4570075
## DL 1 0.10049676 0.5106909
##
## $Cociente_Suma
## Media Desviacion Rango
## DE 0.008333333 0.000808113 0.003808396
## DL 0.008333333 0.000837473 0.004255758
##
## $Cociente_NormaL2
## Media Desviacion Rango
## DE 0.09086440 0.008811444 0.04152571
## DL 0.09083336 0.009128459 0.04638777
par(mfrow=c(2,2))
plot(peach, main="Datos Originales")
plot(z1, main="Z-score")
plot(z2, main="Media_Rango")
plot(z3, main="Min-Max")
plot(z4, main="Cociente_SD")
plot(z5, main="Cociente_Rango")
plot(z6, main="Cociente_Max")
plot(z7, main="Cociente Media")
plot(z8, main="Cociente_Suma")
plot(z9, main="Cociente_NormaL2")
# Función para calcular correlación de Pearson
cor_pearson <- function(x){
cor(x, method = "pearson")
}
correlaciones_pearson <- lapply(datos_lista, cor_pearson)
correlaciones_pearson
## $Original
## DE DL
## DE 1.0000000 0.6145947
## DL 0.6145947 1.0000000
##
## $Z_score
## DE DL
## DE 1.0000000 0.6145947
## DL 0.6145947 1.0000000
##
## $Media_Rango
## DE DL
## DE 1.0000000 0.6145947
## DL 0.6145947 1.0000000
##
## $Min_Max
## DE DL
## DE 1.0000000 0.6145947
## DL 0.6145947 1.0000000
##
## $Cociente_SD
## DE DL
## DE 1.0000000 0.6145947
## DL 0.6145947 1.0000000
##
## $Cociente_Rango
## DE DL
## DE 1.0000000 0.6145947
## DL 0.6145947 1.0000000
##
## $Cociente_Max
## DE DL
## DE 1.0000000 0.6145947
## DL 0.6145947 1.0000000
##
## $Cociente_Media
## DE DL
## DE 1.0000000 0.6145947
## DL 0.6145947 1.0000000
##
## $Cociente_Suma
## DE DL
## DE 1.0000000 0.6145947
## DL 0.6145947 1.0000000
##
## $Cociente_NormaL2
## DE DL
## DE 1.0000000 0.6145947
## DL 0.6145947 1.0000000
# Función para calcular correlación de Spearman
cor_spearman <- function(x){
cor(x, method = "spearman")
}
correlaciones_spearman <- lapply(datos_lista, cor_spearman)
correlaciones_spearman
## $Original
## DE DL
## DE 1.0000000 0.6007362
## DL 0.6007362 1.0000000
##
## $Z_score
## DE DL
## DE 1.0000000 0.6007362
## DL 0.6007362 1.0000000
##
## $Media_Rango
## DE DL
## DE 1.0000000 0.6007362
## DL 0.6007362 1.0000000
##
## $Min_Max
## DE DL
## DE 1.0000000 0.6007362
## DL 0.6007362 1.0000000
##
## $Cociente_SD
## DE DL
## DE 1.0000000 0.6007362
## DL 0.6007362 1.0000000
##
## $Cociente_Rango
## DE DL
## DE 1.0000000 0.6007362
## DL 0.6007362 1.0000000
##
## $Cociente_Max
## DE DL
## DE 1.0000000 0.6007362
## DL 0.6007362 1.0000000
##
## $Cociente_Media
## DE DL
## DE 1.0000000 0.6007362
## DL 0.6007362 1.0000000
##
## $Cociente_Suma
## DE DL
## DE 1.0000000 0.6007362
## DL 0.6007362 1.0000000
##
## $Cociente_NormaL2
## DE DL
## DE 1.0000000 0.6007362
## DL 0.6007362 1.0000000
La correlación de Pearson se define como:
\[ \rho_{XY} = \frac{\operatorname{Cov}(X,Y)} {\sigma_X \sigma_Y} \]
Si aplicamos una transformación lineal positiva:
\[ Z_X = aX + b \] \[ Z_Y = cY + d \]
Entonces:
\[ \operatorname{Cov}(Z_X,Z_Y) = ac\,\operatorname{Cov}(X,Y) \]
y las desviaciones estándar cambian como:
\[ \sigma_{Z_X} = a\sigma_X \] \[ \sigma_{Z_Y} = c\sigma_Y \]
La nueva correlación es:
\[ \rho_{Z_X Z_Y} = \frac{ac\,\operatorname{Cov}(X,Y)} {(a\sigma_X)(c\sigma_Y)} \]
Cancelando términos:
\[ \rho_{Z_X Z_Y} = \frac{\operatorname{Cov}(X,Y)} {\sigma_X \sigma_Y} = \rho_{XY} \]
Conclusión:
La correlación de Pearson no cambia bajo transformaciones lineales con
\(a>0\).
cor(peach, method="pearson")
## DE DL
## DE 1.0000000 0.6145947
## DL 0.6145947 1.0000000
cor(z1, method="pearson")
## DE DL
## DE 1.0000000 0.6145947
## DL 0.6145947 1.0000000
cor(z3, method="pearson")
## DE DL
## DE 1.0000000 0.6145947
## DL 0.6145947 1.0000000
Observamos que todas son iguales.
Spearman se define como la correlación entre los rangos:
\[ \rho_s = \rho(\text{rangos}(X), \text{rangos}(Y)) \]
Si aplicamos:
\[ Z = aX + b \]
con \(a>0\), el orden no cambia.
Si antes:
\[ X_1 < X_2 \]
después:
\[ aX_1 + b < aX_2 + b \]
Por tanto, los rangos son exactamente los mismos.
cor(peach, method="spearman")
## DE DL
## DE 1.0000000 0.6007362
## DL 0.6007362 1.0000000
cor(z1, method="spearman")
## DE DL
## DE 1.0000000 0.6007362
## DL 0.6007362 1.0000000
cor(z3, method="spearman")
## DE DL
## DE 1.0000000 0.6007362
## DL 0.6007362 1.0000000
Las correlaciones son iguales.
Si multiplicamos por un número negativo:
\[ Z = -X \]
El orden se invierte.
z_neg <- -peach
cor(z_neg, method="pearson")
## DE DL
## DE 1.0000000 0.6145947
## DL 0.6145947 1.0000000
cor(z_neg, method="spearman")
## DE DL
## DE 1.0000000 0.6007362
## DL 0.6007362 1.0000000
Aquí la correlación cambia de signo.
Todas las estandarizaciones aplicadas cumplen:
\[ Z = aX + b \quad (a>0) \]
Por eso:
Todos los métodos usados pertenecen a la forma general:
\[ Z = aX + b \quad (a>0) \]
Esto significa que todos son transformaciones lineales positivas.
Consecuencia importante:
\[ Z = \frac{X - \bar X}{s} \]
Propiedades:
Se usa cuando interesa igualar la variabilidad.
\[ Z = \frac{X - \bar X}{r} \]
\[ Z = \frac{X - \min(X)}{r} \]
Muy usada en índices compuestos.
Tienen forma:
\[ Z = \frac{X}{c} \]
No restan media.
\[ Z = \frac{X}{s} \]
\[ Z = \frac{X}{r} \]
\[ Z = \frac{X}{\max(X)} \]
\[ Z = \frac{X}{\bar X} \]
\[ Z = \frac{X}{\sum X} \]
\[ Z = \frac{X}{\sqrt{\sum X^2}} \]
Todos los métodos:
| Método | Centra en 0 | Fija SD=1 | Fija Rango=1 | Fija Media=1 |
|---|---|---|---|---|
| Z-score | ✔ | ✔ | ✘ | ✘ |
| Media/Rango | ✔ | ✘ | ✔ | ✘ |
| Min-Max | ✘ | ✘ | ✔ | ✘ |
| Cociente SD | ✘ | ✔ | ✘ | ✘ |
| Cociente Rango | ✘ | ✘ | ✔ | ✘ |
| Cociente Media | ✘ | ✘ | ✘ | ✔ |
| Cociente Suma | ✘ | ✘ | ✘ | Proporción |
| Norma L2 | ✘ | ✘ | ✘ | Norma=1 |
Restan media:
\[ X - \bar X \]
Ejemplo: Z-score.
Solo dividen:
\[ \frac{X}{c} \]
Ejemplo: cociente media.
Todos los métodos:
\[ Z = aX + b \]
pero difieren en:
La elección del método depende del objetivo del análisis y de la escala de medición.