
#STUDY: VOICE SURVEY NURSING HOMES

#Clean the memory 
rm(list=ls())

#Libraries
library(haven) # for dealing with labelled dataframe
library(tidyverse) # for data handling
library(lubridate) #for dates
library(rstatix) #for pairwise_prop_test and t_tests

#Define path
path <- "yourpath" #Replace "yourpath" with the place where the data is saved

#######################################################################
#Create functions to simplify the code later
########################################################################

#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}}))
}


############################################################################################################################
#Load dataset which includes all variables for the 1,001 completes
############################################################################################################################

datafinalALLCODESregnoID = read_rds(file.path(path,"datafinalALLCODESregnoID.rds"))


######################################################################
#Section 4.1 Data collection 
######################################################################

#"The data were collected between the 22nd of February and the 30th of March 2023"
min(datafinalALLCODESregnoID$DACCESS)
min(datafinalALLCODESregnoID$DPART)

max(datafinalALLCODESregnoID$DACCESS)
max(datafinalALLCODESregnoID$DPART)

#Information for the paragraph about number of invited, etc: see below (different dataset)

#"The average age of these participants (1,0001 completes) is 47 years old"
#"50.5% are female"
#"35.0% have a higher education degree." 

meanandmore(datafinalALLCODESregnoID, AGE)
prop(datafinalALLCODESregnoID,  FEMALE)
prop(datafinalALLCODESregnoID, HIGH_EDUC)

#"On average, they have been registered in the Netquest panel for 6 years and have completed 157 surveys".

#calculate the time between the two dates (in weeks): REGIST (when they registered) and DINVIT (when they were invited)
#first transform the variables as dates (library lubridate)

datafinalALLCODESregnoID$REGIST2 <- parse_date_time(datafinalALLCODESregnoID$REGIST, orders = c("mdy", "dmy", "ymd", "my", "y")) #Use this because different order day/month
datafinalALLCODESregnoID$DINVIT2 <- parse_date_time(datafinalALLCODESregnoID$DINVIT, orders = c("mdy", "dmy", "ymd", "my", "y"))

datafinalALLCODESregnoID <- datafinalALLCODESregnoID %>%
  mutate(
    TIMEINPANEL = as.numeric(difftime(DINVIT2, REGIST2, units = "weeks"))
  )
meanandmore(datafinalALLCODESregnoID,TIMEINPANEL) #mean = 312 weeks, which means 312/52= 6 years

#For number of completed surveys
meanandmore(datafinalALLCODESregnoID, NCOMPLETETOT)

#"23.8% answered from a PC, 2.6% from a tablet and 73.6% from a smartphone."
prop(datafinalALLCODESregnoID, DEVICE)

#"The average response time (computed as focus time) is 9.1 minutes (median=8.2)." 

meanandmore(datafinalALLCODESregnoID, FOCUS) #Report FOCUS since it should be closer to real time spent in the survey

datafinalALLCODESregnoID <- datafinalALLCODESregnoID %>% 
  mutate(FOCUSMIN = FOCUS/60) #variable in seconds, transform in minutes

meanandmore(datafinalALLCODESregnoID, FOCUSMIN)

#"No significant difference between experimental groups in terms of gender, age or education level were found."

propby(datafinalALLCODESregnoID, GRUPO, FEMALE)
proptest(datafinalALLCODESregnoID, FEMALE, GRUPO)

meanandmoreby(datafinalALLCODESregnoID, AGE, GRUPO)
pairwise_t_test(datafinalALLCODESregnoID, AGE ~ GRUPO)

propby(datafinalALLCODESregnoID, GRUPO, HIGH_EDUC)
proptest(datafinalALLCODESregnoID, HIGH_EDUC, GRUPO)

######################################################################
#Section 4.1 Data collection Information about number of invited etc. 
######################################################################

#This is based not only on completes but on all participants, so use another dataset

dataall = read_rds(file.path(path,"clean_dataallcomplete_addvarnoID.rds"))

dataall = as_factor(dataall)

