Paquete para graficación 3D.

library(plot3D)

Coordenadas de un objeto en 3D

# Casa en 3D
casa_3D <- matrix(c(2, 4, 0,
         2, 0, 0,
         0, 0, 0,
         0, 4, 0,
         2, 4, 2,
         2, 0, 2,
         0, 0, 2,
         0, 4, 2,
         1, 0, 3.5,
         1, 4, 3.5,
         0.5, 0, 0.5,
         1.5, 0, 0.5,
         0.5, 0, 0,
         1.5, 0, 0,
         2, 1, 1,
         2, 1, 1.5,
         2, 2, 1,
         2, 2, 1.5), 18, 3, byrow = TRUE)

# Asigna letras a cada punto.
dimnames(casa_3D) <- list(LETTERS[1:nrow(casa_3D)], c("x", "y", "z"))

Gráfica en 2D del objeto original (plano x, y).

plot(casa_3D[, c(1, 2)], pch = 19, asp = 1)

plot(casa_3D[, c(2, 3)], pch = 19, asp = 1)

plot(casa_3D[, c(1, 3)], pch = 19, asp = 1)

Gráfica en 3D del objeto original.

scatter3D(casa_3D[, 1], casa_3D[, 2], casa_3D[, 3], pch = 19, asp = 1)

centroide <- colMeans(casa_3D)
casa_3D_centrada <- t(t(casa_3D) - centroide)
casa_3D_centrada
##            x          y          z
## A  0.7777778  2.5555556 -1.1666667
## B  0.7777778 -1.4444444 -1.1666667
## C -1.2222222 -1.4444444 -1.1666667
## D -1.2222222  2.5555556 -1.1666667
## E  0.7777778  2.5555556  0.8333333
## F  0.7777778 -1.4444444  0.8333333
## G -1.2222222 -1.4444444  0.8333333
## H -1.2222222  2.5555556  0.8333333
## I -0.2222222 -1.4444444  2.3333333
## J -0.2222222  2.5555556  2.3333333
## K -0.7222222 -1.4444444 -0.6666667
## L  0.2777778 -1.4444444 -0.6666667
## M -0.7222222 -1.4444444 -1.1666667
## N  0.2777778 -1.4444444 -1.1666667
## O  0.7777778 -0.4444444 -0.1666667
## P  0.7777778 -0.4444444  0.3333333
## Q  0.7777778  0.5555556 -0.1666667
## R  0.7777778  0.5555556  0.3333333

Gráfica en 3D del objeto original centrado.

scatter3D(casa_3D_centrada[,1], 
              casa_3D_centrada[, 2], 
              casa_3D_centrada[, 3],
              pch = 19, 
              asp = 1)

Adición de los ejes de coordenadas.

ejes <- matrix(c(-1.5,    0,    0, 1.5,   0,   0,
                    0, -1.5,    0,   0, 1.5,   0,
                    0,    0, -1.5,   0,   0, 1.5),
                    byrow = TRUE, 
                    3, 6)
border3D(-2.6, -2.6, -2.6, 2.6, 2.6, 2.6,
         theta = 5,
         phi = 20,
         r = 10)
scatter3D(casa_3D_centrada[,1], 
          casa_3D_centrada[, 2], 
          casa_3D_centrada[, 3],
          pch = 19, 
          asp = 1, 
          add = TRUE)
segments3D(ejes[ ,1], ejes[ ,2], ejes[ ,3],
           ejes[ ,4], ejes[ ,5], ejes[ ,6],
           add = TRUE, 
           col = "red")

nuevas_coord <- matrix(c(1, 1, 1,
                         -1, 2, 1,
                          1, 2, 3),
                       3, 3,
                       byrow = TRUE,)
## Verificación si los vectores son linealmente independientes
det(nuevas_coord)
## [1] 4
## Proceso de Gram-Schmidt para generar una base ortogonal 
## a partir de vectores linealmente independientes.
v1 <- nuevas_coord[1, ]
v2 <- nuevas_coord[2, ]
v3 <- nuevas_coord[3, ]
w1 <- v1
w2 <- v2 - as.numeric((v2%*%w1)/(w1%*%w1)) * w1
w3 <- v3 - as.numeric((v3%*%w1)/(w1%*%w1)) * w1 - as.numeric((v3%*%w2)/(w2%*%w2)) * w2
base_ortogonal <- rbind(w1, w2, w3)

