---
title: "Supplementary online material (SOM3): Results for the WHYQUALITY question"
output:
  html_document: default
  word_document: default
---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(cache = TRUE, cache.extra = rnorm(1))
```


```{r libraries, include = FALSE, cache = FALSE}
#Libraries
library(haven) # for dealing with labelled dataframe
library(tidyverse) # for data handling
library(rstatix) # Para t-test within all pairs of groups
library(lubridate) #for dates
library(knitr) #for creating tables
library(kableExtra) #to make changes in style of the tables
library(psych) #for cohen's kappa
library(irr)#alternative cohen's kappa
```

<br>

## Table of Contents

<br>

Open question WHYQUALITY (answered by all respondents by typing in)

- [InterRater Reliability (IRR)](#interrater-reliability-(irr))
<br>
- [Valid answers](#valid-answers)
<br>
- [Abbreviations](#abbreviations)
<br>
- [Problems detected](#problems-detected)
<br>
- [Number of themes](#number-of-themes)
<br>
- [Number of characters](#number-of-characters)
<br>

```{r load_data, include = FALSE, cache = TRUE}
#Define path
path <- "yourpath" #Replace "yourpath" with the place where the data is saved
data= read_rds(file.path(path,"datafinalALLCODESregnoID.rds"))
```

```{r create_functions, include = FALSE, cache = TRUE}
#Function to get the n and proportions
prop=function(data, var){
  data %>% 
    drop_na({{var}}) %>%
    group_by({{var}}) %>%
    summarise(n=n()) %>% 
    mutate(n_cum=cumsum(n), f=n/
             sum(n), f_cum=cumsum(f)) %>%
    print(n = 80)
}

#Function for proportions but by group
propby=function(data, var1, var2){
  data %>% 
    drop_na({{var1}}, {{var2}}) %>%
    group_by({{var1}}, {{var2}}) %>%
    summarise(n=n()) %>% 
    mutate(n_cum=cumsum(n), f=n/
             sum(n), f_cum=cumsum(f))%>%
    print(n = 80)
}

#Function to test each pairs of groups for proportions
proptest = function(data,valuevar,grupvar) {
  data %>% 
    filter(!is.na({{valuevar}})) %>% #needed when there are NAs 
    filter(!is.na({{grupvar}})) %>% #when also NA in group var
    group_by(rowname={{grupvar}},{{valuevar}}) %>%
    count() %>% 
    pivot_wider(names_from = {{valuevar}},values_from = "n") %>%
    mutate( across(.fns = ~ ifelse(is.na(.x),0,.x))) %>%       
    column_to_rownames() %>% 
    pairwise_prop_test() %>% 
    mutate(
      p.signif=case_when(
        p<0.001 ~ "***",
        p<0.01 ~ "**",
        p<0.05 ~ "*",
        TRUE ~ "ns"),
        .before=4
      )
}

#Function for means, st dev, etc.
meanandmore=function(data, var){
  data %>% 
    filter(!is.na({{var}})) %>% #needed when there are NAs
    summarise(mean=mean({{var}}), stdev=sd({{var}}), min=min({{var}}), max=max({{var}}), median=median({{var}}))
}

#Function for means, st dev, etc. but by group
meanandmoreby=function(data, var1, var2){
  data %>% 
    filter(!is.na({{var1}})) %>% #needed when there are NAs
    group_by({{var2}}) %>%
    summarise(mean=mean({{var1}}), stdev=sd({{var1}}), min=min({{var1}}), max=max({{var1}}), median=median({{var1}}))
}

```




## InterRater Reliability (IRR)


```{r prep_IRR, message=FALSE, warning=FALSE, include = FALSE, cache = TRUE}
# Create a list of data frames for each variable of interest
metrics_list <- list(
  VALID = data%>% select(VALID_QUALITYC1, VALID_QUALITYC2),
  ABBR = data%>% select(ABBR_QUALITYC1, ABBR_QUALITYC2),
  PB = data%>% select(PB_QUALITYC1, PB_QUALITYC2),
  NTHEMES = data%>% select(NTHEMES_QUALITYC1, NTHEMES_QUALITYC2)
)

#Custom function to calculate Cohen's kappa for a data frame
calculate_kappa <- function(data) {
  if (nrow(data) > 0) {
    cohen.kappa(data)$kappa
  } else {
    NA
  }
}

#Custom function to calculate percentage of agreement for a data frame
calculate_agreement_percentage <- function(data) {
  if (nrow(data) > 0) {
    agree(data)$value
  } else {
    NA
  }
}

#Calculate Cohen's kappa and agreement for each metric
kappa_values <- map_dbl(metrics_list, calculate_kappa)
agreement_values <- map_dbl(metrics_list, calculate_agreement_percentage)

