Exercise 1

1. Obtain descriptive statistics (e.g., mean, standard deviation, minimum, and maximum) for all seven variables for the student-level pooled sample.

## [1] 902

Figure 1. Distribution of student- and school-level variables across students

2. Obtain the descriptive statistics for your five individual-level variables for each school.

Figure 2. Distribution of student-level variables across school

3. Obtain the descriptive statistics for the two school-level variables for the school sample.

Figure 3. Distribution of school-level variables across school

4. Conduct a one-way ANOVA for the dependent variable with your school id as the factor.

##                Df  Sum Sq Mean Sq F value   Pr(>F)    
## sid             1    3179    3179   31.89 1.66e-08 ***
## Residuals   15646 1559495     100                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 638 observations deleted due to missingness

Figure 4. Distrubution of mean math scores by school

5. Conduct an OLS regression predicting the dependent variable with your six independent variables obtaining standard regression diagnostics.

## 
## Call:
## lm(formula = n_math ~ n_reading + n_ses + n_men + n_white + ns_blkpct + 
##     ns_type, data = data4)
## 
## Residuals:
## Math Score 
##      Min       1Q   Median       3Q      Max 
## -26.1660  -4.6850  -0.4595   4.3665  30.6248 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       20.269824   0.329765  61.467  < 2e-16 ***
## n_reading          0.639345   0.006165 103.698  < 2e-16 ***
## n_ses              2.344995   0.081011  28.947  < 2e-16 ***
## n_menWomen        -1.599874   0.110891 -14.427  < 2e-16 ***
## n_whiteNon-Whites -0.253630   0.131203  -1.933   0.0532 .  
## ns_blkpct         -0.238712   0.023254 -10.266  < 2e-16 ***
## ns_typeCatholic   -1.278587   0.197483  -6.474 9.81e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.782 on 15210 degrees of freedom
##   (1069 observations deleted due to missingness)
## Multiple R-squared:  0.539,  Adjusted R-squared:  0.5388 
## F-statistic:  2964 on 6 and 15210 DF,  p-value: < 2.2e-16

——————————————————————————–

Exercise 2

Using the variables selected previously, conduct the following analyses:

1. Obtain the results of a one-way ANOVA for your dependent variable in HLM.

# new data
data5 <- data1
model1 <- lmer(n_math ~ 
  (1 | sid), data = data5)
# Display the results
summary(model1)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: n_math ~ (1 | sid)
##    Data: data5
## 
## REML criterion at convergence: 114582.4
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -2.6846 -0.7497 -0.1464  0.6719  3.4368 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  sid      (Intercept) 19.14    4.375   
##  Residual             81.01    9.001   
## Number of obs: 15648, groups:  sid, 884
## 
## Fixed effects:
##             Estimate Std. Error       df t value Pr(>|t|)    
## (Intercept)  50.1230     0.1666 855.2115   300.9   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
report(model1)
## We fitted a constant (intercept-only) linear mixed model (estimated using REML
## and nloptwrap optimizer) to predict n_math (formula: n_math ~ 1). The model
## included sid as random effect (formula: ~1 | sid). . The model's intercept is
## at 50.12 (95% CI [49.80, 50.45], t(15645) = 300.91, p < .001). Within this
## model:
## 
##   -  ()
## 
## Standardized parameters were obtained by fitting the model on a standardized
## version of the dataset. 95% Confidence Intervals (CIs) and p-values were
## computed using a Wald t-distribution approximation.

2. Obtain HLM results predicting your dependent variable using only the student-level variables.

Conduct these analyses (1) uncentered, (2) group-mean centered, and (3) grand-mean centered.

(1) Uncentered

# Fit the model
model2 <- lmer(n_math ~
  n_reading +
  n_ses +
  n_men +
  n_white +
  (1 | sid), data = data5)
