#Load packages

#load packages 
library(tidyverse)
library(psych)
library(ggdist)
library(gghalves)
library(ggcorrplot)
library(RColorBrewer)
library(lavaan)
library(openssl)
library(ggpubr)
library(rstatix)
library(ggprism)
library(patchwork)
library(magrittr)
library(GGally)
library(knitr)

#read in data

#read in data and filter out people who did not provide assent or left it blank
ESSD <- read.csv("ESSD.W1.DSet.clean.csv") %>% filter(assent != "2")

#gender breakdown of the sample

table (ESSD$gender)
## 
##   1   2   3   4 
## 238 199   7   4
table (ESSD$gender, ESSD$school) %>% kable()
1 2
112 126
83 116
2 5
3 1

#gender write-ins

table(ESSD$gender_3_TEXT) %>% kable()
Var1 Freq
Both 1
Gender Fluid 1
Genderfluid 1
Non - binary 1
Non-binary 1
Non-Binary 1
Non-binary 1

#racial breakdown of the sample

table(ESSD$er1) %>% kable()
Var1 Freq
1 5
1,2,3,4,5,6,7 1
1,4,7 1
3 2
3,4 1
3,7 1
4 297
4,5 6
4,5,6 2
4,6 6
4,8 3
5 29
5,6 2
6 8
6,8 1
7 8
8 65

#race write-in responses

table(ESSD$er1_8_TEXT) %>% kable()
Var1 Freq
Mexican/ American 1
America / Mexican 1
American 1
American/ Mexican 1
American/Mexican 1
American/Mexican 1
Don’t know rest 1
Filipino 1
Hispanic 4
Hispanic 6
Hispanic/Latino 3
I’m Mexican 1
Latina/American 1
Latino 1
Latino 2
Latino/American 1
Mexican 5
Mexican 2
Mexican american 1
Mexican American 4
Mexican American 12
MEXICAN AMERICAN 1
Mexican and Salvador 1
Mexican, American 1
Mexican/ American 1
Mexican/American 2
Mexican/American 5
Mexico 1
White 1
White, Mexican 1

#Mean age of the sample

describe(ESSD$age) %>% kable()
vars n mean sd median trimmed mad min max range skew kurtosis se
X1 1 447 13.91946 0.4264271 14 13.96657 0 13 17 4 0.3989576 8.452974 0.0201693

#Item Codebook str_1r Students here have a lot of stereotypes about your racial or ethnic group. str_2r Teachers and principals believe negative stereotypes about your racial/ethnic group. str_3r Your racial or cultural group is represented in stereotypical ways in textbooks and class materials.

pcc_1 Your classes teach you about diverse cultures and traditions. pcc_2 In school you get to do things that help you learn about people of different races and cultures. pcc_3 Your textbooks show people of many different races/ethnicities.

cult_1 In your classes you’ve learned new things about your culture. cult_2 At your school, you have chances to learn about the history and traditions of your culture. cult_3 At your school, you have participated in activities that teach you more about your cultural background.

crit_1 Your teachers encourage awareness of social issues affecting your community. crit_2 In your classes you have learned about racial inequality in the United States. crit_3 You have opportunities to learn about social justice.

Response Options: 1 = Not at all true; 2 = A little true; 3 = Somewhat true; 4 = Very true; 5 = Completely true

Treated as a continuous variable

#Descriptive statistics for School Climate for Diversity Scale items

#Create matrix of crit soc items
cc_soc_vars <- ESSD[ , c('crit_1', 'crit_2', 'crit_3', 'cult_1', 'cult_2', 'cult_3', 'str_1r', 'str_2r', 'str_3r', 'cbs_1', 'cbs_2', 'cbs_3', 'pcc_1', 'pcc_2', 'pcc_3')]