base_ortogonal
##          [,1]       [,2]      [,3]
## w1  1.0000000  1.0000000 1.0000000
## w2 -1.6666667  1.3333333 0.3333333
## w3 -0.2857143 -0.5714286 0.8571429
scatter3D(base_ortogonal[1, ],
          base_ortogonal[2, ],
          base_ortogonal[3, ],
          asp = 1,
          pch = 19)

segmentos <- matrix(c(-1.5, 0, 0, 1.5, 0, 0,
                    0, -1.5, 0, 0, 1.5, 0,
                    0,  0, -1.5, 0, 0, 1.5),
                    byrow = TRUE, 
                    3, 6)
border3D(-1.5, -1.5, -1.5, 1.5, 1.5, 1.5, col = "darkgreen", r = 10,
         theta = 40, phi = 10)
arrows3D(c(0, 0, 0), c(0, 0, 0), c(0, 0, 0), 
         base_ortogonal[1, ], base_ortogonal[2, ], base_ortogonal[3, ],
         add = TRUE)
segments3D(segmentos[ ,1], segmentos[ ,2], segmentos[ ,3],
           segmentos[ ,4], segmentos[ ,5], segmentos[ ,6],
           add = TRUE, 
           col = "red")

var(casa_3D)
##            x         y          z
## x 0.71241830 0.0130719 0.01960784
## y 0.01307190 3.0849673 0.42156863
## z 0.01960784 0.4215686 1.35294118
var(casa_3D_centrada)
##            x         y          z
## x 0.71241830 0.0130719 0.01960784
## y 0.01307190 3.0849673 0.42156863
## z 0.01960784 0.4215686 1.35294118
val_vec_prop <- eigen(var(casa_3D_centrada))
val_vec_prop
## eigen() decomposition
## $values
## [1] 3.1822444 1.2562638 0.7118186
## 
## $vectors
##              [,1]        [,2]          [,3]
## [1,] -0.006940592  0.02971358  9.995344e-01
## [2,] -0.974418891 -0.22473944 -8.526996e-05
## [3,] -0.224632262  0.97396575 -3.051330e-02

Valores propios

val_vec_prop$values
## [1] 3.1822444 1.2562638 0.7118186

Vectores propios

val_vec_prop$vectors
##              [,1]        [,2]          [,3]
## [1,] -0.006940592  0.02971358  9.995344e-01
## [2,] -0.974418891 -0.22473944 -8.526996e-05
## [3,] -0.224632262  0.97396575 -3.051330e-02

Operaciones con matrices y vectores (“tips”)

Definción de un vector.

v1 <- c(3, 4, 2, 6)
v1
## [1] 3 4 2 6

Definición de matrices.

m1 <- matrix(c(3, 1, 0, 2,
               0, -1, 2, 0), 
             nrow = 4, 
             ncol = 2)
m1
##      [,1] [,2]
## [1,]    3    0
## [2,]    1   -1
## [3,]    0    2
## [4,]    2    0
m2 <- matrix(c(3, 1, 0, 2,
               0, -1, 2, 0),
             nrow = 4, 
             ncol = 2, 
             byrow = TRUE)
m2
##      [,1] [,2]
## [1,]    3    1
## [2,]    0    2
## [3,]    0   -1
## [4,]    2    0

Transpuesta de una matriz.

t(m1)
##      [,1] [,2] [,3] [,4]
## [1,]    3    1    0    2
## [2,]    0   -1    2    0

División de matrices por un vector.

m1/v1
##           [,1]  [,2]
## [1,] 1.0000000  0.00
## [2,] 0.2500000 -0.25
## [3,] 0.0000000  1.00
## [4,] 0.3333333  0.00
t(m1)/v1
##      [,1]       [,2] [,3] [,4]
## [1,]    1  0.5000000  0.0    1
## [2,]    0 -0.1666667  0.5    0
t(t(m1)/v1)
##      [,1]       [,2]
## [1,]  1.0  0.0000000
## [2,]  0.5 -0.1666667
## [3,]  0.0  0.5000000
## [4,]  1.0  0.0000000
t(m1)%*%v1
##      [,1]
## [1,]   25
## [2,]    0