setwd("C:/Users/perez/OneDrive/Documentos/UNIVERSIDAD/SEMESTRES/SEPTIMO SEMESTRE/ECONOMETRIA/SEGUNDO EXAMEN")
gpa <- read_dta("gpa.dta")
n1 <- gpa %>%
  group_by(student) %>%
  summarise(ocasiones = n()) %>%
  ungroup() %>%
  group_by(ocasiones) %>%
  summarise(frecuencia = n()) %>%
  ungroup() %>%
  mutate(porcentaje = round(100 * frecuencia / sum(frecuencia), 2))

knitr::kable(n1, align = "c")
ocasiones frecuencia porcentaje
6 200 100
# Modelo nulo  (Intercepto alateorio)
M0 <- lmer(gpa ~ 1 + (1|student), REML = FALSE, data = gpa)
summary(M0)
## Linear mixed model fit by maximum likelihood . t-tests use Satterthwaite's
##   method [lmerModLmerTest]
## Formula: gpa ~ 1 + (1 | student)
##    Data: gpa
## 
##      AIC      BIC   logLik deviance df.resid 
##    919.5    934.7   -456.7    913.5     1197 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -3.6505 -0.5505  0.0606  0.6353  2.5742 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  student  (Intercept) 0.05677  0.2383  
##  Residual             0.09759  0.3124  
## Number of obs: 1200, groups:  student, 200
## 
## Fixed effects:
##              Estimate Std. Error        df t value Pr(>|t|)    
## (Intercept)   2.86500    0.01911 199.99999   149.9   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summ(M0)
## MODEL INFO:
## Observations: 1200
## Dependent Variable: gpa
## Type: Mixed effects linear regression 
## 
## MODEL FIT:
## AIC = 919.46, BIC = 934.73
## Pseudo-R² (fixed effects) = 0.00
## Pseudo-R² (total) = 0.37 
## 
## FIXED EFFECTS:
## --------------------------------------------------------
##                     Est.   S.E.   t val.     d.f.      p
## ----------------- ------ ------ -------- -------- ------
## (Intercept)         2.87   0.02   149.93   200.00   0.00
## --------------------------------------------------------
## 
## p values calculated using Satterthwaite d.f.
## 
## RANDOM EFFECTS:
## ------------------------------------
##   Group      Parameter    Std. Dev. 
## ---------- ------------- -----------
##  student    (Intercept)     0.24    
##  Residual                   0.31    
## ------------------------------------
## 
## Grouping variables:
## ---------------------------
##   Group    # groups   ICC  
## --------- ---------- ------
##  student     200      0.37 
## ---------------------------
# M1 : Tiempo  (Intercepto alateorio)
M1 <- lmer(gpa ~ time + (1|student), REML = FALSE, data = gpa)
summary(M1)
## Linear mixed model fit by maximum likelihood . t-tests use Satterthwaite's
##   method [lmerModLmerTest]
## Formula: gpa ~ time + (1 | student)
##    Data: gpa
## 
##      AIC      BIC   logLik deviance df.resid 
##    401.6    422.0   -196.8    393.6     1196 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -3.6188 -0.6370 -0.0002  0.6366  2.8330 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  student  (Intercept) 0.06336  0.2517  
##  Residual             0.05803  0.2409  
## Number of obs: 1200, groups:  student, 200
## 
## Fixed effects:
##              Estimate Std. Error        df t value Pr(>|t|)    
## (Intercept) 2.599e+00  2.165e-02 3.244e+02  120.05   <2e-16 ***
## time        1.063e-01  4.072e-03 1.000e+03   26.11   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##      (Intr)
## time -0.470
#M2 : Tiempo + Horas de trabajo (Intercepto alateorio)
M2 <- lmer(gpa ~ time + job+ (1|student), REML = FALSE, data = gpa)
summary(M2)
## Linear mixed model fit by maximum likelihood . t-tests use Satterthwaite's
##   method [lmerModLmerTest]
## Formula: gpa ~ time + job + (1 | student)
##    Data: gpa
## 
##      AIC      BIC   logLik deviance df.resid 
##    330.3    355.7   -160.1    320.3     1195 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -3.6460 -0.6034 -0.0087  0.6408  2.8738 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  student  (Intercept) 0.05336  0.2310  
##  Residual             0.05561  0.2358  
## Number of obs: 1200, groups:  student, 200
## 
## Fixed effects:
##               Estimate Std. Error         df t value Pr(>|t|)    
## (Intercept)  2.946e+00  4.446e-02  1.191e+03  66.255   <2e-16 ***
## time         1.032e-01  4.002e-03  9.951e+02  25.778   <2e-16 ***
## job         -1.609e-01  1.836e-02  1.093e+03  -8.764   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##      (Intr) time  
## time -0.303       
## job  -0.890  0.090
#M3 : Tiempo + Highschool + Genero (Intercepto alateorio)
M3 <- lmer(gpa ~ time + highgpa + sex + (1|student), REML = FALSE, data = gpa)
summary(M3)
## Linear mixed model fit by maximum likelihood . t-tests use Satterthwaite's
##   method [lmerModLmerTest]
## Formula: gpa ~ time + highgpa + sex + (1 | student)
##    Data: gpa
## 
##      AIC      BIC   logLik deviance df.resid 
##    381.0    411.6   -184.5    369.0     1194 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -3.5192 -0.6310 -0.0125  0.6361  2.8782 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  student  (Intercept) 0.05491  0.2343  
##  Residual             0.05803  0.2409  
## Number of obs: 1200, groups:  student, 200
## 
## Fixed effects:
##              Estimate Std. Error        df t value Pr(>|t|)    
## (Intercept) 2.241e+00  9.605e-02 2.046e+02  23.336  < 2e-16 ***
## time        1.063e-01  4.072e-03 1.000e+03  26.109  < 2e-16 ***
## highgpa     9.239e-02  3.030e-02 2.000e+02   3.049   0.0026 ** 
## sex         1.558e-01  3.608e-02 2.000e+02   4.319 2.47e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##         (Intr) time   highgp
## time    -0.106              
## highgpa -0.957  0.000       
## sex     -0.265  0.000  0.072
# Comparacion de todos los modelos 
tab_model(M0, M1, M2, M3, dv.labels = c("Modelo 0", "Modelo 1", "Modelo 2", "Modelo 3"))
  Modelo 0 Modelo 1 Modelo 2 Modelo 3