# Display the results
summary(model2)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: n_math ~ n_reading + n_ses + n_men + n_white + (1 | sid)
##    Data: data5
## 
## REML criterion at convergence: 102762.6
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -3.7533 -0.6734 -0.0655  0.6229  4.5674 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  sid      (Intercept)  3.766   1.941   
##  Residual             42.817   6.544   
## Number of obs: 15459, groups:  sid, 880
## 
## Fixed effects:
##              Estimate Std. Error        df t value Pr(>|t|)    
## (Intercept) 1.775e+01  3.352e-01 1.434e+04  52.939  < 2e-16 ***
## n_reading   6.312e-01  6.067e-03 1.539e+04 104.040  < 2e-16 ***
## n_ses       2.018e+00  8.411e-02 1.441e+04  23.989  < 2e-16 ***
## n_men       1.633e+00  1.083e-01 1.522e+04  15.086  < 2e-16 ***
## n_white     5.218e-01  1.373e-01 1.055e+04   3.801 0.000145 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##           (Intr) n_rdng n_ses  n_men 
## n_reading -0.910                     
## n_ses      0.392 -0.324              
## n_men     -0.269  0.123 -0.065       
## n_white   -0.185 -0.108 -0.179 -0.017
report(model2)
## We fitted a linear mixed model (estimated using REML and nloptwrap optimizer)
## to predict n_math with n_reading, n_ses, n_men and n_white (formula: n_math ~
## n_reading + n_ses + n_men + n_white). The model included sid as random effect
## (formula: ~1 | sid). The model's total explanatory power is substantial
## (conditional R2 = 0.56) and the part related to the fixed effects alone
## (marginal R2) is of 0.52. The model's intercept, corresponding to n_reading =
## 0, n_ses = 0, n_men = 0 and n_white = 0, is at 17.75 (95% CI [17.09, 18.40],
## t(15452) = 52.94, p < .001). Within this model:
## 
##   - The effect of n reading is statistically significant and positive (beta =
## 0.63, 95% CI [0.62, 0.64], t(15452) = 104.04, p < .001; Std. beta = 0.63, 95%
## CI [0.62, 0.64])
##   - The effect of n ses is statistically significant and positive (beta = 2.02,
## 95% CI [1.85, 2.18], t(15452) = 23.99, p < .001; Std. beta = 0.15, 95% CI
## [0.14, 0.17])
##   - The effect of n men is statistically significant and positive (beta = 1.63,
## 95% CI [1.42, 1.85], t(15452) = 15.09, p < .001; Std. beta = 0.08, 95% CI
## [0.07, 0.09])
##   - The effect of n white is statistically significant and positive (beta = 0.52,
## 95% CI [0.25, 0.79], t(15452) = 3.80, p < .001; Std. beta = 0.02, 95% CI [0.01,
## 0.04])
## 
## Standardized parameters were obtained by fitting the model on a standardized
## version of the dataset. 95% Confidence Intervals (CIs) and p-values were
## computed using a Wald t-distribution approximation.
visualize(model2, plot = "model")
## Note: You didn't choose to plot n_white so I am inputting the median

(2) Group-mean centered (within group)

# Making group mean (school mean) variables
group_means <- data5 %>%
  group_by(sid) %>%
  summarise(
    n_reading_group_mean = mean(n_reading, na.rm = TRUE),
    n_ses_group_mean = mean(n_ses, na.rm = TRUE),
    n_men_group_mean = mean(n_men, na.rm = TRUE),
    n_white_group_mean = mean(n_white, na.rm = TRUE)
  )

# Merge the group means back into the original data
data6 <- inner_join(data5, group_means, by = "sid")

# Perform group-mean centering
data6 <- data6 %>%
  mutate(
    n_reading_centered = n_reading - n_reading_group_mean,
    n_ses_centered = n_ses - n_ses_group_mean,
    n_men_centered = n_men - n_men_group_mean,
    n_white_centered = n_white - n_white_group_mean
  )

# Fit the model
model3 <- lmer(n_math ~
    n_reading_centered + 
    n_ses_centered + 
    n_men_centered + 
    n_white_centered + 
    (1 | sid), data = data6)
# Display the results
summary(model3)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: n_math ~ n_reading_centered + n_ses_centered + n_men_centered +  
##     n_white_centered + (1 | sid)
##    Data: data6
## 
## REML criterion at convergence: 103907.2
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -3.6195 -0.6641 -0.0671  0.6168  4.4732 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  sid      (Intercept) 21.6     4.648   
##  Residual             42.8     6.542   
## Number of obs: 15459, groups:  sid, 880
## 
## Fixed effects:
##                     Estimate Std. Error        df t value Pr(>|t|)    
## (Intercept)        5.010e+01  1.672e-01 8.537e+02 299.604   <2e-16 ***
## n_reading_centered 6.232e-01  6.205e-03 1.457e+04 100.429   <2e-16 ***
## n_ses_centered     1.771e+00  8.955e-02 1.458e+04  19.779   <2e-16 ***
## n_men_centered     1.642e+00  1.099e-01 1.458e+04  14.939   <2e-16 ***
## n_white_centered   2.261e-01  1.554e-01 1.457e+04   1.455    0.146    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) n_rdn_ n_ss_c n_mn_c
## n_rdng_cntr  0.000                     
## n_ses_cntrd -0.003 -0.289              
## n_men_cntrd  0.002  0.122 -0.061       
## n_wht_cntrd  0.000 -0.079 -0.151 -0.013
report(model3)
## We fitted a linear mixed model (estimated using REML and nloptwrap optimizer)
## to predict n_math with n_reading_centered, n_ses_centered, n_men_centered and
## n_white_centered (formula: n_math ~ n_reading_centered + n_ses_centered +
## n_men_centered + n_white_centered). The model included sid as random effect
## (formula: ~1 | sid). The model's total explanatory power is substantial
## (conditional R2 = 0.57) and the part related to the fixed effects alone
## (marginal R2) is of 0.36. The model's intercept, corresponding to
## n_reading_centered = 0, n_ses_centered = 0, n_men_centered = 0 and
## n_white_centered = 0, is at 50.10 (95% CI [49.77, 50.43], t(15452) = 299.60, p
## < .001). Within this model:
## 
##   - The effect of n reading centered is statistically significant and positive
## (beta = 0.62, 95% CI [0.61, 0.64], t(15452) = 100.43, p < .001; Std. beta =
## 0.56, 95% CI [0.55, 0.57])
##   - The effect of n ses centered is statistically significant and positive (beta
## = 1.77, 95% CI [1.60, 1.95], t(15452) = 19.78, p < .001; Std. beta = 0.11, 95%
## CI [0.10, 0.12])
##   - The effect of n men centered is statistically significant and positive (beta
## = 1.64, 95% CI [1.43, 1.86], t(15452) = 14.94, p < .001; Std. beta = 0.08, 95%
## CI [0.07, 0.09])
##   - The effect of n white centered is statistically non-significant and positive
## (beta = 0.23, 95% CI [-0.08, 0.53], t(15452) = 1.46, p = 0.146; Std. beta =
## 7.82e-03, 95% CI [-2.72e-03, 0.02])
## 
## Standardized parameters were obtained by fitting the model on a standardized
## version of the dataset. 95% Confidence Intervals (CIs) and p-values were
## computed using a Wald t-distribution approximation.

