Carga de Datos

library(readr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
options(scipen = 999)
archivo<-"C:\\Users\\corte\\Desktop\\practica_1_ejercicio.csv"
datos<-read_csv(file = archivo)
## Parsed with column specification:
## cols(
##   Y = col_double(),
##   X1 = col_double(),
##   X2 = col_double()
## )
Y_<-datos %>% select("Y") %>% as.matrix()->mat_y
print(mat_y)
##        Y
## [1,]  90
## [2,] 100
## [3,] 110
## [4,] 135
## [5,] 145
## [6,] 165
X_<-datos %>% mutate(Cte=1) %>% select("Cte","X1","X2") %>% as.matrix()->mat_x 
print(mat_x)
##      Cte X1 X2
## [1,]   1  1  0
## [2,]   1  2  0
## [3,]   1  3  0
## [4,]   1  4  1
## [5,]   1  5  1
## [6,]   1  6  1

Calcule el producto escalar de la primera y segunda columnas de X.

columna_1<-as.matrix(mat_x[,1])
columna_2<-as.matrix(mat_x[,2])
Producto_escalar<-t(columna_1)%*%columna_2
print(Producto_escalar)
##      [,1]
## [1,]   21

Calcule X′X y X′Y.

XX<-t(mat_x)%*%mat_x
print(XX)
##     Cte X1 X2
## Cte   6 21  3
## X1   21 91 15
## X2    3 15  3
XY<-t(mat_x)%*%mat_y %>% as.matrix()
print(XY)
##        Y
## Cte  745
## X1  2875
## X2   445

Obtenga la inversa de X′X.

XX_inv<- solve(XX)
print(XX_inv)
##           Cte    X1        X2
## Cte  1.333333 -0.50  1.166667
## X1  -0.500000  0.25 -0.750000
## X2   1.166667 -0.75  2.916667

Calcule el producto de [X′X]^−1 y X′Y

productoinversa<-t(XX_inv)%*%XY
print(productoinversa)
##            Y
## Cte 75.00000
## X1  12.50000
## X2  10.83333

Calcule la matriz de proyección P compruebe que P es una matriz idempotente

mat_proy<-mat_x %*% XX_inv %*% t(mat_x)
print(mat_proy)
##                           [,1]                     [,2]
## [1,]  0.5833333333333332593185 0.3333333333333333148296
## [2,]  0.3333333333333333148296 0.3333333333333332593185
## [3,]  0.0833333333333333703408 0.3333333333333332593185
## [4,]  0.2500000000000001110223 0.0000000000000002220446
## [5,]  0.0000000000000002220446 0.0000000000000002220446
## [6,] -0.2499999999999997779554 0.0000000000000002220446
##                           [,3]                      [,4]
## [1,]  0.0833333333333333703408  0.2499999999999997779554
## [2,]  0.3333333333333332038073 -0.0000000000000001110223
## [3,]  0.5833333333333331482962 -0.2500000000000000000000
## [4,] -0.2499999999999996669331  0.5833333333333334813631
## [5,]  0.0000000000000002220446  0.3333333333333337034077
## [6,]  0.2500000000000002220446  0.0833333333333337034077
##                           [,5]                      [,6]
## [1,] -0.0000000000000001110223 -0.2500000000000001110223
## [2,] -0.0000000000000001665335 -0.0000000000000002220446
## [3,]  0.0000000000000000000000  0.2499999999999997779554
## [4,]  0.3333333333333334813631  0.0833333333333337034077
## [5,]  0.3333333333333337034077  0.3333333333333337034077
## [6,]  0.3333333333333337034077  0.5833333333333337034077

Calcule la proyección de y sobre X, Yˆ = PY

proyec_y<-mat_proy%*%mat_y
print(proyec_y)
##             Y
## [1,]  87.5000
## [2,] 100.0000
## [3,] 112.5000
## [4,] 135.8333
## [5,] 148.3333
## [6,] 160.8333

Calcule la diferencia entre Y e Yˆ

difY_Y<-proyec_y-mat_y
print(difY_Y)
##                           Y
## [1,] -2.5000000000001136868
## [2,] -0.0000000000001136868
## [3,]  2.4999999999999431566
## [4,]  0.8333333333335133375
## [5,]  3.3333333333335701809
## [6,] -4.1666666666664582408

Obtenga los autovalores de la matriz X′X ¿Son todos positivos? ¿Porqué? R//Todos los autovalores son positivos porque los elementos de la matriz XX son positivos y porque es una matriz simetrica

autovalores_XX<-eigen(XX)
print(autovalores_XX)
## eigen() decomposition
## $values
## [1] 98.356654  1.377669  0.265677
## 
## $vectors
##            [,1]       [,2]       [,3]
## [1,] -0.2238107  0.8591615  0.4601633
## [2,] -0.9616881 -0.1179798 -0.2474608
## [3,] -0.1583188 -0.4979179  0.8526505

Obtenga los autovalores de la matriz P. Compruebe que la traza de P es igual a la suma de sus autovalores

auto_matP=eigen(mat_proy)
print(auto_matP)
## eigen() decomposition
## $values
## [1]  1.0000000000000015543122  0.9999999999999996669331
## [3]  0.9999999999999995559108  0.0000000000000013199503
## [5]  0.0000000000000001381689 -0.0000000000000025535006
## 
## $vectors
##            [,1]        [,2]        [,3]        [,4]        [,5]
## [1,] 0.17027481  0.55319973 -0.49830702 -0.01465309  0.64533088
## [2,] 0.11959527  0.05982443 -0.56165055  0.50484213 -0.50503474
## [3,] 0.06891573 -0.43355086 -0.62499408 -0.49018903 -0.14029614
## [4,] 0.61246753  0.42129574  0.17529049  0.01769475 -0.38703946
## [5,] 0.56178799 -0.07207956  0.11194696 -0.51092544 -0.01154811
## [6,] 0.51110845 -0.56545485  0.04860342  0.49323069  0.39858757
##                [,6]
## [1,]  0.00005384555
## [2,] -0.39590529389
## [3,]  0.39585144835
## [4,]  0.51628869734
## [5,] -0.63677979188
## [6,]  0.12049109454

Obtenga los autovalores de la matriz I−P y I+P

id=diag(1, nrow = 6)
print(id)
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1    0    0    0    0    0
## [2,]    0    1    0    0    0    0
## [3,]    0    0    1    0    0    0
## [4,]    0    0    0    1    0    0
## [5,]    0    0    0    0    1    0
## [6,]    0    0    0    0    0    1
mat_idP=id-mat_proy
print(mat_idP)
##                           [,1]                      [,2]
## [1,]  0.4166666666666667406815 -0.3333333333333333148296
## [2,] -0.3333333333333333148296  0.6666666666666667406815
## [3,] -0.0833333333333333703408 -0.3333333333333332593185
## [4,] -0.2500000000000001110223 -0.0000000000000002220446
## [5,] -0.0000000000000002220446 -0.0000000000000002220446
## [6,]  0.2499999999999997779554 -0.0000000000000002220446
##                           [,3]                      [,4]
## [1,] -0.0833333333333333703408 -0.2499999999999997779554
## [2,] -0.3333333333333332038073  0.0000000000000001110223
## [3,]  0.4166666666666668517038  0.2500000000000000000000
## [4,]  0.2499999999999996669331  0.4166666666666665186369
## [5,] -0.0000000000000002220446 -0.3333333333333337034077
## [6,] -0.2500000000000002220446 -0.0833333333333337034077
##                           [,5]                      [,6]
## [1,]  0.0000000000000001110223  0.2500000000000001110223
## [2,]  0.0000000000000001665335  0.0000000000000002220446
## [3,]  0.0000000000000000000000 -0.2499999999999997779554
## [4,] -0.3333333333333334813631 -0.0833333333333337034077
## [5,]  0.6666666666666662965923 -0.3333333333333337034077
## [6,] -0.3333333333333337034077  0.4166666666666662965923
autovalores_idP=eigen(mat_idP)
print(autovalores_idP)
## eigen() decomposition
## $values
## [1]  1.0000000000000002220446  0.9999999999999998889777
## [3]  0.9999999999999994448885  0.0000000000000006298480
## [5]  0.0000000000000002415019 -0.0000000000000008479877
## 
## $vectors
##              [,1]        [,2]         [,3]        [,4]       [,5]
## [1,]  0.011460289  0.48745947 -0.422987696 -0.08688822  0.6790624
## [2,]  0.004197312  0.02426751  0.816125074 -0.39233960  0.2839582
## [3,] -0.015657601 -0.51172699 -0.393137379 -0.69779098 -0.1111460
## [4,]  0.394533813 -0.51072167  0.013157252  0.53997696  0.1840166
## [5,] -0.816185516  0.02225688  0.003535813  0.23452558 -0.2110876
## [6,]  0.421651703  0.48846479 -0.016693065 -0.07092580 -0.6061918
##           [,6]
## [1,] 0.3386119
## [2,] 0.3142781
## [3,] 0.2899442
## [4,] 0.5078347
## [5,] 0.4835009
## [6,] 0.4591670
autovalores_idP$values
## [1]  1.0000000000000002220446  0.9999999999999998889777
## [3]  0.9999999999999994448885  0.0000000000000006298480
## [5]  0.0000000000000002415019 -0.0000000000000008479877
mat_idP=id+mat_proy
print(mat_idP)
##                           [,1]                     [,2]
## [1,]  1.5833333333333332593185 0.3333333333333333148296
## [2,]  0.3333333333333333148296 1.3333333333333332593185
## [3,]  0.0833333333333333703408 0.3333333333333332593185
## [4,]  0.2500000000000001110223 0.0000000000000002220446
## [5,]  0.0000000000000002220446 0.0000000000000002220446
## [6,] -0.2499999999999997779554 0.0000000000000002220446
##                           [,3]                      [,4]
## [1,]  0.0833333333333333703408  0.2499999999999997779554
## [2,]  0.3333333333333332038073 -0.0000000000000001110223
## [3,]  1.5833333333333330372739 -0.2500000000000000000000
## [4,] -0.2499999999999996669331  1.5833333333333334813631
## [5,]  0.0000000000000002220446  0.3333333333333337034077
## [6,]  0.2500000000000002220446  0.0833333333333337034077
##                           [,5]                      [,6]
## [1,] -0.0000000000000001110223 -0.2500000000000001110223
## [2,] -0.0000000000000001665335 -0.0000000000000002220446
## [3,]  0.0000000000000000000000  0.2499999999999997779554
## [4,]  0.3333333333333334813631  0.0833333333333337034077
## [5,]  1.3333333333333337034077  0.3333333333333337034077
## [6,]  0.3333333333333337034077  1.5833333333333337034077
autovalores_idP=eigen(mat_idP)
print(autovalores_idP)
## eigen() decomposition
## $values
## [1] 2 2 2 1 1 1
## 
## $vectors
##            [,1]       [,2]        [,3]         [,4]         [,5]
## [1,] -0.1138717  0.3811271  0.65200363 -0.078635361  0.640687425
## [2,] -0.1845675 -0.1004822  0.53774668 -0.564835333 -0.589599590
## [3,] -0.2552633 -0.5820914  0.42348974  0.643470694 -0.051087835
## [4,] -0.4702333  0.5998184 -0.04931457  0.360195210 -0.344932322
## [5,] -0.5409291  0.1182091 -0.16357151  0.001715635 -0.001910616
## [6,] -0.6116249 -0.3634001 -0.27782846 -0.361910845  0.346842938
##               [,6]
## [1,] -0.0016644573
## [2,]  0.0001928336
## [3,]  0.0014716238
## [4,]  0.4098143121
## [5,] -0.8164925430
## [6,]  0.4066782310