#"From the 4,789 panelists invited to the survey"
#value save d in variable "NINVITED"

prop(dataall, type)

#"1,860 started it" --> total number of observations in this dataset

# "17 were excluded for not passing basic anti-fraud checks"
# Correspond to: "completeISO" (2) -> finished survey so quickly that Netquest excluded them from completes
# + "filteroutISO" (10) -> gender or age do not correspond to registry
# + "filterout_nq_fraud_relevantID" (3)
# + "filterout_reCaptcha" (2) -> did not properly check "I am not a robot"

# "185 for exceeding the quotas"
# 34 quotaFull_Sexo_edad + 146 QuotaFull_educacion + 5 FilterOut_rec_comunidadautonoma

# "170 for not giving their explicit consent to participate"
# 170 FilterOut_CONSENT

# "205 for stating being not at all familiar with the way nursing homes work in Spain."
# 205 FilterOut_CONOCE_FILTER

# "113 abandoned on the initial survey page"
#this has been deduced as follows: the 282 with type=="." or "" are people dropping out during the survey 

pb = dataall %>% filter(type=="." | type=="") #create new dataset called "pb" with only the type=="." or ""
prop(pb, CONSENT) #169 out of 282 answered to the consent question
#So 113 did not go further than the informed consent page (CONSENT was a compulsory question)

# "Thus, 1,170 really started the survey and were eligible to participate."
#computed as 1860-17-185-170-205-113=1170

# "In addition, 25 broke-off before getting to our first experimental question."

prop(dataall, TRANSPARENT)#N=1,147 here, but it includes 2 completeISO that shoud not be counted, 
#so 1,145 eligible units answered the question just before the first experimental one

#Thus, 1,145 were assigned to one of the experimental groups: those are the ones considered for our analyses about participation. 

#Finally, another 144 panelists broke-off during or after the experimental questions. 
#in "type" we have the final of completes = 1,001, so computed as the difference: 1145-1001=144.

#Thus, 1,001 panelists completed the full survey: those are the ones considered for our analyses about data quality"

##############################################
#Section 4.3 The questionnaire
############################################

#A warning message was shown to 102 participants who tried to skip a question when multiple items were presented on the same page

datafinalALLCODESregnoID$GRIDWARNING[is.na(datafinalALLCODESregnoID$GRIDWARNING)] <- 0 #the ones who did not see the message are as NA, replace by 0
prop(datafinalALLCODESregnoID, GRIDWARNING)


####################################################
#Section 4.5.1 Analyses regarding rates of response 
###################################################

#"considering all the individuals who responded to the questions that came just before each experimental question (i.e., 1,145 respondents for OQ1 and 1,040 for OQ2"

data = dataall %>%
  filter(type=="complete" | type=="." | type=="" )#exclude 2 completeISO for quality reason

prop(data, TRANSPARENT)
prop(data, TRUSTNH)

################################################
#Section 4.5.2 Analyses regarding data quality 
###############################################

#"We should note however that due to the sample size (less than 200 per group when focusing only on the valid answers)..." 

propby(datafinalALLCODESregnoID, GRUPO, VALID_TRANSPC3)
propby(datafinalALLCODESregnoID, GRUPO, VALID_TRUSTC3)

propby(datafinalALLCODESregnoID, GRUPO, PB1_TRANSPC1)
propby(datafinalALLCODESregnoID, GRUPO, PB1_TRUSTC1)


############################################################################
#Section 5.1 Table 2 (has been transformed in Figure 1 later; see separate R script)
############################################################################

#WHYTRANSP question
#####################

###Break-off

#number of abandon in control group
abcontrol <- data %>% 
  filter(GRUPO2TRANSP == "Control_abandon") %>%
  count()

#number of abandon in PushDictation group
abdictation <- data %>%
  filter(GRUPO2TRANSP == "PushDictation_abandon") %>%
  count()

#number of abandon in PushRecording group
abvoice <- data %>%
  filter(GRUPO2TRANSP == "PushRecording_abandon") %>%
  count()