(3) Grand-mean centered

# Perform grand-mean centering
data6 <- data6 %>%
  mutate(
    n_reading_grand_mean_centered = n_reading - mean(n_reading, na.rm = TRUE),
    n_ses_grand_mean_centered = n_ses - mean(n_ses, na.rm = TRUE),
    n_men_grand_mean_centered = n_men - mean(n_men, na.rm = TRUE),
    n_white_grand_mean_centered = n_white - mean(n_white, na.rm = TRUE)
  )

# Fit the model
model4 <- lmer(n_math ~
  n_reading_grand_mean_centered +
  n_ses_grand_mean_centered +
  n_men_grand_mean_centered +
  n_white_grand_mean_centered +
  (1 | sid), data = data6)

# Display the results
summary(model4)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: n_math ~ n_reading_grand_mean_centered + n_ses_grand_mean_centered +  
##     n_men_grand_mean_centered + n_white_grand_mean_centered +      (1 | sid)
##    Data: data6
## 
## REML criterion at convergence: 102762.6
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -3.7533 -0.6734 -0.0655  0.6229  4.5674 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  sid      (Intercept)  3.766   1.941   
##  Residual             42.817   6.544   
## Number of obs: 15459, groups:  sid, 880
## 
## Fixed effects:
##                                Estimate Std. Error        df t value Pr(>|t|)
## (Intercept)                   5.031e+01  8.564e-02 8.236e+02 587.476  < 2e-16
## n_reading_grand_mean_centered 6.312e-01  6.067e-03 1.539e+04 104.040  < 2e-16
## n_ses_grand_mean_centered     2.018e+00  8.411e-02 1.441e+04  23.989  < 2e-16
## n_men_grand_mean_centered     1.633e+00  1.083e-01 1.522e+04  15.086  < 2e-16
## n_white_grand_mean_centered   5.218e-01  1.373e-01 1.055e+04   3.801 0.000145
##                                  
## (Intercept)                   ***
## n_reading_grand_mean_centered ***
## n_ses_grand_mean_centered     ***
## n_men_grand_mean_centered     ***
## n_white_grand_mean_centered   ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) n_r___ n_s___ n_m___
## n_rdng_gr__  0.003                     
## n_ss_grnd__ -0.004 -0.324              
## n_mn_grnd__  0.003  0.123 -0.065       
## n_wht_grn__  0.010 -0.108 -0.179 -0.017
report(model4)
## We fitted a linear mixed model (estimated using REML and nloptwrap optimizer)
## to predict n_math with n_reading_grand_mean_centered,
## n_ses_grand_mean_centered, n_men_grand_mean_centered and
## n_white_grand_mean_centered (formula: n_math ~ n_reading_grand_mean_centered +
## n_ses_grand_mean_centered + n_men_grand_mean_centered +
## n_white_grand_mean_centered). The model included sid as random effect (formula:
## ~1 | sid). The model's total explanatory power is substantial (conditional R2 =
## 0.56) and the part related to the fixed effects alone (marginal R2) is of 0.52.
## The model's intercept, corresponding to n_reading_grand_mean_centered = 0,
## n_ses_grand_mean_centered = 0, n_men_grand_mean_centered = 0 and
## n_white_grand_mean_centered = 0, is at 50.31 (95% CI [50.14, 50.48], t(15452) =
## 587.48, p < .001). Within this model:
## 
##   - The effect of n reading grand mean centered is statistically significant and
## positive (beta = 0.63, 95% CI [0.62, 0.64], t(15452) = 104.04, p < .001; Std.
## beta = 0.63, 95% CI [0.62, 0.64])
##   - The effect of n ses grand mean centered is statistically significant and
## positive (beta = 2.02, 95% CI [1.85, 2.18], t(15452) = 23.99, p < .001; Std.
## beta = 0.15, 95% CI [0.14, 0.17])
##   - The effect of n men grand mean centered is statistically significant and
## positive (beta = 1.63, 95% CI [1.42, 1.85], t(15452) = 15.09, p < .001; Std.
## beta = 0.08, 95% CI [0.07, 0.09])
##   - The effect of n white grand mean centered is statistically significant and
## positive (beta = 0.52, 95% CI [0.25, 0.79], t(15452) = 3.80, p < .001; Std.
## beta = 0.02, 95% CI [0.01, 0.04])
## 
## Standardized parameters were obtained by fitting the model on a standardized
## version of the dataset. 95% Confidence Intervals (CIs) and p-values were
## computed using a Wald t-distribution approximation.

