Parte 1

options(scipen = 99999999)
load("C:/Users/Walter Alemán/Desktop/UES V/ECONOMETRIA/Ejercicios/Ejercicio 2/datos_cajas.RData")
Modelo_cajas<-lm(formula = Tiempo ~ Distancia+N_cajas, data= datos_cajas)
summary(Modelo_cajas)
## 
## Call:
## lm(formula = Tiempo ~ Distancia + N_cajas, data = datos_cajas)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -9.2716 -0.5405  0.5212  1.4051  2.9381 
## 
## Coefficients:
##             Estimate Std. Error t value  Pr(>|t|)    
## (Intercept)   2.3112     5.8573   0.395   0.70007    
## Distancia     0.4559     0.1468   3.107   0.00908 ** 
## N_cajas       0.8772     0.1530   5.732 0.0000943 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.141 on 12 degrees of freedom
## Multiple R-squared:  0.7368, Adjusted R-squared:  0.6929 
## F-statistic:  16.8 on 2 and 12 DF,  p-value: 0.0003325

Parte 2

matriz_X<-model.matrix(Modelo_cajas)
matriz_XX<-t(matriz_X)%*%matriz_X

# Matriz A
Matriz_A<-solve(matriz_XX)%*%t(matriz_X)
print(Matriz_A)
##                        1            2            3            4            5
## (Intercept)  0.459747079  0.505626389 -0.317731768  0.707001469  0.053149816
## Distancia   -0.003015297 -0.009318829  0.018819615 -0.019989342 -0.006641453
## N_cajas     -0.017147338 -0.009890695 -0.007919488 -0.004479623  0.011082085
##                        6            7            8             9          10
## (Intercept) -0.166576988  0.633594572 -0.125532551  0.1260628274 -0.90735239
## Distancia    0.006550474 -0.009903692  0.009409808  0.0003379213  0.02334256
## N_cajas      0.002768355 -0.016090251 -0.003959744 -0.0038254420  0.01780152
##                       11           12           13            14          15
## (Intercept)  0.277217608  0.368482344  0.487274665 -0.3674581822 -0.73350489
## Distancia   -0.011931220 -0.007473259 -0.006797416  0.0001559637  0.01645417
## N_cajas      0.006862401 -0.005142468 -0.012793352  0.0238754370  0.01885861
# Matriz P
Matriz_P<-matriz_X%*%Matriz_A
print(Matriz_P)
##              1            2           3            4           5           6
## 1   0.19781478  0.127154573  0.16766180  0.062524965 -0.03527291 0.057620774
## 2   0.12715457  0.124295239  0.03396629  0.140073563  0.05334477 0.038710181
## 3   0.16766180  0.033966286  0.35585795 -0.137368460 -0.10168744 0.123125512
## 4   0.06252497  0.140073563 -0.13736846  0.257600846  0.15524536 0.006698639
## 5  -0.03527291  0.053344771 -0.10168744  0.155245361  0.18408997 0.046742309
## 6   0.05762077  0.038710181  0.12312551  0.006698639  0.04674231 0.086318088
## 7   0.17558129  0.144648497  0.07654437  0.133523089  0.01345706 0.036955589
## 8   0.11716423  0.050316476  0.21126231 -0.035350897 -0.01751039 0.094896089
## 9   0.09794605  0.077129229  0.10132526  0.055636570  0.03786105 0.067680430
## 10 -0.02906036 -0.056765574  0.20436525 -0.131155907  0.05122193 0.136694350
## 11 -0.01209498  0.081873124 -0.13140718  0.199703669  0.18629079 0.030873007
## 12  0.09285990  0.104513848  0.01812731  0.131114317  0.07550894 0.044246890
## 13  0.15541865  0.125438973  0.08744449  0.109054124  0.01789770 0.046274418
## 14 -0.12402490 -0.005427535 -0.12246527  0.112857904  0.23285894 0.067134558
## 15 -0.05129385 -0.039271650  0.11324781 -0.060157783  0.09995191 0.116029165
##              7           8          9          10          11           12
## 1   0.17558129  0.11716423 0.09794605 -0.02906036 -0.01209498  0.092859897
## 2   0.14464850  0.05031648 0.07712923 -0.05676557  0.08187312  0.104513848
## 3   0.07654437  0.21126231 0.10132526  0.20436525 -0.13140718  0.018127310
## 4   0.13352309 -0.03535090 0.05563657 -0.13115591  0.19970367  0.131114317
## 5   0.01345706 -0.01751039 0.03786105  0.05122193  0.18629079  0.075508940
## 6   0.03695559  0.09489609 0.06768043  0.13669435  0.03087301  0.044246890
## 7   0.18301556  0.07160552 0.08894348 -0.08682757  0.04935470  0.112467995
## 8   0.07160552  0.13896449 0.08399596  0.13551596 -0.03237026  0.042396988
## 9   0.08894348  0.08399596 0.07465547  0.05440619  0.04101064  0.069478345
## 10 -0.08682757  0.13551596 0.05440619  0.34795579 -0.01326471 -0.021162536
## 11  0.04935470 -0.03237026 0.04101064 -0.01326471  0.20329083  0.095597926
## 12  0.11246799  0.04239699 0.06947834 -0.02116254  0.09559793  0.094228911
## 13  0.15702161  0.07705558 0.08545596 -0.04568349  0.04428588  0.099852268
## 14 -0.07689788 -0.02789930 0.01907176  0.16357209  0.20867158  0.042323339
## 15 -0.07939330  0.08995724 0.04540362  0.29018859  0.04818497 -0.001554438
##             13           14           15
## 1   0.15541865 -0.124024902 -0.051293849
## 2   0.12543897 -0.005427535 -0.039271650
## 3   0.08744449 -0.122465266  0.113247813
## 4   0.10905412  0.112857904 -0.060157783
## 5   0.01789770  0.232858944  0.099951911
## 6   0.04627442  0.067134558  0.116029165
## 7   0.15702161 -0.076897883 -0.079393301
## 8   0.07705558 -0.027899299  0.089957240
## 9   0.08545596  0.019071756  0.045403621
## 10 -0.04568349  0.163572088  0.290188586
## 11  0.04428588  0.208671580  0.048184973
## 12  0.09985227  0.042323339 -0.001554438
## 13  0.13743085 -0.052866482 -0.044080529
## 14 -0.05286648  0.352392093  0.210699107
## 15 -0.04408053  0.210699107  0.262089133
# Matriz M
n<-nrow(matriz_X)
Matriz_M<-diag(n)-Matriz_P
print(Matriz_M)
##              1            2           3            4           5            6
## 1   0.80218522 -0.127154573 -0.16766180 -0.062524965  0.03527291 -0.057620774
## 2  -0.12715457  0.875704761 -0.03396629 -0.140073563 -0.05334477 -0.038710181
## 3  -0.16766180 -0.033966286  0.64414205  0.137368460  0.10168744 -0.123125512
## 4  -0.06252497 -0.140073563  0.13736846  0.742399154 -0.15524536 -0.006698639
## 5   0.03527291 -0.053344771  0.10168744 -0.155245361  0.81591003 -0.046742309
## 6  -0.05762077 -0.038710181 -0.12312551 -0.006698639 -0.04674231  0.913681912
## 7  -0.17558129 -0.144648497 -0.07654437 -0.133523089 -0.01345706 -0.036955589
## 8  -0.11716423 -0.050316476 -0.21126231  0.035350897  0.01751039 -0.094896089
## 9  -0.09794605 -0.077129229 -0.10132526 -0.055636570 -0.03786105 -0.067680430
## 10  0.02906036  0.056765574 -0.20436525  0.131155907 -0.05122193 -0.136694350
## 11  0.01209498 -0.081873124  0.13140718 -0.199703669 -0.18629079 -0.030873007
## 12 -0.09285990 -0.104513848 -0.01812731 -0.131114317 -0.07550894 -0.044246890
## 13 -0.15541865 -0.125438973 -0.08744449 -0.109054124 -0.01789770 -0.046274418
## 14  0.12402490  0.005427535  0.12246527 -0.112857904 -0.23285894 -0.067134558
## 15  0.05129385  0.039271650 -0.11324781  0.060157783 -0.09995191 -0.116029165
##              7           8           9          10          11           12
## 1  -0.17558129 -0.11716423 -0.09794605  0.02906036  0.01209498 -0.092859897
## 2  -0.14464850 -0.05031648 -0.07712923  0.05676557 -0.08187312 -0.104513848
## 3  -0.07654437 -0.21126231 -0.10132526 -0.20436525  0.13140718 -0.018127310
## 4  -0.13352309  0.03535090 -0.05563657  0.13115591 -0.19970367 -0.131114317
## 5  -0.01345706  0.01751039 -0.03786105 -0.05122193 -0.18629079 -0.075508940
## 6  -0.03695559 -0.09489609 -0.06768043 -0.13669435 -0.03087301 -0.044246890
## 7   0.81698444 -0.07160552 -0.08894348  0.08682757 -0.04935470 -0.112467995
## 8  -0.07160552  0.86103551 -0.08399596 -0.13551596  0.03237026 -0.042396988
## 9  -0.08894348 -0.08399596  0.92534453 -0.05440619 -0.04101064 -0.069478345
## 10  0.08682757 -0.13551596 -0.05440619  0.65204421  0.01326471  0.021162536
## 11 -0.04935470  0.03237026 -0.04101064  0.01326471  0.79670917 -0.095597926
## 12 -0.11246799 -0.04239699 -0.06947834  0.02116254 -0.09559793  0.905771089
## 13 -0.15702161 -0.07705558 -0.08545596  0.04568349 -0.04428588 -0.099852268
## 14  0.07689788  0.02789930 -0.01907176 -0.16357209 -0.20867158 -0.042323339
## 15  0.07939330 -0.08995724 -0.04540362 -0.29018859 -0.04818497  0.001554438
##             13           14           15
## 1  -0.15541865  0.124024902  0.051293849
## 2  -0.12543897  0.005427535  0.039271650
## 3  -0.08744449  0.122465266 -0.113247813
## 4  -0.10905412 -0.112857904  0.060157783
## 5  -0.01789770 -0.232858944 -0.099951911
## 6  -0.04627442 -0.067134558 -0.116029165
## 7  -0.15702161  0.076897883  0.079393301
## 8  -0.07705558  0.027899299 -0.089957240
## 9  -0.08545596 -0.019071756 -0.045403621
## 10  0.04568349 -0.163572088 -0.290188586
## 11 -0.04428588 -0.208671580 -0.048184973
## 12 -0.09985227 -0.042323339  0.001554438
## 13  0.86256915  0.052866482  0.044080529
## 14  0.05286648  0.647607907 -0.210699107
## 15  0.04408053 -0.210699107  0.737910867