#Create a tibble with the results
results_tibble <- tibble(
  Metric = names(metrics_list),
  Cohen_Kappa = round(kappa_values,2),
  Agreement_Percentage = round(agreement_values, 2)
)
```

<center><b>
Cohen's Kappa and percentage of agreement across Coders 1 and 2 for each variable studied
</center></b>
```{r results_IRR, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE}
kable(results_tibble, format = "html")%>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, bold = TRUE) %>%
  row_spec(0:nrow(results_tibble), align = "c")
```


## Valid answers

### Proportions per group

<center><b>
Proportions of non sense, not in line, DK and valid answers using the codes of Coder 1
</center></b>

```{r prep_valid1, message=FALSE, include = FALSE, cache = TRUE}
tibbleValidC1 = propby(data, GRUPO, VALID_QUALITYC1)
#Select relevant columns and reshape the data using spread
table_dataValidC1 <-  tibbleValidC1 %>%
    select(VALID_QUALITYC1, GRUPO, f) %>%
     spread(key = GRUPO, value = f) %>%
     arrange(VALID_QUALITYC1)

#Extract n_cum
max_n_cum_values_ValidC1 <- tibbleValidC1 %>%
  group_by(GRUPO) %>%
  summarize(max_n_cum = max(n_cum))

#Extract the values for specific groups
control_max_n_cum_ValidC1 <- max_n_cum_values_ValidC1$max_n_cum[max_n_cum_values_ValidC1$GRUPO == "Control"]
pushdictation_max_n_cum_ValidC1 <- max_n_cum_values_ValidC1$max_n_cum[max_n_cum_values_ValidC1$GRUPO == "PushDictation"]
pushrecording_max_n_cum_ValidC1 <- max_n_cum_values_ValidC1$max_n_cum[max_n_cum_values_ValidC1$GRUPO == "PushRecording"]
choice_max_n_cum_ValidC1 <- max_n_cum_values_ValidC1$max_n_cum[max_n_cum_values_ValidC1$GRUPO == "Choice"]

#Put together last row
total_obs_row_ValidC1 <- tibble(
  VALID_QUALITYC1 = "Number of observations",
  Control = control_max_n_cum_ValidC1,
  PushDictation = pushdictation_max_n_cum_ValidC1,
  PushRecording = pushrecording_max_n_cum_ValidC1,
  Choice = choice_max_n_cum_ValidC1
)

#Put together with the rest
table_with_total_ValidC1 = bind_rows(
  table_dataValidC1 %>% 
  mutate(across(-VALID_QUALITYC1, ~ format(.x*100,nsmall=1,digits=1))) %>% #and change format decimals  
  mutate_all(as.character),
  total_obs_row_ValidC1 %>% 
  mutate_all(as.character)
  )%>%
select(1, 2, 5, 3, 4)
```

```{r result_valid1, message=FALSE, include = TRUE, cache = TRUE}
#Create the table using kable in R Markdown
kable(table_with_total_ValidC1, format = "html", col.names = c("Coder 1", "Control", "Choice", "PushDictation", "PushRecording"), align=c("l","c","c","c","c")) %>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, align = "c", bold = TRUE) 
```


<center><b>
Proportions of non sense, not in line, DK and valid answers using the codes of Coder 2
</center></b>

```{r prep_valid2, message=FALSE, include = FALSE, cache = TRUE}
tibbleValidC2 = propby(data, GRUPO, VALID_QUALITYC2)
#Select relevant columns and reshape the data using spread
table_dataValidC2 <-  tibbleValidC2 %>%
  select(VALID_QUALITYC2, GRUPO, f) %>%
  spread(key = GRUPO, value = f) %>%
  arrange(VALID_QUALITYC2)

#Extract n_cum
max_n_cum_values_ValidC2 <- tibbleValidC2 %>%
  group_by(GRUPO) %>%
  summarize(max_n_cum = max(n_cum))

#Extract the values for specific groups
control_max_n_cum_ValidC2 <- max_n_cum_values_ValidC2$max_n_cum[max_n_cum_values_ValidC2$GRUPO == "Control"]
pushdictation_max_n_cum_ValidC2 <- max_n_cum_values_ValidC2$max_n_cum[max_n_cum_values_ValidC2$GRUPO == "PushDictation"]
pushrecording_max_n_cum_ValidC2 <- max_n_cum_values_ValidC2$max_n_cum[max_n_cum_values_ValidC2$GRUPO == "PushRecording"]
choice_max_n_cum_ValidC2 <- max_n_cum_values_ValidC2$max_n_cum[max_n_cum_values_ValidC2$GRUPO == "Choice"]