3. Obtain HLM results predicting your dependent variable with both the student-level and school-level variables using two of the centering approaches.

Only using the school-level variables to predict the intercept, conduct these analyses (a) with coefficients for all the student-level variables fixed and (b) allowing the all student-level coefficients to randomly vary.

(a-1) Random Intercept Model: Group-Mean Centering with School-Level Variables

model5 <- lmer(n_math ~
  n_reading_centered +
  n_ses_centered +
  n_men_centered +
  n_white_centered +
  ns_blkpct +
  ns_public +
  (1 | sid), data = data6)

summary(model5)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: n_math ~ n_reading_centered + n_ses_centered + n_men_centered +  
##     n_white_centered + ns_blkpct + ns_public + (1 | sid)
##    Data: data6
## 
## REML criterion at convergence: 102098.1
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -3.6256 -0.6649 -0.0659  0.6102  4.5121 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  sid      (Intercept) 18.32    4.280   
##  Residual             42.62    6.529   
## Number of obs: 15217, groups:  sid, 865
## 
## Fixed effects:
##                      Estimate Std. Error         df t value Pr(>|t|)    
## (Intercept)         5.351e+01  5.133e-01  9.190e+02 104.239  < 2e-16 ***
## n_reading_centered  6.225e-01  6.234e-03  1.436e+04  99.863  < 2e-16 ***
## n_ses_centered      1.772e+00  9.020e-02  1.436e+04  19.642  < 2e-16 ***
## n_men_centered      1.634e+00  1.105e-01  1.436e+04  14.791  < 2e-16 ***
## n_white_centered    1.981e-01  1.563e-01  1.436e+04   1.267  0.20504    
## ns_blkpct          -6.889e-01  5.978e-02  8.832e+02 -11.524  < 2e-16 ***
## ns_public          -1.517e+00  5.283e-01  9.182e+02  -2.871  0.00418 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) n_rdn_ n_ss_c n_mn_c n_wht_ ns_blk
## n_rdng_cntr  0.000                                   
## n_ses_cntrd  0.000 -0.286                            
## n_men_cntrd  0.000  0.121 -0.060                     
## n_wht_cntrd  0.000 -0.080 -0.149 -0.013              
## ns_blkpct   -0.238  0.000 -0.001  0.003  0.000       
## ns_public   -0.891  0.000 -0.001  0.000  0.001 -0.108
report(model5)
## We fitted a linear mixed model (estimated using REML and nloptwrap optimizer)
## to predict n_math with n_reading_centered, n_ses_centered, n_men_centered,
## n_white_centered, ns_blkpct and ns_public (formula: n_math ~ n_reading_centered
## + n_ses_centered + n_men_centered + n_white_centered + ns_blkpct + ns_public).
## The model included sid as random effect (formula: ~1 | sid). The model's total
## explanatory power is substantial (conditional R2 = 0.58) and the part related
## to the fixed effects alone (marginal R2) is of 0.39. The model's intercept,
## corresponding to n_reading_centered = 0, n_ses_centered = 0, n_men_centered =
## 0, n_white_centered = 0, ns_blkpct = 0 and ns_public = 0, is at 53.51 (95% CI
## [52.50, 54.52], t(15208) = 104.24, p < .001). Within this model:
## 
##   - The effect of n reading centered is statistically significant and positive
## (beta = 0.62, 95% CI [0.61, 0.63], t(15208) = 99.86, p < .001; Std. beta =
## 0.56, 95% CI [0.55, 0.57])
##   - The effect of n ses centered is statistically significant and positive (beta
## = 1.77, 95% CI [1.59, 1.95], t(15208) = 19.64, p < .001; Std. beta = 0.11, 95%
## CI [0.10, 0.12])
##   - The effect of n men centered is statistically significant and positive (beta
## = 1.63, 95% CI [1.42, 1.85], t(15208) = 14.79, p < .001; Std. beta = 0.08, 95%
## CI [0.07, 0.09])
##   - The effect of n white centered is statistically non-significant and positive
## (beta = 0.20, 95% CI [-0.11, 0.50], t(15208) = 1.27, p = 0.205; Std. beta =
## 6.86e-03, 95% CI [-3.75e-03, 0.02])
##   - The effect of ns blkpct is statistically significant and negative (beta =
## -0.69, 95% CI [-0.81, -0.57], t(15208) = -11.52, p < .001; Std. beta = -0.17,
## 95% CI [-0.20, -0.14])
##   - The effect of ns public is statistically significant and negative (beta =
## -1.52, 95% CI [-2.55, -0.48], t(15208) = -2.87, p = 0.004; Std. beta = -0.04,
## 95% CI [-0.07, -0.01])
## 
## Standardized parameters were obtained by fitting the model on a standardized
## version of the dataset. 95% Confidence Intervals (CIs) and p-values were
## computed using a Wald t-distribution approximation.

(a-2) Random Intercept Model: Grand-Mean Centering with School-Level Variables

model6 <- lmer(n_math ~
  n_reading_grand_mean_centered +
  n_ses_grand_mean_centered +
  n_men_grand_mean_centered +
  n_white_grand_mean_centered +
  ns_blkpct +
  ns_public +
  (1 | sid), data = data6)
