library(readxl)
Investiment_Equation <- read_excel("C:/Users/Familia/Desktop/PRATICAS DE R STUDIO/Investiment_Equation.xlsx")

ecuacion_inversion<-lm(formula = InvReal~Trend+Inflation+PNBr+Interest,data = Investiment_Equation)
library(stargazer)
stargazer(ecuacion_inversion,title = "Ecuacion de Inversion",type = "text")
## 
## Ecuacion de Inversion
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                               InvReal          
## -----------------------------------------------
## Trend                        -0.016***         
##                               (0.002)          
##                                                
## Inflation                     0.00002          
##                               (0.001)          
##                                                
## PNBr                         0.665***          
##                               (0.054)          
##                                                
## Interest                      -0.240*          
##                               (0.120)          
##                                                
## Constant                     -0.503***         
##                               (0.054)          
##                                                
## -----------------------------------------------
## Observations                    15             
## R2                             0.973           
## Adjusted R2                    0.962           
## Residual Std. Error       0.007 (df = 10)      
## F Statistic           90.089*** (df = 4; 10)   
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01
model.matrix(ecuacion_inversion)->mat_X
n<-nrow(mat_X)
matriz_M<-diag(n)-mat_X%*%solve(t(mat_X)%*%mat_X)%*%t(mat_X)
Y<-Investiment_Equation$InvReal
residuos<-matriz_M%*%Y
print(residuos)
##             [,1]
## 1  -0.0100602233
## 2  -0.0009290882
## 3   0.0029656679
## 4   0.0078576839
## 5   0.0028109133
## 6   0.0006259732
## 7   0.0075909286
## 8  -0.0055352778
## 9  -0.0037254127
## 10  0.0006953129
## 11  0.0019904770
## 12 -0.0001288433
## 13 -0.0101976729
## 14  0.0068712384
## 15 -0.0008316770
confint(object = ecuacion_inversion,parm = "PNBr",level = .93)
##         3.5 %   96.5 %
## PNBr 0.554777 0.774317