---
title: "Base Study 2 — Quantitative Analysis"
author: "Gian Luca Liehner"
date: "`r format(Sys.Date(), '%B %d, %Y')`"
format:
  html:
    toc: true
    toc-location: left
editor: source
---

```{r setup}
#| message: false
#| echo: false
knitr::opts_chunk$set(cache = TRUE, message = TRUE)

# Loading packages 
library(tidyverse) # Data manipulation
library(questionr) # Frequency tables
library(kableExtra) # Nice kable tables
library(janitor) # Make databases look nice
library(careless) # Longstrings
library(psych) # Scale manipulation
library(Hmisc) # Social data manipulation
library(corrplot) # Correlation plots
library(likert) # Visualising likert scales
library(lubridate) # Date manipulation
library(likert)
library(sjPlot)
library(sjmisc)

# Theme used for dissertation
theme_dissertation <- function () { 
    theme_minimal(base_size=12) +
        theme(
            panel.background  = element_blank(),
            legend.background = element_rect(fill="transparent", colour=NA),
            legend.key = element_rect(fill="transparent", colour=NA), 
            text=element_text(size=12,  family="Gill Sans"),
            plot.background = element_rect(fill = "transparent", color = NA),
            axis.title.y = element_text(margin = margin(t = 0, r = 20, b = 0, l = 0), size = 12),
            axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 0, l = 0), size = 12),
            plot.title = element_text(margin=margin(0,0,10,0)),
            legend.margin=margin(l = 10,),
            #panel.grid.major = element_line(colour = "#565555", size = .25)
            plot.caption = element_text(size=8, color = "#333333", face = "italic"),
            plot.subtitle = element_text(size=8, color = "#333333")
        )
}

# Colours
colour_dis <- c(
  teal        = "#006d77",
  mint        = "#83c5be",
  off_white   = "#edf6f9",
  peach       = "#ffddd2",
  clay        = "#e29578",
  blue        = "#457b9d",
  blue_light  = "#a8dadc",
  pink        = "#ef476f",
  pink_light  = "#ffb4c8"
)
```

# Loading data

```{r loading-data}
#| warning: false
raw_data <- read_csv("raw_data.csv")
n = raw_data %>% tally()

```

# Overview of raw data

Data before cleaning and with a the data of today (2025/10/13) has N = `r raw_data %>% tally()`.

```{r overview}
is.na(raw_data) %>% colSums() %>% kable(caption = "List of all variables before cleaning including NAs", col.names = c("Variable name")) %>% kable_styling(bootstrap_options = c("striped", "hover")) %>%  scroll_box(height = "300px")
```

# Data Cleaning

## Screening

Only accepting surveys that are matching the screening criteria

```{r screening}
data <- raw_data %>% 
  filter(quality_control == "complete") %>% 
  filter(Progress == 100) %>% 
  filter(Finished == 1) %>% 
  filter(is.na(Q_TerminateFlag)) %>%  
  filter(StartDate >= lubridate::as_datetime("2022-04-28 18:00:00"))
```

Dropped cases `r data %>% tally()-n` `r  n <- data %>% tally()`

## Removing duplicated respondents

```{r remove-double-replies}
# If respondent ID is duplicated in the data, remove entries
data <- data %>%
  arrange(rid) %>%       # optional: ensures deterministic order
  distinct(rid, .keep_all = TRUE)
```

Dropped cases `r data %>% tally()-n` `r  n <- data %>% tally()`

## Filtering out minors

```{r minors}
data <- data %>% filter(age >= 18) 
```

Dropped cases `r data %>% tally()-n` `r  n <- data %>% tally()`

## Removing speeders

```{r median-time}
#| echo: false
median_completion_time <- median(as.numeric(data$`Duration (in seconds)`))
```

Median survey time is `r median_completion_time` seconds (`r round(median_completion_time/60,2)` minutes)

```{r speeders}
data <- data %>% filter(!as.numeric(`Duration (in seconds)`) < median_completion_time/2)
```

Dropped cases `r data %>% tally()-n` `r  n <- data %>% tally()`