summary(model6)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: n_math ~ n_reading_grand_mean_centered + n_ses_grand_mean_centered +  
##     n_men_grand_mean_centered + n_white_grand_mean_centered +  
##     ns_blkpct + ns_public + (1 | sid)
##    Data: data6
## 
## REML criterion at convergence: 101045.9
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -3.7639 -0.6733 -0.0628  0.6213  4.5908 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  sid      (Intercept)  3.396   1.843   
##  Residual             42.642   6.530   
## Number of obs: 15217, groups:  sid, 865
## 
## Fixed effects:
##                                 Estimate Std. Error         df t value Pr(>|t|)
## (Intercept)                    4.994e+01  2.857e-01  9.562e+02 174.817  < 2e-16
## n_reading_grand_mean_centered  6.302e-01  6.100e-03  1.515e+04 103.309  < 2e-16
## n_ses_grand_mean_centered      2.048e+00  8.448e-02  1.403e+04  24.241  < 2e-16
## n_men_grand_mean_centered      1.617e+00  1.088e-01  1.500e+04  14.859  < 2e-16
## n_white_grand_mean_centered    2.677e-01  1.418e-01  1.185e+04   1.888    0.059
## ns_blkpct                     -2.534e-01  3.372e-02  9.824e+02  -7.513 1.30e-13
## ns_public                      1.194e+00  2.928e-01  9.483e+02   4.076 4.96e-05
##                                  
## (Intercept)                   ***
## n_reading_grand_mean_centered ***
## n_ses_grand_mean_centered     ***
## n_men_grand_mean_centered     ***
## n_white_grand_mean_centered   .  
## ns_blkpct                     ***
## ns_public                     ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) n_r___ n_s___ n_m___ n_w___ ns_blk
## n_rdng_gr__ -0.056                                   
## n_ss_grnd__ -0.057 -0.319                            
## n_mn_grnd__  0.009  0.122 -0.065                     
## n_wht_grn__ -0.062 -0.097 -0.171 -0.013              
## ns_blkpct   -0.237  0.039  0.007  0.010  0.248       
## ns_public   -0.898  0.047  0.057 -0.012 -0.020 -0.101
report(model6)
## We fitted a linear mixed model (estimated using REML and nloptwrap optimizer)
## to predict n_math with n_reading_grand_mean_centered,
## n_ses_grand_mean_centered, n_men_grand_mean_centered,
## n_white_grand_mean_centered, ns_blkpct and ns_public (formula: n_math ~
## n_reading_grand_mean_centered + n_ses_grand_mean_centered +
## n_men_grand_mean_centered + n_white_grand_mean_centered + ns_blkpct +
## ns_public). The model included sid as random effect (formula: ~1 | sid). The
## model's total explanatory power is substantial (conditional R2 = 0.56) and the
## part related to the fixed effects alone (marginal R2) is of 0.52. The model's
## intercept, corresponding to n_reading_grand_mean_centered = 0,
## n_ses_grand_mean_centered = 0, n_men_grand_mean_centered = 0,
## n_white_grand_mean_centered = 0, ns_blkpct = 0 and ns_public = 0, is at 49.94
## (95% CI [49.38, 50.50], t(15208) = 174.82, p < .001). Within this model:
## 
##   - The effect of n reading grand mean centered is statistically significant and
## positive (beta = 0.63, 95% CI [0.62, 0.64], t(15208) = 103.31, p < .001; Std.
## beta = 0.63, 95% CI [0.61, 0.64])
##   - The effect of n ses grand mean centered is statistically significant and
## positive (beta = 2.05, 95% CI [1.88, 2.21], t(15208) = 24.24, p < .001; Std.
## beta = 0.16, 95% CI [0.14, 0.17])
##   - The effect of n men grand mean centered is statistically significant and
## positive (beta = 1.62, 95% CI [1.40, 1.83], t(15208) = 14.86, p < .001; Std.
## beta = 0.08, 95% CI [0.07, 0.09])
##   - The effect of n white grand mean centered is statistically non-significant
## and positive (beta = 0.27, 95% CI [-0.01, 0.55], t(15208) = 1.89, p = 0.059;
## Std. beta = 0.01, 95% CI [-4.72e-04, 0.03])
##   - The effect of ns blkpct is statistically significant and negative (beta =
## -0.25, 95% CI [-0.32, -0.19], t(15208) = -7.51, p < .001; Std. beta = -0.06,
## 95% CI [-0.08, -0.05])
##   - The effect of ns public is statistically significant and positive (beta =
## 1.19, 95% CI [0.62, 1.77], t(15208) = 4.08, p < .001; Std. beta = 0.03, 95% CI
## [0.02, 0.05])
## 
## Standardized parameters were obtained by fitting the model on a standardized
## version of the dataset. 95% Confidence Intervals (CIs) and p-values were
## computed using a Wald t-distribution approximation.

(b-1) Random Slope Model: Group-Mean Centering with School-Level Variables