#Put together last row
total_obs_row_ValidC2 <- tibble(
  VALID_QUALITYC2 = "Number of observations",
  Control = control_max_n_cum_ValidC2,
  PushDictation = pushdictation_max_n_cum_ValidC2,
  PushRecording = pushrecording_max_n_cum_ValidC2,
  Choice = choice_max_n_cum_ValidC2
)

#Put together with the rest
table_with_total_ValidC2 = bind_rows(
  table_dataValidC2 %>% 
    mutate(across(-VALID_QUALITYC2, ~ format(.x*100,nsmall=1,digits=1))) %>% #and change format decimals  
    mutate_all(as.character),
  total_obs_row_ValidC2 %>% 
    mutate_all(as.character)
)%>%
select(1, 2, 5, 3, 4)
```
 
```{r result_valid2, message=FALSE, include = TRUE, cache = TRUE}
#Create the table using kable in R Markdown
kable(table_with_total_ValidC2, format = "html", col.names = c("Coder 2", "Control", "Choice", "PushDictation", "PushRecording"), align=c("l","c","c","c","c")) %>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, align = "c", bold = TRUE) 
```

<center><b>
Proportions of non sense, not in line, DK and valid answers using the codes of Coder 3
</center></b>

```{r prep_valid3, message=FALSE, include = FALSE, cache = TRUE}
tibbleValidC3 = propby(data, GRUPO, VALID_QUALITYC3)
#Select relevant columns and reshape the data using spread
table_dataValidC3 <-  tibbleValidC3 %>%
  select(VALID_QUALITYC3, GRUPO, f) %>%
  spread(key = GRUPO, value = f) %>%
  arrange(VALID_QUALITYC3)

#Extract n_cum
max_n_cum_values_ValidC3 <- tibbleValidC3 %>%
  group_by(GRUPO) %>%
  summarize(max_n_cum = max(n_cum))

#Extract the values for specific groups
control_max_n_cum_ValidC3 <- max_n_cum_values_ValidC3$max_n_cum[max_n_cum_values_ValidC3$GRUPO == "Control"]
pushdictation_max_n_cum_ValidC3 <- max_n_cum_values_ValidC3$max_n_cum[max_n_cum_values_ValidC3$GRUPO == "PushDictation"]
pushrecording_max_n_cum_ValidC3 <- max_n_cum_values_ValidC3$max_n_cum[max_n_cum_values_ValidC3$GRUPO == "PushRecording"]
choice_max_n_cum_ValidC3 <- max_n_cum_values_ValidC3$max_n_cum[max_n_cum_values_ValidC3$GRUPO == "Choice"]

#Put together last row
total_obs_row_ValidC3 <- tibble(
  VALID_QUALITYC3 = "Number of observations",
  Control = control_max_n_cum_ValidC3,
  PushDictation = pushdictation_max_n_cum_ValidC3,
  PushRecording = pushrecording_max_n_cum_ValidC3,
  Choice = choice_max_n_cum_ValidC3
)

#Put together with the rest
table_with_total_ValidC3 = bind_rows(
  table_dataValidC3 %>% 
    mutate(across(-VALID_QUALITYC3, ~ format(.x*100,nsmall=1,digits=1))) %>% #and change format decimals  
    mutate_all(as.character),
  total_obs_row_ValidC3 %>% 
    mutate_all(as.character)
)%>%
select(1, 2, 5, 3, 4)
```

```{r result_valid3, message=FALSE, include = TRUE, cache = TRUE}
#Create the table using kable in R Markdown
kable(table_with_total_ValidC3, format = "html", col.names = c("Coder 3", "Control", "Choice", "PushDictation", "PushRecording"), align=c("l","c","c","c","c")) %>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, align = "c", bold = TRUE) 
```

### Tests of significance across groups

```{r dummies_valid, message=FALSE, include = FALSE, cache = TRUE}

#use the proptest function but need first to create dummies for each of the 4 situations (nonsense, not in line...) and coder

data<- data%>%
     mutate(nonsense_WHYQUALITYC1 = ifelse(VALID_QUALITYC1 == "1 Non sense", 1, 0),
           notinline_WHYQUALITYC1 = ifelse(VALID_QUALITYC1 == "2 Not in line", 1, 0),
           DK_WHYQUALITYC1 = ifelse(VALID_QUALITYC1 == "3 DK", 1, 0),
            valid_WHYQUALITYC1 = ifelse(VALID_QUALITYC1 == "4 Valid", 1, 0),
            )

data<- data%>%
     mutate(nonsense_WHYQUALITYC2 = ifelse(VALID_QUALITYC2 == "1 Non sense", 1, 0),
            notinline_WHYQUALITYC2 = ifelse(VALID_QUALITYC2 == "2 Not in line", 1, 0),
            DK_WHYQUALITYC2 = ifelse(VALID_QUALITYC2 == "3 DK", 1, 0),
            valid_WHYQUALITYC2 = ifelse(VALID_QUALITYC2 == "4 Valid", 1, 0),
            )