Parte 3

library(magrittr)
residuos_Modelo<-Modelo_cajas$residuals
matriz_y<-datos_cajas$Tiempo
residuos_por_matrices<-Matriz_M%*%matriz_y

comparativa<-cbind(residuos_Modelo, residuos_por_matrices, residuos_Modelo-residuos_por_matrices) %>% as.data.frame() %>% round(digits = 2)

names(comparativa)<-c("residuos_modelo", "Residuos_matrices", "Diferencia")
print(comparativa)
##    residuos_modelo Residuos_matrices Diferencia
## 1            -0.76             -0.76          0
## 2             0.13              0.13          0
## 3            -0.32             -0.32          0
## 4             2.94              2.94          0
## 5            -9.27             -9.27          0
## 6             0.77              0.77          0
## 7             1.31              1.31          0
## 8            -2.09             -2.09          0
## 9             1.43              1.43          0
## 10            0.52              0.52          0
## 11            0.52              0.52          0
## 12            1.38              1.38          0
## 13           -1.02             -1.02          0
## 14            2.89              2.89          0
## 15            1.59              1.59          0

Parte 4

descomposicion<-eigen(matriz_XX)
Autovalores<-descomposicion$values
print(Autovalores)
## [1] 16976.7781334   709.9345923     0.2872743
print(Autovalores>0)
## [1] TRUE TRUE TRUE