# Specify the model with fixed student-level coefficients
model7 <- lmer(
  n_math ~
  n_reading_centered +
  n_ses_centered +
  n_men_centered +
  n_white_centered +
  ns_blkpct +
  ns_public +
  (1+ n_reading_centered | sid),
  data = data6
)
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, :
## Model failed to converge with max|grad| = 0.0482497 (tol = 0.002, component 1)
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, : Model is nearly unidentifiable: very large eigenvalue
##  - Rescale variables?
# Print the model summary
summary(model7)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: n_math ~ n_reading_centered + n_ses_centered + n_men_centered +  
##     n_white_centered + ns_blkpct + ns_public + (1 + n_reading_centered |  
##     sid)
##    Data: data6
## 
## REML criterion at convergence: 102035.5
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -3.3873 -0.6617 -0.0765  0.6108  4.5118 
## 
## Random effects:
##  Groups   Name               Variance  Std.Dev. Corr
##  sid      (Intercept)        18.400775 4.2896       
##           n_reading_centered  0.006037 0.0777   0.63
##  Residual                    42.117979 6.4898       
## Number of obs: 15217, groups:  sid, 865
## 
## Fixed effects:
##                      Estimate Std. Error         df t value Pr(>|t|)    
## (Intercept)         5.366e+01  5.024e-01  9.272e+02 106.804  < 2e-16 ***
## n_reading_centered  6.156e-01  6.830e-03  8.409e+02  90.130  < 2e-16 ***
## n_ses_centered      1.752e+00  8.998e-02  1.435e+04  19.473  < 2e-16 ***
## n_men_centered      1.637e+00  1.103e-01  1.436e+04  14.851  < 2e-16 ***
## n_white_centered    2.084e-01  1.562e-01  1.433e+04   1.334 0.182070    
## ns_blkpct          -6.603e-01  5.833e-02  8.860e+02 -11.321  < 2e-16 ***
## ns_public          -1.780e+00  5.159e-01  9.203e+02  -3.449 0.000587 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) n_rdn_ n_ss_c n_mn_c n_wht_ ns_blk
## n_rdng_cntr  0.068                                   
## n_ses_cntrd -0.006 -0.260                            
## n_men_cntrd  0.001  0.109 -0.060                     
## n_wht_cntrd -0.009 -0.075 -0.148 -0.013              
## ns_blkpct   -0.237 -0.032  0.000  0.004  0.017       
## ns_public   -0.889  0.016  0.006 -0.002  0.004 -0.108
## optimizer (nloptwrap) convergence code: 0 (OK)
## Model failed to converge with max|grad| = 0.0482497 (tol = 0.002, component 1)
## Model is nearly unidentifiable: very large eigenvalue
##  - Rescale variables?
report(model7)
## We fitted a linear mixed model (estimated using REML and nloptwrap optimizer)
## to predict n_math with n_reading_centered, n_ses_centered, n_men_centered,
## n_white_centered, ns_blkpct and ns_public (formula: n_math ~ n_reading_centered
## + n_ses_centered + n_men_centered + n_white_centered + ns_blkpct + ns_public).
## The model included n_reading_centered as random effects (formula: ~1 +
## n_reading_centered | sid). The model's total explanatory power is substantial
## (conditional R2 = 0.58) and the part related to the fixed effects alone
## (marginal R2) is of 0.39. The model's intercept, corresponding to
## n_reading_centered = 0, n_ses_centered = 0, n_men_centered = 0,
## n_white_centered = 0, ns_blkpct = 0 and ns_public = 0, is at 53.66 (95% CI
## [52.67, 54.64], t(15206) = 106.80, p < .001). Within this model:
## 
##   - The effect of n reading centered is statistically significant and positive
## (beta = 0.62, 95% CI [0.60, 0.63], t(15206) = 90.13, p < .001; Std. beta =
## 0.55, 95% CI [0.54, 0.57])
##   - The effect of n ses centered is statistically significant and positive (beta
## = 1.75, 95% CI [1.58, 1.93], t(15206) = 19.47, p < .001; Std. beta = 0.11, 95%
## CI [0.10, 0.12])
##   - The effect of n men centered is statistically significant and positive (beta
## = 1.64, 95% CI [1.42, 1.85], t(15206) = 14.85, p < .001; Std. beta = 0.08, 95%
## CI [0.07, 0.09])
##   - The effect of n white centered is statistically non-significant and positive
## (beta = 0.21, 95% CI [-0.10, 0.51], t(15206) = 1.33, p = 0.182; Std. beta =
## 7.21e-03, 95% CI [-3.38e-03, 0.02])
##   - The effect of ns blkpct is statistically significant and negative (beta =
## -0.66, 95% CI [-0.77, -0.55], t(15206) = -11.32, p < .001; Std. beta = -0.17,
## 95% CI [-0.20, -0.14])
##   - The effect of ns public is statistically significant and negative (beta =
## -1.78, 95% CI [-2.79, -0.77], t(15206) = -3.45, p < .001; Std. beta = -0.05,
## 95% CI [-0.08, -0.02])
## 
## Standardized parameters were obtained by fitting the model on a standardized
## version of the dataset. 95% Confidence Intervals (CIs) and p-values were
## computed using a Wald t-distribution approximation.

(b-2) Random Slope Model: Grand-Mean Centering with School-Level Variables