#number of abandon in choice group
abchoice<- data %>%
  filter(GRUPO2TRANSP == "Choice_abandon") %>%
  count()

#total number of observations in each of the 4 groups
totalcontrol <- data %>%
  filter(str_detect(GRUPO2TRANSP, "Control")) %>%
  count()

totaldictation <- data %>%
  filter(str_detect(GRUPO2TRANSP, "PushDictation")) %>%
  count()

totalvoice <- data %>%
  filter(str_detect(GRUPO2TRANSP, "PushRecording")) %>%
  count()

totalchoice <- data %>%
  filter(str_detect(GRUPO2TRANSP, "Choice")) %>%
  count()

#Abandon rates presented in Table 2

abcontrolrate=abcontrol*100/totalcontrol
abcontrolrate

abdictationrate=abdictation*100/totaldictation
abdictationrate

abvoicerate=abvoice*100/totalvoice
abvoicerate

abchoicerate=abchoice*100/totalchoice
abchoicerate


#Test for significance of differences in breakoff (Fisher tests)

#test choice vs control
resultab_chc <- fisher.test(matrix(c(abchoice$n, abcontrol$n, totalchoice$n - abchoice$n, totalcontrol$n - abcontrol$n), nrow = 2))
print(resultab_chc)#signif

#test pushdictation vs control
resultab_dc <- fisher.test(matrix(c(abdictation$n, abcontrol$n, totaldictation$n - abdictation$n, totalcontrol$n - abcontrol$n), nrow = 2))
print(resultab_dc)#signif

#test pushrecording vs control
resultab_vc <- fisher.test(matrix(c(abvoice$n, abcontrol$n, totalvoice$n - abvoice$n, totalcontrol$n - abcontrol$n), nrow = 2))
print(resultab_vc)#signif

#test pushdictation vs choice
resultab_dch <- fisher.test(matrix(c(abdictation$n, abchoice$n, totaldictation$n - abdictation$n, totalchoice$n - abchoice$n), nrow = 2))
print(resultab_dch)#signif

#test pushrecording vs pushdictation
resultab_vd <- fisher.test(matrix(c(abvoice$n, abdictation$n, totalvoice$n - abvoice$n, totaldictation$n - abdictation$n), nrow = 2))
print(resultab_vd)#NS

#test pushrecording vs choice
resultab_vch <- fisher.test(matrix(c(abvoice$n, abchoice$n, totalvoice$n - abvoice$n, totalchoice$n - abchoice$n), nrow = 2))
print(resultab_vch)#signif


###Item non response

#total numbers per group are the same, only need to create number of item nonresponse

inrcontrol <- data %>%
  filter(GRUPO2TRANSP == "Control_noanswer") %>%
  count()

inrdictation <- data %>%
  filter(GRUPO2TRANSP == "PushDictation_nodictation") %>%
  count()

inrvoice <- data %>%
  filter(GRUPO2TRANSP == "PushRecording_noanswer") %>%
  count()

inrchoice <- data %>%
  filter(GRUPO2TRANSP == "Choice_noanswer") %>%
  count()

#Item nonresponse rates presented in Table 2

inrcontrolrate=inrcontrol*100/totalcontrol
inrcontrolrate

inrdictationrate=inrdictation*100/totaldictation
inrdictationrate

inrvoicerate=inrvoice*100/totalvoice
inrvoicerate

inrchoicerate=inrchoice*100/totalchoice
inrchoicerate

#Test for significance of differences in item nonresponse (Fisher tests)

#test choice vs control
resultinr_chc <- fisher.test(matrix(c(inrchoice$n, inrcontrol$n, totalchoice$n - inrchoice$n, totalcontrol$n - inrcontrol$n), nrow = 2))
print(resultinr_chc)#signif

#test pushdictation vs control
resultinr_dc <- fisher.test(matrix(c(inrdictation$n, inrcontrol$n, totaldictation$n - inrdictation$n, totalcontrol$n - inrcontrol$n), nrow = 2))
print(resultinr_dc)#signif

