---
title: "Main Analysis"
author: "Connor Esterwood"
date: "`r Sys.Date()`"
output: rmdformats::html_clean
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r, include=FALSE}

#Checks packages and installs/loads if needed.

install.packages("pacman")

pacman::p_load(
rmdformats,
readxl,
lme4,
lmerTest,
easystats,
kableExtra,
tidyverse,
formattable,
gtsummary,
psych,
sjPlot,
emmeans,
viridis,
hrbrthemes,
multcomp,
reghelper,
interactions,
formattable,
sjplot,
  )
```

```{r, include = FALSE}

# Write a small custom formatter for summary ANOVA tables

custom_format <- formatter("span",
                           style = x ~ style(#font.weight = "bold",
                             color = ifelse(
                               x < 0.05, "Green", ifelse(x > 0.05, "red", "black")
                             )),
                           x ~ icontext(ifelse(x < 0.05, "ok-sign", "remove-sign"), x))

```

## Read and Summarize Data

```{r, echo=FALSE}

## Read in data 
############ WIDE ##############

Data_Wide <- read.csv("Data_Wide.csv",fileEncoding = 'UTF-8-BOM')

############ TALL ##############


Data_Tall <- read.csv("Data_Measures.csv", fileEncoding = 'UTF-8-BOM')

##############################
## make dataset with a few variables to summarize

summary_dat <- Data_Tall %>% dplyr::select(condition,Time)

##############################
## summarize the data

summary_dat %>% tbl_summary(by=Time) %>% as_kable_extra() %>% kable_classic_2()

```

## Construct Mixed Linear Models

### Clean up Conditiosn to Experimenal and Summarize

```{r, echo=FALSE}

Experimental_Conditions <- Data_Tall[which(
    Data_Tall$condition != "No_Error"),]

##############################
## Specify Factors in Model

Experimental_Conditions$condition <- factor(Experimental_Conditions$condition, 
levels = c('No_Repair','Denial','Apology','Promise'))

Experimental_Conditions$Time <- factor(Experimental_Conditions$Time)

##############################
## make dataset with a few variables to summarize

summary_dat <- Experimental_Conditions %>% dplyr::select(
    condition,
    Time,
    Trust_Change, 
    Trust_Prop_Avg_Auto, 
    Agentic_Mind_Avg, 
    Experiential_Mind_Avg
    )

##############################
## summarize the data

summary_dat %>% tbl_summary(by = Time, statistic = list(
    all_continuous() ~ "{mean} ({sd})")) %>%
    as_kable_extra() %>%
    kable_classic_2()
```

### Run Mixed Linear Models

```{r, echo=TRUE, message=FALSE}

# Model Construction

### Factors and Leveling

Experimental_Conditions$condition <-
  factor(
    Experimental_Conditions$condition,
    levels = c('No_Repair', 'Denial', 'Apology', 'Promise')
  )

Experimental_Conditions$Time <-
  factor(Experimental_Conditions$Time, levels = c('T1', 'T2', 'T3'))



### Create Models

Baseline <- lmer(
  Trust_Change ~
    condition +
    Agentic_Mind_Avg +
    Experiential_Mind_Avg +
    Time +
    Trust_Prop_Avg_Auto +
    (1 | Sub_ID),
  data = Experimental_Conditions
)



Reduced_1 <- lmer(
  Trust_Change ~
    condition +
    Agentic_Mind_Avg +
    Experiential_Mind_Avg +
    Time +
    Trust_Prop_Avg_Auto +
    condition:Agentic_Mind_Avg +
    condition:Experiential_Mind_Avg +
    (1 | Sub_ID),
  data = Experimental_Conditions
)


Reduced_2 <- lmer(
  Trust_Change ~
    condition +
    Agentic_Mind_Avg +
    Experiential_Mind_Avg +
    Time +
    Trust_Prop_Avg_Auto +
    condition:Agentic_Mind_Avg +
    condition:Experiential_Mind_Avg +
    Agentic_Mind_Avg:Time +
    Experiential_Mind_Avg:Time +
    (1 | Sub_ID),
  data = Experimental_Conditions
)