# Specify the model with random student-level coefficients
model8 <- lmer(
  n_math ~
  n_reading_grand_mean_centered +
  n_ses_grand_mean_centered +
  n_men_grand_mean_centered +
  n_white_grand_mean_centered +
  ns_blkpct +
  ns_public +
  (n_reading + n_ses + n_men + n_white | sid),
  data = data6,
)
## boundary (singular) fit: see help('isSingular')
## Warning: Model failed to converge with 1 negative eigenvalue: -6.1e+01
# Print the model summary
summary(model8)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: n_math ~ n_reading_grand_mean_centered + n_ses_grand_mean_centered +  
##     n_men_grand_mean_centered + n_white_grand_mean_centered +  
##     ns_blkpct + ns_public + (n_reading + n_ses + n_men + n_white |      sid)
##    Data: data6
## 
## REML criterion at convergence: 100937.8
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -3.3664 -0.6675 -0.0746  0.6132  4.5700 
## 
## Random effects:
##  Groups   Name        Variance  Std.Dev. Corr                   
##  sid      (Intercept) 12.146531 3.48519                         
##           n_reading    0.006297 0.07936  -0.84                  
##           n_ses        0.718998 0.84794   0.75 -0.27            
##           n_men        1.437430 1.19893  -0.49  0.33 -0.47      
##           n_white      2.052109 1.43252  -0.37  0.02 -0.65  0.33
##  Residual             41.338020 6.42946                         
## Number of obs: 15217, groups:  sid, 865
## 
## Fixed effects:
##                                 Estimate Std. Error         df t value Pr(>|t|)
## (Intercept)                    4.999e+01  2.958e-01  1.030e+03 168.972  < 2e-16
## n_reading_grand_mean_centered  6.245e-01  6.736e-03  8.204e+02  92.702  < 2e-16
## n_ses_grand_mean_centered      1.973e+00  8.954e-02  8.178e+02  22.033  < 2e-16
## n_men_grand_mean_centered      1.606e+00  1.163e-01  7.923e+02  13.811  < 2e-16
## n_white_grand_mean_centered    3.220e-01  1.489e-01  7.648e+02   2.162 0.030909
## ns_blkpct                     -2.384e-01  3.192e-02  9.752e+02  -7.468 1.81e-13
## ns_public                      1.008e+00  3.029e-01  1.033e+03   3.327 0.000908
##                                  
## (Intercept)                   ***
## n_reading_grand_mean_centered ***
## n_ses_grand_mean_centered     ***
## n_men_grand_mean_centered     ***
## n_white_grand_mean_centered   *  
## ns_blkpct                     ***
## ns_public                     ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) n_r___ n_s___ n_m___ n_w___ ns_blk
## n_rdng_gr__  0.011                                   
## n_ss_grnd__ -0.041 -0.305                            
## n_mn_grnd__  0.033  0.154 -0.120                     
## n_wht_grn__ -0.069 -0.079 -0.239  0.031              
## ns_blkpct   -0.223  0.018  0.002  0.008  0.272       
## ns_public   -0.909  0.055  0.068 -0.010 -0.014 -0.089
## optimizer (nloptwrap) convergence code: 0 (OK)
## boundary (singular) fit: see help('isSingular')
report(model8)
## boundary (singular) fit: see help('isSingular')
## Random effect variances not available. Returned R2 does not account for random effects.
## boundary (singular) fit: see help('isSingular')
## Random effect variances not available. Returned R2 does not account for random effects.
## We fitted a linear mixed model (estimated using REML and nloptwrap optimizer)
## to predict n_math with n_reading_grand_mean_centered,
## n_ses_grand_mean_centered, n_men_grand_mean_centered,
## n_white_grand_mean_centered, ns_blkpct and ns_public (formula: n_math ~
## n_reading_grand_mean_centered + n_ses_grand_mean_centered +
## n_men_grand_mean_centered + n_white_grand_mean_centered + ns_blkpct +
## ns_public). The model included n_reading as random effects (formula: ~n_reading
## + n_ses + n_men + n_white | sid). The model's explanatory power related to the
## fixed effects alone (marginal R2) is 0.55. The model's intercept, corresponding
## to n_reading_grand_mean_centered = 0, n_ses_grand_mean_centered = 0,
## n_men_grand_mean_centered = 0, n_white_grand_mean_centered = 0, ns_blkpct = 0
## and ns_public = 0, is at 49.99 (95% CI [49.41, 50.57], t(15194) = 168.97, p <
## .001). Within this model:
## 
##   - The effect of n reading grand mean centered is statistically significant and
## positive (beta = 0.62, 95% CI [0.61, 0.64], t(15194) = 92.70, p < .001; Std.
## beta = 0.62, 95% CI [0.61, 0.63])
##   - The effect of n ses grand mean centered is statistically significant and
## positive (beta = 1.97, 95% CI [1.80, 2.15], t(15194) = 22.03, p < .001; Std.
## beta = 0.15, 95% CI [0.14, 0.16])
##   - The effect of n men grand mean centered is statistically significant and
## positive (beta = 1.61, 95% CI [1.38, 1.83], t(15194) = 13.81, p < .001; Std.
## beta = 0.08, 95% CI [0.07, 0.09])
##   - The effect of n white grand mean centered is statistically significant and
## positive (beta = 0.32, 95% CI [0.03, 0.61], t(15194) = 2.16, p = 0.031; Std.
## beta = 0.02, 95% CI [1.79e-03, 0.03])
##   - The effect of ns blkpct is statistically significant and negative (beta =
## -0.24, 95% CI [-0.30, -0.18], t(15194) = -7.47, p < .001; Std. beta = -0.06,
## 95% CI [-0.08, -0.04])
##   - The effect of ns public is statistically significant and positive (beta =
## 1.01, 95% CI [0.41, 1.60], t(15194) = 3.33, p < .001; Std. beta = 0.03, 95% CI
## [0.01, 0.05])
## 
## Standardized parameters were obtained by fitting the model on a standardized
## version of the dataset. 95% Confidence Intervals (CIs) and p-values were
## computed using a Wald t-distribution approximation.
tab_model(model1, model2, model3, model4)
  Math Score Math Score Math Score Math Score
