1. estimar un modelo para explicar los ingresos usando todas las variables disponibles en el dataframe
#importacion de datos
options(scipen = 999999)
load("C:/doc R/datos_parcial_1.RData")
library(lmtest)
modelo_ingreso<- lm(income~sex+status+verbal+gamble, data = teengamb)
summary(modelo_ingreso)
## 
## Call:
## lm(formula = income ~ sex + status + verbal + gamble, data = teengamb)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.9561 -1.9072 -0.6399  1.1958  7.1716 
## 
## Coefficients:
##             Estimate Std. Error t value  Pr(>|t|)    
## (Intercept)  4.41097    2.00311   2.202    0.0332 *  
## sex          0.09035    1.07210   0.084    0.9332    
## status      -0.06279    0.03250  -1.932    0.0601 .  
## verbal       0.24660    0.26492   0.931    0.3572    
## gamble       0.07214    0.01491   4.839 0.0000179 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.736 on 42 degrees of freedom
## Multiple R-squared:  0.4581, Adjusted R-squared:  0.4065 
## F-statistic: 8.877 on 4 and 42 DF,  p-value: 0.00002743

#b)calcule el intervalo de confianza del 92.5% para las variables “verbal” y “gamble”

confint(object = modelo_ingreso,parm = "verbal",level = .925)
##            3.75 %  96.25 %
## verbal -0.2370767 0.730284
confint(object = modelo_ingreso,parm = "gamble",level = .925)
##            3.75 %    96.25 %
## gamble 0.04492279 0.09936007

#c) el modelo es estadisticamente significativo

##El valor P del modelo gobal es inferior al 0.05 por lo tanto el modelo es estadisticamente significativo

#d) calcule las matrices A,P y M

#matriz de informacion
matriz.x<-model.matrix(modelo_ingreso)
matriz.xx<-t(matriz.x)%*%(matriz.x)

#matriz A
matriz.A<-solve(matriz.xx)%*%t(matriz.x)
matriz.A[1:5,1:5]
##                         1              2             3             4
## (Intercept) -0.1074542196 -0.02196090939  0.0166636550  0.1071925261
## sex          0.0809403937  0.02485495618  0.0546438428  0.0478553863
## status       0.0012534153 -0.00199128049  0.0004440685  0.0004276380
## verbal       0.0053215827  0.01872696513 -0.0052703695 -0.0181905266
## gamble       0.0002006996 -0.00007513551 -0.0001277504 -0.0001794904
##                         5
## (Intercept) -0.1997769992
## sex          0.1347212618
## status       0.0034635069
## verbal      -0.0012648221
## gamble       0.0009505469
#calculando matriz P
matriz.P<-matriz.x%*%matriz.A
matriz.P[1:5,1:5]
##            1          2          3          4          5
## 1 0.07998301 0.05115446 0.05179203 0.03133324 0.10146454
## 2 0.05115446 0.09695391 0.04157846 0.02149756 0.02180388
## 3 0.05179203 0.04157846 0.05611581 0.06172736 0.05550509
## 4 0.03133324 0.02149756 0.06172736 0.09294939 0.03380216
## 5 0.10146454 0.02180388 0.05550509 0.03380216 0.16858435
#matriz C
n<-nrow(matriz.x)
matriz.M<-diag(n)-matriz.P
matriz.M[1:5,1:5]
##             1           2           3           4           5
## 1  0.92001699 -0.05115446 -0.05179203 -0.03133324 -0.10146454
## 2 -0.05115446  0.90304609 -0.04157846 -0.02149756 -0.02180388
## 3 -0.05179203 -0.04157846  0.94388419 -0.06172736 -0.05550509
## 4 -0.03133324 -0.02149756 -0.06172736  0.90705061 -0.03380216
## 5 -0.10146454 -0.02180388 -0.05550509 -0.03380216  0.83141565