# Pakete laden ####
library(lavaan) # Strukturgleichungsmodelle
## This is lavaan 0.6-20
## lavaan is FREE software! Please report any bugs.
library(report) # Einfaches Erstellen von statistischen Berichten
library(tidyverse) # Datenmanagement und Visualisierung: https://www.tidyverse.org/

# Datensatz lesen und relevante Variablen auswählen und umbenennen ####
# from https://osf.io/5mjr4/
d <- readxl::read_excel(here::here("data/Kubin_etal_2025.xlsx")) |> 
  mutate(
    Age = as.numeric(Age),
    Gender = factor(Gender, labels = c("male", "female")),
    Harm = rowMeans(pick(starts_with("Harm"))), # harmful
    Lie = rowMeans(pick(starts_with("Lie"))), # is a lie
    Censor = rowMeans(pick(starts_with("Censor"))), # censor her
    Condition = factor(Condition, labels = c("Fact", "Experience")) # Treamtment
  ) |> 
  filter(DogAtt == 1) #14 did not pass attention checks leave 397 participants 
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `Age = as.numeric(Age)`.
## Caused by warning:
## ! NAs introduced by coercion
# Spezifikation des Modells
# Neues Outcome: Behave
modell_behave <- "
# Modelle
# Mediator 1: Harm
Harm ~ a1 * Condition
# Mediator 2: False
Lie ~ a2 * Condition 
# aV: Endorsement of Censirship
Behave ~ c_ * Condition + b1 * Harm + b2 * Lie

# Kovarianz zwischen Mediatoren
Harm ~~ Lie

# Effekte
# Indirekte Effekte
indirect_harm := a1 * b1 
indirect_lie := a2 * b2 

# Direkter Effekt
direct := c_

# Totaler Effekt
total := direct + indirect_harm + indirect_lie
"

# Ergebnisse
sem(modell_behave, data = d) |>
  report_table(metrics = FALSE, standardize = FALSE) |>
  filter(Component %in% c("Regression", "Defined")) |> 
  select(-Label, -Component, -Fit)
## Warning: lavaan->fitmeasures():  
##    Unknown argument 'standardize' for 'fitmeasures'
## Parameter                                  | Coefficient |             CI
## -------------------------------------------------------------------------
## Harm ~ Condition                           |       -0.81 | [-1.14, -0.48]
## Lie ~ Condition                            |       -1.55 | [-1.83, -1.26]
## Behave ~ Condition                         |       -0.35 | [-0.70, -0.01]
## Behave ~ Harm                              |        0.26 | [ 0.15,  0.36]
## Behave ~ Lie                               |        0.30 | [ 0.18,  0.43]
## indirect_harm := a1*b1                     |       -0.21 | [-0.33, -0.09]
## indirect_lie := a2*b2                      |       -0.47 | [-0.68, -0.26]
## direct := c_                               |       -0.35 | [-0.70, -0.01]
## total := direct+indirect_harm+indirect_lie |       -1.03 | [-1.37, -0.70]
## 
## Parameter                                  |      z |      p
## ------------------------------------------------------------
## Harm ~ Condition                           |  -4.84 | < .001
## Lie ~ Condition                            | -10.72 | < .001
## Behave ~ Condition                         |  -2.02 | 0.043 
## Behave ~ Harm                              |   4.66 | < .001
## Behave ~ Lie                               |   4.79 | < .001
## indirect_harm := a1*b1                     |  -3.36 | < .001
## indirect_lie := a2*b2                      |  -4.37 | < .001
## direct := c_                               |  -2.02 | 0.043 
## total := direct+indirect_harm+indirect_lie |  -6.01 | < .001