Practica con matrices

Creacion de matrices

# Variable dependiente
matriz_Y<-matrix(data = c(30,20,36,24,40),
          nrow = 5,
          ncol = 1,byrow = TRUE)
colnames(matriz_Y) <-c("matriz_Y")
print(matriz_Y)
##      matriz_Y
## [1,]       30
## [2,]       20
## [3,]       36
## [4,]       24
## [5,]       40
# Variables Explicativas y La columna del termino independiente

matriz_X <- cbind(rep(x = 1, 5),
          matrix(data = c(4, 10, 3, 8, 6, 11, 4, 9,8, 12),
                 nrow = 5,
                 ncol = 2,
                 byrow = TRUE))
colnames(matriz_X) <-c("Cte", "X1", "X2")
print(matriz_X)
##      Cte X1 X2
## [1,]   1  4 10
## [2,]   1  3  8
## [3,]   1  6 11
## [4,]   1  4  9
## [5,]   1  8 12

Importando los datos de un archivo csv

library(readr)
## Warning: package 'readr' was built under R version 4.4.2
practica_1 <- read_csv("C:/Users/PBFCIS-SPP-02/Downloads/practica_1.csv")
## Rows: 5 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (3): Y, X1, X2
## 
## ℹ 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.
print(practica_1)
## # A tibble: 5 × 3
##       Y    X1    X2
##   <dbl> <dbl> <dbl>
## 1    30     4    10
## 2    20     3     8
## 3    36     6    11
## 4    24     4     9
## 5    40     8    12

Calculando el producto de matrices

## Usando la matriz X & Y creadas anteriormente
# La siguiente operacion obtiene la matriz X'X
matriz_XX <- t(matriz_X) %*% matriz_X
print(matriz_XX)
##     Cte  X1  X2
## Cte   5  25  50
## X1   25 141 262
## X2   50 262 510
# La siguiente operacion obtiene la matriz X'Y
matriz_XY <- t(matriz_X) %*% matriz_Y 
print(matriz_XY)
##     matriz_Y
## Cte      150
## X1       812
## X2      1552

Calculando la inversa de matrices

XX_inv <- solve(matriz_XX)
print(XX_inv)
##        Cte     X1    X2
## Cte 40.825  4.375 -6.25
## X1   4.375  0.625 -0.75
## X2  -6.250 -0.750  1.00

Obtencion del Estimador MCO βˆ

# A traves del producto de la inversa de X’X, con la matriz X’Y
Beta <- XX_inv %*% matriz_XY
colnames(Beta) <- c("parametros")
print(Beta)
##     parametros
## Cte     -23.75
## X1       -0.25
## X2        5.50
# A traves de la solucion de ecuaciones de las normales, usando el comando solve 
Beta_ <- solve(a= matriz_XX, b= matriz_XY )
colnames(Beta_) <- c("parametros")
print(Beta_)
##     parametros
## Cte     -23.75
## X1       -0.25
## X2        5.50

Obtencion de autovalores y autovectores

autovalores <- eigen(matriz_XX)
# Muestra los autovalores y los autovectores
print(autovalores)
## eigen() decomposition
## $values
## [1] 650.78185037   5.19448432   0.02366531
## 
## $vectors
##             [,1]       [,2]       [,3]
## [1,] -0.08623239  0.1629390  0.9828606
## [2,] -0.45874789 -0.8822205  0.1060061
## [3,] -0.88437229  0.4417441 -0.1508239
# Muestra solo los autovalores
print(autovalores$values)
## [1] 650.78185037   5.19448432   0.02366531