Reduced_3 <- lmer(
  Trust_Change ~
    condition +
    Agentic_Mind_Avg +
    Experiential_Mind_Avg +
    Time +
    Trust_Prop_Avg_Auto +
    condition:Agentic_Mind_Avg +
    condition:Experiential_Mind_Avg +
    Time:Experiential_Mind_Avg +
    Time:Agentic_Mind_Avg +
    condition:Time +
    (1 | Sub_ID),
  data = Experimental_Conditions
)


Full_Model <- lmer(
  Trust_Change ~
    condition +
    Agentic_Mind_Avg +
    Experiential_Mind_Avg +
    Time +
    Trust_Prop_Avg_Auto +
    condition:Agentic_Mind_Avg +
    condition:Experiential_Mind_Avg +
    Time:Agentic_Mind_Avg +
    Time:Experiential_Mind_Avg +
    Time:condition +
    condition:Agentic_Mind_Avg:Time +
    condition:Experiential_Mind_Avg:Time +
    (1 | Sub_ID),
  data = Experimental_Conditions
)


```

### Summarize Results of Model

```{r, echo=FALSE}

tab_model(Baseline, Reduced_1, Reduced_2, Reduced_3, Full_Model)

```

### Compare Models

```{r, echo=FALSE}

anova(Baseline, Reduced_1, Reduced_2, Reduced_3, Full_Model)

```

### Construct a Supplmentary Correlation Matrix For Reduced Model

> Note only examined and reported the correlations among main effects (apologies, denials, promises, agetnic mind, experiential mind, and violation event (Time)) in this correlation plot for our analysis.

```{r, echo=FALSE}

cortab <- (cov2cor(vcov(Reduced_1)))

cortab

```

## Probing interactions

### Agentic Mind Attribution

Vizualize Model Interactions

```{r, echo=FALSE}

interact_plot(model = Reduced_1, 
pred = Agentic_Mind_Avg, 
modx = condition, 
colors = "CUD", 
x.label = "Agentic Mind", 
y.label = "Trust Change"
)

```

#### Slopes Signficiant From Zero?

Are the Slopes significantly different from zero?

```{r, echo=FALSE}

sig_f_0 <- emtrends(Reduced_1, ~ condition, var = "Agentic_Mind_Avg")

test(sig_f_0)

```

#### Slopes Signficiant From Each Other?

Are the slopes different from each other?

```{r,echo=FALSE}

######################
# Removed No Repair Condition

Only_Repairs <- Experimental_Conditions[which(Experimental_Conditions$condition != "No_Repair"),]

######################
# Factor and Check
factor(Only_Repairs$condition)


######################
# New Model
  

Reduced_1.1 <- lmer(
  Trust_Change ~
    condition +
    Agentic_Mind_Avg +
    Experiential_Mind_Avg +
    Time +
    Trust_Prop_Avg_Auto +
    condition:Agentic_Mind_Avg +
    condition:Experiential_Mind_Avg +
    (1 | Sub_ID),
  data = Only_Repairs
)  
  
#######################
# Pairwise Comparisons
  
sig_f_others <- emtrends(Reduced_1.1, pairwise ~ condition, var = "Agentic_Mind_Avg")

sig_f_others

```

### Experiential Mind Attribution

Vizualize Model Interactions

```{r, echo=FALSE}

interact_plot(model = Reduced_1, 
pred = Experiential_Mind_Avg, 
modx = condition, 
colors = "CUD", 
x.label = "Experiential Mind", 
y.label = "Trust Change"
)

```

#### Slopes Signficiant From Zero?

Are the Slopes significantly different from zero?

```{r, echo=FALSE}

sig_f_0 <- emtrends(Reduced_1, ~ condition, var = "Experiential_Mind_Avg")

test(sig_f_0)

```

#### Slopes Signficiant From Each Other?

Are the slopes different from each other?

```{r,echo=FALSE}

sig_f_others <- emtrends(Reduced_1.1, pairwise ~ condition, var = "Experiential_Mind_Avg")

sig_f_others$contrasts

```