#test pushrecording vs control
resultinr_vc <- fisher.test(matrix(c(inrvoice$n, inrcontrol$n, totalvoice$n - inrvoice$n, totalcontrol$n - inrcontrol$n), nrow = 2))
print(resultinr_vc)#signif

#test pushdictation vs choice
resultinr_dch <- fisher.test(matrix(c(inrdictation$n, inrchoice$n, totaldictation$n - inrdictation$n, totalchoice$n - inrchoice$n), nrow = 2))
print(resultinr_dch)#NS

#test pushrecording vs pushdictation
resultinr_vd <- fisher.test(matrix(c(inrvoice$n, inrdictation$n, totalvoice$n - inrvoice$n, totaldictation$n - inrdictation$n), nrow = 2))
print(resultinr_vd)#signif

#test pushrecording vs choice
resultinr_vch <- fisher.test(matrix(c(inrvoice$n, inrchoice$n, totalvoice$n - inrvoice$n, totalchoice$n - inrchoice$n), nrow = 2))
print(resultinr_vch)#signif

###Rate of response

#total numbers per group are the same, create the number of responses

rcontrol <- data %>%
  filter(GRUPO2TRANSP == "Control_answer") %>%
  count()

rdictation <- data %>%
  filter(GRUPO2TRANSP == "PushDictation_dictated1" | GRUPO2TRANSP == "PushDictation_dictated2" | GRUPO2TRANSP == "PushDictation_text2") %>%
  count()

rvoice <- data %>%
  filter(GRUPO2TRANSP == "PushRecording_recorded1" | GRUPO2TRANSP == "PushRecording_recordedonly2" |GRUPO2TRANSP == "PushRecording_several2" |GRUPO2TRANSP == "PushRecording_textonly2") %>%
  count()

rchoice <- data %>%
  filter(GRUPO2TRANSP == "Choice_dictatedonly" | GRUPO2TRANSP == "Choice_recordedonly" | GRUPO2TRANSP == "Choice_textonly" | GRUPO2TRANSP == "Choice_several" ) %>%
  count()

#Response rates presented in Table 2

rcontrolrate=rcontrol*100/totalcontrol
rcontrolrate

rdictationrate=rdictation*100/totaldictation
rdictationrate

rvoicerate=rvoice*100/totalvoice
rvoicerate

rchoicerate=rchoice*100/totalchoice
rchoicerate

#Test for significance of differences in response rates (Fisher tests)

#test choice vs control
resultr_chc <- fisher.test(matrix(c(rchoice$n, rcontrol$n, totalchoice$n - rchoice$n, totalcontrol$n - rcontrol$n), nrow = 2))
print(resultr_chc)#signif

#test pushdictation vs control
resultr_dc <- fisher.test(matrix(c(rdictation$n, rcontrol$n, totaldictation$n - rdictation$n, totalcontrol$n - rcontrol$n), nrow = 2))
print(resultr_dc)#signif

#test pushrecording vs control
resultr_vc <- fisher.test(matrix(c(rvoice$n, rcontrol$n, totalvoice$n - rvoice$n, totalcontrol$n - rcontrol$n), nrow = 2))
print(resultr_vc)#signif

#test pushdictation vs choice
resultr_dch <- fisher.test(matrix(c(rdictation$n, rchoice$n, totaldictation$n - rdictation$n, totalchoice$n - rchoice$n), nrow = 2))
print(resultr_dch)#signif

#test pushrecording vs pushdictation
resultr_vd <- fisher.test(matrix(c(rvoice$n, rdictation$n, totalvoice$n - rvoice$n, totaldictation$n - rdictation$n), nrow = 2))
print(resultr_vd)#signif

#test pushrecording vs choice
resultr_vch <- fisher.test(matrix(c(rvoice$n, rchoice$n, totalvoice$n - rvoice$n, totalchoice$n - rchoice$n), nrow = 2))
print(resultr_vch)#signif

###Responses through voice

#proportions computed for those who provided an answer (calculated before: rdictation, rvoice and rchoice)
#control group excluded (no voice option)

#Number of answers provided through voice

