---
title: "Supplementary online material (SOM6): Quality of the 2 experimental questions"
subtitle: "Regression analyses"
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(knitr) #for creating tables
library(kableExtra) #to make changes in style of the tables
library(stargazer)#to create table with several regressions
#next block below is to get more space between columns in the tables
```

```{css, echo = FALSE}
table, td, th {
  border: none;
  padding-left: 0.6em;
  padding-right: 0.6em;
  margin-left: auto;
  margin-right: auto;
  margin-top: 0.6em;
  margin-bottom: 0.6em;
}
```

```{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"))
```


<br>
<br>

# Table of Contents


- [Valid Answers](#valid-answers)
<br>

- [Answered Both Aspects](#answered-both-aspects)
<br>

- [Use Abbreviations](#use-abbreviations)
<br>

- [No Problems Detected](#no-problems-detected)
<br>

- [Number of Themes](#number-of-themes)
<br>

- [Number of Characters](#number-of-characters)
<br>
<br>

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

#Create list of control variables
control_TRANSP <- c("FEMALE", "THIRTYLESS", "SIXTYPLUS" , "LOW_EDUC", "HIGH_EDUC", "SPANISH", "KNOW_FILTERnum", "SOCTRUSTnum", "CONFIDENTIALdum", "COMFORTdum", "logNCOMPLETE3M", "DIDNOTKNOWDICTATION", "USEDICTATIONDL", "DIDNOTKNOWVOICE", "USEVOICEDL", "MOBILE", "NBTASKS", "HOME", "PEOPLEAROUND", "TRANSP_EXTREME")

control_TRUST <- c("FEMALE",  "THIRTYLESS","SIXTYPLUS" , "LOW_EDUC", "HIGH_EDUC", "SPANISH", "KNOW_FILTERnum", "SOCTRUSTnum", "CONFIDENTIALdum", "COMFORTdum", "logNCOMPLETE3M", "DIDNOTKNOWDICTATION", "USEDICTATIONDL", "DIDNOTKNOWVOICE", "USEVOICEDL", "MOBILE", "NBTASKS", "HOME", "PEOPLEAROUND", "TRUST_EXTREME")

#Formula to then use the list in regressions
create_formulaTRANSP <- function(response_var, treatment_var) {
  control_formula <- paste(control_TRANSP, collapse = " + ")
  formula_str <- paste(response_var, "~", treatment_var, "+", control_formula)
  formula(as.formula(formula_str))
}

create_formulaTRUST <- function(response_var, treatment_var) {
  control_formula <- paste(control_TRUST, collapse = " + ")
  formula_str <- paste(response_var, "~", treatment_var, "+", control_formula)
  formula(as.formula(formula_str))
}
```    


# Valid Answers

```{r dummies_valid, message=FALSE, include = FALSE, cache = TRUE}
#Create dummies 
data <- data %>%
     mutate(valid_TRANSP = ifelse(VALID_TRANSPC3 == "4 Valid", 1, 0),
            valid_TRUST = ifelse(VALID_TRUSTC3 == "4 Valid", 1, 0)
            )
```

<center>

```{r logistic_validC3, message=FALSE, warning=FALSE, include = FALSE, cache = TRUE}
#Perform logistic regression
valid_TRANSPC3 <- glm(create_formulaTRANSP("valid_TRANSP", "GRUPO"), data = data, family = "binomial")
valid_TRUSTC3 <- glm(create_formulaTRUST("valid_TRUST", "GRUPO"), data = data, family = "binomial")
```


```{r table_validC3, message=FALSE, warning=FALSE, include = FALSE, cache = TRUE, results = 'asis'}
#Generate regression tables using stargazer
stargazer(valid_TRANSPC3, valid_TRUSTC3,
          title = "Regression Valid answers (Coder 3)",
          align = TRUE,
          header = FALSE,
          out = "regression_validC3.html")
```


```{r display_validC3, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE, results = 'asis'}
#Display the regression tables in the document
htmltools::includeHTML("regression_validC3.html")
```


</center>

<br>

<br>


# Answered Both Aspects

```{r dummies_aspects, message=FALSE, include = FALSE, cache = TRUE}
data <- data %>%
     mutate(both_TRANSPC1 = ifelse(ASPECTS_TRANSPC1 == "Both parts", 1, 0),
           both_TRANSPC2 = ifelse(ASPECTS_TRANSPC2 == "Both parts", 1, 0),
           both_TRUSTC1 = ifelse(ASPECTS_TRUSTC1 == "Both parts", 1, 0),
           both_TRUSTC2 = ifelse(ASPECTS_TRUSTC2 == "Both parts", 1, 0)
            )
```

<center>

```{r logistic_aspects, message=FALSE, warning=FALSE, include = FALSE, cache = TRUE}
aspC1_TRANSP <- glm(create_formulaTRANSP("both_TRANSPC1", "GRUPO"), data = data, family = "binomial")
aspC2_TRANSP <- glm(create_formulaTRANSP("both_TRANSPC2", "GRUPO"), data = data, family = "binomial")
aspC1_TRUST <- glm(create_formulaTRUST("both_TRUSTC1", "GRUPO"), data = data, family = "binomial")
aspC2_TRUST <- glm(create_formulaTRUST("both_TRUSTC2", "GRUPO"), data = data, family = "binomial")
```


```{r table_aspects, message=FALSE, warning=FALSE, include = FALSE, cache = TRUE, results = 'asis'}
stargazer(aspC1_TRANSP, aspC2_TRANSP, aspC1_TRUST, aspC2_TRUST, 
          title = "Regression Answered Both Aspects",
          align = TRUE,
          header = FALSE,
          out = "regression_aspects.html")
```


```{r display_aspects, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE, results = 'asis'}
htmltools::includeHTML("regression_aspects.html")
```

</center>

<br>
<br>


# Use Abbreviations

```{r dummy_abbr, message=FALSE, include = FALSE, cache = TRUE}
data <- data %>%
     mutate(abbr_TRANSPC1 = ifelse(ABBR_TRANSPC1 == "Abbreviation(s)", 1, 0),
           abbr_TRANSPC2 = ifelse(ABBR_TRANSPC2 == "Abbreviation(s)", 1, 0),
           abbr_TRUSTC1 = ifelse(ABBR_TRUSTC1 == "Abbreviation(s)", 1, 0),
           abbr_TRUSTC2 = ifelse(ABBR_TRUSTC2 == "Abbreviation(s)", 1, 0)
            )
```

<center>

```{r logistic_abbr, message=FALSE, warning=FALSE, include = FALSE, cache = TRUE}
abbrC1_TRANSP <- glm(create_formulaTRANSP("abbr_TRANSPC1", "GRUPO"), data = data, family = "binomial")
abbrC2_TRANSP <- glm(create_formulaTRANSP("abbr_TRANSPC2", "GRUPO"), data = data, family = "binomial")
abbrC1_TRUST <- glm(create_formulaTRUST("abbr_TRUSTC1", "GRUPO"), data = data, family = "binomial")
abbrC2_TRUST <- glm(create_formulaTRUST("abbr_TRUSTC2", "GRUPO"), data = data, family = "binomial")
```


```{r table_abbrev, message=FALSE, warning=FALSE, include = FALSE, cache = TRUE, results = 'asis'}
stargazer(abbrC1_TRANSP, abbrC2_TRANSP, abbrC1_TRUST, abbrC2_TRUST,
          title = "Regression Abbreviations",
          align = TRUE,
          header = FALSE,
          out = "regression_abbr.html")
```


```{r display_abbrev, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE, results = 'asis'}
htmltools::includeHTML("regression_abbr.html")
```

</center>

<br>
<br>


# No Problems Detected


```{r dummies_pb, message=FALSE, include = FALSE, cache = TRUE}
data <- data %>%
     mutate(nopb_TRANSPC1 = ifelse(PB1_TRANSPC1 == "No problem detected", 1, 0),
           nopb_TRANSPC2 = ifelse(PB1_TRANSPC2 == "No problem detected", 1, 0),
           nopb_TRUSTC1 = ifelse(PB1_TRUSTC1 == "No problem detected", 1, 0),
           nopb_TRUSTC2 = ifelse(PB1_TRUSTC2 == "No problem detected", 1, 0),
            )
```

<center>

```{r logistic_pb, message=FALSE, warning=FALSE, include = FALSE, cache = TRUE}
nopbC1_TRANSP <- glm(create_formulaTRANSP("nopb_TRANSPC1", "GRUPO"), data = data, family = "binomial")
nopbC2_TRANSP <- glm(create_formulaTRANSP("nopb_TRANSPC2", "GRUPO"), data = data, family = "binomial")
nopbC1_TRUST <- glm(create_formulaTRUST("nopb_TRUSTC1", "GRUPO"), data = data, family = "binomial")
nopbC2_TRUST <- glm(create_formulaTRUST("nopb_TRUSTC2", "GRUPO"), data = data, family = "binomial")
```


```{r table_pb, message=FALSE, warning=FALSE, include = FALSE, cache = TRUE, results = 'asis'}
stargazer(nopbC1_TRANSP, nopbC2_TRANSP,  nopbC1_TRUST, nopbC2_TRUST,
          title = "Regression No Problems Detected",
          align = TRUE,
          header = FALSE,
          out = "regression_nopb.html")
```

```{r display_pb, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE, results = 'asis'}
htmltools::includeHTML("regression_nopb.html")
```

</center>

<br>
<br>

# Number of Themes

<center>

```{r reg_nthemes, message=FALSE, warning=FALSE, include = FALSE, cache = TRUE}

names(data)[names(data) == "NTHEMES_TRANSP1b"] <- "ntheme_TRANSPC1"
names(data)[names(data) == "NTHEMES_TRANSP2b"] <- "ntheme_TRANSPC2"
names(data)[names(data) == "NTHEMES_TRUST1b"] <- "ntheme_TRUSTC1"
names(data)[names(data) == "NTHEMES_TRUST2b"] <- "ntheme_TRUSTC2"

#Perform linear regression
ntC1_TRANSP <- lm(create_formulaTRANSP("ntheme_TRANSPC1", "GRUPO"), data = data)
ntC2_TRANSP <- lm(create_formulaTRANSP("ntheme_TRANSPC2", "GRUPO"), data = data)
ntC1_TRUST <- lm(create_formulaTRUST("ntheme_TRUSTC1", "GRUPO"), data = data)
ntC2_TRUST <- lm(create_formulaTRUST("ntheme_TRUSTC2", "GRUPO"), data = data)
```


```{r table_nthemes, message=FALSE, warning=FALSE, include = FALSE, cache = TRUE, results = 'asis'}
stargazer(ntC1_TRANSP, ntC2_TRANSP, ntC1_TRUST, ntC2_TRUST,
          title = "Regression Number of Themes",
          align = TRUE,
          header = FALSE,
          out = "regression_nthemes.html")
```


```{r display_nthemes, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE, results = 'asis'}
htmltools::includeHTML("regression_nthemes.html")
```

</center>

<br>
<br>


# Number of Characters 


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

#Create a new variable which is the number of characters 
data$nchar_TRANSP <- nchar(data$WHYTRANSP_FINAL.x)
data$nchar_TRUST <- nchar(data$WHYTRUSTNH_FINAL.x)
```

<center>

```{r reg_nchar, message=FALSE, warning=FALSE, include = FALSE, cache = TRUE}
nch_TRANSP <- lm(create_formulaTRANSP("nchar_TRANSP", "GRUPO"), data = data)
nch_TRUST <- lm(create_formulaTRUST("nchar_TRUST", "GRUPO"), data = data)
```


```{r table_nchar, message=FALSE, warning=FALSE, include = FALSE, cache = TRUE, results = 'asis'}
stargazer(nch_TRANSP, nch_TRUST,
          title = "Regression Number of Characters",
          align = TRUE,
          header = FALSE,
          out = "regression_nchar.html")
```


```{r display_nchar, message=FALSE, warning=FALSE, include = TRUE, cache = TRUE, results = 'asis'}
htmltools::includeHTML("regression_nchar.html")
```

</center>

<br>
<br>