# Data manipulation
## Collapsing experiment conditions

```{r collapsing}
#| include: false
# Extracting condition
data <- data %>%
  mutate(condition = case_when(
    watched_video_contro == "1" ~ "control",
    watched_video_glo    == "1" ~ "global",
    watched_video_local  == "1" ~ "local",
    watched_video_mixed  == "1" ~ "mixed",
    TRUE ~ NA_character_
  ))

# Collapsing use intention
data <- data %>%
  mutate(across(starts_with("use_intention_"), as.numeric)) %>%
  mutate(
    use_intention_1 = coalesce(!!!select(., starts_with("use_intention_1"))),
    use_intention_2 = coalesce(!!!select(., starts_with("use_intention_2"))),
    use_intention_3 = coalesce(!!!select(., starts_with("use_intention_3"))),
    use_intention_4 = coalesce(!!!select(., starts_with("use_intention_4"))),
    use_intention_5 = coalesce(!!!select(., starts_with("use_intention_5")))
  ) 
# Collapsing trust
data <- data %>%
  mutate(across(starts_with("trust_"), as.numeric)) %>%
  mutate(
    trust_1 = coalesce(!!!select(., starts_with("trust_1"))),
    trust_2 = coalesce(!!!select(., starts_with("trust_2"))),
    trust_3 = coalesce(!!!select(., starts_with("trust_3"))),
    trust_4 = coalesce(!!!select(., starts_with("trust_4"))),
    trust_5 = coalesce(!!!select(., starts_with("trust_5"))),
    trust_6 = coalesce(!!!select(., starts_with("trust_6"))),
    trust_7 = coalesce(!!!select(., starts_with("trust_7"))),
    trust_8 = coalesce(!!!select(., starts_with("trust_8"))),
    trust_9 = coalesce(!!!select(., starts_with("trust_9"))),
  )
# Collapsing taigers
data <- data %>%
  mutate(across(starts_with("taigers_"), as.numeric)) %>%
  mutate(
    taigers_1 = coalesce(!!!select(., starts_with("taigers_1"))),
    taigers_2 = coalesce(!!!select(., starts_with("taigers_2"))),
    taigers_3 = coalesce(!!!select(., starts_with("taigers_3"))),
    taigers_4 = coalesce(!!!select(., starts_with("taigers_4")))
  ) 
```
New variable names are:

- `condition` for the experimental condition
- `use_intention_1` to `use_intention_5`
- `trust_1` to `trust_9`
- `taigers_1` to `taigers_4`

## Data selection

Selecting only relevant data for dissertation

```{r data-selection}
data <- data %>% select(
  ResponseId,
  age,
  gender,
  school_edu,
  professional_edu,
  professional_edu_text = professional_edu_4_TEXT,
  jobtype,
  starts_with("dispo_trust"),
  starts_with("locus_control"),
  starts_with("ati"),
  starts_with("mental_model"),
  starts_with("ai_exp"),
  starts_with("gaais"), 
  condition,
  use_intention_1:use_intention_5,
  trust_1:trust_9,
  taigers_1:taigers_4,
  rid
)
```

## Longstring cleaning

```{r longstring}
# Locus of control
data <- data %>% filter(longstring(select(., starts_with("locus"))) != 4)
data <- data %>% filter(longstring(select(., starts_with("ati"))) != 9)
data <- data %>% filter(longstring(select(., starts_with("gaais"))) != 20)
```

Dropped cases `r data %>% tally()-n` `r  n <- data %>% tally()`

---

**Final N:** `r n`


## Recoding data