rdictationvi <- data %>% #vi for voice input
  filter(GRUPO2TRANSP == "PushDictation_dictated1" | GRUPO2TRANSP == "PushDictation_dictated2") %>%
  count()

rvoicevi <- data %>%
  filter(GRUPO2TRANSP == "PushRecording_recorded1" | GRUPO2TRANSP == "PushRecording_recordedonly2" |GRUPO2TRANSP == "PushRecording_several2") %>%
  count()

rchoicevi <- data %>%
  filter(GRUPO2TRANSP == "Choice_dictatedonly" | GRUPO2TRANSP == "Choice_recordedonly" | GRUPO2TRANSP == "Choice_several" ) %>%
  count()


#Response rates through voice presented in Table 2

rdictationvirate=rdictationvi*100/rdictation
rdictationvirate

rvoicevirate=rvoicevi*100/rvoice
rvoicevirate

rchoicevirate=rchoicevi*100/rchoice
rchoicevirate

#Test for significance of differences in response rates (Fisher tests)

#test pushdictation vs choice
resultrvi_dch <- fisher.test(matrix(c(rdictationvi$n, rchoicevi$n, rdictation$n - rdictationvi$n, rchoice$n - rchoicevi$n), nrow = 2))
print(resultrvi_dch)#signif

#test pushrecording vs pushdictation
resultrvi_vd <- fisher.test(matrix(c(rvoicevi$n, rdictationvi$n, rvoice$n - rvoicevi$n, rdictation$n - rdictationvi$n), nrow = 2))
print(resultrvi_vd)#signif

#test pushrecording vs choice
resultrvi_vch <- fisher.test(matrix(c(rvoicevi$n, rchoicevi$n, rvoice$n - rvoicevi$n, rchoice$n - rchoicevi$n), nrow = 2))
print(resultrvi_vch)#signif


#WHYTRUST question
#######################

###Break-off

#number of abandon in each of the 4 groups
abcontrol2 <- data %>% #to differentiate the second experimental question, add "2" to all names
  filter(GRUPO2TRUSTNH == "Control_abandon") %>%
  count()

abdictation2 <- data %>%
  filter(GRUPO2TRUSTNH == "PushDictation_abandon") %>%
  count()

abvoice2 <- data %>%
  filter(GRUPO2TRUSTNH == "PushRecording_abandon") %>%
  count()

abchoice2 <- data %>%
  filter(GRUPO2TRUSTNH == "Choice_abandon") %>%
  count()

#total number of observations in each of the 4 groups
totalcontrol2 <- data %>%
  filter(str_detect(GRUPO2TRUSTNH, "Control")) %>%
  count()

totaldictation2 <- data %>%
  filter(str_detect(GRUPO2TRUSTNH, "PushDictation")) %>%
  count()

totalvoice2 <- data %>%
  filter(str_detect(GRUPO2TRUSTNH, "PushRecording")) %>%
  count()

totalchoice2 <- data %>%
  filter(str_detect(GRUPO2TRUSTNH, "Choice")) %>%
  count()

#abandon rates presented in Table 2

abcontrolrate2=abcontrol2*100/totalcontrol2
abcontrolrate2

abdictationrate2=abdictation2*100/totaldictation2
abdictationrate2

abvoicerate2=abvoice2*100/totalvoice2
abvoicerate2

abchoicerate2=abchoice2*100/totalchoice2
abchoicerate2

#Test for significance of differences in breakoff (Fisher tests)

#test choice vs control
resultab_chc2 <- fisher.test(matrix(c(abchoice2$n, abcontrol2$n, totalchoice2$n - abchoice2$n, totalcontrol2$n - abcontrol2$n), nrow = 2))
print(resultab_chc2)#NS

#test pushdictation vs control
resultab_dc2 <- fisher.test(matrix(c(abdictation2$n, abcontrol2$n, totaldictation2$n - abdictation2$n, totalcontrol2$n - abcontrol2$n), nrow = 2))
print(resultab_dc2)#NS

#test pushrecording vs control
resultab_vc2 <- fisher.test(matrix(c(abvoice2$n, abcontrol2$n, totalvoice2$n - abvoice2$n, totalcontrol2$n - abcontrol2$n), nrow = 2))
print(resultab_vc2)#signif

#test pushdictation vs choice
resultab_dch2 <- fisher.test(matrix(c(abdictation2$n, abchoice2$n, totaldictation2$n - abdictation2$n, totalchoice2$n - abchoice2$n), nrow = 2))
print(resultab_dch2)#NS

#test pushrecording vs pushdictation
resultab_vd2 <- fisher.test(matrix(c(abvoice2$n, abdictation2$n, totalvoice2$n - abvoice2$n, totaldictation2$n - abdictation2$n), nrow = 2))
print(resultab_vd2)#NS

#test pushrecording vs choice
resultab_vch2 <- fisher.test(matrix(c(abvoice2$n, abchoice2$n, totalvoice2$n - abvoice2$n, totalchoice2$n - abchoice2$n), nrow = 2))
print(resultab_vch2)#NS


###Item non response

#total numbers per group are the same, create the number of item nonresponse

inrcontrol2 <- data %>%
  filter(GRUPO2TRUSTNH == "Control_noanswer") %>%
  count()

inrdictation2 <- data %>%
  filter(GRUPO2TRUSTNH == "PushDictation_nodictation") %>%
  count()

inrvoice2 <- data %>%
  filter(GRUPO2TRUSTNH == "PushRecording_noanswer") %>%
  count()

inrchoice2 <- data %>%
  filter(GRUPO2TRUSTNH == "Choice_noanswer") %>%
  count()


#Item nonresponse rates presented in Table 2

inrcontrolrate2=inrcontrol2*100/totalcontrol2
inrcontrolrate2

inrdictationrate2=inrdictation2*100/totaldictation2
inrdictationrate2

inrvoicerate2=inrvoice2*100/totalvoice2
inrvoicerate2

inrchoicerate2=inrchoice2*100/totalchoice2
inrchoicerate2

#Test for significance of differences in item nonresponse (Fisher tests)

#test choice vs control
resultinr_chc2 <- fisher.test(matrix(c(inrchoice2$n, inrcontrol2$n, totalchoice2$n - inrchoice2$n, totalcontrol2$n - inrcontrol2$n), nrow = 2))
print(resultinr_chc2)#signif

#test pushdictation vs control
resultinr_dc2 <- fisher.test(matrix(c(inrdictation2$n, inrcontrol2$n, totaldictation2$n - inrdictation2$n, totalcontrol2$n - inrcontrol2$n), nrow = 2))
print(resultinr_dc2)#signif

#test pushrecording vs control
resultinr_vc2 <- fisher.test(matrix(c(inrvoice2$n, inrcontrol2$n, totalvoice2$n - inrvoice2$n, totalcontrol2$n - inrcontrol2$n), nrow = 2))
print(resultinr_vc2)#signif

#test pushdictation vs choice
resultinr_dch2 <- fisher.test(matrix(c(inrdictation2$n, inrchoice2$n, totaldictation2$n - inrdictation2$n, totalchoice2$n - inrchoice2$n), nrow = 2))
print(resultinr_dch2)#signif

#test pushrecording vs pushdictation
resultinr_vd2 <- fisher.test(matrix(c(inrvoice2$n, inrdictation2$n, totalvoice2$n - inrvoice2$n, totaldictation2$n - inrdictation2$n), nrow = 2))
print(resultinr_vd2)#NS

#test pushrecording vs choice
resultinr_vch2 <- fisher.test(matrix(c(inrvoice2$n, inrchoice2$n, totalvoice2$n - inrvoice2$n, totalchoice2$n - inrchoice2$n), nrow = 2))
print(resultinr_vch2)#signif


###Response

#total numbers per group are the same, create the number of responses

rcontrol2 <- data %>%
  filter(GRUPO2TRUSTNH == "Control_answer") %>%
  count()

rdictation2 <- data %>%
  filter(GRUPO2TRUSTNH == "PushDictation_dictated1" | GRUPO2TRUSTNH == "PushDictation_dictated2" | GRUPO2TRUSTNH == "PushDictation_text2") %>%
  count()

rvoice2 <- data %>%
  filter(GRUPO2TRUSTNH == "PushRecording_recorded1" | GRUPO2TRUSTNH == "PushRecording_recordedonly2" |GRUPO2TRUSTNH == "PushRecording_several2" |GRUPO2TRUSTNH == "PushRecording_textonly2") %>%
  count()

rchoice2 <- data %>%
  filter(GRUPO2TRUSTNH == "Choice_dictatedonly" | GRUPO2TRUSTNH == "Choice_recordedonly" | GRUPO2TRUSTNH == "Choice_textonly" | GRUPO2TRUSTNH == "Choice_several" ) %>%
  count()


#Response rates presented in Table 2

rcontrolrate2=rcontrol2*100/totalcontrol2
rcontrolrate2

rdictationrate2=rdictation2*100/totaldictation2
rdictationrate2

rvoicerate2=rvoice2*100/totalvoice2
rvoicerate2

rchoicerate2=rchoice2*100/totalchoice2
rchoicerate2

#Test for significance of differences in response rates (Fisher tests)

#test choice vs control
resultr_chc2 <- fisher.test(matrix(c(rchoice2$n, rcontrol2$n, totalchoice2$n - rchoice2$n, totalcontrol2$n - rcontrol2$n), nrow = 2))
print(resultr_chc2)#signif

#test pushdictation vs control
resultr_dc2 <- fisher.test(matrix(c(rdictation2$n, rcontrol2$n, totaldictation2$n - rdictation2$n, totalcontrol2$n - rcontrol2$n), nrow = 2))
print(resultr_dc2)#signif

#test pushrecording vs control
resultr_vc2 <- fisher.test(matrix(c(rvoice2$n, rcontrol2$n, totalvoice2$n - rvoice2$n, totalcontrol2$n - rcontrol2$n), nrow = 2))
print(resultr_vc2)#signif

#test pushdictation vs choice
resultr_dch2 <- fisher.test(matrix(c(rdictation2$n, rchoice2$n, totaldictation2$n - rdictation2$n, totalchoice2$n - rchoice2$n), nrow = 2))
print(resultr_dch2)#signif

#test pushrecording vs pushdictation
resultr_vd2 <- fisher.test(matrix(c(rvoice2$n, rdictation2$n, totalvoice2$n - rvoice2$n, totaldictation2$n - rdictation2$n), nrow = 2))
print(resultr_vd2)#NS

#test pushrecording vs choice
resultr_vch2 <- fisher.test(matrix(c(rvoice2$n, rchoice2$n, totalvoice2$n - rvoice2$n, totalchoice2$n - rchoice2$n), nrow = 2))
print(resultr_vch2)#signif


###Responses through voice

#proportions are computed for those who provided an answer (calculated before: rdictation2, rvoice2 and rchoice2)
#control group is excluded (no voice option)

#Compute the number of answers provided through voice

rdictationvi2 <- data %>% #vi for voice input
  filter(GRUPO2TRUSTNH == "PushDictation_dictated1" | GRUPO2TRUSTNH == "PushDictation_dictated2") %>%
  count()

rvoicevi2 <- data %>%
  filter(GRUPO2TRUSTNH == "PushRecording_recorded1" | GRUPO2TRUSTNH == "PushRecording_recordedonly2" |GRUPO2TRUSTNH == "PushRecording_several2") %>%
  count()

rchoicevi2 <- data %>%
  filter(GRUPO2TRUSTNH == "Choice_dictatedonly" | GRUPO2TRUSTNH == "Choice_recordedonly" | GRUPO2TRUSTNH == "Choice_several" ) %>%
  count()

#Response rates through voice presented in Table 2

rdictationvirate2=rdictationvi2*100/rdictation2
rdictationvirate2

rvoicevirate2=rvoicevi2*100/rvoice2
rvoicevirate2

