18  Manova

18.1 Manova

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
pisa <- read_csv("input/pisa_idn.csv") |> 
  rename(belong = ST034Q03TA,
         intimidation = ST267Q04JA)
Rows: 23368 Columns: 11
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
dbl (11): ST034Q03TA, CNTSCHID, BELONG, ST267Q04JA, CNTRYID, age, sex, ESCS,...

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
library("lme4")


fm1 <- lmer(MATH ~ ESCS + (ESCS | intimidation), pisa)

summary(fm1)
Linear mixed model fit by REML ['lmerMod']
Formula: MATH ~ ESCS + (ESCS | intimidation)
   Data: pisa

REML criterion at convergence: 258142.3

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-2.9068 -0.7186 -0.0540  0.6348  4.0977 

Random effects:
 Groups       Name        Variance Std.Dev. Corr
 intimidation (Intercept)  111.616 10.565       
              ESCS           3.849  1.962   1.00
 Residual                 3672.100 60.598       
Number of obs: 23368, groups:  intimidation, 4

Fixed effects:
            Estimate Std. Error t value
(Intercept)  401.388      5.388   74.50
ESCS          16.515      1.064   15.52

Correlation of Fixed Effects:
     (Intr)
ESCS 0.963 
optimizer (nloptwrap) convergence code: 0 (OK)
boundary (singular) fit: see help('isSingular')

18.2 Ancova

m1 <- aov(MATH ~ ESCS+BELONG, data = pisa)

18.3 Regresi Berganda

m2 <- lm(MATH ~ ESCS+BELONG+growth, data = pisa)

18.4 Pemodelan Multilevel

fm2 <- lmer(MATH ~ ESCS + (1 | CNTSCHID), 
          data = pisa, REML = FALSE)

18.5 Regresi Logistik

m3 <- glm(growth ~ MATH + ESCS, data = pisa, family = "binomial")

18.6 Analisis Faktor

#library(psych)

#data <- pisa |> 
  #select(ESCS, MATH)

#m4 <- fa(data,
         #nfactors = 2,
         #fm = "pa", 
         #rotate = "varimax") 

18.7 SEM

library("lavaan")
This is lavaan 0.6-18
lavaan is FREE software! Please report any bugs.
example(sem)

sem> ## The industrialization and Political Democracy Example 
sem> ## Bollen (1989), page 332
sem> model <- ' 
sem+   # latent variable definitions
sem+      ind60 =~ x1 + x2 + x3
sem+      dem60 =~ y1 + a*y2 + b*y3 + c*y4
sem+      dem65 =~ y5 + a*y6 + b*y7 + c*y8
sem+ 
sem+   # regressions
sem+     dem60 ~ ind60
sem+     dem65 ~ ind60 + dem60
sem+ 
sem+   # residual correlations
sem+     y1 ~~ y5
sem+     y2 ~~ y4 + y6
sem+     y3 ~~ y7
sem+     y4 ~~ y8
sem+     y6 ~~ y8
sem+ '

sem> fit <- sem(model, data = PoliticalDemocracy)

sem> summary(fit, fit.measures = TRUE)
lavaan 0.6-18 ended normally after 66 iterations

  Estimator                                         ML
  Optimization method                           NLMINB
  Number of model parameters                        31
  Number of equality constraints                     3

  Number of observations                            75

Model Test User Model:
                                                      
  Test statistic                                40.179
  Degrees of freedom                                38
  P-value (Chi-square)                           0.374

Model Test Baseline Model:

  Test statistic                               730.654
  Degrees of freedom                                55
  P-value                                        0.000

User Model versus Baseline Model:

  Comparative Fit Index (CFI)                    0.997
  Tucker-Lewis Index (TLI)                       0.995

Loglikelihood and Information Criteria:

  Loglikelihood user model (H0)              -1548.818
  Loglikelihood unrestricted model (H1)      -1528.728
                                                      
  Akaike (AIC)                                3153.636
  Bayesian (BIC)                              3218.526
  Sample-size adjusted Bayesian (SABIC)       3130.277

Root Mean Square Error of Approximation:

  RMSEA                                          0.028
  90 Percent confidence interval - lower         0.000
  90 Percent confidence interval - upper         0.087
  P-value H_0: RMSEA <= 0.050                    0.665
  P-value H_0: RMSEA >= 0.080                    0.083

Standardized Root Mean Square Residual:

  SRMR                                           0.056

Parameter Estimates:

  Standard errors                             Standard
  Information                                 Expected
  Information saturated (h1) model          Structured

Latent Variables:
                   Estimate  Std.Err  z-value  P(>|z|)
  ind60 =~                                            
    x1                1.000                           
    x2                2.180    0.138   15.751    0.000
    x3                1.818    0.152   11.971    0.000
  dem60 =~                                            
    y1                1.000                           
    y2         (a)    1.191    0.139    8.551    0.000
    y3         (b)    1.175    0.120    9.755    0.000
    y4         (c)    1.251    0.117   10.712    0.000
  dem65 =~                                            
    y5                1.000                           
    y6         (a)    1.191    0.139    8.551    0.000
    y7         (b)    1.175    0.120    9.755    0.000
    y8         (c)    1.251    0.117   10.712    0.000

Regressions:
                   Estimate  Std.Err  z-value  P(>|z|)
  dem60 ~                                             
    ind60             1.471    0.392    3.750    0.000
  dem65 ~                                             
    ind60             0.600    0.226    2.661    0.008
    dem60             0.865    0.075   11.554    0.000

Covariances:
                   Estimate  Std.Err  z-value  P(>|z|)
 .y1 ~~                                               
   .y5                0.583    0.356    1.637    0.102
 .y2 ~~                                               
   .y4                1.440    0.689    2.092    0.036
   .y6                2.183    0.737    2.960    0.003
 .y3 ~~                                               
   .y7                0.712    0.611    1.165    0.244
 .y4 ~~                                               
   .y8                0.363    0.444    0.817    0.414
 .y6 ~~                                               
   .y8                1.372    0.577    2.378    0.017

Variances:
                   Estimate  Std.Err  z-value  P(>|z|)
   .x1                0.081    0.019    4.182    0.000
   .x2                0.120    0.070    1.729    0.084
   .x3                0.467    0.090    5.177    0.000
   .y1                1.855    0.433    4.279    0.000
   .y2                7.581    1.366    5.549    0.000
   .y3                4.956    0.956    5.182    0.000
   .y4                3.225    0.723    4.458    0.000
   .y5                2.313    0.479    4.831    0.000
   .y6                4.968    0.921    5.393    0.000
   .y7                3.560    0.710    5.018    0.000
   .y8                3.308    0.704    4.701    0.000
    ind60             0.449    0.087    5.175    0.000
   .dem60             3.875    0.866    4.477    0.000
   .dem65             0.164    0.227    0.725    0.469