describe(cc_soc_vars) %>% kable()
vars n mean sd median trimmed mad min max range skew kurtosis se
crit_1 1 417 2.997602 1.165404 3 2.997015 1.4826 1 5 4 0.0137338 -0.7209386 0.0570701
crit_2 2 424 3.617924 1.179117 4 3.729412 1.4826 1 5 4 -0.5930965 -0.4818832 0.0572630
crit_3 3 423 3.182033 1.142650 3 3.197640 1.4826 1 5 4 -0.0831016 -0.7684268 0.0555575
cult_1 4 426 3.286385 1.298924 3 3.356725 1.4826 1 5 4 -0.2764276 -1.0341767 0.0629331
cult_2 5 427 3.470726 1.204826 4 3.556851 1.4826 1 5 4 -0.4456470 -0.7410155 0.0583056
cult_3 6 424 2.320755 1.301370 2 2.170588 1.4826 1 5 4 0.6513422 -0.7400155 0.0632001
str_1r 7 426 2.586854 1.237917 3 2.502924 1.4826 1 5 4 0.3059753 -0.8556516 0.0599773
str_2r 8 421 1.698337 1.091841 1 1.462908 0.0000 1 5 4 1.5578738 1.5265275 0.0532131
str_3r 9 422 2.289099 1.214256 2 2.159763 1.4826 1 5 4 0.5710610 -0.6205825 0.0591091
cbs_1 10 423 2.957447 1.303690 3 2.946903 1.4826 1 5 4 -0.0559133 -1.0609892 0.0633876
cbs_2 11 420 2.902381 1.219372 3 2.877976 1.4826 1 5 4 0.1468721 -0.8557284 0.0594993
cbs_3 12 423 2.638298 1.466578 3 2.548673 1.4826 1 5 4 0.2427654 -1.3729458 0.0713075
pcc_1 13 427 3.435597 1.137268 4 3.501458 1.4826 1 5 4 -0.3810130 -0.5663893 0.0550362
pcc_2 14 429 3.475525 1.124279 4 3.536232 1.4826 1 5 4 -0.3770954 -0.6082189 0.0542807
pcc_3 15 429 3.682984 1.109523 4 3.782609 1.4826 1 5 4 -0.6130781 -0.3475921 0.0535683

#Create Matrix for School Climate for Diversity Scale

#create a correlation matrix for cs vars
CCsocmatrix <-cor(cc_soc_vars, use = "complete.obs")


ggcorrplot(CCsocmatrix, hc.order = TRUE, type = "lower", lab = TRUE, ggtheme = ggplot2::theme_gray,
   colors = c("#6D9EC1", "white", "#E46726"))

#CFA for School Racial Climate Scale

#CFA for School Engagement Measures
SRC_CFA <- 'CC_soc =~ 1*crit_1 + crit_2 + crit_3
            Ctr_soc =~ 1*cult_1 + cult_2 + cult_3 
            Stp_soc =~ 1*str_1r + str_2r + str_3r
            CB_soc =~ 1*cbs_1 + cbs_2 + cbs_3
            PRC_soc =~ 1*pcc_1 + pcc_2 + pcc_3'


SRC_CFA <- sem(SRC_CFA, data = ESSD, estimator = "MLR")

