---
title: "Zukunft Paper: LGCM Nonlinear"
author: "TDK"
date: "2025-02-03"
output: html_document
---

Zukunft Paper: LGCM Nonlinear – Latent Growth Curve Models

Dieses Dokument zeigt den kompletten Workflow von der Datenaufbereitung über die Modellschätzung bis zur Visualisierung der latent growth curve Modelle.

1. Setup und Datenimport

# Setze das Working Directory auf den Ordner dieser Datei
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
# Lade benötigte Pakete (falls nicht installiert, installiere sie vorher)
library(lavaan)
library(readr)
library(dplyr)
library(semPlot)
library(semTools)
library(ggplot2)
library(haven)
library(skimr)
library(purrr)
library(stringr)
library(tidyr)
# Lade externe Funktionen (z.B. transform_data, model_fit_calc) aus einer separaten Datei
source("functions_for_models.R")
# Lese den Datensatz ein (Passe den Pfad ggf. an)
data <- read_dta("Quantitative Daten/Datensatz/allwaves_2850cases(1).dta")
# Optional: View(data)
# Transformiere die Daten (Funktion aus functions_for_models.R)
data <- transform_data(data)

# Wandele alle 'haven_labelled'-Spalten in numerische Spalten um
data[] <- lapply(data, function(x) {
  if (inherits(x, "haven_labelled")) {
    as.numeric(x)
  } else {
    x
  }
})

2. Überblick über die Originalvariablen