Predictors Estimates CI p Estimates CI p Estimates CI p Estimates CI p
(Intercept) 2.87 2.83 – 2.90 <0.001 2.60 2.56 – 2.64 <0.001 2.95 2.86 – 3.03 <0.001 2.24 2.05 – 2.43 <0.001
time 0.11 0.10 – 0.11 <0.001 0.10 0.10 – 0.11 <0.001 0.11 0.10 – 0.11 <0.001
job -0.16 -0.20 – -0.12 <0.001
gpa in high school 0.09 0.03 – 0.15 0.002
sex 0.16 0.09 – 0.23 <0.001
Random Effects
σ2 0.10 0.06 0.06 0.06
τ00 0.06 student 0.06 student 0.05 student 0.05 student
ICC 0.37 0.52 0.49 0.49
N 200 student 200 student 200 student 200 student
Observations 1200 1200 1200 1200
Marginal R2 / Conditional R2 0.000 / 0.368 0.214 / 0.624 0.257 / 0.621 0.269 / 0.624
#M5
M5 <- lmer(gpa ~ time + job + highgpa + sex + (1+time|student), REML = FALSE, data = gpa)
summary(M5)
## Linear mixed model fit by maximum likelihood . t-tests use Satterthwaite's
##   method [lmerModLmerTest]
## Formula: gpa ~ time + job + highgpa + sex + (1 + time | student)
##    Data: gpa
## 
##      AIC      BIC   logLik deviance df.resid 
##    198.2    244.0    -90.1    180.2     1191 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -3.1817 -0.5316 -0.0084  0.5427  3.3505 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev. Corr 
##  student  (Intercept) 0.038967 0.19740       
##           time        0.003912 0.06255  -0.21
##  Residual             0.041765 0.20436       
## Number of obs: 1200, groups:  student, 200
## 
## Fixed effects:
##               Estimate Std. Error         df t value Pr(>|t|)    
## (Intercept)  2.527e+00  9.260e-02  2.915e+02  27.293  < 2e-16 ***
## time         1.040e-01  5.622e-03  1.993e+02  18.493  < 2e-16 ***
## job         -1.196e-01  1.746e-02  1.036e+03  -6.852 1.25e-11 ***
## highgpa      8.983e-02  2.647e-02  1.979e+02   3.393 0.000834 ***
## sex          1.168e-01  3.153e-02  1.981e+02   3.703 0.000276 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##         (Intr) time   job    highgp
## time    -0.108                     
## job     -0.426  0.061              
## highgpa -0.874  0.001  0.017       
## sex     -0.252  0.002  0.028  0.073
#M6
M6 <- lmer(gpa ~ time + job + highgpa + sex + sex*highgpa + (1+time|student), REML = FALSE, data = gpa)
summary(M6) 
## Linear mixed model fit by maximum likelihood . t-tests use Satterthwaite's
##   method [lmerModLmerTest]
## Formula: gpa ~ time + job + highgpa + sex + sex * highgpa + (1 + time |  
##     student)
##    Data: gpa
## 
##      AIC      BIC   logLik deviance df.resid 
##    199.9    250.8    -89.9    179.9     1190 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -3.2060 -0.5330 -0.0107  0.5401  3.3492 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev. Corr 
##  student  (Intercept) 0.038738 0.19682       
##           time        0.003911 0.06254  -0.20
##  Residual             0.041768 0.20437       
## Number of obs: 1200, groups:  student, 200
## 
## Fixed effects:
##               Estimate Std. Error         df t value Pr(>|t|)    
## (Intercept)  2.579e+00  1.268e-01  2.480e+02  20.338  < 2e-16 ***
## time         1.040e-01  5.622e-03  1.993e+02  18.493  < 2e-16 ***
## job         -1.199e-01  1.747e-02  1.035e+03  -6.864 1.16e-11 ***
## highgpa      7.299e-02  3.869e-02  1.981e+02   1.887   0.0607 .  
## sex          2.176e-02  1.618e-01  1.982e+02   0.135   0.8931    
## highgpa:sex  3.166e-02  5.302e-02  1.980e+02   0.597   0.5511    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) time   job    highgp sex   
## time        -0.080                            
## job         -0.331  0.061                     
## highgpa     -0.935  0.002  0.033              
## sex         -0.707  0.002  0.034  0.725       
## highgpa:sex  0.684 -0.002 -0.029 -0.730 -0.981
# Comparacion de todos los modelos 
tab_model(M5, M6, dv.labels = c("Modelo 5", "Modelo 6"))
  Modelo 5 Modelo 6
Predictors Estimates CI p Estimates CI p
(Intercept) 2.53 2.35 – 2.71 <0.001 2.58 2.33 – 2.83 <0.001
time 0.10 0.09 – 0.12 <0.001 0.10 0.09 – 0.11 <0.001
job -0.12 -0.15 – -0.09 <0.001 -0.12 -0.15 – -0.09 <0.001
gpa in high school 0.09 0.04 – 0.14 0.001 0.07 -0.00 – 0.15 0.059
sex 0.12 0.05 – 0.18 <0.001 0.02 -0.30 – 0.34 0.893
highgpa:sex 0.03 -0.07 – 0.14 0.550
Random Effects
σ2 0.04 0.04
τ00 0.04 student 0.04 student
τ11 0.00 student.time 0.00 student.time
ρ01 -0.21 student -0.20 student
ICC 0.60 0.60
N 200 student 200 student
Observations 1200 1200
Marginal R2 / Conditional R2 0.288 / 0.713 0.288 / 0.714