```{r conversion}
# Converting to numeric 
data <- data %>% mutate(across(c(-ResponseId, -professional_edu_text, -condition), ~ as.numeric(.x)))

# Recoding gender 
data$gender <- factor(data$gender,
                      levels = c(1,2,3,4),
                      labels = c("male",
                                 "female",
                                 "diverse",
                                 "not specified"
                                 )
                      )

data$school_edu <- factor(data$school_edu,
                          levels = c(1,2,3,4),
                          labels = c("No education degree",
                                      "Secondary school degree (i.e. Hauptschulabschluss)",
                                      "Secondary school degree (i.e. Realschulabschluss)",
                                      "High school diploma (e.g Abitur)")
                          )

data$professional_edu <- factor(data$professional_edu,
                          levels = c(1,2,3),
                          labels = c("No professional education degree",
                                     "Completed apprenticeship",
                                     "Completed studies")
                          )

data$jobtype <- factor(data$jobtype,
                          levels = c(1,2,3,4,5,6,7),
                          labels = c("Student/ apprenticeship",
                                     "Employee & Student",
                                     "Employee",
                                     "Employer",
                                     "Self employed without employees",
                                     "Retired",
                                     "currently unemployed")
                          )

```

# Sample description

## Age
```{r age}
#| echo: false
psych::describe(data$age, fast = TRUE) %>% kable(caption = "Descriptives for age", digits = 2, row.names = FALSE) %>%  kable_material()
data %>% 
  ggplot +
  aes(age) +
  geom_histogram(binwidth = 1, fill=colour_dis["mint"], color=colour_dis["teal"], size=.3) +
  geom_density(aes(y = ..density..*nrow(data)), color=colour_dis["pink"]) +
  geom_vline(xintercept = mean(data$age), linetype="dotted", color = colour_dis["pink"], size=1.5) +
  labs(x="Age", title = "Age histrogram", y="Absolute count", caption = "Blue dotted line represents mean, and pink solid line represents density") +
  theme_dissertation()
```

## Gender

```{r gender}
#| echo: false
questionr::freq(data$gender, sort = "dec") %>% kable(caption = "Frequency table for gender", digits = 2) %>%  kable_material()
```

## School education

```{r  school_edu}
#| echo: false
questionr::freq(data$school_edu, sort = "dec") %>% kable(caption = "Frequency table for school education", digits = 2) %>%  kable_material()
```

## Professional education


```{r prof_edu}
#| echo: false
questionr::freq(data$professional_edu, sort = "dec") %>% kable(caption = "Frequency table for professional education", digits = 2) %>%  kable_material()
```

```{r prof_edu_others}
#| echo: false
data %>% filter(!is.na(professional_edu_text)) %>% select(professional_edu_text) %>% kable(caption = "Other education levels for professional education") %>% kable_material()
```

## Occupation 

```{r occupation}
#| echo: false
questionr::freq(data$jobtype, sort = "dec") %>% kable(caption = "Frequency table for current occupation", digits = 2) %>%  kable_material()
```

# Personality traits

## Locus of control

```{r control}
#| echo: false

control_items <- data %>% select(starts_with("locus"))
control_keys <- list(control_scores = c(
  "locus_control_1",
  "locus_control_2",
  "-locus_control_3_n",
  "-locus_control_4_n"
))

control_scores <- scoreItems(control_keys, control_items, min = 1, max = 6)

data <- data %>% bind_cols(control_scores$scores %>% as_tibble())
control_scores$scores %>% psych::describe(fast = TRUE, ranges = FALSE) %>% kable(caption = "Scores for scale 'locus of control'", digits = 2, row.names = FALSE) %>% kable_material()
```

The reliability for the *locus of control scale* is `r round(control_scores$alpha, 2)`.

## Disposition to trust

```{r dispo-trust}
#| echo: false

dispo_trust_items <- data %>% select(starts_with("dispo_trust"))
dispo_trust_keys <- list(dispo_trust_scores = c(
  "dispo_trust_1",
  "dispo_trust_2",
  "dispo_trust_3",
  "dispo_trust_4"
))

dispo_trust_scores <- scoreItems(dispo_trust_keys, dispo_trust_items, min = 1, max = 6)

data <- data %>% bind_cols(dispo_trust_scores$scores %>% as_tibble())
dispo_trust_scores$scores %>% psych::describe(fast = TRUE, ranges = FALSE) %>% kable(caption = "Scores for scale 'trust disposition'", digits = 2, row.names = FALSE) %>% kable_material()
```

The reliability for the *disposition to trust scale* is `r round(dispo_trust_scores$alpha, 2)`

## Affinity for technology


