Carga de datos

library(readr)
Practica_2 <- read_csv("Practica_2.csv")
head(Practica_2,n=5)
## # A tibble: 5 x 3
##       Y    X1    X2
##   <dbl> <dbl> <dbl>
## 1   320    50   7.4
## 2   450    53   5.1
## 3   370    60   5.1
## 4   470    63   3.9
## 5   420    69   1.4

Carga de datos desde Excel

library(readxl)
Practica_2 <- read_excel("Practica_2.xlsx")
head(Practica_2,n=6)
## # A tibble: 6 x 3
##       Y    X1    X2
##   <dbl> <dbl> <dbl>
## 1   320    50   7.4
## 2   450    53   5.1
## 3   370    60   4.2
## 4   470    63   3.9
## 5   420    69   1.4
## 6   500    82   2.2

Correr el Modelo

library(stargazer)
Modelo_clase<-lm(formula = Y~X1+X2,data = Practica_2)
#Usando summary
summary(Modelo_clase)
## 
## Call:
## lm(formula = Y ~ X1 + X2, data = Practica_2)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -141.777  -61.071   -5.082   47.894  233.648 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  85.5785    54.6183   1.567   0.1356    
## X1            3.9407     0.2666  14.782 3.91e-11 ***
## X2           14.8069     5.5184   2.683   0.0157 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 89.72 on 17 degrees of freedom
## Multiple R-squared:  0.9317, Adjusted R-squared:  0.9236 
## F-statistic: 115.9 on 2 and 17 DF,  p-value: 1.241e-10
#Usando stargazer
stargazer(Modelo_clase,title = "Practica_2",type = "text",digits = 6)
## 
## Practica
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                                  Y             
## -----------------------------------------------
## X1                          3.940655***        
##                             (0.266588)         
##                                                
## X2                          14.806940**        
##                             (5.518434)         
##                                                
## Constant                     85.578480         
##                             (54.618340)        
##                                                
## -----------------------------------------------
## Observations                    20             
## R2                           0.931676          
## Adjusted R2                  0.923638          
## Residual Std. Error     89.723970 (df = 17)    
## F Statistic         115.907300*** (df = 2; 17) 
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01

Matrices A & P & M

mat_x<-model.matrix(Modelo_clase)
#Matriz X'X
mat_xx<-t(mat_x)%*%mat_x
print(mat_xx)
##             (Intercept)       X1       X2
## (Intercept)        20.0   3036.0   121.20
## X1               3036.0 574618.0 18754.20
## X2                121.2  18754.2   999.94
# Matriz A
solve(mat_xx)%*%t(mat_x)->mat_A
print(mat_A)
##                         1            2             3             4
## (Intercept)  0.1508077901  0.195593791  0.2057292699  0.2082622006
## X1          -0.0009145579 -0.000860842 -0.0007883899 -0.0007583539
## X2           0.0062742749 -0.002461712 -0.0059491225 -0.0071194859
##                         5             6            7             8
## (Intercept)  0.2534684789  0.2200787879  0.095841240  0.1182323621
## X1          -0.0006757859 -0.0005704936 -0.000468421 -0.0004177170
## X2          -0.0166475553 -0.0137752252  0.004169153 -0.0007958626
##                         9            10            11            12
## (Intercept) -0.0495190882 -0.1407984904  7.256449e-02  0.0797242271
## X1          -0.0004258804 -0.0003148759 -4.524082e-06  0.0002951924
## X2           0.0270903855  0.0393723855 -3.610187e-03 -0.0122994120
##                        13            14            15            16
## (Intercept)  0.0192865616 -0.0356536977  0.0125057884  0.0154552404
## X1           0.0004616369  0.0005739293  0.0007153854  0.0008219656
## X2          -0.0064955520 -0.0002423712 -0.0117328861 -0.0148893751
##                        17           18            19           20
## (Intercept) -0.0411661276 -0.128632988 -0.1586341705 -0.093145667
## X1           0.0008188497  0.000807986  0.0008102501  0.000894646
## X2          -0.0054678653  0.009237741  0.0141317166  0.001210957
# Matriz P
mat_xx%*%mat_A->mat_P
print(mat_P)
##                1    2    3    4    5    6   7     8     9    10    11    12
## (Intercept)  1.0  1.0  1.0  1.0  1.0  1.0   1   1.0   1.0   1.0   1.0   1.0
## X1          50.0 53.0 60.0 63.0 69.0 82.0 100 104.0 113.0 130.0 150.0 181.0
## X2           7.4  5.1  4.2  3.9  1.4  2.2   7   5.7  13.1  16.4   5.1   2.9
##                13    14    15    16    17    18    19    20
## (Intercept)   1.0   1.0   1.0   1.0   1.0   1.0   1.0   1.0
## X1          202.0 217.0 229.0 240.0 243.0 247.0 249.0 254.0
## X2            4.5   6.2   3.2   2.4   4.9   8.8  10.1   6.7
#Martiz M
mat_M<-diag(x=20,nrow = 3,ncol = 20)-mat_P
print(mat_M)
##                 1     2     3     4     5     6    7      8      9     10
## (Intercept)  19.0  -1.0  -1.0  -1.0  -1.0  -1.0   -1   -1.0   -1.0   -1.0
## X1          -50.0 -33.0 -60.0 -63.0 -69.0 -82.0 -100 -104.0 -113.0 -130.0
## X2           -7.4  -5.1  15.8  -3.9  -1.4  -2.2   -7   -5.7  -13.1  -16.4
##                 11     12     13     14     15     16     17     18     19
## (Intercept)   -1.0   -1.0   -1.0   -1.0   -1.0   -1.0   -1.0   -1.0   -1.0
## X1          -150.0 -181.0 -202.0 -217.0 -229.0 -240.0 -243.0 -247.0 -249.0
## X2            -5.1   -2.9   -4.5   -6.2   -3.2   -2.4   -4.9   -8.8  -10.1
##                 20
## (Intercept)   -1.0
## X1          -254.0
## X2            -6.7