summary(SRC_CFA, fit.measures = T, standardized = T, rsquare = T)
## lavaan 0.6-9 ended normally after 43 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        40
##                                                       
##                                                   Used       Total
##   Number of observations                           396         457
##                                                                   
## Model Test User Model:
##                                                Standard      Robust
##   Test Statistic                                171.088     156.492
##   Degrees of freedom                                 80          80
##   P-value (Chi-square)                            0.000       0.000
##   Scaling correction factor                                   1.093
##        Yuan-Bentler correction (Mplus variant)                     
## 
## Model Test Baseline Model:
## 
##   Test statistic                              2036.757    1777.010
##   Degrees of freedom                               105         105
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  1.146
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.953       0.954
##   Tucker-Lewis Index (TLI)                       0.938       0.940
##                                                                   
##   Robust Comparative Fit Index (CFI)                         0.956
##   Robust Tucker-Lewis Index (TLI)                            0.943
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -8640.721   -8640.721
##   Scaling correction factor                                  1.080
##       for the MLR correction                                      
##   Loglikelihood unrestricted model (H1)      -8555.177   -8555.177
##   Scaling correction factor                                  1.089
##       for the MLR correction                                      
##                                                                   
##   Akaike (AIC)                               17361.442   17361.442
##   Bayesian (BIC)                             17520.698   17520.698
##   Sample-size adjusted Bayesian (BIC)        17393.777   17393.777
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.054       0.049
##   90 Percent confidence interval - lower         0.043       0.038
##   90 Percent confidence interval - upper         0.065       0.060
##   P-value RMSEA <= 0.05                          0.283       0.536
##                                                                   
##   Robust RMSEA                                               0.051
##   90 Percent confidence interval - lower                     0.039
##   90 Percent confidence interval - upper                     0.063
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.052       0.052
## 
## Parameter Estimates:
## 
##   Standard errors                             Sandwich
##   Information bread                           Observed
##   Observed information based on                Hessian
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   CC_soc =~                                                             
##     crit_1            1.000                               0.757    0.644
##     crit_2            1.021    0.119    8.614    0.000    0.774    0.651
##     crit_3            1.051    0.107    9.802    0.000    0.796    0.703
##   Ctr_soc =~                                                            
##     cult_1            1.000                               1.090    0.844
##     cult_2            0.904    0.047   19.431    0.000    0.985    0.822
##     cult_3            0.771    0.058   13.319    0.000    0.841    0.642
##   Stp_soc =~                                                            
##     str_1r            1.000                               0.789    0.635
##     str_2r            0.910    0.110    8.243    0.000    0.717    0.655
##     str_3r            1.054    0.133    7.895    0.000    0.831    0.683
##   CB_soc =~                                                             
##     cbs_1             1.000                               0.815    0.630
##     cbs_2             1.288    0.142    9.041    0.000    1.050    0.855
##     cbs_3             1.000    0.128    7.802    0.000    0.816    0.557
##   PRC_soc =~                                                            
##     pcc_1             1.000                               0.902    0.786
##     pcc_2             0.921    0.067   13.696    0.000    0.831    0.732
##     pcc_3             0.861    0.069   12.457    0.000    0.776    0.696
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   CC_soc ~~                                                             
##     Ctr_soc           0.594    0.074    8.001    0.000    0.720    0.720
##     Stp_soc           0.113    0.055    2.051    0.040    0.189    0.189
##     CB_soc            0.128    0.057    2.263    0.024    0.207    0.207
##     PRC_soc           0.546    0.068    8.088    0.000    0.799    0.799
##   Ctr_soc ~~                                                            
##     Stp_soc           0.307    0.065    4.715    0.000    0.358    0.358
##     CB_soc            0.092    0.064    1.428    0.153    0.103    0.103
##     PRC_soc           0.749    0.072   10.435    0.000    0.762    0.762
##   Stp_soc ~~                                                            
##     CB_soc            0.105    0.050    2.079    0.038    0.163    0.163
##     PRC_soc           0.209    0.056    3.755    0.000    0.293    0.293
##   CB_soc ~~                                                             
##     PRC_soc           0.122    0.058    2.111    0.035    0.166    0.166
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .crit_1            0.808    0.073   11.119    0.000    0.808    0.585
##    .crit_2            0.812    0.089    9.074    0.000    0.812    0.576
##    .crit_3            0.650    0.073    8.913    0.000    0.650    0.506
##    .cult_1            0.479    0.057    8.455    0.000    0.479    0.287
##    .cult_2            0.466    0.057    8.149    0.000    0.466    0.324
##    .cult_3            1.009    0.074   13.671    0.000    1.009    0.588
##    .str_1r            0.922    0.090   10.199    0.000    0.922    0.597
##    .str_2r            0.686    0.097    7.074    0.000    0.686    0.571
##    .str_3r            0.791    0.105    7.550    0.000    0.791    0.534
##    .cbs_1             1.012    0.109    9.245    0.000    1.012    0.604
##    .cbs_2             0.407    0.122    3.324    0.001    0.407    0.269
##    .cbs_3             1.482    0.144   10.299    0.000    1.482    0.690
##    .pcc_1             0.505    0.061    8.328    0.000    0.505    0.383
##    .pcc_2             0.599    0.074    8.141    0.000    0.599    0.464
##    .pcc_3             0.643    0.062   10.444    0.000    0.643    0.516
##     CC_soc            0.574    0.084    6.811    0.000    1.000    1.000
##     Ctr_soc           1.188    0.098   12.179    0.000    1.000    1.000
##     Stp_soc           0.622    0.105    5.947    0.000    1.000    1.000
##     CB_soc            0.665    0.115    5.794    0.000    1.000    1.000
##     PRC_soc           0.814    0.089    9.183    0.000    1.000    1.000
## 
## R-Square:
##                    Estimate
##     crit_1            0.415
##     crit_2            0.424
##     crit_3            0.494
##     cult_1            0.713
##     cult_2            0.676
##     cult_3            0.412
##     str_1r            0.403
##     str_2r            0.429
##     str_3r            0.466
##     cbs_1             0.396
##     cbs_2             0.731
##     cbs_3             0.310
##     pcc_1             0.617
##     pcc_2             0.536
##     pcc_3             0.484