```{r ati}
#| echo: false

ati_items <- data %>% select(starts_with("ati"))
ati_keys <- list(ati_scores = c(
  "ati_1",
  "ati_2",
  "-ati_3_n",
  "ati_4",
  "ati_5",
  "-ati_6_n",
  "ati_7",
  "-ati_8_n",
  "ati_9"
))

ati_scores <- scoreItems(ati_keys, ati_items, min = 1, max = 6)

data <- data %>% bind_cols(ati_scores$scores %>% as_tibble())
ati_scores$scores %>% psych::describe(fast = TRUE, ranges = FALSE) %>% kable(caption = "Scores for scale 'affinity for technology'", digits = 2, row.names = FALSE) %>% kable_material()
```

The reliability for the *affinty for technology scale* is `r round(ati_scores$alpha, 2)`

## Experience with AI 

```{r exp}
#| echo: false

ai_exp_items <- data %>% select(starts_with("ai_exp"))
ai_exp_keys <- list(ai_exp_scores = c(
  "ai_exp_1",
  "ai_exp_2",
  "ai_exp_3",
  "ai_exp_4",
  "ai_exp_5",
  "ai_exp_6",
  "ai_exp_7",
  "ai_exp_8",
  "ai_exp_9"
))

ai_exp_scores <- scoreItems(ai_exp_keys, ai_exp_items, min = 1, max = 6)

data <- data %>% bind_cols(ai_exp_scores$scores %>% as_tibble())
ai_exp_scores$scores %>% psych::describe(fast = TRUE, ranges = FALSE) %>% kable(caption = "Scores for scale 'experience with AI'", digits = 2, row.names = FALSE) %>% kable_material()
```


The reliability for the *Experience with AI scale* is `r round(ai_exp_scores$alpha, 2)`

## General attitude towards AI (GAAIS) 

```{r gaais}
#| echo: false

gaais_items <- data %>% select(starts_with("gaais"))
gaais_keys <- list(gaais_scores = c(
  "gaais_1",
  "gaais_2",
  "-gaais_3_n",
  "gaais_4",
  "gaais_5",
  "-gaais_6_n",
  "gaais_7",
  "gaais_8_n",
  "-gaais_9_n",
  "-gaais_10_n",
  "gaais_11",
  "gaais_12",
  "gaais_13",
  "gaais_14",
  "-gaais_15_n",
  "gaais_16",
  "gaais_17",
  "gaais_18",
  "-gaais_19_n",
  "-gaais_20_n"
))

gaais_scores <- scoreItems(gaais_keys, gaais_items, min = 1, max = 6)

data <- data %>% bind_cols(gaais_scores$scores %>% as_tibble())
gaais_scores$scores %>% psych::describe(fast = TRUE, ranges = FALSE) %>% kable(caption = "Scores for scale 'GAAIS'", digits = 2, row.names = FALSE) %>% kable_material()
```


The reliability for the *GAAIS scale* is `r round(gaais_scores$alpha, 2)`

# Descriptives

## Mental models

```{r}
#| echo: false
#| message: false

data %>% select(starts_with("mental")) -> mental_models_likert_data

likert_plot_mental_models<- plot_likert(mental_models_likert_data, 
                           values = "sum.outside",
                           show.prc.sign = TRUE,  
                           wrap.labels = 64, 
                           title = "Mental models of AI", 
                           reverse.scale = TRUE, 
                           axis.titles=rev(c("Level of agreement", "Statements about artificial intelligence")),
                           axis.labels=c(
                             "3. AI is a software tool like any other.",
                             "2. AI is like a team member",
                             "4. AI is nothing more than linear algebra.",
                             "1. AI has something magical, \n superhuman about it."
                                    ), 
                           sort.frq = "pos.asc",
                           show.n = FALSE) + 
  theme_dissertation()+
  scale_fill_manual(labels=c("strongly disagree", "disagree", "rather disagree", "rather agree", "agree", "strongly agree"),
                    values=c("#ed6976", "#f18c90", "#f5acac", "#a6d5c1", "#81c6ab", "#50b796"))+
  theme(legend.position="bottom", text = element_text(size = 20)) # Legende unten

likert_plot_mental_models
ggsave('./exports/mental_models_ai.png', width = 14, height = 9, units = "cm", scale = 2) # Speichern der Grafik als PNG
ggsave('./exports/mental_models_ai.svg', width = 14, height = 9, units = "cm", scale = 2) # Speichern der Grafik als PNG
```