data<- data%>%
     mutate(nonsense_WHYQUALITYC3 = ifelse(VALID_QUALITYC3 == "1 Non sense", 1, 0),
            notinline_WHYQUALITYC3 = ifelse(VALID_QUALITYC3 == "2 Not in line", 1, 0),
            DK_WHYQUALITYC3 = ifelse(VALID_QUALITYC3 == "3 DK", 1, 0),
            valid_WHYQUALITYC3 = ifelse(VALID_QUALITYC3 == "4 Valid", 1, 0),
            )
```

<center><b>
Tests non sense Coder 1
</center></b>
```{r nonsense_c1, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE}
nonsenseC1=proptest(data, nonsense_WHYQUALITYC1, GRUPO)
kable(nonsenseC1, format = "html")%>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, bold = TRUE) %>%
  row_spec(0:nrow(nonsenseC1), align = "c")
```

<center><b>
Tests non sense Coder 2
</center></b>
```{r nonsense_c2, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE}
nonsenseC2=proptest(data, nonsense_WHYQUALITYC2, GRUPO)
kable(nonsenseC2, format = "html")%>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, bold = TRUE) %>%
  row_spec(0:nrow(nonsenseC2), align = "c")
```

<center><b>
Tests non sense Coder 3
</center></b>
```{r nonsense_c3, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE}
nonsenseC3=proptest(data, nonsense_WHYQUALITYC3, GRUPO)
kable(nonsenseC3, format = "html")%>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, bold = TRUE) %>%
  row_spec(0:nrow(nonsenseC3), align = "c")
```

<center><b>
Tests not in line Coder 1
</center></b>
```{r notinline_c1, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE}
notinlineC1=proptest(data, notinline_WHYQUALITYC1, GRUPO)
kable(notinlineC1, format = "html")%>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, bold = TRUE) %>%
  row_spec(0:nrow(notinlineC1), align = "c")
```

<center><b>
Tests not in line Coder 2
</center></b>
```{r notinline_c2, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE}
notinlineC2=proptest(data, notinline_WHYQUALITYC2, GRUPO)
kable(notinlineC2, format = "html")%>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, bold = TRUE) %>%
  row_spec(0:nrow(notinlineC2), align = "c")
```

<center><b>
Tests not in line Coder 3
</center></b>
```{r notinline_c3, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE}
notinlineC3=proptest(data, notinline_WHYQUALITYC3, GRUPO)
kable(notinlineC3, format = "html")%>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, bold = TRUE) %>%
  row_spec(0:nrow(notinlineC3), align = "c")
```

<center><b>
Tests DK Coder 1
</center></b>
```{r DK_c1, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE}
dkC1=proptest(data, DK_WHYQUALITYC1, GRUPO)
kable(dkC1, format = "html")%>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, bold = TRUE) %>%
  row_spec(0:nrow(dkC1), align = "c")
```

<center><b>
Tests DK Coder 2
</center></b>
```{r DK_c2, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE}
dkC2=proptest(data, DK_WHYQUALITYC2, GRUPO)
kable(dkC2, format = "html")%>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, bold = TRUE) %>%
  row_spec(0:nrow(dkC2), align = "c")
```

<center><b>
Tests DK Coder 3
</center></b>
```{r DK_c3, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE}
dkC3=proptest(data, DK_WHYQUALITYC3, GRUPO)
kable(dkC3, format = "html")%>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, bold = TRUE) %>%
  row_spec(0:nrow(dkC3), align = "c")
```

<center><b>
Tests valid Coder 1
</center></b>
```{r valid_c1, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE}
validC1=proptest(data, valid_WHYQUALITYC1, GRUPO)
kable(validC1, format = "html")%>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, bold = TRUE) %>%
  row_spec(0:nrow(validC1), align = "c")
```

<center><b>
Tests valid Coder 2
</center></b>
```{r valid_c2, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE}
validC2=proptest(data, valid_WHYQUALITYC2, GRUPO)
kable(validC2, format = "html")%>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, bold = TRUE) %>%
  row_spec(0:nrow(validC2), align = "c")
```

<center><b>
Tests valid Coder 3
</center></b>
```{r valid_c31, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE}
validC3=proptest(data, valid_WHYQUALITYC3, GRUPO)
kable(validC3, format = "html")%>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, bold = TRUE) %>%
  row_spec(0:nrow(validC3), align = "c")
