Parte A

library(readxl)
Investiment_Equation <- read_excel("C:/Users/Walter Alemán/Desktop/UES V/ECONOMETRIA/Ejercicios/Ejercicio 4/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.015***         
##                               (0.002)          
##                                                
## Inflation                      0.003           
##                               (0.003)          
##                                                
## PNBr                         0.687***          
##                               (0.060)          
##                                                
## Interest                      -0.134           
##                               (0.167)          
##                                                
## Trend:Inflation               -0.0004          
##                              (0.0004)          
##                                                
## Constant                     -0.547***         
##                               (0.072)          
##                                                
## -----------------------------------------------
## Observations                    15             
## R2                             0.975           
## Adjusted R2                    0.962           
## Residual Std. Error       0.007 (df = 9)       
## F Statistic            71.190*** (df = 5; 9)   
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01

Parte B

model.matrix(ecuacion_inversion)->matriz_X
n<-nrow(matriz_X)
M<-diag(n)-matriz_X%*%solve(t(matriz_X)%*%matriz_X)%*%t(matriz_X)
Y<-Investiment_Equation$InvReal

residuos<-M%*%Y
print(residuos)
##             [,1]
## 1  -0.0084922407
## 2  -0.0018675543
## 3   0.0024166755
## 4   0.0090505139
## 5   0.0045262364
## 6  -0.0017713890
## 7   0.0042959123
## 8  -0.0045218176
## 9  -0.0032851778
## 10  0.0009731395
## 11  0.0017395253
## 12 -0.0005073766
## 13 -0.0084676348
## 14  0.0089240448
## 15 -0.0030128571

Parte C

confint(object = ecuacion_inversion, parm = "PNBr", level = .93)
##          3.5 %  96.5 %
## PNBr 0.5643321 0.80995