## Trust

```{r trust}
#| echo: false

trust_items <- data %>% select(starts_with("trust"))
trust_keys <- list(trust_scores = c(
  "-trust_1",
  "-trust_2",
  "trust_3",
  "trust_4",
  "trust_5",
  "trust_6",
  "trust_7",
  "-trust_8",
  "-trust_9"
))

trust_scores <- scoreItems(trust_keys, trust_items, min = 1, max = 6)

data <- data %>% bind_cols(trust_scores$scores %>% as_tibble())
trust_scores$scores %>% psych::describe(fast = TRUE, ranges = FALSE) %>% kable(caption = "Scores for scale 'trust in AI'", digits = 2, row.names = FALSE) %>% kable_material()
```

The reliability for the *Trust in AI* is `r round(trust_scores$alpha, 2)`

## Use intention

```{r use-intention}
#| echo: false

use_intention_items <- data %>% select(starts_with("use_intention"))
use_intention_keys <- list(use_intention_scores = c(
  "use_intention_1",
  "-use_intention_2",
  "use_intention_3",
  "use_intention_4"
))

use_intention_scores <- scoreItems(use_intention_keys, use_intention_items, min = 1, max = 6)

data <- data %>% bind_cols(use_intention_scores$scores %>% as_tibble())
use_intention_scores$scores %>% psych::describe(fast = TRUE, ranges = FALSE) %>% kable(caption = "Scores for scale 'use intention of AI'", digits = 2, row.names = FALSE) %>% kable_material()
```

# Interference
The reliability for the *Use intention of AI* is `r round(use_intention_scores$alpha, 2)` 

```{r recoding-explanation}
#| echo: false
data <- data %>%
  mutate(explanation = if_else(condition == "control", 0, 1)) %>% 
  mutate(global = if_else(condition == "global" | condition == "mixed", 1, 0)) %>% 
  mutate(local = if_else(condition == "local" | condition == "mixed", 1, 0)) %>% 
    mutate(explanation = factor(explanation,
                                levels = c(0,1),
                                labels = c("No explanation", "Explanation")
                                )
    )
```

## Model 1: Experiment condition

```{r}
model_1 <- lm(cbind(trust_scores, use_intention_scores) ~ local * global, data)
summary(model_1)

```

## Model 2: User diversity

```{r model-2}
model_2 <- lm(cbind(trust_scores, use_intention_scores) ~ local * global + age + dispo_trust_scores + ati_scores + gaais_scores, data)

summary(model_2, test="Pillai")

```

```{r}
anova(model_1, model_2)
summary(model_2)$r.squared 
```


# Exkurs

Also evaluating the TAIGERS variable

## TAIGERS variable

```{r taigers}
#| echo: false

taigers_items <- data %>% select(taigers_1, taigers_2, taigers_3, taigers_4)
taigers_keys <- list(taigers_scores = c(
  "taigers_1",
  "taigers_2",
  "taigers_3",
  "taigers_4"

))

taigers_scores <- scoreItems(taigers_keys, taigers_items, min = 1, max = 6)

data <- data %>% bind_cols(taigers_scores$scores %>% as_tibble())

taigers_scores$scores %>% psych::describe(fast = TRUE, ranges = FALSE) %>% kable(caption = "Scores for scale 'understandability'", digits = 2, row.names = FALSE) %>% kable_material()
```

The reliability for the *explanatory power* is `r round(taigers_scores$alpha, 2)`

```{r}
model_3 <- manova(cbind(trust_scores, use_intention_scores, taigers_scores) ~ local * global + age + dispo_trust_scores + ati_scores + gaais_scores, data)
summary(model_3, test = "Pillai")
summary.aov(model_2)

anova(model_1, model_2)
```


