#Ejercicio 1 (datos del ejemplo)

#Ingresando matriz X

options(scipen = 99999999)
matriz_xx<-matrix(data = c(3.92,3.61,3.32,3.07,3.06,3.11,3.21,3.26,3.42,3.42,3.45,3.58,3.66,3.78,3.82,3.97,4.07,4.25,4.41,4.49,4.7,4.58,4.69,4.71,4.78,7298,6855,6636,6506,6450,6402,6368,6340,6349,6352,6361,6369,6546,6672,6890,7115,7327,7546,7931,8097,8468,8717,8991,9179,9318),nrow = 25,ncol = 2,byrow = FALSE)
print(matriz_xx)
##       [,1] [,2]
##  [1,] 3.92 7298
##  [2,] 3.61 6855
##  [3,] 3.32 6636
##  [4,] 3.07 6506
##  [5,] 3.06 6450
##  [6,] 3.11 6402
##  [7,] 3.21 6368
##  [8,] 3.26 6340
##  [9,] 3.42 6349
## [10,] 3.42 6352
## [11,] 3.45 6361
## [12,] 3.58 6369
## [13,] 3.66 6546
## [14,] 3.78 6672
## [15,] 3.82 6890
## [16,] 3.97 7115
## [17,] 4.07 7327
## [18,] 4.25 7546
## [19,] 4.41 7931
## [20,] 4.49 8097
## [21,] 4.70 8468
## [22,] 4.58 8717
## [23,] 4.69 8991
## [24,] 4.71 9179
## [25,] 4.78 9318

#Generando X’X

XtrX<- t(matriz_xx) %*% matriz_xx
print(XtrX)
##             [,1]         [,2]
## [1,]    379.2928     710932.3
## [2,] 710932.3200 1335796275.0

#Ingresando matriz Y

options(scipen = 99999999)
matriz_Y<-matrix(data = c(0.75,0.71,0.66,0.61,0.7,0.72,0.77,0.74,0.9,0.82,0.75,0.77,0.78,0.84,0.79,0.7,0.68,0.72,0.55,0.63,0.56,0.41,0.51,0.47,0.32),nrow = 25,ncol = 1,byrow = FALSE)
print(matriz_Y)
##       [,1]
##  [1,] 0.75
##  [2,] 0.71
##  [3,] 0.66
##  [4,] 0.61
##  [5,] 0.70
##  [6,] 0.72
##  [7,] 0.77
##  [8,] 0.74
##  [9,] 0.90
## [10,] 0.82
## [11,] 0.75
## [12,] 0.77
## [13,] 0.78
## [14,] 0.84
## [15,] 0.79
## [16,] 0.70
## [17,] 0.68
## [18,] 0.72
## [19,] 0.55
## [20,] 0.63
## [21,] 0.56
## [22,] 0.41
## [23,] 0.51
## [24,] 0.47
## [25,] 0.32

#Generando X’Y

XtrY<- t(matriz_xx) %*% matriz_Y
print(XtrY)
##             [,1]
## [1,]     63.6124
## [2,] 119215.9400

#Calculando la Inversa de X′X, es decir (X′X)−1 A través de Gauss -Jordan, u otro método (usando 10 decimales para el redondedo) se obtiene:

inv_matriz_xx<-solve(XtrX)
print(inv_matriz_xx)
##               [,1]             [,2]
## [1,]  1.0832428129 -0.0005765192945
## [2,] -0.0005765193  0.0000003075815

#Calculando el estimador de parametros β

beta<-inv_matriz_xx%*%XtrY
print(beta)
##                 [,1]
## [1,]  0.177385485833
## [2,] -0.000005160319

#También es posible a través de la solución del sistema de ecuaciones normales X′X⋅β=X′Y

beta<-solve(XtrX,XtrY)
print(beta)
##                 [,1]
## [1,]  0.177385485833
## [2,] -0.000005160319

#Ejercicio 2 (datos de ejercicio)

options(scipen = 99999999)
matriz_A<-matrix(data = c(50,53,60,63,69,82,100,104,113,130,150,181,202,217,229,240,243,247,249,254,
                    7.4,5.1,4.2,3.9,1.4,2.2,7.0,5.7,13.1,16.4,5.1,2.9,4.5,6.2,3.2,2.4,4.9,8.8,10.1,6.1),nrow = 20,ncol = 2,byrow = FALSE)
print(matriz_A)
##       [,1] [,2]
##  [1,]   50  7.4
##  [2,]   53  5.1
##  [3,]   60  4.2
##  [4,]   63  3.9
##  [5,]   69  1.4
##  [6,]   82  2.2
##  [7,]  100  7.0
##  [8,]  104  5.7
##  [9,]  113 13.1
## [10,]  130 16.4
## [11,]  150  5.1
## [12,]  181  2.9
## [13,]  202  4.5
## [14,]  217  6.2
## [15,]  229  3.2
## [16,]  240  2.4
## [17,]  243  4.9
## [18,]  247  8.8
## [19,]  249 10.1
## [20,]  254  6.1

#Generando A´A

AtrA<-t(matriz_A)%*%matriz_A
print(AtrA)
##          [,1]     [,2]
## [1,] 574618.0 18601.80
## [2,]  18601.8   992.26

#Ingresando matriz B

options(scipen = 99999999)
matriz_B<-matrix(data = c(320,450,370,470,420,500,570,640,670,780,690,700,910,930,940,1070,1160,1210,1450,1220),nrow = 20,ncol = 1,byrow = FALSE)
print(matriz_B)
##       [,1]
##  [1,]  320
##  [2,]  450
##  [3,]  370
##  [4,]  470
##  [5,]  420
##  [6,]  500
##  [7,]  570
##  [8,]  640
##  [9,]  670
## [10,]  780
## [11,]  690
## [12,]  700
## [13,]  910
## [14,]  930
## [15,]  940
## [16,] 1070
## [17,] 1160
## [18,] 1210
## [19,] 1450
## [20,] 1220

#Generando A´B

AtrB<- t(matriz_A) %*% matriz_B
print(AtrB)
##         [,1]
## [1,] 2801880
## [2,]   98350

#Calculando la Inversa de X′X, es decir (X′X)−1 A través de Gauss -Jordan, u otro método (usando 10 decimales para el redondedo) se obtiene:

inv_matriz_A<-solve(AtrA)
print(inv_matriz_A)
##                 [,1]           [,2]
## [1,]  0.000004426896 -0.00008299059
## [2,] -0.000082990587  0.00256361669

#Calculando el estimador de parametros β

betaAB<-inv_matriz_A%*%AtrB
print(betaAB)
##           [,1]
## [1,]  4.241508
## [2,] 19.602036

##También es posible a través de la solución del sistema de ecuaciones normales X′X⋅β=X′Y

beta<-solve(AtrA,AtrB)
print(beta)
##           [,1]
## [1,]  4.241508
## [2,] 19.602036