```

<br>
<br>

# For valid answers only from now on

<br>


## Abbreviations

### Proportions per group

<center><b>
Abbreviations, using the codes of Coder 1
</center></b>

```{r prep_abbr1, message=FALSE, include = FALSE, cache = TRUE}
tibbleAbbrC1 = propby(data, GRUPO, ABBR_QUALITYC1)
#Select relevant columns and reshape the data using spread
table_dataAbbrC1 <-  tibbleAbbrC1 %>%
    select(ABBR_QUALITYC1, GRUPO, f) %>%
     spread(key = GRUPO, value = f) %>%
     arrange(ABBR_QUALITYC1)

#Extract n_cum
max_n_cum_values_AbbrC1 <- tibbleAbbrC1 %>%
  group_by(GRUPO) %>%
  summarize(max_n_cum = max(n_cum))

#Extract the values for specific groups
control_max_n_cum_AbbrC1 <- max_n_cum_values_AbbrC1$max_n_cum[max_n_cum_values_AbbrC1$GRUPO == "Control"]
pushdictation_max_n_cum_AbbrC1 <- max_n_cum_values_AbbrC1$max_n_cum[max_n_cum_values_AbbrC1$GRUPO == "PushDictation"]
pushrecording_max_n_cum_AbbrC1 <- max_n_cum_values_AbbrC1$max_n_cum[max_n_cum_values_AbbrC1$GRUPO == "PushRecording"]
choice_max_n_cum_AbbrC1 <- max_n_cum_values_AbbrC1$max_n_cum[max_n_cum_values_AbbrC1$GRUPO == "Choice"]

#Put together last row
total_obs_row_AbbrC1 <- tibble(
  ABBR_QUALITYC1 = "Number of observations",
  Control = control_max_n_cum_AbbrC1,
  PushDictation = pushdictation_max_n_cum_AbbrC1,
  PushRecording = pushrecording_max_n_cum_AbbrC1,
  Choice = choice_max_n_cum_AbbrC1
)

#Put together with the rest
table_with_total_AbbrC1 = bind_rows(
  table_dataAbbrC1 %>% 
  mutate(across(-ABBR_QUALITYC1, ~ format(.x*100,nsmall=1,digits=1))) %>% #and change format decimals  
  mutate_all(as.character),
  total_obs_row_AbbrC1 %>% 
  mutate_all(as.character)
  )%>%
select(1, 2, 5, 3, 4)
```

```{r result_abbr1, message=FALSE, include = TRUE, cache = TRUE}
#Create the table using kable in R Markdown
kable(table_with_total_AbbrC1, format = "html", col.names = c("Coder 1", "Control", "Choice", "PushDictation", "PushRecording"), align=c("l","c","c","c","c")) %>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, align = "c", bold = TRUE) 
```


<center><b>
Abbreviations, using the codes of Coder 2
</center></b>

```{r prep_abbr2, message=FALSE, include = FALSE, cache = TRUE}
tibbleAbbrC2 = propby(data, GRUPO, ABBR_QUALITYC2)
#Select relevant columns and reshape the data using spread
table_dataAbbrC2 <-  tibbleAbbrC2 %>%
  select(ABBR_QUALITYC2, GRUPO, f) %>%
  spread(key = GRUPO, value = f) %>%
  arrange(ABBR_QUALITYC2)

#Extract n_cum
max_n_cum_values_AbbrC2 <- tibbleAbbrC2 %>%
  group_by(GRUPO) %>%
  summarize(max_n_cum = max(n_cum))

#Extract the values for specific groups
control_max_n_cum_AbbrC2 <- max_n_cum_values_AbbrC2$max_n_cum[max_n_cum_values_AbbrC2$GRUPO == "Control"]
pushdictation_max_n_cum_AbbrC2 <- max_n_cum_values_AbbrC2$max_n_cum[max_n_cum_values_AbbrC2$GRUPO == "PushDictation"]
pushrecording_max_n_cum_AbbrC2 <- max_n_cum_values_AbbrC2$max_n_cum[max_n_cum_values_AbbrC2$GRUPO == "PushRecording"]
choice_max_n_cum_AbbrC2 <- max_n_cum_values_AbbrC2$max_n_cum[max_n_cum_values_AbbrC2$GRUPO == "Choice"]

#Put together last row
total_obs_row_AbbrC2 <- tibble(
  ABBR_QUALITYC2 = "Number of observations",
  Control = control_max_n_cum_AbbrC2,
  PushDictation = pushdictation_max_n_cum_AbbrC2,
  PushRecording = pushrecording_max_n_cum_AbbrC2,
  Choice = choice_max_n_cum_AbbrC2
)

#Put together with the rest
table_with_total_AbbrC2 = bind_rows(
  table_dataAbbrC2 %>% 
    mutate(across(-ABBR_QUALITYC2, ~ format(.x*100,nsmall=1,digits=1))) %>% #and change format decimals  
    mutate_all(as.character),
  total_obs_row_AbbrC2 %>% 
    mutate_all(as.character)
)%>%
  select(1, 2, 5, 3, 4)