#Additional Visualizations ##crit_1 historgram

#histogram for crit_1
Crit1Hist <- ggplot(ESSD) +
  geom_histogram(aes(x = crit_1), bins = 5, col="black", fill = 'darkolivegreen4')

#see histogram 
Crit1Hist

##crit_2 historgram

#histogram for crit_1
Crit2Hist <- ggplot(ESSD) +
  geom_histogram(aes(x = crit_2), bins = 5, col="black", fill = 'darkolivegreen4')

#see histogram 
Crit2Hist

##crit_3 historgram

#histogram for crit_1
Crit3Hist <- ggplot(ESSD) +
  geom_histogram(aes(x = crit_3), bins = 5, col="black", fill = 'darkolivegreen4')

#see histogram 
Crit3Hist

##cult_1 historgram

#histogram for cult_1
Cult1Hist <- ggplot(ESSD) +
  geom_histogram(aes(x = cult_1), bins = 5, col="black", fill = 'darkolivegreen4')

#see histogram 
Cult1Hist

##cult_2 historgram

#histogram for cult_1
Cult2Hist <- ggplot(ESSD) +
  geom_histogram(aes(x = cult_2), bins = 5, col="black", fill = 'darkolivegreen4')

#see histogram 
Cult2Hist

##cult_3 historgram

#histogram for cult_1
Cult3Hist <- ggplot(ESSD) +
  geom_histogram(aes(x = cult_3), bins = 5, col="black", fill = 'darkolivegreen4')

#see histogram 
Cult3Hist

##pcc_1 historgram

#histogram for pcc_1
Pcc1Hist <- ggplot(ESSD) +
  geom_histogram(aes(x = pcc_1), bins = 5, col="black", fill = 'darkolivegreen4')

#see histogram 
Pcc1Hist

##pcc_2 historgram

#histogram for pcc_2
Pcc2Hist <- ggplot(ESSD) +
  geom_histogram(aes(x = pcc_2), bins = 5, col="black", fill = 'darkolivegreen4')

#see histogram 
Pcc2Hist

##pcc_3 historgram

#histogram for pcc_3
Pcc3Hist <- ggplot(ESSD) +
  geom_histogram(aes(x = pcc_3), bins = 5, col="black", fill = 'darkolivegreen4')

#see histogram 
Pcc3Hist

##str_1r historgram

#histogram for str_1r
Str1Hist <- ggplot(ESSD) +
  geom_histogram(aes(x = str_1r), bins = 5, col="black", fill = 'darkolivegreen4')

#see histogram 
Str1Hist

##str_2r historgram

#histogram for str_2r
Str2Hist <- ggplot(ESSD) +
  geom_histogram(aes(x = str_2r), bins = 5, col="black", fill = 'darkolivegreen4')

#see histogram 
Str2Hist

##str_3r historgram

#histogram for str_3r
Str3Hist <- ggplot(ESSD) +
  geom_histogram(aes(x = str_3r), bins = 5, col="black", fill = 'darkolivegreen4')

#see histogram 
Str3Hist