Last active
January 1, 2017 02:09
-
-
Save ytarazona/fa6af9d29245f73867625dc4ed2c9652 to your computer and use it in GitHub Desktop.
Matriz de Covarianza
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Simulamos una matriz de un raster con filas y columnas | |
M <- matrix(c(11:14,9:12,21:24,1:4),4,4) # Raster convertido a matriz | |
i <- ncol(M); j <- nrow(M) # número de columnas y filas | |
# Crear las medias para cada columna | |
M_mean <- matrix(data=1, nrow=i) %*% cbind(mean(M[,1]),mean(M[,2]),mean(M[,3]),mean(M[,4])) | |
# Creamos una diferencia de Matriz | |
DIF <- M - M_mean | |
# Obtenemos la matriz de covarianza | |
COV <- (i-1)^-1*t(DIF) %*% DIF | |
COV | |
[,1] [,2] [,3] [,4] | |
[1,] 1.666667 1.666667 1.666667 1.666667 | |
[2,] 1.666667 1.666667 1.666667 1.666667 | |
[3,] 1.666667 1.666667 1.666667 1.666667 | |
[4,] 1.666667 1.666667 1.666667 1.666667 | |
# Podemos calcular directamente la matriz de covarianza con la siguiente función | |
COV <- cov(M) | |
# Calculamos los valores propios y vectores propios | |
COV_eigen <- eigen(COV) | |
COV_eigen | |
$values | |
[1] 6.666667e+00 2.664535e-15 2.220446e-16 -4.440892e-16 | |
$vectors | |
[,1] [,2] [,3] [,4] | |
[1,] -0.5 0.8660254 0.0000000 0.0000000 | |
[2,] -0.5 -0.2886751 -0.5773503 -0.5773503 | |
[3,] -0.5 -0.2886751 0.7886751 -0.2113249 | |
[4,] -0.5 -0.2886751 -0.2113249 0.7886751 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment