rm(list=ls(all=TRUE)) setwd("C:/Users/habra/OneDrive - University of Bath/Uni of Bath DESKTOP-OUI3274/04 Study 4 - IPSA/Data/Data to be shared") # Load data expenses_fulldata <-read.csv("240917 - Expenses data.csv", stringsAsFactors = FALSE) pay_fulldata <-read.csv("240920 - pay data.csv", stringsAsFactors = FALSE) # install.packages("tidyverse") # Now combining the trust measures into a single column library(tidyverse) expenses_fulldata <- unite(expenses_fulldata, col="govt_trust", c("X4A", "X8A", "X10A", "X12A"), na.rm=TRUE) expenses_fulldata <- unite(expenses_fulldata, col="PM_trust", c("X4B", "X8B", "X10B", "X12B"), na.rm=TRUE) expenses_fulldata <- unite(expenses_fulldata, col="local_trust", c("X4C", "X8C", "X10C", "X12C"), na.rm=TRUE) expenses_fulldata <- unite(expenses_fulldata, col="motive_trust", c("X4D", "X8D", "X10D", "X12D"), na.rm=TRUE) expenses_fulldata <- unite(expenses_fulldata, col="integ_trust", c("X4E", "X8E", "X10E", "X12E"), na.rm=TRUE) expenses_fulldata <- unite(expenses_fulldata, col="intouch_trust", c("X4F", "X8F", "X10F", "X12F"), na.rm=TRUE) expenses_fulldata <- unite(expenses_fulldata, col="comp_trust", c("X4G", "X8G", "X10G", "X12G"), na.rm=TRUE) expenses_fulldata$study <-study <-rep(("expenses"), each=1004) pay_fulldata <- unite(pay_fulldata, col="govt_trust", c("X4A", "X8A", "X10A", "X12A"), na.rm=TRUE) pay_fulldata <- unite(pay_fulldata, col="PM_trust", c("X4B", "X8B", "X10B", "X12B"), na.rm=TRUE) pay_fulldata <- unite(pay_fulldata, col="local_trust", c("X4C", "X8C", "X10C", "X12C"), na.rm=TRUE) pay_fulldata <- unite(pay_fulldata, col="motive_trust", c("X4D", "X8D", "X10D", "X12D"), na.rm=TRUE) pay_fulldata <- unite(pay_fulldata, col="integ_trust", c("X4E", "X8E", "X10E", "X12E"), na.rm=TRUE) pay_fulldata <- unite(pay_fulldata, col="intouch_trust", c("X4F", "X8F", "X10F", "X12F"), na.rm=TRUE) pay_fulldata <- unite(pay_fulldata, col="comp_trust", c("X4G", "X8G", "X10G", "X12G"), na.rm=TRUE) pay_fulldata$study <-study <-rep(("pay"), each=955) expenses_fulldata[,5:30] <-lapply(expenses_fulldata[,5:30], as.numeric) # Now combining question on views of expenses expenses_fulldata <- unite(expenses_fulldata, col="opinion", c("X6...Q20", "X7...Q20", "X9...Q20", "X11...Q20"), na.rm=TRUE) library(plyr) library(dplyr) # simplifying the names of each treatment group expenses_fulldata$Block.Order <-replace(expenses_fulldata$Block.Order, expenses_fulldata$Block.Order=="Introduction and consent | Prolific ID | Control group | Political interest, party preference and Brexit vote | Own income and experience of expenses | Education", "control") expenses_fulldata$Block.Order <-replace(expenses_fulldata$Block.Order, expenses_fulldata$Block.Order=="Introduction and consent | Prolific ID | Experimental treatment 1 | Political interest, party preference and Brexit vote | Own income and experience of expenses | Education","cond1") expenses_fulldata$Block.Order <-replace(expenses_fulldata$Block.Order, expenses_fulldata$Block.Order=="Introduction and consent | Prolific ID | Experimental treatment 2 | Political interest, party preference and Brexit vote | Own income and experience of expenses | Education","cond2") expenses_fulldata$Block.Order <-replace(expenses_fulldata$Block.Order, expenses_fulldata$Block.Order=="Introduction and consent | Prolific ID | Experimental treatment 3 | Political interest, party preference and Brexit vote | Own income and experience of expenses | Education", "cond3") pay_fulldata$Block.Order <-replace(pay_fulldata$Block.Order, pay_fulldata$Block.Order=="Introduction and consent | Prolific ID | Control group | Political interest, party preference and Brexit vote | Income | Education", "control") pay_fulldata$Block.Order <-replace(pay_fulldata$Block.Order, pay_fulldata$Block.Order=="Introduction and consent | Prolific ID | Experimental treatment 1 | Political interest, party preference and Brexit vote | Income | Education","cond1") pay_fulldata$Block.Order <-replace(pay_fulldata$Block.Order, pay_fulldata$Block.Order=="Introduction and consent | Prolific ID | Experimental treatment 2 | Political interest, party preference and Brexit vote | Income | Education","cond2") pay_fulldata$Block.Order <-replace(pay_fulldata$Block.Order, pay_fulldata$Block.Order=="Introduction and consent | Prolific ID | Experimental treatment 3 | Political interest, party preference and Brexit vote | Income | Education","cond3") ## Two versions of block order from trial run which omitted Prolific ID pay_fulldata$Block.Order <-replace(pay_fulldata$Block.Order, pay_fulldata$Block.Order=="Introduction and consent | Control group | Political interest, party preference and Brexit vote | Income | Education", "control") pay_fulldata$Block.Order <-replace(pay_fulldata$Block.Order, pay_fulldata$Block.Order=="Introduction and consent | Experimental treatment 1 | Political interest, party preference and Brexit vote | Income | Education","cond1") pay_fulldata$Block.Order <-replace(pay_fulldata$Block.Order, pay_fulldata$Block.Order=="Introduction and consent | Experimental treatment 2 | Political interest, party preference and Brexit vote | Income | Education","cond2") pay_fulldata$Block.Order <-replace(pay_fulldata$Block.Order, pay_fulldata$Block.Order=="Introduction and consent | Experimental treatment 3 | Political interest, party preference and Brexit vote | Income | Education","cond3") # Using rename() to change column names expenses_fulldata <- expenses_fulldata %>% dplyr::rename ("guess" = "X5...Q19", "brexit_vote" = "X15...Q21", "party_pref" = "X14...Q20", "pol_interest" = "X13...Q18", "income" = "X16...Q13", "expenses_experience" = "X17...Q14", "education" = "X18...Q23", "prolific_party_pref" = "X_1", "treatment" = "Block.Order") pay_fulldata[,5:30] <-lapply(pay_fulldata[,5:30], as.numeric) pay_fulldata <- unite(pay_fulldata, col="opinion", c("pay_view_1", "pay_view_2", "pay_view_3", "pay_view_4"), na.rm=TRUE) pay_fulldata <- pay_fulldata %>% dplyr::rename ("prolific_party_pref" = "X_1", "treatment"= "Block.Order", "guess"="pay_guess") alldata <-rbind.fill( pay_fulldata, expenses_fulldata) alldata$opinion<-as.numeric(alldata$opinion) # Now want to find any participants that have failed attention check # First, checking if all trust ratings match alldata_temp <- alldata %>% select("govt_trust", "PM_trust", "local_trust", "motive_trust", "integ_trust", "intouch_trust", "comp_trust")%>% rowwise %>% dplyr::mutate(match = n_distinct(unlist(cur_data())) == 1) %>% ungroup() alldata$match <- alldata_temp$match # If they are all the same but are all 3 (neither agree nor disagree) then ok alldata$trust_sum <-rowSums(alldata[,5:11]) alldata$failed <-ifelse((alldata$match==TRUE)& (alldata$trust_sum!=21),1,0) # Creating a new dataframe so I can view the ones who have failed and check I've got it right before I delete them failed <-alldata[alldata$failed ==1,] # Double checking how many answered 'neither agree nor disagree' for everything unsure <-alldata[alldata$match ==TRUE,] # creating a new dataframe excluding the ones who failed the attention check newdata <-alldata[alldata$failed !=1,] # creating new scores for the reverse-coded items newdata$motive_trust_reversed <-6-newdata$motive_trust newdata$comp_trust_reversed <-6-newdata$comp_trust # creating an overall trust score newdata$total_trust <-newdata$govt_trust + newdata$PM_trust + newdata$local_trust + newdata$motive_trust_reversed + newdata$integ_trust + newdata$intouch_trust + newdata$comp_trust_reversed # creating an average trust score newdata$mean_trust <-newdata$total_trust/7 # Just checking - no significant difference between the control groups in each test t.test(newdata$mean_trust[newdata$study =="pay" & newdata$treatment =="control"], newdata$mean_trust[newdata$study =="expenses" & newdata$treatment =="control"]) t.test(newdata$govt_trust[newdata$study =="pay" & newdata$treatment =="control"], newdata$govt_trust[newdata$study =="expenses" & newdata$treatment =="control"]) # Now testing differences between treatments depending whether pay or expenses t.test(newdata$mean_trust[newdata$study =="pay" & newdata$treatment =="cond1"], newdata$mean_trust[newdata$study =="expenses" & newdata$treatment =="cond1"]) t.test(newdata$mean_trust[newdata$study =="pay" & newdata$treatment =="cond2"], newdata$mean_trust[newdata$study =="expenses" & newdata$treatment =="cond2"]) t.test(newdata$mean_trust[newdata$study =="pay" & newdata$treatment =="cond3"], newdata$mean_trust[newdata$study =="expenses" & newdata$treatment =="cond3"]) # Finding mean values mean(newdata$opinion[newdata$study =="pay" & newdata$treatment =="cond2"], na.rm=TRUE) mean(newdata$opinion[newdata$study =="pay" & newdata$treatment =="cond3"], na.rm=TRUE) mean(newdata$opinion[newdata$study =="expenses" & newdata$treatment =="cond2"], na.rm=TRUE) mean(newdata$opinion[newdata$study =="pay" & newdata$treatment !="control"], na.rm=TRUE) ## On average people thought MPs pay was somewhat too high (3.7/5) - omitting control group who were not told the figure mean(newdata$opinion[newdata$study =="expenses" & newdata$treatment !="control"], na.rm=TRUE) ## On average people thought MPs' expenses were much too high (4.4/5) - also omitting control group ## This difference is highly significant (p<0.001) t.test (newdata$opinion[newdata$study =="expenses" & newdata$treatment !="control"],newdata$opinion[newdata$study =="pay" & newdata$treatment !="control"], na.rm=TRUE) t.test (newdata$opinion[newdata$study =="expenses"],newdata$opinion[newdata$study =="pay" ], na.rm=TRUE) # seeing if anything different happens with the anova when I combine both studies to increase the amount of data (no) combined_mean_trust_aov <-aov(newdata$mean_trust~newdata$treatment) summary(combined_mean_trust_aov) TukeyHSD(combined_mean_trust_aov) combined_mean_trust_aov2 <-aov(newdata$mean_trust[newdata$treatment!="control"]~newdata$treatment[newdata$treatment!="control"]) summary(combined_mean_trust_aov2) TukeyHSD(combined_mean_trust_aov2) ## ANOVA tests for expenses only expenses_aov <-aov(newdata$govt_trust[newdata$study =="expenses"]~newdata$treatment[newdata$study =="expenses"]) summary(expenses_aov) TukeyHSD(expenses_aov) expenses_aov2 <-aov(newdata$mean_trust[newdata$study =="expenses"]~newdata$treatment[newdata$study =="expenses"]) summary(expenses_aov2) TukeyHSD(expenses_aov2) expenses_aov3 <-aov(newdata$opinion[newdata$study =="expenses"]~newdata$treatment[newdata$study =="expenses"]) summary(expenses_aov3) TukeyHSD(expenses_aov3) # Now running ANOVA tests for differences within the three treatment conditions, omitting the control group expenses_aov4<-aov(newdata$mean_trust[newdata$study =="expenses" & newdata$treatment!="control"]~newdata$treatment[newdata$study =="expenses" & newdata$treatment!="control"]) summary(expenses_aov4) TukeyHSD(expenses_aov4) expenses_aov5 <-aov(newdata$mean_trust[newdata$study =="expenses" & newdata$Nationality=="United Kingdom"]~newdata$treatment[newdata$study =="expenses" & newdata$Nationality=="United Kingdom"]) summary(expenses_aov2) TukeyHSD(expenses_aov2) expenses_opinion_aov2<-aov(newdata$opinion[newdata$study =="expenses" & newdata$treatment!="control"]~newdata$treatment[newdata$study =="expenses" & newdata$treatment!="control"]) summary(expenses_opinion_aov2) TukeyHSD(expenses_opinion_aov2) t.test(newdata$govt_trust[newdata$study =="expenses" & newdata$treatment=="cond1"], newdata$govt_trust[newdata$study =="expenses" & newdata$treatment=="cond3" ]) ## ANOVA tests for pay only pay_aov <-aov(newdata$govt_trust[newdata$study =="pay"]~newdata$treatment[newdata$study =="pay"]) summary(pay_aov) TukeyHSD(pay_aov) pay_aov2 <-aov(newdata$mean_trust[newdata$study =="pay"]~newdata$treatment[newdata$study =="pay"]) summary(pay_aov2) TukeyHSD(pay_aov2) pay_aov3 <-aov(newdata$opinion[newdata$study =="pay"]~newdata$treatment[newdata$study =="pay"]) summary(pay_aov3) TukeyHSD(pay_aov3) pay_opinion_aov1<-aov(newdata$opinion[newdata$study =="pay" ]~newdata$treatment[newdata$study =="pay" ]) summary(pay_opinion_aov1) TukeyHSD(pay_opinion_aov1) pay_opinion_aov2<-aov(newdata$opinion[newdata$study =="pay" & newdata$treatment!="control"]~newdata$treatment[newdata$study =="pay" & newdata$treatment!="control"]) summary(pay_opinion_aov2) TukeyHSD(pay_opinion_aov2) ### Creating a table of results mean_trust_pay <- rbind (mean(newdata$mean_trust[newdata$study =="pay" & newdata$treatment=="control"], na.rm=TRUE), mean(newdata$mean_trust[newdata$study =="pay" & newdata$treatment=="cond1"], na.rm=TRUE), mean(newdata$mean_trust[newdata$study =="pay" & newdata$treatment=="cond2"], na.rm=TRUE), mean(newdata$mean_trust[newdata$study =="pay" & newdata$treatment=="cond3"], na.rm=TRUE)) govt_trust_pay <- rbind (mean(newdata$govt_trust[newdata$study =="pay" & newdata$treatment=="control"], na.rm=TRUE), mean(newdata$govt_trust[newdata$study =="pay" & newdata$treatment=="cond1"], na.rm=TRUE), mean(newdata$govt_trust[newdata$study =="pay" & newdata$treatment=="cond2"], na.rm=TRUE), mean(newdata$govt_trust[newdata$study =="pay" & newdata$treatment=="cond3"], na.rm=TRUE)) opinion_pay <- rbind (mean(newdata$opinion[newdata$study =="pay" & newdata$treatment=="control"], na.rm=TRUE), mean(newdata$opinion[newdata$study =="pay" & newdata$treatment=="cond1"], na.rm=TRUE), mean(newdata$opinion[newdata$study =="pay" & newdata$treatment=="cond2"], na.rm=TRUE), mean(newdata$opinion[newdata$study =="pay" & newdata$treatment=="cond3"], na.rm=TRUE)) mean_trust_expenses <- rbind (mean(newdata$mean_trust[newdata$study =="expenses" & newdata$treatment=="control"], na.rm=TRUE), mean(newdata$mean_trust[newdata$study =="expenses" & newdata$treatment=="cond1"], na.rm=TRUE), mean(newdata$mean_trust[newdata$study =="expenses" & newdata$treatment=="cond2"], na.rm=TRUE), mean(newdata$mean_trust[newdata$study =="expenses" & newdata$treatment=="cond3"], na.rm=TRUE)) govt_trust_expenses <- rbind (mean(newdata$govt_trust[newdata$study =="expenses" & newdata$treatment=="control"], na.rm=TRUE), mean(newdata$govt_trust[newdata$study =="expenses" & newdata$treatment=="cond1"], na.rm=TRUE), mean(newdata$govt_trust[newdata$study =="expenses" & newdata$treatment=="cond2"], na.rm=TRUE), mean(newdata$govt_trust[newdata$study =="expenses" & newdata$treatment=="cond3"], na.rm=TRUE)) opinion_expenses <- rbind (mean(newdata$opinion[newdata$study =="expenses" & newdata$treatment=="control"], na.rm=TRUE), mean(newdata$opinion[newdata$study =="expenses" & newdata$treatment=="cond1"], na.rm=TRUE), mean(newdata$opinion[newdata$study =="expenses" & newdata$treatment=="cond2"], na.rm=TRUE), mean(newdata$opinion[newdata$study =="expenses" & newdata$treatment=="cond3"], na.rm=TRUE)) mean_scores<-cbind.data.frame(govt_trust_expenses,mean_trust_expenses, opinion_expenses, govt_trust_pay, mean_trust_pay, opinion_pay) mean_scores <-round(mean_scores, digits = 2) rownames(mean_scores) <-c('Control', 'Condition 1', 'Condition 2', 'Condition 3') write.csv(mean_scores, 'mean_scores.csv') ## Now looking at the trust battery - trying to do factor analysis ## Results of this will be reported in a separate paper # Creating a new table with just the trust scores trustscores <-cbind.data.frame(newdata$govt_trust, newdata$PM_trust, newdata$local_trust, newdata$motive_trust_reversed, newdata$integ_trust, newdata$intouch_trust, newdata$comp_trust_reversed) colnames(trustscores) <-c('govt_trust', 'PM_trust', 'local_trust', 'motive_trust', 'integ_trust', 'intouch_trust', 'comp_trust') trustscores <-na.omit(trustscores) cor_subtests <- cor(trustscores) round(cor_subtests, 2) # now calculating Cronbach's Alpha psych::alpha(trustscores) efa_1 <- factanal(x = trustscores, factors = 1) class(efa_1) efa_1 loadings <- efa_1$loadings # Factor loadings loadings[1] as.numeric(loadings) sum(loadings^2) # SS Loadings sum(loadings^2) / ncol(trustscores) # Proportion Var ## 43% of variance explained by single factor model ## now re-running with 2 factors efa_2 <- factanal(x = trustscores, factors = 2) class(efa_2) efa_2 ## and 3 efa_3 <- factanal(x = trustscores, factors = 3) class(efa_3) efa_3 ## can't test more than 3 factors with 7 variables ## wondering about differences between parties partydiffaov <-aov(newdata$mean_trust[newdata$prolific_party_pref!="ND"]~newdata$prolific_party_pref[newdata$prolific_party_pref!="ND"]) summary(partydiffaov) TukeyHSD(partydiffaov) table(newdata$prolific_party_pref) LabTrust <- mean (newdata$mean_trust[newdata$prolific_party_pref=="Labour"], na.rm = TRUE) ConsTrust <- mean (newdata$mean_trust[newdata$prolific_party_pref=="Conservative"], na.rm = TRUE) ReformTrust <- mean (newdata$mean_trust[newdata$prolific_party_pref=="Reform UK"], na.rm = TRUE) GreenTrust <- mean (newdata$mean_trust[newdata$prolific_party_pref=="Green Party"], na.rm = TRUE) LibDemTrust <- mean (newdata$mean_trust[newdata$prolific_party_pref=="Liberal Democrats"], na.rm = TRUE) SNPTrust <- mean (newdata$mean_trust[newdata$prolific_party_pref=="Scottish National Party (SNP)"], na.rm = TRUE) OtherTrust <- mean (newdata$mean_trust[newdata$prolific_party_pref=="ND" | newdata$prolific_party_pref== "UKIP" |newdata$prolific_party_pref== "Plaid Cymru" | newdata$prolific_party_pref== "Other"], na.rm = TRUE) partytrust <-cbind (LabTrust, ConsTrust, ReformTrust, GreenTrust, LibDemTrust, SNPTrust, OtherTrust) colnames(partytrust) <-c('Labour', 'Conservative', 'Reform UK', 'Green Party', 'Lib Dems', 'SNP', 'Other') partytrust<- round(partytrust, digits = 2) ## Clearly a really big difference between parties in terms of trust # Recoding other variables so that I can use them in a linear regression model newdata$brexit = car::recode (newdata$brexit_vote, "3 = 'Other'; 4 = 'Other'; 5 = 'Other'; 6 = 'Other'; 7 = 'Other'; 1 = 'Leave'; 2 = 'Remain';", as.factor = TRUE) newdata$university = car:: recode (newdata$education, "1 = 'No'; 2 = 'No'; 3 = 'No'; 4 = 'Yes'; 5 = 'Yes'; 6 = 'Yes'; 7 = 'No'; ", as.factor = TRUE) newdata$party = car:: recode (newdata$party_pref, "1 = 'Conservative'; 2 = 'Labour'; 3 = 'Liberal Democrats'; 4 = 'Green Party'; 5 = 'Reform UK'; 6 = 'Scottish National Party'; 7 = 'Other'; 8 = 'Other'; 9 = 'Other'; 10 ='None'; 11 ='Other'") newdata$interest = car:: recode (newdata$pol_interest, "1 = 4; 2 = 3; 3 = 2; 4 = 1;") # newdata$expenses_experience <- as.factor (newdata$expenses_experience) newdata$expenses_experience = car:: recode (newdata$expenses_experience, "1 = 'yes'; 2 = 'no'; 3 = 'no';", as.factor = TRUE) # Checking how many respondents were in each party table (newdata$party) # Multiple linear regression model for whole dataset combined lmfull <-lm(newdata$mean_trust~ newdata$treatment + newdata$party + newdata$Age + newdata$Sex + newdata$university + newdata$brexit) summary(lmfull) newdata<- within(newdata, treatment <- relevel(factor(treatment), ref = "control")) # Now looking at just the pay study, with mean trust as DV lmfullpay <-lm(mean_trust~ treatment + party + Age + Sex + income + university + brexit + interest, data = newdata[newdata$study =="pay",]) summary(lmfullpay) # Pay again but with opinion as the DV lm_opinion_pay <-lm(opinion~ treatment + party + Age + Sex + income + university + brexit + interest, data = newdata[newdata$study =="pay",]) summary(lm_opinion_pay) # Same thing for expenses lm_opinion_expenses <-lm(opinion~ treatment + party + Age + Sex + expenses_experience + university + brexit + interest, data = newdata[newdata$study =="expenses",]) summary(lm_opinion_expenses) lmfullexpenses <-lm(mean_trust~ treatment + party + Age + Sex + expenses_experience + university + brexit + interest, data = newdata[newdata$study =="expenses",]) summary(lmfullexpenses) # Exporting some nice tables of the regression models library (broom) tidyexpenses <-tidy(lmfullexpenses) write.csv(tidyexpenses, 'tidyexpenses.csv') tidypay <-tidy(lmfullpay) write.csv(tidypay, 'tidypay.csv') tidyopinionexpenses <-tidy(lm_opinion_expenses) write.csv(tidyopinionexpenses,'tidyopinionexpenses.csv') tidy_opinion_pay <-tidy(lm_opinion_pay) write.csv(tidy_opinion_pay, 'tidy_opinion_pay.csv') library (knitr) kable(tidyexpenses) ## 'Other' in the Brexit Vote field is mainly people who were not eligible to vote, presumnably because they were either ## under 18 or not living in the UK. Think this is just telling us the same thing as the age variable so will probably omit ## in the final analysis ## Here are the t-tests for the 'findings' section of the paper exp_t1<- t.test(newdata$mean_trust[newdata$treatment=="control"], newdata$mean_trust[newdata$treatment=="cond1" & newdata$study=="expenses"]) exp_t2<- t.test(newdata$mean_trust[newdata$treatment=="control"], newdata$mean_trust[newdata$treatment=="cond2" & newdata$study=="expenses"]) exp_t3<-t.test(newdata$mean_trust[newdata$treatment=="control"], newdata$mean_trust[newdata$treatment=="cond3" & newdata$study=="expenses"]) exp_t4 <-t.test(newdata$mean_trust[newdata$treatment=="cond1" & newdata$study=="expenses"], newdata$mean_trust[newdata$treatment=="cond2" & newdata$study=="expenses"]) exp_t5 <-t.test(newdata$mean_trust[newdata$treatment=="cond1" & newdata$study=="expenses"], newdata$mean_trust[newdata$treatment=="cond3" & newdata$study=="expenses"]) exp_t6 <-t.test(newdata$mean_trust[newdata$treatment=="cond2" & newdata$study=="expenses"], newdata$mean_trust[newdata$treatment=="cond3" & newdata$study=="expenses"]) exp_tstats <-rbind(exp_t1$statistic, exp_t2$statistic, exp_t3$statistic, exp_t4$statistic, exp_t5$statistic, exp_t6$statistic ) exp_parameter <-rbind(exp_t1$parameter, exp_t2$parameter, exp_t3$parameter, exp_t4$parameter, exp_t5$parameter, exp_t6$parameter ) exp_sig <-rbind(exp_t1$p.value, exp_t2$p.value, exp_t3$p.value, exp_t4$p.value, exp_t5$p.value, exp_t6$p.value ) exp_t.tests<-cbind.data.frame(exp_tstats, exp_parameter, exp_sig) rownames(exp_t.tests) <-c('Control v Cond1', 'Control v Cond2', 'Control v Cond3', 'Cond1 v Cond2', 'Cond1 v Cond3', 'Cond2 v Cond3') colnames(exp_t.tests) <-c('t','df','sig') t.test(newdata$govt_trust[newdata$treatment=="control" & newdata$study=="expenses"], newdata$govt_trust[newdata$treatment=="cond1" & newdata$study=="expenses"]) t.test(newdata$opinion[newdata$treatment=="control" & newdata$study=="expenses"], newdata$opinion[newdata$treatment!="control" & newdata$study=="expenses"]) t.test(newdata$opinion[newdata$treatment=="cond1" | newdata$treatment=="cond2" & newdata$study=="expenses"], newdata$opinion[newdata$treatment=="cond3" & newdata$study=="expenses"]) t.test(newdata$opinion[newdata$treatment=="control" & newdata$study=="expenses"], newdata$opinion[newdata$treatment=="cond3" & newdata$study=="expenses"]) ### t-tests for mean trust vs control group - pay pay_t1 <- t.test(newdata$mean_trust[newdata$treatment=="control"], # & newdata$study=="pay"], newdata$mean_trust[newdata$treatment=="cond1" & newdata$study=="pay"]) pay_t2 <-t.test(newdata$mean_trust[newdata$treatment=="control"], # & newdata$study=="pay"], newdata$mean_trust[newdata$treatment=="cond2" & newdata$study=="pay"]) pay_t3 <-t.test(newdata$mean_trust[newdata$treatment=="control"], # & newdata$study=="pay"], newdata$mean_trust[newdata$treatment=="cond3" & newdata$study=="pay"]) pay_t4 <-t.test(newdata$mean_trust[newdata$treatment=="cond1" & newdata$study=="pay"], newdata$mean_trust[newdata$treatment=="cond2" & newdata$study=="pay"]) pay_t5 <-t.test(newdata$mean_trust[newdata$treatment=="cond1" & newdata$study=="pay"], newdata$mean_trust[newdata$treatment=="cond3" & newdata$study=="pay"]) pay_t6 <-t.test(newdata$mean_trust[newdata$treatment=="cond2" & newdata$study=="pay"], newdata$mean_trust[newdata$treatment=="cond3" & newdata$study=="pay"]) pay_tstats <-rbind(pay_t1$statistic, pay_t2$statistic, pay_t3$statistic, pay_t4$statistic, pay_t5$statistic, pay_t6$statistic ) pay_parameter <-rbind(pay_t1$parameter, pay_t2$parameter, pay_t3$parameter, pay_t4$parameter, pay_t5$parameter, pay_t6$parameter ) pay_sig <-rbind(pay_t1$p.value, pay_t2$p.value, pay_t3$p.value, pay_t4$p.value, pay_t5$p.value, pay_t6$p.value ) pay_t.tests<-cbind.data.frame(pay_tstats, pay_parameter, pay_sig) rownames(pay_t.tests) <-c('Control v Cond1', 'Control v Cond2', 'Control v Cond3', 'Cond1 v Cond2', 'Cond1 v Cond3', 'Cond2 v Cond3') colnames(pay_t.tests) <-c('t','df','sig') exp_t.tests$study <-"expenses" pay_t.tests$study <-"pay" # Creating a table of results t.tests<-rbind.data.frame(pay_t.tests,exp_t.tests) t.tests$t<- round(t.tests$t, digits = 2) t.tests$sig<- round(t.tests$sig, digits = 2) t.tests$df<- round(t.tests$df, digits = 0) t.tests ##### Now repeating for 'opinion' exp_op1<- t.test(newdata$opinion[newdata$treatment=="control" & newdata$study=="expenses"], newdata$opinion[newdata$treatment=="cond1" & newdata$study=="expenses"]) exp_op2<- t.test(newdata$opinion[newdata$treatment=="control" & newdata$study=="expenses"], newdata$opinion[newdata$treatment=="cond2" & newdata$study=="expenses"]) exp_op3<-t.test(newdata$opinion[newdata$treatment=="control" & newdata$study=="expenses"], newdata$opinion[newdata$treatment=="cond3" & newdata$study=="expenses"]) exp_op4 <-t.test(newdata$opinion[newdata$treatment=="cond1" & newdata$study=="expenses"], newdata$opinion[newdata$treatment=="cond2" & newdata$study=="expenses"]) exp_op5 <-t.test(newdata$opinion[newdata$treatment=="cond1" & newdata$study=="expenses"], newdata$opinion[newdata$treatment=="cond3" & newdata$study=="expenses"]) exp_op6 <-t.test(newdata$opinion[newdata$treatment=="cond2" & newdata$study=="expenses"], newdata$opinion[newdata$treatment=="cond3" & newdata$study=="expenses"]) ### t-tests for opinion vs control group - pay pay_op1 <- t.test(newdata$opinion[newdata$treatment=="control" & newdata$study=="pay"], newdata$opinion[newdata$treatment=="cond1" & newdata$study=="pay"]) pay_op2 <-t.test(newdata$opinion[newdata$treatment=="control" & newdata$study=="pay"], newdata$opinion[newdata$treatment=="cond2" & newdata$study=="pay"]) pay_op3 <-t.test(newdata$opinion[newdata$treatment=="control" & newdata$study=="pay"], newdata$opinion[newdata$treatment=="cond3" & newdata$study=="pay"]) pay_op4 <-t.test(newdata$opinion[newdata$treatment=="cond1" & newdata$study=="pay"], newdata$opinion[newdata$treatment=="cond2" & newdata$study=="pay"]) pay_op5 <-t.test(newdata$opinion[newdata$treatment=="cond1" & newdata$study=="pay"], newdata$opinion[newdata$treatment=="cond3" & newdata$study=="pay"]) pay_op6 <-t.test(newdata$opinion[newdata$treatment=="cond2" & newdata$study=="pay"], newdata$opinion[newdata$treatment=="cond3" & newdata$study=="pay"]) # Creating a table of results exp_op_stats <-rbind(exp_op1$statistic, exp_op2$statistic, exp_op3$statistic, exp_op4$statistic, exp_op5$statistic, exp_op6$statistic ) exp_op_parameter <-rbind(exp_op1$parameter, exp_op2$parameter, exp_op3$parameter, exp_op4$parameter, exp_op5$parameter, exp_op6$parameter ) exp_op_sig <-rbind(exp_op1$p.value, exp_op2$p.value, exp_op3$p.value, exp_op4$p.value, exp_op5$p.value, exp_op6$p.value ) exp_op.tests<-cbind.data.frame(exp_op_stats, exp_op_parameter, exp_op_sig) rownames(exp_op.tests) <-c('Control v Cond1', 'Control v Cond2', 'Control v Cond3', 'Cond1 v Cond2', 'Cond1 v Cond3', 'Cond2 v Cond3') colnames(exp_op.tests) <-c('t','df','sig') # same for the pay study pay_op_stats <-rbind(pay_op1$statistic, pay_op2$statistic, pay_op3$statistic, pay_op4$statistic, pay_op5$statistic, pay_op6$statistic ) pay_op_parameter <-rbind(pay_op1$parameter, pay_op2$parameter, pay_op3$parameter, pay_op4$parameter, pay_op5$parameter, pay_op6$parameter ) pay_op_sig <-rbind(pay_op1$p.value, pay_op2$p.value, pay_op3$p.value, pay_op4$p.value, pay_op5$p.value, pay_op6$p.value ) pay_op.tests<-cbind.data.frame(pay_op_stats, pay_op_parameter, pay_op_sig) rownames(pay_op.tests) <-c('Control v Cond1', 'Control v Cond2', 'Control v Cond3', 'Cond1 v Cond2', 'Cond1 v Cond3', 'Cond2 v Cond3') colnames(pay_op.tests) <-c('t','df','sig') exp_op.tests$study <-"expenses" pay_op.tests$study <-"pay" op.tests<-rbind.data.frame(pay_op.tests,exp_op.tests) op.tests$t<- round(op.tests$t, digits = 2) op.tests$sig<- round(op.tests$sig, digits = 2) op.tests$df<- round(op.tests$df, digits = 0) op.tests ## Looking at some comparisons between the two studies t.test(newdata$mean_trust[newdata$treatment=="control" & newdata$study=="pay"], newdata$mean_trust[newdata$treatment=="control" & newdata$study=="expenses"]) t.test(newdata$mean_trust[newdata$treatment=="cond1" & newdata$study=="pay"], newdata$mean_trust[newdata$treatment=="cond1" & newdata$study=="expenses"]) t.test(newdata$govt_trust[newdata$treatment=="cond1" & newdata$study=="pay"], newdata$govt_trust[newdata$treatment=="cond1" & newdata$study=="expenses"]) t.test(newdata$mean_trust[newdata$treatment=="cond2" & newdata$study=="pay"], newdata$mean_trust[newdata$treatment=="cond2" & newdata$study=="expenses"]) t.test(newdata$mean_trust[newdata$treatment=="cond3" & newdata$study=="pay"], newdata$mean_trust[newdata$treatment=="cond3" & newdata$study=="expenses"]) t.test(newdata$opinion[newdata$treatment=="cond1" & newdata$study=="pay"], newdata$opinion[newdata$treatment=="cond1" & newdata$study=="expenses"]) t.test(newdata$opinion[newdata$treatment=="control" & newdata$study=="pay"], newdata$opinion[newdata$treatment=="control" & newdata$study=="expenses"]) t.test(newdata$opinion[newdata$treatment=="cond2" & newdata$study=="pay"], newdata$opinion[newdata$treatment=="cond2" & newdata$study=="expenses"]) t.test(newdata$opinion[newdata$treatment=="cond3" & newdata$study=="pay"], newdata$opinion[newdata$treatment=="cond3" & newdata$study=="expenses"]) t.test(newdata$mean_trust[newdata$treatment!="control" & newdata$study=="pay"], newdata$mean_trust[newdata$treatment!="control" & newdata$study=="expenses"]) t.test(newdata$opinion[newdata$study=="pay"], newdata$opinion[newdata$study=="expenses"]) library ("ggplot2") # Creating a box plot of results newdata %>% mutate(treatment = fct_relevel(treatment, "control", "cond1", "cond2", "cond3")) %>% ggplot(aes (x=treatment, y=opinion, fill=study))+ geom_boxplot()+ # scale_fill_viridis(discrete = TRUE, alpha=0.6) + # geom_jitter(color="black", size=0.4, alpha=0.9) + theme( legend.position="right", plot.title = element_text(size=11) ) + scale_fill_grey(start=0.8, end=0.5)+ ggtitle("Are pay and expenses too low, too high or about right?") + scale_x_discrete(labels=c("control","cond 1","cond 2","cond 3")) + xlab("Condition")+ ylab("Opinion (1 = too low / 5 = too high)")+ labs(fill="Study") # now trying to plot confidence intervals SumData<-cbind.data.frame(newdata$opinion, newdata$mean_trust, newdata$treatment, newdata$study) SumData<-na.omit(SumData) colnames(SumData)<-c('opinion', 'mean_trust','treatment', 'study') # New version of the data which uses both control groups (as they are the same) to give more power but only uses # the treatment groups from one study at a time SumDataExp<-SumData[SumData$study=='pay' & SumData$treatment=='control' | SumData$study=='expenses',] SumDataPay<-SumData[SumData$study=='expenses' & SumData$treatment=='control' | SumData$study=='pay',] library(rcompanion) SumPay = groupwiseMean(opinion ~ treatment, data = SumData[SumData$study=='pay',], conf = 0.95, digits = 3, traditional = FALSE, percentile = TRUE, na.rm=TRUE) SumPay$study<-c("pay", "pay","pay","pay") SumPay SumExp = groupwiseMean(opinion ~ treatment, data = SumData[SumData$study=='expenses',], conf = 0.95, digits = 3, traditional = FALSE, percentile = TRUE, na.rm=T) SumExp$study<-c("expenses", "expenses","expenses","expenses") SumExp Sum <-rbind(SumExp, SumPay) labels <-c (cond1 = "cond1", cond2 = "cond2", cond3 ="cond3", control = "control") library(ggplot2) Sum %>% mutate(treatment= fct_relevel (treatment, "cond3", "cond2","cond1","control"))%>% ggplot(aes(x = treatment, y = Mean, ymin = Percentile.lower, ymax = Percentile.upper, colour = study)) + geom_point(position = position_dodge(width = 0.2), size=4, shape=18) + geom_errorbar(position = position_dodge(width = 0.2), width = 0.3, linewidth = 1) + coord_flip() + scale_colour_manual(values = c("blue", "red")) + scale_x_discrete(labels=labels, name="Treatment")+ theme_bw() + theme(panel.grid.major.x = element_line(colour = "grey", linetype = "dashed"), panel.grid.major.y = element_blank(), panel.grid.minor.x = element_blank()) + ylab("Opinion")+ labs(color = "Study") ## now going to do the same for the trust measure PlotPay = groupwiseMean(mean_trust ~ treatment, # data = SumData[SumData$study=='pay',], na.rm=TRUE, data = SumDataPay, conf = 0.95, digits = 3, traditional = FALSE, percentile = TRUE) PlotPay$study<-c("Pay", "Pay","Pay","Pay") PlotExp = groupwiseMean(mean_trust ~ treatment, # data = SumData[SumData$study=='expenses',], data = SumDataExp, conf = 0.95, digits = 3, traditional = FALSE, percentile = TRUE, na.rm=T) PlotExp$study<-c("Expenses", "Expenses","Expenses","Expenses") Plot <-rbind(PlotExp, PlotPay) labels <-c (cond1 = "cond1", cond2 = "cond2", cond3 ="cond3", control = "control") library(ggplot2) Plot %>% mutate(treatment= fct_relevel (treatment, "cond3", "cond2","cond1","control"))%>% ggplot(aes(x = treatment, y = Mean, ymin = Percentile.lower, ymax = Percentile.upper, colour = study)) + geom_point(position = position_dodge(width = 0.2), size=4, shape=18) + facet_wrap(~study)+ geom_errorbar(position = position_dodge(width = 0.2), width = 0.3, linewidth = 1) + coord_flip() + scale_colour_manual(values = c("blue", "red"), guide="none") + scale_x_discrete(labels=labels, name="Treatment")+ theme_bw() + theme(panel.grid.major.x = element_line(colour = "grey", linetype = "dashed"), panel.grid.major.y = element_blank(), panel.grid.minor.x = element_blank()) + ylab("Trust") # Here is a side-by-side version of the box plot. # I don't use this in the paper as the plot of confidence intervals is clearer SumData %>% mutate(treatment = fct_relevel(treatment, "control", "cond1", "cond2", "cond3")) %>% ggplot(aes (x=treatment, y=mean_trust, fill=study))+ geom_boxplot()+ facet_wrap(~study)+ theme( legend.position="right", plot.title = element_text(size=11) ) + ggtitle("Overall trust by condition") + scale_x_discrete(labels=c("control","cond 1","cond 2","cond 3")) + xlab("Condition")+ ylab("Overall trust")+ labs(fill="Study") # Exporting tables of the t.tests for trust and opinion write.csv(t.tests, 't_tests.csv') write.csv(op.tests, 'op_tests.csv') ## ENDS ##