```

```{r result_abbr2, message=FALSE, include = TRUE, cache = TRUE}
#Create the table using kable in R Markdown
kable(table_with_total_AbbrC2, format = "html", col.names = c("Coder 2", "Control", "Choice", "PushDictation", "PushRecording"), align=c("l","c","c","c","c")) %>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, align = "c", bold = TRUE) 
```

### Tests of significance across groups

<center><b>
Tests abbreviations Coder 1
</center></b>
```{r testabbr_c1, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE}
testabbrC1=proptest(data, ABBR_QUALITYC1, GRUPO)
kable(testabbrC1, format = "html")%>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, bold = TRUE) %>%
  row_spec(0:nrow(testabbrC1), align = "c")
```

<center><b>
Tests abbreviations Coder 2
</center></b>
```{r testabbr_c2, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE}
testabbrC2=proptest(data, ABBR_QUALITYC2, GRUPO)
kable(testabbrC2, format = "html")%>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, bold = TRUE) %>%
  row_spec(0:nrow(testabbrC2), align = "c")
```

<br>


## Problems detected

### Proportions per group

<center><b>
Problems, using the codes of Coder 1
</center></b>


```{r prep_pb1, message=FALSE, include = FALSE, cache = TRUE}
tibblePBC1 = propby(data, GRUPO, PB_QUALITYC1)
#Select relevant columns and reshape the data using spread
table_dataPBC1 <-  tibblePBC1 %>%
    select(PB_QUALITYC1, GRUPO, f) %>%
     spread(key = GRUPO, value = f) %>%
     arrange(PB_QUALITYC1)

#Extract n_cum
max_n_cum_values_PBC1 <- tibblePBC1 %>%
  group_by(GRUPO) %>%
  summarize(max_n_cum = max(n_cum))

#Extract the values for specific groups
control_max_n_cum_PBC1 <- max_n_cum_values_PBC1$max_n_cum[max_n_cum_values_PBC1$GRUPO == "Control"]
pushdictation_max_n_cum_PBC1 <- max_n_cum_values_PBC1$max_n_cum[max_n_cum_values_PBC1$GRUPO == "PushDictation"]
pushrecording_max_n_cum_PBC1 <- max_n_cum_values_PBC1$max_n_cum[max_n_cum_values_PBC1$GRUPO == "PushRecording"]
choice_max_n_cum_PBC1 <- max_n_cum_values_PBC1$max_n_cum[max_n_cum_values_PBC1$GRUPO == "Choice"]

#Put together last row
total_obs_row_PBC1 <- tibble(
  PB_QUALITYC1 = "Number of observations",
  Control = control_max_n_cum_PBC1,
  PushDictation = pushdictation_max_n_cum_PBC1,
  PushRecording = pushrecording_max_n_cum_PBC1,
  Choice = choice_max_n_cum_PBC1
)

#Put together with the rest
table_with_total_PBC1 = bind_rows(
  table_dataPBC1 %>% 
  mutate(across(-PB_QUALITYC1, ~ format(.x*100,nsmall=1,digits=1))) %>% #and change format decimals  
  mutate_all(as.character),
  total_obs_row_PBC1 %>% 
  mutate_all(as.character)
  )%>%
select(1, 2, 5, 3, 4)
```

```{r result_pb1, message=FALSE, include = TRUE, cache = TRUE}
#Create the table using kable in R Markdown
kable(table_with_total_PBC1, format = "html", col.names = c("Coder 1", "Control", "Choice", "PushDictation", "PushRecording"), align=c("l","c","c","c","c")) %>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, align = "c", bold = TRUE) 
```


<center><b>
Problems, using the codes of Coder 2
</center></b>

```{r prep_pb2, message=FALSE, include = FALSE, cache = TRUE}
tibblePBC2 = propby(data, GRUPO, PB_QUALITYC2)
#Select relevant columns and reshape the data using spread
table_dataPBC2 <-  tibblePBC2 %>%
  select(PB_QUALITYC2, GRUPO, f) %>%
  spread(key = GRUPO, value = f) %>%
  arrange(PB_QUALITYC2)

#Extract n_cum
max_n_cum_values_PBC2 <- tibblePBC2 %>%
  group_by(GRUPO) %>%
  summarize(max_n_cum = max(n_cum))