# Übersicht für das "Gesund"-Modell:
skim(data %>% select(gesund2, gesund3, gesund4, gesund5))
Data summary
Name data %>% select(gesund2, …
Number of rows 772
Number of columns 4
_______________________
Column type frequency:
numeric 4
________________________
Group variables None

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
gesund2 84 0.89 1.24 0.50 1 1 1 1 4 ▇▂▁▁▁
gesund3 335 0.57 1.28 0.55 1 1 1 1 4 ▇▂▁▁▁
gesund4 341 0.56 1.32 0.60 1 1 1 2 4 ▇▂▁▁▁
gesund5 462 0.40 1.39 0.62 1 1 1 2 4 ▇▃▁▁▁
skim(data %>% select(v_201_22, v_201_32, v_201_41, v_201_51))
Data summary
Name data %>% select(v_201_22,…
Number of rows 772
Number of columns 4
_______________________
Column type frequency:
numeric 4
________________________
Group variables None

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
v_201_22 84 0.89 1.24 0.50 1 1 1 1 4 ▇▂▁▁▁
v_201_32 335 0.57 1.28 0.55 1 1 1 1 4 ▇▂▁▁▁
v_201_41 336 0.56 1.36 0.73 1 1 1 2 6 ▇▁▁▁▁
v_201_51 462 0.40 1.39 0.62 1 1 1 2 4 ▇▃▁▁▁
# Übersicht für das "Beruf"-Modell:
skim(data %>% select(beruf2, beruf3, beruf4, beruf5))
Data summary
Name data %>% select(beruf2, b…
Number of rows 772
Number of columns 4
_______________________
Column type frequency:
numeric 4
________________________
Group variables None

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
beruf2 88 0.89 1.23 0.53 1 1 1 1 4 ▇▂▁▁▁
beruf3 337 0.56 1.17 0.43 1 1 1 1 3 ▇▁▁▁▁
beruf4 337 0.56 1.22 0.51 1 1 1 1 4 ▇▂▁▁▁
beruf5 462 0.40 1.27 0.58 1 1 1 1 4 ▇▂▁▁▁
# Übersicht für das "Familie"-Modell:
skim(data %>% select(familie2, familie3, familie4, familie5))
Data summary
Name data %>% select(familie2,…
Number of rows 772
Number of columns 4
_______________________
Column type frequency:
numeric 4
________________________
Group variables None

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
familie2 115 0.85 1.52 0.79 1 1 1 2 4 ▇▃▁▁▁
familie3 346 0.55 1.56 0.87 1 1 1 2 4 ▇▂▁▂▁
familie4 365 0.53 1.70 0.94 1 1 1 2 4 ▇▃▁▂▁
familie5 478 0.38 1.81 1.00 1 1 1 2 4 ▇▃▁▂▂
# Übersicht für das "Freunde"-Modell:
skim(data %>% select(freunde2, freunde3, freunde4, freunde5))
Data summary
Name data %>% select(freunde2,…
Number of rows 772
Number of columns 4
_______________________
Column type frequency:
numeric 4
________________________
Group variables None

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
freunde2 79 0.90 1.27 0.54 1 1 1 1 4 ▇▂▁▁▁
freunde3 331 0.57 1.25 0.53 1 1 1 1 4 ▇▂▁▁▁
freunde4 344 0.55 1.30 0.59 1 1 1 1 4 ▇▂▁▁▁
freunde5 462 0.40 1.31 0.66 1 1 1 1 4 ▇▂▁▁▁

3. Schätzung der Latent Growth Curve Modelle

3.1 Familie

model_familie <- '
  i =~ 1*familie2 + 1*familie3 + 1*familie4 + 1*familie5
  s =~ 0*familie2 + a*familie3 + b*familie4 + 3*familie5

  i ~ 1
  s ~ 1

  familie2 ~ 0*1
  familie3 ~ 0*1
  familie4 ~ 0*1
  familie5 ~ 0*1

  i ~~ i
  s ~~ s
  i ~~ s
'

fit_familie <- sem(
  model_familie,
  data      = data,
  missing   = "ML",
  estimator = "MLR",
  cluster   = "schulid"
)

cat("Fit Familie:\n")
## Fit Familie:
summary(fit_familie, fit.measures = TRUE, standardized = TRUE)
## lavaan 0.6-18 ended normally after 36 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        11
## 
##                                                   Used       Total
##   Number of observations                           705         772
##   Number of clusters [schulid]                      97            
##   Number of missing patterns                        15            
## 
## Model Test User Model:
##                                               Standard      Scaled
##   Test Statistic                                10.780      11.897
##   Degrees of freedom                                 3           3
##   P-value (Chi-square)                           0.013       0.008
##   Scaling correction factor                                  0.906
##     Yuan-Bentler correction (Mplus variant)                       
## 
## Model Test Baseline Model:
## 
##   Test statistic                               637.575     386.607
##   Degrees of freedom                                 6           6
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  1.649
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.988       0.977
##   Tucker-Lewis Index (TLI)                       0.975       0.953
##                                                                   
##   Robust Comparative Fit Index (CFI)                         0.986
##   Robust Tucker-Lewis Index (TLI)                            0.972
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -1971.426   -1971.426
##   Scaling correction factor                                  1.504
##       for the MLR correction                                      
##   Loglikelihood unrestricted model (H1)      -1966.036   -1966.036
##   Scaling correction factor                                  1.376
##       for the MLR correction                                      
##                                                                   
##   Akaike (AIC)                                3964.852    3964.852
##   Bayesian (BIC)                              4014.992    4014.992
##   Sample-size adjusted Bayesian (SABIC)       3980.065    3980.065
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.061       0.065
##   90 Percent confidence interval - lower         0.024       0.028
##   90 Percent confidence interval - upper         0.102       0.108
##   P-value H_0: RMSEA <= 0.050                    0.270       0.221
##   P-value H_0: RMSEA >= 0.080                    0.246       0.317
##                                                                   
##   Robust RMSEA                                               0.090
##   90 Percent confidence interval - lower                     0.029
##   90 Percent confidence interval - upper                     0.157
##   P-value H_0: Robust RMSEA <= 0.050                         0.118
##   P-value H_0: Robust RMSEA >= 0.080                         0.667
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.045       0.045
## 
## Parameter Estimates:
## 
##   Standard errors                        Robust.cluster
##   Information                                  Observed
##   Observed information based on                 Hessian
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   i =~                                                                  
##     familie2          1.000                               0.725    0.918
##     familie3          1.000                               0.725    0.858
##     familie4          1.000                               0.725    0.802
##     familie5          1.000                               0.725    0.715
##   s =~                                                                  
##     familie2          0.000                               0.000    0.000
##     familie3   (a)    1.265    0.319    3.960    0.000    0.291    0.344
##     familie4   (b)    2.831    0.529    5.357    0.000    0.651    0.720
##     familie5          3.000                               0.690    0.680
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   i ~~                                                                  
##     s                -0.060    0.036   -1.655    0.098   -0.361   -0.361
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##     i                 1.519    0.033   45.739    0.000    2.094    2.094
##     s                 0.060    0.014    4.404    0.000    0.259    0.259
##    .familie2          0.000                               0.000    0.000
##    .familie3          0.000                               0.000    0.000
##    .familie4          0.000                               0.000    0.000
##    .familie5          0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##     i                 0.526    0.085    6.226    0.000    1.000    1.000
##     s                 0.053    0.020    2.617    0.009    1.000    1.000
##    .familie2          0.099    0.075    1.308    0.191    0.099    0.158
##    .familie3          0.256    0.037    6.845    0.000    0.256    0.358
##    .familie4          0.208    0.067    3.099    0.002    0.208    0.255
##    .familie5          0.388    0.086    4.532    0.000    0.388    0.377

3.2 Freunde

model_freunde <- '
  i =~ 1*freunde2 + 1*freunde3 + 1*freunde4 + 1*freunde5
  s =~ 0*freunde2 + a*freunde3 + b*freunde4 + 3*freunde5

  i ~ 1
  s ~ 1

  freunde2 ~ 0*1
  freunde3 ~ 0*1
  freunde4 ~ 0*1
  freunde5 ~ 0*1

  i ~~ i
  s ~~ s
  i ~~ s
'

fit_freunde <- sem(
  model_freunde,
  data      = data,
  missing   = "ML",
  estimator = "MLR",
  cluster   = "schulid"
)

cat("Fit Freunde:\n")
## Fit Freunde:
summary(fit_freunde, fit.measures = TRUE, standardized = TRUE)
## lavaan 0.6-18 ended normally after 56 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        11
## 
##                                                   Used       Total
##   Number of observations                           722         772
##   Number of clusters [schulid]                      97            
##   Number of missing patterns                        15            
## 
## Model Test User Model:
##                                               Standard      Scaled
##   Test Statistic                                 4.513          NA
##   Degrees of freedom                                 3           3
##   P-value (Chi-square)                           0.211          NA
##   Scaling correction factor                                     NA
##     Yuan-Bentler correction (Mplus variant)                       
## 
## Model Test Baseline Model:
## 
##   Test statistic                               163.959      97.810
##   Degrees of freedom                                 6           6
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  1.676
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.990          NA
##   Tucker-Lewis Index (TLI)                       0.981          NA
##                                                                   
##   Robust Comparative Fit Index (CFI)                         0.991
##   Robust Tucker-Lewis Index (TLI)                            0.981
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -1511.966   -1511.966
##   Scaling correction factor                                  2.350
##       for the MLR correction                                      
##   Loglikelihood unrestricted model (H1)      -1509.709   -1509.709
##   Scaling correction factor                                  1.779
##       for the MLR correction                                      
##                                                                   
##   Akaike (AIC)                                3045.931    3045.931
##   Bayesian (BIC)                              3096.333    3096.333
##   Sample-size adjusted Bayesian (SABIC)       3061.405    3061.405
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.026       0.000
##   90 Percent confidence interval - lower         0.000       0.000
##   90 Percent confidence interval - upper         0.073       0.000
##   P-value H_0: RMSEA <= 0.050                    0.748          NA
##   P-value H_0: RMSEA >= 0.080                    0.025          NA
##                                                                   
##   Robust RMSEA                                               0.038
##   90 Percent confidence interval - lower                     0.000
##   90 Percent confidence interval - upper                     0.135
##   P-value H_0: Robust RMSEA <= 0.050                         0.469
##   P-value H_0: Robust RMSEA >= 0.080                         0.316
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.026       0.026
## 
## Parameter Estimates:
## 
##   Standard errors                        Robust.cluster
##   Information                                  Observed
##   Observed information based on                 Hessian
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   i =~                                                                  
##     freunde2          1.000                               0.325    0.602
##     freunde3          1.000                               0.325    0.610
##     freunde4          1.000                               0.325    0.544
##     freunde5          1.000                               0.325    0.495
##   s =~                                                                  
##     freunde2          0.000                               0.000    0.000
##     freunde3   (a)   -0.473    5.836   -0.081    0.935   -0.026   -0.048
##     freunde4   (b)    2.864    8.116    0.353    0.724    0.155    0.259
##     freunde5          3.000                               0.162    0.247
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   i ~~                                                                  
##     s                -0.000    0.006   -0.053    0.958   -0.018   -0.018
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##     i                 1.269    0.039   32.756    0.000    3.903    3.903
##     s                 0.018    0.054    0.325    0.745    0.324    0.324
##    .freunde2          0.000                               0.000    0.000
##    .freunde3          0.000                               0.000    0.000
##    .freunde4          0.000                               0.000    0.000
##    .freunde5          0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##     i                 0.106    0.024    4.403    0.000    1.000    1.000
##     s                 0.003    0.009    0.320    0.749    1.000    1.000
##    .freunde2          0.186    0.029    6.483    0.000    0.186    0.638
##    .freunde3          0.177    0.048    3.723    0.000    0.177    0.625
##    .freunde4          0.229    0.082    2.783    0.005    0.229    0.642
##    .freunde5          0.302    0.070    4.331    0.000    0.302    0.699

3.3 Beruf

model_beruf <- '
  i =~ 1*beruf2 + 1*beruf3 + 1*beruf4 + 1*beruf5
  s =~ 0*beruf2 + a*beruf3 + b*beruf4 + 3*beruf5

  i ~ 1
  s ~ 1

  beruf2 ~ 0*1
  beruf3 ~ 0*1
  beruf4 ~ 0*1
  beruf5 ~ 0*1

  i ~~ i
  s ~~ 0*s
  i ~~ 0*s
'

fit_beruf <- sem(
  model_beruf,
  data      = data,
  missing   = "ML",
  estimator = "MLR",
  cluster   = "schulid"
)

cat("Fit Beruf:\n")
## Fit Beruf:
summary(fit_beruf, fit.measures = TRUE, standardized = TRUE)
## lavaan 0.6-18 ended normally after 113 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         9
## 
##                                                   Used       Total
##   Number of observations                           719         772
##   Number of clusters [schulid]                      97            
##   Number of missing patterns                        15            
## 
## Model Test User Model:
##                                               Standard      Scaled
##   Test Statistic                                 7.297       4.741
##   Degrees of freedom                                 5           5
##   P-value (Chi-square)                           0.199       0.448
##   Scaling correction factor                                  1.539
##     Yuan-Bentler correction (Mplus variant)                       
## 
## Model Test Baseline Model:
## 
##   Test statistic                               116.020      65.092
##   Degrees of freedom                                 6           6
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  1.782
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.979       1.000
##   Tucker-Lewis Index (TLI)                       0.975       1.005
##                                                                   
##   Robust Comparative Fit Index (CFI)                         1.000
##   Robust Tucker-Lewis Index (TLI)                            1.003
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -1323.825   -1323.825
##   Scaling correction factor                                  2.708
##       for the MLR correction                                      
##   Loglikelihood unrestricted model (H1)      -1320.177   -1320.177
##   Scaling correction factor                                  2.291
##       for the MLR correction                                      
##                                                                   
##   Akaike (AIC)                                2665.650    2665.650
##   Bayesian (BIC)                              2706.851    2706.851
##   Sample-size adjusted Bayesian (SABIC)       2678.274    2678.274
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.025       0.000
##   90 Percent confidence interval - lower         0.000       0.000
##   90 Percent confidence interval - upper         0.062       0.043
##   P-value H_0: RMSEA <= 0.050                    0.845       0.982
##   P-value H_0: RMSEA >= 0.080                    0.004       0.000
##                                                                   
##   Robust RMSEA                                               0.000
##   90 Percent confidence interval - lower                     0.000
##   90 Percent confidence interval - upper                     0.101
##   P-value H_0: Robust RMSEA <= 0.050                         0.665
##   P-value H_0: Robust RMSEA >= 0.080                         0.134
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.045       0.045
## 
## Parameter Estimates:
## 
##   Standard errors                        Robust.cluster
##   Information                                  Observed
##   Observed information based on                 Hessian
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   i =~                                                                  
##     beruf2            1.000                               0.260    0.484
##     beruf3            1.000                               0.260    0.599
##     beruf4            1.000                               0.260    0.520
##     beruf5            1.000                               0.260    0.453
##   s =~                                                                  
##     beruf2            0.000                               0.000    0.000
##     beruf3     (a)   -4.383    4.513   -0.971    0.331   -0.000   -0.000
##     beruf4     (b)   -0.345    2.232   -0.155    0.877   -0.000   -0.000
##     beruf5            3.000                               0.000    0.000
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   i ~~                                                                  
##     s                 0.000                                 NaN      NaN
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##     i                 1.233    0.021   60.113    0.000    4.739    4.739
##     s                 0.014    0.011    1.295    0.195      Inf      Inf
##    .beruf2            0.000                               0.000    0.000
##    .beruf3            0.000                               0.000    0.000
##    .beruf4            0.000                               0.000    0.000
##    .beruf5            0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##     i                 0.068    0.014    4.708    0.000    1.000    1.000
##     s                 0.000                                 NaN      NaN
##    .beruf2            0.221    0.032    6.959    0.000    0.221    0.766
##    .beruf3            0.121    0.022    5.391    0.000    0.121    0.641
##    .beruf4            0.182    0.030    6.154    0.000    0.182    0.729
##    .beruf5            0.262    0.047    5.568    0.000    0.262    0.794

3.4 Gesundheit

model_gesund <- '
  i =~ 1*gesund2 + 1*gesund3 + 1*gesund4 + 1*gesund5
  s =~ 0*gesund2 + a*gesund3 + b*gesund4 + 3*gesund5

  i ~ 1
  s ~ 1

  gesund2 ~ 0*1
  gesund3 ~ 0*1
  gesund4 ~ 0*1
  gesund5 ~ 0*1

  i ~~ i
  s ~~ s
  i ~~ s
'

fit_gesund <- sem(
  model_gesund,
  data      = data,
  missing   = "ML",
  estimator = "MLR",
  cluster   = "schulid"
)

cat("Fit Gesundheit:\n")
## Fit Gesundheit:
summary(fit_gesund, fit.measures = TRUE, standardized = TRUE)
## lavaan 0.6-18 ended normally after 49 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        11
## 
##                                                   Used       Total
##   Number of observations                           719         772
##   Number of clusters [schulid]                      97            
##   Number of missing patterns                        15            
## 
## Model Test User Model:
##                                               Standard      Scaled
##   Test Statistic                                 6.603       7.090
##   Degrees of freedom                                 3           3
##   P-value (Chi-square)                           0.086       0.069
##   Scaling correction factor                                  0.931
##     Yuan-Bentler correction (Mplus variant)                       
## 
## Model Test Baseline Model:
## 
##   Test statistic                               163.925      89.263
##   Degrees of freedom                                 6           6
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  1.836
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.977       0.951
##   Tucker-Lewis Index (TLI)                       0.954       0.902
##                                                                   
##   Robust Comparative Fit Index (CFI)                         0.982
##   Robust Tucker-Lewis Index (TLI)                            0.963
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -1449.145   -1449.145
##   Scaling correction factor                                  2.150
##       for the MLR correction                                      
##   Loglikelihood unrestricted model (H1)      -1445.844   -1445.844
##   Scaling correction factor                                  1.889
##       for the MLR correction                                      
##                                                                   
##   Akaike (AIC)                                2920.291    2920.291
##   Bayesian (BIC)                              2970.647    2970.647
##   Sample-size adjusted Bayesian (SABIC)       2935.719    2935.719
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.041       0.044
##   90 Percent confidence interval - lower         0.000       0.000
##   90 Percent confidence interval - upper         0.084       0.088
##   P-value H_0: RMSEA <= 0.050                    0.569       0.522
##   P-value H_0: RMSEA >= 0.080                    0.070       0.094
##                                                                   
##   Robust RMSEA                                               0.054
##   90 Percent confidence interval - lower                     0.000
##   90 Percent confidence interval - upper                     0.130
##   P-value H_0: Robust RMSEA <= 0.050                         0.372
##   P-value H_0: Robust RMSEA >= 0.080                         0.353
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.030       0.030
## 
## Parameter Estimates:
## 
##   Standard errors                        Robust.cluster
##   Information                                  Observed
##   Observed information based on                 Hessian
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   i =~                                                                  
##     gesund2           1.000                               0.367    0.740
##     gesund3           1.000                               0.367    0.671
##     gesund4           1.000                               0.367    0.617
##     gesund5           1.000                               0.367    0.589
##   s =~                                                                  
##     gesund2           0.000                               0.000    0.000
##     gesund3    (a)    2.044    0.990    2.066    0.039    0.281    0.513
##     gesund4    (b)    3.360    0.826    4.069    0.000    0.461    0.774
##     gesund5           3.000                               0.412    0.660
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   i ~~                                                                  
##     s                -0.026    0.024   -1.089    0.276   -0.515   -0.515
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##     i                 1.239    0.021   59.930    0.000    3.374    3.374
##     s                 0.028    0.013    2.191    0.028    0.206    0.206
##    .gesund2           0.000                               0.000    0.000
##    .gesund3           0.000                               0.000    0.000
##    .gesund4           0.000                               0.000    0.000
##    .gesund5           0.000                               0.000    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##     i                 0.135    0.078    1.727    0.084    1.000    1.000
##     s                 0.019    0.007    2.613    0.009    1.000    1.000
##    .gesund2           0.112    0.082    1.365    0.172    0.112    0.453
##    .gesund3           0.192    0.024    7.961    0.000    0.192    0.641
##    .gesund4           0.182    0.041    4.445    0.000    0.182    0.513
##    .gesund5           0.240    0.042    5.740    0.000    0.240    0.618

3.2 Ausgabe der Modellfit Werte fuer die Modelle

# Speichere die Modelle
save(fit_gesund, fit_beruf, fit_familie, fit_freunde, file = "rev1_lgcm_models.RData")
models <- list(fit_gesund, fit_beruf, fit_familie, fit_freunde)
model_fit_calc(models, "rev1_lgcm_models.RData")  # Funktion aus functions_for_models.R
##           Model     chisq df      AIC      BIC       CFI CFI_scaled       TLI
## 1  model_gesund  6.603210  3 2920.291 2970.647 0.9771840  0.9508832 0.9543680
## 2   model_beruf  7.296758  5 2665.650 2706.851 0.9791242  1.0000000 0.9749491
## 3 model_familie 10.780169  3 3964.852 4014.992 0.9876813  0.9766231 0.9753626
## 4 model_freunde  4.512578  3 3045.931 3096.333 0.9904242         NA 0.9808485
##   TLI_scaled      RMSEA RMSEA_scaled       SRMR
## 1  0.9017663 0.04087142   0.04354279 0.03037172
## 2  1.0052655 0.02527599   0.00000000 0.04458265
## 3  0.9532461 0.06065119   0.06486005 0.04459053
## 4         NA 0.02642589   0.00000000 0.02604549
##           Model     chisq df      AIC      BIC       CFI CFI_scaled       TLI
## 1  model_gesund  6.603210  3 2920.291 2970.647 0.9771840  0.9508832 0.9543680
## 2   model_beruf  7.296758  5 2665.650 2706.851 0.9791242  1.0000000 0.9749491
## 3 model_familie 10.780169  3 3964.852 4014.992 0.9876813  0.9766231 0.9753626
## 4 model_freunde  4.512578  3 3045.931 3096.333 0.9904242         NA 0.9808485
##   TLI_scaled      RMSEA RMSEA_scaled       SRMR
## 1  0.9017663 0.04087142   0.04354279 0.03037172
## 2  1.0052655 0.02527599   0.00000000 0.04458265
## 3  0.9532461 0.06065119   0.06486005 0.04459053
## 4         NA 0.02642589   0.00000000 0.02604549

4. Visualisierung der Growth-Kurven

Für alle vier Modelle werden die Slope‑Loadings extrahiert, die latenten Faktorwerte via predict() genutzt und die individuellen Trajektorien (sehr transparent) sowie die durchschnittliche Kurve (grün) werden geplottet.

# Erstelle eine Liste der Modelle mit Namen
models <- list(
  gesund   = fit_gesund,
  beruf    = fit_beruf,
  familie  = fit_familie,
  freunde  = fit_freunde
)

# Für jedes Modell: Extrahiere Slope-Loadings, berechne Vorhersagen und bringe in langes Format
pred_all_long <- map2_df(models, names(models), function(fit_obj, model_name) {
  
  # Extrahiere die Slope-Loadings (hier: frei geschätzte Werte für Wave3 und Wave4)
  loadings <- parameterEstimates(fit_obj) %>%
    filter(lhs == "s", op == "=~") %>%
    pull(est)
  
  if(length(loadings) == 0) {
    stop("Für das Modell '", model_name, "' wurden keine Slope-Loadings gefunden. Überprüfe den Namen des latenten Faktors.")
  }
  
  # Vorhersage der latenten Faktorwerte (angenommen: 1. Spalte = Intercept, 2. = Slope)
  pred_scores <- predict(fit_obj)
  
  # Berechne für jede Loading: predicted_value = i + loading * s
  pred_long <- map(loadings, function(x) {
    pred_scores[,1] + x * pred_scores[,2]
  }) %>%
    reduce(cbind) %>%                
    as.data.frame() %>%
    setNames(str_c("Wave ", 1:length(loadings))) %>%  
    mutate(id = row_number()) %>%
    pivot_longer(-id, names_to = "wave", values_to = "pred") %>%
    mutate(model = model_name)
  
  pred_long
})

# Plot: Einzeltrajektorien (sehr transparent) plus durchschnittliche Kurve (grün), je Modell in Facetten
plot_obj <- pred_all_long %>% 
  ggplot(aes(x = wave, y = pred, group = id)) +
  geom_line(alpha = 0.01) +
  stat_summary(
    aes(group = 1),
    fun = mean,
    geom = "line",
    size = 1.0,
    color = "green"
  ) +
  facet_wrap(~ model) +
  theme_bw() +
  labs(y = "Outcome", x = "Survey Wave", 
       title = "Non-linear Growth Trajectories",
       subtitle = "Einzelpfade und durchschnittlicher Verlauf pro Modell")

print(plot_obj)

library(svglite)

ggsave(filename = "rev1_plot_nonlinear_growth.svg",
       plot = plot_obj, width = 12, height = 12, dpi = 300)