Predictors Estimates CI p Estimates CI p Estimates CI p Estimates CI p
(Intercept) 50.12 49.80 – 50.45 <0.001 17.75 17.09 – 18.40 <0.001 50.10 49.77 – 50.43 <0.001 50.31 50.14 – 50.48 <0.001
Reading Score 0.63 0.62 – 0.64 <0.001
Socioeconomic Status 2.02 1.85 – 2.18 <0.001
Gender=Men 1.63 1.42 – 1.85 <0.001
Race=white 0.52 0.25 – 0.79 <0.001
Reading Score 0.62 0.61 – 0.64 <0.001
Socioeconomic Status 1.77 1.60 – 1.95 <0.001
Gender=Men 1.64 1.43 – 1.86 <0.001
Race=white 0.23 -0.08 – 0.53 0.146
Reading Score 0.63 0.62 – 0.64 <0.001
Socioeconomic Status 2.02 1.85 – 2.18 <0.001
Gender=Men 1.63 1.42 – 1.85 <0.001
Race=white 0.52 0.25 – 0.79 <0.001
Random Effects
σ2 81.01 42.82 42.80 42.82
τ00 19.14 sid 3.77 sid 21.60 sid 3.77 sid
ICC 0.19 0.08 0.34 0.08
N 884 sid 880 sid 880 sid 880 sid
Observations 15648 15459 15459 15459
Marginal R2 / Conditional R2 0.000 / 0.191 0.517 / 0.556 0.360 / 0.575 0.517 / 0.556
tab_model(model5, model6)
  Math Score Math Score
Predictors Estimates CI p Estimates CI p
(Intercept) 53.51 52.50 – 54.52 <0.001 49.94 49.38 – 50.50 <0.001
Reading Score 0.62 0.61 – 0.63 <0.001
Socioeconomic Status 1.77 1.59 – 1.95 <0.001
Gender=Men 1.63 1.42 – 1.85 <0.001
Race=white 0.20 -0.11 – 0.50 0.205
SCH:Percentage of Black
Students
-0.69 -0.81 – -0.57 <0.001 -0.25 -0.32 – -0.19 <0.001
SCH:School Type=Public -1.52 -2.55 – -0.48 0.004 1.19 0.62 – 1.77 <0.001
Reading Score 0.63 0.62 – 0.64 <0.001
Socioeconomic Status 2.05 1.88 – 2.21 <0.001
Gender=Men 1.62 1.40 – 1.83 <0.001
Race=white 0.27 -0.01 – 0.55 0.059
Random Effects
σ2 42.62 42.64
τ00 18.32 sid 3.40 sid
ICC 0.30 0.07
N 865 sid 865 sid
Observations 15217 15217
Marginal R2 / Conditional R2 0.393 / 0.576 0.524 / 0.559
tab_model(model7, model8)
  Math Score Math Score
Predictors Estimates CI p Estimates CI p
(Intercept) 53.66 52.67 – 54.64 <0.001 49.99 49.41 – 50.57 <0.001
Reading Score 0.62 0.60 – 0.63 <0.001
Socioeconomic Status 1.75 1.58 – 1.93 <0.001
Gender=Men 1.64 1.42 – 1.85 <0.001
Race=white 0.21 -0.10 – 0.51 0.182
SCH:Percentage of Black
Students
-0.66 -0.77 – -0.55 <0.001 -0.24 -0.30 – -0.18 <0.001
SCH:School Type=Public -1.78 -2.79 – -0.77 0.001 1.01 0.41 – 1.60 0.001
Reading Score 0.62 0.61 – 0.64 <0.001
Socioeconomic Status 1.97 1.80 – 2.15 <0.001
Gender=Men 1.61 1.38 – 1.83 <0.001
Race=white 0.32 0.03 – 0.61 0.031
Random Effects
σ2 42.12 41.34
τ00 18.40 sid 12.15 sid
τ11 0.01 sid.n_reading_centered 0.01 sid.n_reading
  0.72 sid.n_ses
  1.44 sid.n_men
  2.05 sid.n_white
ρ01 0.63 sid -0.84
  0.75
  -0.49
  -0.37
ICC 0.31  
N 865 sid 865 sid
Observations 15217 15217
Marginal R2 / Conditional R2 0.387 / 0.577 0.545 / NA
save.image("Exercise_231019.RData")