#Extract the values for specific groups
control_max_n_cum_PBC2 <- max_n_cum_values_PBC2$max_n_cum[max_n_cum_values_PBC2$GRUPO == "Control"]
pushdictation_max_n_cum_PBC2 <- max_n_cum_values_PBC2$max_n_cum[max_n_cum_values_PBC2$GRUPO == "PushDictation"]
pushrecording_max_n_cum_PBC2 <- max_n_cum_values_PBC2$max_n_cum[max_n_cum_values_PBC2$GRUPO == "PushRecording"]
choice_max_n_cum_PBC2 <- max_n_cum_values_PBC2$max_n_cum[max_n_cum_values_PBC2$GRUPO == "Choice"]

#Put together last row
total_obs_row_PBC2 <- tibble(
  PB_QUALITYC2 = "Number of observations",
  Control = control_max_n_cum_PBC2,
  PushDictation = pushdictation_max_n_cum_PBC2,
  PushRecording = pushrecording_max_n_cum_PBC2,
  Choice = choice_max_n_cum_PBC2
)

#Put together with the rest
table_with_total_PBC2 = bind_rows(
  table_dataPBC2 %>% 
    mutate(across(-PB_QUALITYC2, ~ format(.x*100,nsmall=1,digits=1))) %>% #and change format decimals  
    mutate_all(as.character),
  total_obs_row_PBC2 %>% 
    mutate_all(as.character)
)%>%
  select(1, 2, 5, 3, 4)
```

```{r result_pb2, message=FALSE, include = TRUE, cache = TRUE}
#Create the table using kable in R Markdown
kable(table_with_total_PBC2, format = "html", col.names = c("Coder 2", "Control", "Choice", "PushDictation", "PushRecording"), align=c("l","c","c","c","c")) %>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, align = "c", bold = TRUE) 
```

### Tests of significance across groups

```{r dummies_pb, message=FALSE, include = FALSE, cache = TRUE}

#create dummies for each situation and coder
#no code "repeated part of a sentence" so we do not create this dummy (different to experimental questions where we have it)

data<- data%>%
     mutate(noproblem_QUALITYC1 = ifelse(PB_QUALITYC1 == "No problem detected", 1, 0),
           incomplete_QUALITYC1 = ifelse(PB_QUALITYC1 == "Incomplete sentence(s)", 1, 0),
           otherpb_QUALITYC1 = ifelse(PB_QUALITYC1 == "Other problem(s)", 1, 0)
            )

data<- data%>%
     mutate(noproblem_QUALITYC2 = ifelse(PB_QUALITYC2 == "No problem detected", 1, 0),
           incomplete_QUALITYC2 = ifelse(PB_QUALITYC2 == "Incomplete sentence(s)", 1, 0),
           otherpb_QUALITYC2 = ifelse(PB_QUALITYC2 == "Other problem(s)", 1, 0)
            )
```

<center><b>
Tests problems Coder 1: no problem detected 
</center></b>
```{r testnopb_c1, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE}
testnopbC1=proptest(data, noproblem_QUALITYC1, GRUPO)
kable(testnopbC1, format = "html")%>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, bold = TRUE) %>%
  row_spec(0:nrow(testnopbC1), align = "c")
```

<center><b>
Tests problems Coder 2: no problem detected 
</center></b>
```{r testnopb_c2, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE}
testnopbC2=proptest(data, noproblem_QUALITYC2, GRUPO)
kable(testnopbC2, format = "html")%>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, bold = TRUE) %>%
  row_spec(0:nrow(testnopbC2), align = "c")
```

<center><b>
Tests problems Coder 1: Incomplete sentence(s) 
</center></b>
```{r testincomplete_c1, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE}
testincompleteC1=proptest(data, incomplete_QUALITYC1, GRUPO)
kable(testincompleteC1, format = "html")%>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, bold = TRUE) %>%
  row_spec(0:nrow(testincompleteC1), align = "c")
```

<center><b>
Tests problems Coder 2: Incomplete sentence(s) 
</center></b>
```{r testincomplete_c2, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE}
testincompleteC2=proptest(data, incomplete_QUALITYC2, GRUPO)
kable(testincompleteC2, format = "html")%>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, bold = TRUE) %>%
  row_spec(0:nrow(testincompleteC2), align = "c")
```

<center><b>
Tests problems Coder 1: Other problem(s)
</center></b>
```{r testotherpb_c1, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE}
testotherpbC1=proptest(data, otherpb_QUALITYC1, GRUPO)
kable(testotherpbC1, format = "html")%>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, bold = TRUE) %>%
  row_spec(0:nrow(testotherpbC1), align = "c")