rchoicevirate2=rchoicevi2*100/rchoice2
rchoicevirate2

#Test for significance of differences in response rates (Fisher tests)

#test pushdictation vs choice
resultrvi_dch2 <- fisher.test(matrix(c(rdictationvi2$n, rchoicevi2$n, rdictation2$n - rdictationvi2$n, rchoice2$n - rchoicevi2$n), nrow = 2))
print(resultrvi_dch2)#signif

#test pushrecording vs pushdictation
resultrvi_vd2 <- fisher.test(matrix(c(rvoicevi2$n, rdictationvi2$n, rvoice2$n - rvoicevi2$n, rdictation2$n - rdictationvi2$n), nrow = 2))
print(resultrvi_vd2)#signif

#test pushrecording vs choice
resultrvi_vch2 <- fisher.test(matrix(c(rvoicevi2$n, rchoicevi2$n, rvoice2$n - rvoicevi2$n, rchoice2$n - rchoicevi2$n), nrow = 2))
print(resultrvi_vch2)#signif



############################################################################
#Section 5.2 Table 3 (has been transformed in Figure 2 later; see separate R script)
############################################################################

#See SOM 5: all the results are extracted from there.



###############################################################################
#Section 6.3 Practical implications
###############################################################################

#Completion times ... are also significantly shorter for the experimental groups compared to the Control group
#even taking into account the potential follow-up for those skipping the question in a first place:
#for instance, for OQ1, the median completion time is 27 seconds in the PushRecording, 31 seconds in the PushDictation
#and the Choice groups, and 61 seconds in the Control group

#See SOM 5 


###############################################################################
#Appendix 2 Table A2 
###############################################################################

#WHYTRANSP
data %>%
  group_by(GRUPO, GRUPO2TRANSP) %>%
  count() %>%
  print(n = 30)

#WHYTRUST
data %>%
  group_by(GRUPO, GRUPO2TRUSTNH) %>%
  count() %>%
  print(n = 30)


###############################################################################
#Appendix 3 table A3 (info about independent variables for the regressions)
###############################################################################

prop(datafinalALLCODESregnoID, FEMALE)

meanandmore(datafinalALLCODESregnoID, AGE)
prop(datafinalALLCODESregnoID, THIRTYLESS)
prop(datafinalALLCODESregnoID, SIXTYPLUS)
#Middle age group proportion=100-THIRTYLESS-SIXTYPLUS

prop(datafinalALLCODESregnoID, LOW_EDUC)
prop(datafinalALLCODESregnoID, HIGH_EDUC)
#Middle education proportion=100-LOW_EDUC-HIGH_EDUC

prop(datafinalALLCODESregnoID, SPANISH)

prop(datafinalALLCODESregnoID, KNOW_FILTERnum)

prop(datafinalALLCODESregnoID, SOCTRUSTnum)

prop(datafinalALLCODESregnoID, CONFIDENTIALnum)

prop(datafinalALLCODESregnoID, COMFORTnum)

meanandmore(datafinalALLCODESregnoID, logNCOMPLETE3M)

prop(datafinalALLCODESregnoID, DIDNOTKNOWDICTATION)
prop(datafinalALLCODESregnoID, USEDICTATIONDL)

prop(datafinalALLCODESregnoID, DIDNOTKNOWVOICE)
prop(datafinalALLCODESregnoID, USEVOICEDL)

prop(datafinalALLCODESregnoID, MOBILE)

prop(datafinalALLCODESregnoID, NBTASKS)

prop(datafinalALLCODESregnoID, HOME)

prop(datafinalALLCODESregnoID, PEOPLEAROUND)

prop(datafinalALLCODESregnoID, TRANSP_EXTREME)

prop(datafinalALLCODESregnoID, TRUST_EXTREME)


###############################################################################
#Appendix 4 Table A4 
###############################################################################

#Self-reported use of dictation and voice recording 

propby(datafinalALLCODESregnoID, GRUPO, USEDDICTATION)

propby(datafinalALLCODESregnoID, GRUPO, USEDVOICE)