```

<center><b>
Tests problems Coder 2: Other problem(s)
</center></b>

<br>
<center>
We do not have any "Other problem(s)" for this coder.
</center>

<br>
<br>


## Number of themes

### Proportions per group

<center><b>
Number of themes, using the codes of Coder 1: average and median
</center></b>

```{r prep_themes1, message=FALSE, include = FALSE, cache = TRUE}
tibbleThemesC1 = meanandmoreby(data, NTHEMES_QUALITYC1, GRUPO)
#Select relevant columns and reshape the data using spread
table_themesC1 <-  tibbleThemesC1 %>%
  select(GRUPO, mean, median) %>% 
  gather(Statistic, Value, mean, median) %>%
  pivot_wider(names_from = GRUPO, values_from = Value) %>%
  separate_rows(Statistic, sep = "_") %>%
  select(1, 2, 5, 3, 4)
```


```{r result_themes1, message=FALSE, include = TRUE, cache = TRUE}
#Create the table using kable in R Markdown
kable(table_themesC1, format = "html", col.names = c("Coder 1",  "Control", "Choice", "PushDictation", "PushRecording"), digits = 2) %>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, bold = TRUE) %>%
  row_spec(0:nrow(table_themesC1), align = "c")
```


<center><b>
Number of themes, using the codes of Coder 2: average and median
</center></b>

```{r prep_themes2, message=FALSE, include = FALSE, cache = TRUE}
tibbleThemesC2 = meanandmoreby(data, NTHEMES_QUALITYC2, GRUPO)
#Select relevant columns and reshape the data using spread
table_themesC2 <-  tibbleThemesC2 %>%
  select(GRUPO, mean, median) %>% 
  gather(Statistic, Value, mean, median) %>%
  pivot_wider(names_from = GRUPO, values_from = Value) %>%
  separate_rows(Statistic, sep = "_") %>%
  select(1, 2, 5, 3, 4)
```


```{r result_themes2, message=FALSE, include = TRUE, cache = TRUE}
#Create the table using kable in R Markdown
kable(table_themesC2, format = "html", col.names = c("Coder 2", "Control", "Choice", "PushDictation", "PushRecording"), digits = 2) %>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, bold = TRUE) %>%
  row_spec(0:nrow(table_themesC2), align = "c")
```


<center><b>
Tests differences average number of themes Coder 1
</center></b>
```{r testthemes_c1, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE}
testthemesC1=pairwise_t_test(data, NTHEMES_QUALITYC1 ~ GRUPO)
testthemesC1 = testthemesC1 %>% 
  select(group1, group2, p, p.signif, p.adj, p.adj.signif)
kable(testthemesC1, format = "html")%>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, bold = TRUE) %>%
  row_spec(0:nrow(testthemesC1), align = "c")
```

<center><b>
Tests differences average number of themes Coder 2
</center></b>
```{r testthemes_c2, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE}
testthemesC2=pairwise_t_test(data, NTHEMES_QUALITYC2 ~ GRUPO)
testthemesC2 = testthemesC2 %>% 
  select(group1, group2, p, p.signif, p.adj, p.adj.signif)
kable(testthemesC2, format = "html")%>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, bold = TRUE) %>%
  row_spec(0:nrow(testthemesC1), align = "c")
```

<br>


## Number of characters

```{r prep_nchar, message=FALSE, include = FALSE, cache = TRUE}
#For missings, replace the dots by NA
data$WHYQUALITY_OPEN[data$WHYQUALITY_OPEN=="."] <- NA

#Create a new variable which is the number of characters in the responses
data$NCHARWHYQUALITY <- nchar(data$WHYQUALITY_OPEN)

#Prepare data
tibbleNchar = meanandmoreby(data, NCHARWHYQUALITY, GRUPO)
#Select relevant columns and reshape the data using spread
table_nchar <-  tibbleNchar %>%
  select(GRUPO, mean, median) %>% 
  gather(Statistic, Value, mean, median) %>%
  pivot_wider(names_from = GRUPO, values_from = Value) %>%
  separate_rows(Statistic, sep = "_") %>%
  select(1, 2, 5, 3, 4)
```

<center><b>
Number of characters: average and median
</center></b>

```{r result_nchar, message=FALSE, include = TRUE, cache = TRUE}
#Create the table using kable in R Markdown
kable(table_nchar, format = "html", col.names = c("No. Char", "Control", "Choice", "PushDictation", "PushRecording"), digits = 0) %>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, bold = TRUE) %>%
  row_spec(0:nrow(table_nchar), align = "c")
```


<center><b>
Tests differences average number of characters
</center></b>
```{r testnchar, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE}
testnchar=pairwise_t_test(data, NCHARWHYQUALITY ~ GRUPO)
testnchar = testnchar %>% 
  select(group1, group2, p, p.signif, p.adj, p.adj.signif)
kable(testnchar, format = "html")%>%
  kable_styling(full_width = FALSE) %>%
  row_spec(0, bold = TRUE) %>%
  row_spec(0:nrow(testthemesC1), align = "c")
```

<br>
<br>


