####WORKSAPCE SETTING####
#working directory
setwd("C:/Users/vitto/Desktop/Ethicub/Data analysis")
#libraries
library(dplyr)
##
## Caricamento pacchetto: 'dplyr'
## I seguenti oggetti sono mascherati da 'package:stats':
##
## filter, lag
## I seguenti oggetti sono mascherati da 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyr)
library(stringr)
library(magrittr)
##
## Caricamento pacchetto: 'magrittr'
## Il seguente oggetto è mascherato da 'package:tidyr':
##
## extract
####SCORING LAB SOSCI DATA####
#reading dataset
LabData <- read.csv("data_ethicub.csv",sep=",") %>%
#primi dati sono test, che taglio
{slice(.,7:nrow(.))} %T>%
{print(paste("N partecipanti:",nrow(.)))}
## [1] "N partecipanti: 27"
###Scoring###
#Agency + creation dataframe
df <- LabData %>%
select(contains("AE01")) %>%
mutate_if(is.character,as.numeric) %>%
apply(1,mean) %>%
as.matrix(ncol=1) %>%
data.frame %>%
rename(agency_POST=1) %>%
round(2)
#Experience
df$exp <- LabData %>%
select(contains("AE02")) %>%
mutate_if(is.character,as.numeric) %>%
apply(1,mean)%>%
round(2)
#IOS
df$IOS <- LabData %>%
select(contains("IO01")) %>%
mutate_if(is.character,as.numeric) %>%
unlist %>%
as.vector %>%
round(2)
#likeability
df$like_POST <- LabData %>%
select(contains("LI01")) %>%
mutate_if(is.character,as.numeric) %>%
mutate(
LI01_04_R = 6 - LI01_04,
LI01_04 = NULL
) %>%
apply(1,mean) %>%
round(2)
#MAS ROBOT
df$MAS_Res_iCub <- LabData %>%
select(MA01_02, MA01_03, MA01_05, MA01_07, MA01_09, MA01_12, MA01_14) %>%
mutate_if(is.character,as.numeric) %>%
apply(1,mean)%>%
round(2)
df$MAS_Ext_iCub <- LabData %>%
select(MA01_04, MA01_06, MA01_10, MA01_11) %>%
mutate_if(is.character,as.numeric) %>%
#all are reverse
apply(c(1,2),function(x){5-x})%>%
apply(1,mean)%>%
round(2)
df$MAS_Group_iCub <- LabData %>%
select(MA01_01, MA01_08, MA01_13) %>%
mutate_if(is.character,as.numeric) %>%
mutate(
MA01_01_R = 5-MA01_01,
MA01_01 = NULL,
MA01_13_R = 5-MA01_13,
MA01_13 = NULL,
) %>%
apply(1,mean)%>%
round(2)
#MAS HUMAN
df$MAS_Res_Human <- LabData %>%
select(MA02_02, MA02_03, MA02_05, MA02_07, MA02_09, MA02_12, MA02_14) %>%
mutate_if(is.character,as.numeric) %>%
apply(1,mean)%>%
round(2)
df$MAS_Ext_Human <- LabData %>%
select(MA02_04, MA02_06, MA02_10, MA02_11) %>%
mutate_if(is.character,as.numeric) %>%
#all are reverse
apply(c(1,2),function(x){5-x})%>%
apply(1,mean)%>%
round(2)
df$MAS_Group_Human <- LabData %>%
select(MA02_01, MA02_08, MA02_13) %>%
mutate_if(is.character,as.numeric) %>%
mutate(
MA02_01_R = 5-MA02_01,
MA02_01 = NULL,
MA02_13_R = 5-MA02_13,
MA02_13 = NULL,
) %>%
apply(1,mean)%>%
round(2)
#Need for cognition
df$nc <- LabData %>%
select(contains("NC01")) %>%
mutate_if(is.character,as.numeric) %>%
mutate(
NC01_03_R = 5-NC01_03,
NC01_03 = NULL,
NC01_04_R = 5-NC01_04,
NC01_04 = NULL,
NC01_05_R = 5-NC01_05,
NC01_05 = NULL,
NC01_07_R = 5-NC01_07,
NC01_07 = NULL,
NC01_08_R = 5-NC01_08,
NC01_08 = NULL,
NC01_09_R = 5-NC01_09,
NC01_09 = NULL,
NC01_12_R = 5-NC01_12,
NC01_12 = NULL,
NC01_16_R = 5-NC01_16,
NC01_16 = NULL,
NC01_17_R = 5-NC01_17,
NC01_17 = NULL,
) %>%
apply(1,mean)%>%
round(2)
#sense of agency
df$SoNA <- LabData %>%
select(PE01_01, PE01_04, PE01_08, PE01_09, PE01_12, PE01_13) %>%
mutate_if(is.character,as.numeric) %>%
apply(1,mean)%>%
round(2)
df$SoPA <- LabData %>%
select(PE01_02, PE01_03, PE01_05, PE01_06, PE01_07, PE01_10, PE01_11) %>%
mutate_if(is.character,as.numeric) %>%
apply(1,mean)%>%
round(2)
#Big Five
df$BFI_ext <- LabData %>%
select(PE02_01, PE02_06, PE02_11, PE02_16, PE02_21, PE02_26) %>%
mutate_if(is.character,as.numeric) %>%
mutate(
PE02_01_R = 6-PE02_01,
PE02_01 = NULL,
PE02_21_R = 6-PE02_21,
PE02_21 = NULL,
PE02_26_R = 6-PE02_26,
PE02_26 = NULL,
) %>%
apply(1,mean)%>%
round(2)
df$BFI_agr <- LabData %>%
select(PE02_02, PE02_07, PE02_12, PE02_17, PE02_22, PE02_27) %>%
mutate_if(is.character,as.numeric) %>%
mutate(
PE02_07_R = 6-PE02_07,
PE02_07 = NULL,
PE02_17_R = 6-PE02_17,
PE02_17 = NULL,
PE02_27_R = 6-PE02_27,
PE02_27 = NULL,
) %>%
apply(1,mean)%>%
round(2)
df$BFI_con <- LabData %>%
select(PE02_03, PE02_08, PE02_13, PE02_18, PE02_23, PE02_28) %>%
mutate_if(is.character,as.numeric) %>%
mutate(
PE02_03_R = 6-PE02_03,
PE02_03 = NULL,
PE02_08_R = 6-PE02_08,
PE02_08 = NULL,
PE02_28_R = 6-PE02_28,
PE02_28 = NULL,
) %>%
apply(1,mean)%>%
round(2)
df$BFI_neg <- LabData %>%
select(PE02_04, PE02_09, PE02_14, PE02_19, PE02_24, PE02_29) %>%
mutate_if(is.character,as.numeric) %>%
mutate(
PE02_14_R = 6-PE02_14,
PE02_14 = NULL,
PE02_19_R = 6-PE02_19,
PE02_19 = NULL,
PE02_24_R = 6-PE02_24,
PE02_24 = NULL,
) %>%
apply(1,mean)%>%
round(2)
df$BFI_ope <- LabData %>%
select(PE02_05, PE02_10, PE02_15, PE02_20, PE02_25) %>%
mutate_if(is.character,as.numeric) %>%
mutate(
PE02_10_R = 6-PE02_10,
PE02_10 = NULL,
PE02_20_R = 6-PE02_20,
PE02_20 = NULL,
# PE02_30_R = 6-PE02_30,
# PE02_30 = NULL,
) %>%
apply(1,mean)%>%
round(2)
#Warmth & Competence
df$warmth_POST <- LabData %>%
select(contains("WC01")) %>%
mutate_if(is.character,as.numeric) %>%
apply(1,mean)%>%
round(2)
df$warmth_PRE <- LabData %>%
select(contains("WC03")) %>%
mutate_if(is.character,as.numeric) %>%
apply(1,mean)%>%
round(2)
df$comp_POST <- LabData %>%
select(contains("WC02")) %>%
mutate_if(is.character,as.numeric) %>%
apply(1,mean)%>%
round(2)
df$comp_PRE <- LabData %>%
select(contains("WC04")) %>%
mutate_if(is.character,as.numeric) %>%
apply(1,mean)%>%
round(2)
#ID
df$ID1 <- LabData %>%
select(contains("WE05")) %>%
unlist %>%
gsub("'","",.) %>%
as.vector
####SCORING HOME SOSCI DATA####
HomeData <- read.csv("data_ethicub-pre.csv") %>%
slice(.,2:nrow(.))
#####Matching with LabData#####
#ID
ID2 <- HomeData %>%
select(WE01_01) %>%
unlist %>%
gsub("'","",.) %>%
as.vector
#Keep only people who altready anwered lab quest.
homeData_paired <- HomeData[ID2 %in% df$ID1,]
ID2_paired <- ID2[ID2 %in% df$ID1]
#There are duplicated answers. Keeping last one (assuming at home they failed
#first time and then corrected it. Hardly they did it correctly and went to do
#it again.)
homeData_paired_undupl <- homeData_paired[!duplicated(ID2_paired,fromLast=T),]
ID2_paired_undupl <- ID2_paired[!duplicated(ID2_paired,fromLast=T)]
#Ordering HomeData in the same way of LabData
homeData_paired_undupl_ordered <- homeData_paired_undupl[match(df$ID1,ID2_paired_undupl),]
#shortening variable name and deleting useless ones
HomeData_Clean <- homeData_paired_undupl_ordered
rm(homeData_paired,homeData_paired_undupl,homeData_paired_undupl_ordered)
#####Cleaning and inclusion in dataframe####
#Naming house data as OUT to separate from PRE administration in lab
#likeability
df$like_OUT <- HomeData_Clean %>%
select(contains("LK01")) %>%
mutate_if(is.character,as.numeric) %>%
mutate(
LK01_04_R = 6-LK01_04,
LK01_04 = NULL
) %>%
apply(1,mean)%>%
round(2)
#experience
df$exp_OUT <- HomeData_Clean %>%
select(contains("LK02")) %>%
mutate_if(is.character,as.numeric) %>%
apply(1,mean)%>%
round(2)
#agency
df$agency_OUT <- HomeData_Clean %>%
select(contains("LK03")) %>%
mutate_if(is.character,as.numeric) %>%
apply(1,mean)%>%
round(2)
#competence
df$comp_OUT <- HomeData_Clean %>%
select(contains("WC01")) %>%
mutate_if(is.character,as.numeric) %>%
apply(1,mean)%>%
round(2)
#warmth
df$warmth_OUT <- HomeData_Clean %>%
select(contains("WC02")) %>%
mutate_if(is.character,as.numeric) %>%
apply(1,mean)%>%
round(2)
#age
df$age <- HomeData_Clean %>%
select(contains("WE04")) %>%
mutate_if(is.character,as.numeric) %>%
unlist
#gender
df$gen <- HomeData_Clean %>%
select(contains("WE03")) %>%
lapply(.,function(x){gsub(1,"f",x)}) %>%
lapply(.,function(x){gsub(2,"m",x)}) %>%
lapply(.,function(x){gsub(3,"nb",x)}) %>%
lapply(.,function(x){gsub(4,"o",x)}) %>%
unlist %>%
as.vector %>%
as.factor
#Met icub
df$met <- HomeData_Clean %>%
select(contains("WE02")) %>%
lapply(.,function(x){gsub(1,"si",x)}) %>%
lapply(.,function(x){gsub(2,"no",x)}) %>%
unlist %>%
as.vector %>%
as.factor
####ORDERING DF####
df <- df[,c(
'ID1',
'age',
'gen',
'met',
'agency_OUT',
'agency_POST',
'exp',
'IOS',
'like_OUT',
'like_POST',
'MAS_Res_iCub',
'MAS_Ext_iCub',
'MAS_Group_iCub',
'MAS_Res_Human',
'MAS_Ext_Human',
'MAS_Group_Human',
'nc',
'SoNA',
'SoPA',
'BFI_ext',
'BFI_agr',
'BFI_con',
'BFI_neg',
'BFI_ope',
'warmth_OUT',
'warmth_PRE',
'warmth_POST',
'comp_OUT',
'comp_PRE',
'comp_POST',
'exp_OUT'
)]
####CLEANING LOG DATA####
log <- matrix(ncol = 9,nrow=0) %>% data.frame
for (i in 2:28){
log[(i*8-7):(i*8),] <- read.table(
paste("Logs/raw data/logs_000",
str_pad(i,2,pad="0"),
"/data.log",
sep=""),
sep="\r") %>%
unlist %>%
matrix(ncol=4,byrow=T) %>%
apply(c(1,2),function(x){gsub("^.{3}","",x)}) %>%
data.frame %>%
rename("DIL"=1,"INI"=2,"AGR"=3,"UPD"=4) %>%
separate_wider_regex(1:4,c("time" = ".{17}","val" = ".*"),names_sep = "_") %>%
{lapply(.,function(x){gsub(" DILEMMA | AGREED | INIT | UPDATE ","",x)})} %>%
#Subsitute dilemmas with D1,D2,D3...
{lapply(.,function(x){gsub("Un assistente medico robot .*","D1",x)})} %>%
{lapply(.,function(x){gsub("Un robot di ultima gener.*","D2",x)})} %>%
{lapply(.,function(x){gsub("I detenuti maschi di un car.*","D3",x)})} %>%
{lapply(.,function(x){gsub("Un robot autonomo dell'Aerona.*","D4",x)})} %>%
{lapply(.,function(x){gsub("Un banco alimentare ha un ass.*","D5",x)})} %>%
{lapply(.,function(x){gsub("Un robot assistente medico lav.*","D6",x)})} %>%
{lapply(.,function(x){gsub("In una zona lontana della .*","D7",x)})} %>%
{lapply(.,function(x){gsub("Nel pronto soccorso di un grande .*","D8",x)})} %>%
data.frame %>%
separate_wider_delim(6," ",names=c("ifAGR","AGR_val")) %>%
mutate(SUB = rep(df$ID1[i-1],8)) %>%
mutate(
#Time recorded as time intervals
across(contains("time"),as.numeric),
across(contains("time"),function(x){round(x,2)}),
UPD_time = UPD_time - AGR_time,
AGR_time = AGR_time - INI_time,
INI_time = INI_time - DIL_time,
DIL_time = NULL,
#ifAGR as logical
ifAGR = ifAGR %>% as.numeric %>% as.logical,
#Opinions as numbers
INI_val = INI_val %>% as.numeric,
AGR_val = AGR_val %>% as.numeric,
UPD_val = UPD_val %>% as.numeric,) %T>%
{assign("coln",colnames(.),1)}
}
colnames(log) <- coln
#write csv with clean data at the end
#subsitute NAs with empty values for excel
df_nice <- df
df_nice[is.na(df_nice)] <- ""
## Warning in `[<-.factor`(`*tmp*`, thisvar, value = ""): livello factore non
## valido, generato NA
## Warning in `[<-.factor`(`*tmp*`, thisvar, value = ""): livello factore non
## valido, generato NA
write.csv(df_nice,"quest_dataframe.csv")
write.csv(log,"log_dataframe.csv")
####DATA ANALYSIS####
#Change opinion if iCub disagrees?
log_noAGR <- log %>%
filter(ifAGR==FALSE)
#Se la risposta di iCub è minore di quella del partecipante invertire risposta, così l'effetto è da un'unico lato
for (i in 1:nrow(log_noAGR)){
if (log_noAGR$INI_val[i]-log_noAGR$AGR_val[i] > 0){
log_noAGR$UPD_val[i] <- 101-log_noAGR$UPD_val[i]
}
}
#T-test per dilemma
for (i in log_noAGR$DIL_val[!duplicated(log_noAGR$DIL_val)]) {
paste("----Dilemma",i,"------------------------") %>% print
t.test(log_noAGR$INI_val[log_noAGR$DIL_val==i],
log_noAGR$UPD_val[log_noAGR$DIL_val==i],
paired = TRUE) %>%
print
}
## [1] "----Dilemma D2 ------------------------"
##
## Paired t-test
##
## data: log_noAGR$INI_val[log_noAGR$DIL_val == i] and log_noAGR$UPD_val[log_noAGR$DIL_val == i]
## t = 2.5068, df = 13, p-value = 0.02625
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
## 3.396092 45.746765
## sample estimates:
## mean difference
## 24.57143
##
## [1] "----Dilemma D3 ------------------------"
##
## Paired t-test
##
## data: log_noAGR$INI_val[log_noAGR$DIL_val == i] and log_noAGR$UPD_val[log_noAGR$DIL_val == i]
## t = 2.7937, df = 13, p-value = 0.01521
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
## 6.250062 48.892796
## sample estimates:
## mean difference
## 27.57143
##
## [1] "----Dilemma D4 ------------------------"
##
## Paired t-test
##
## data: log_noAGR$INI_val[log_noAGR$DIL_val == i] and log_noAGR$UPD_val[log_noAGR$DIL_val == i]
## t = 2.5075, df = 12, p-value = 0.02753
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
## 3.105794 44.278822
## sample estimates:
## mean difference
## 23.69231
##
## [1] "----Dilemma D8 ------------------------"
##
## Paired t-test
##
## data: log_noAGR$INI_val[log_noAGR$DIL_val == i] and log_noAGR$UPD_val[log_noAGR$DIL_val == i]
## t = 4.8473, df = 13, p-value = 0.0003187
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
## 30.13076 78.58353
## sample estimates:
## mean difference
## 54.35714
##
## [1] "----Dilemma D1 ------------------------"
##
## Paired t-test
##
## data: log_noAGR$INI_val[log_noAGR$DIL_val == i] and log_noAGR$UPD_val[log_noAGR$DIL_val == i]
## t = 1.085, df = 12, p-value = 0.2992
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
## -6.59159 19.66851
## sample estimates:
## mean difference
## 6.538462
##
## [1] "----Dilemma D5 ------------------------"
##
## Paired t-test
##
## data: log_noAGR$INI_val[log_noAGR$DIL_val == i] and log_noAGR$UPD_val[log_noAGR$DIL_val == i]
## t = 1.5279, df = 12, p-value = 0.1524
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
## -3.702802 21.087418
## sample estimates:
## mean difference
## 8.692308
##
## [1] "----Dilemma D6 ------------------------"
##
## Paired t-test
##
## data: log_noAGR$INI_val[log_noAGR$DIL_val == i] and log_noAGR$UPD_val[log_noAGR$DIL_val == i]
## t = 3.0349, df = 13, p-value = 0.009573
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
## 9.674246 57.468612
## sample estimates:
## mean difference
## 33.57143
##
## [1] "----Dilemma D7 ------------------------"
##
## Paired t-test
##
## data: log_noAGR$INI_val[log_noAGR$DIL_val == i] and log_noAGR$UPD_val[log_noAGR$DIL_val == i]
## t = 5.8091, df = 12, p-value = 8.354e-05
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
## 38.36118 84.40805
## sample estimates:
## mean difference
## 61.38462
####Manipulation check: difference between competent and warm condition####
#Adding condition row based on pattern we followed
df <- df %>%
mutate(
condition = c(rep(c("c","c","w","w"),nrow(df_nice)))[1:nrow(df_nice)]
)
#Comparing i livelli di warmth and competence. Solo il warmth POST presenta una vaga diff.
#PRE
t.test(
df$warmth_PRE[df$condition=="c"],
df$warmth_PRE[df$condition=="w"],
)
##
## Welch Two Sample t-test
##
## data: df$warmth_PRE[df$condition == "c"] and df$warmth_PRE[df$condition == "w"]
## t = 0.19411, df = 22.866, p-value = 0.8478
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.5838663 0.7047454
## sample estimates:
## mean of x mean of y
## 3.714286 3.653846
t.test(
df$comp_PRE[df$condition=="c"],
df$comp_PRE[df$condition=="w"]
)
##
## Welch Two Sample t-test
##
## data: df$comp_PRE[df$condition == "c"] and df$comp_PRE[df$condition == "w"]
## t = -0.80993, df = 18.97, p-value = 0.428
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.7602273 0.3360515
## sample estimates:
## mean of x mean of y
## 3.157143 3.369231
#POST
t.test(
df$warmth_POST[df$condition=="c"],
df$warmth_POST[df$condition=="w"],
)
##
## Welch Two Sample t-test
##
## data: df$warmth_POST[df$condition == "c"] and df$warmth_POST[df$condition == "w"]
## t = -1.8695, df = 17.978, p-value = 0.07794
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -1.02111113 0.05957267
## sample estimates:
## mean of x mean of y
## 3.442308 3.923077
t.test(
df$comp_POST[df$condition=="c"],
df$comp_POST[df$condition=="w"]
)
##
## Welch Two Sample t-test
##
## data: df$comp_POST[df$condition == "c"] and df$comp_POST[df$condition == "w"]
## t = -1.4661, df = 23.013, p-value = 0.1562
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.7789202 0.1327664
## sample estimates:
## mean of x mean of y
## 3.153846 3.476923
#Comparing i livelli di warmth and competence corretti per la warmth and competence a casa
t.test(
df$warmth_PRE[df$condition=="c"]-df$warmth_OUT[df$condition=="c"],
df$warmth_PRE[df$condition=="w"]-df$warmth_OUT[df$condition=="w"]
)
##
## Welch Two Sample t-test
##
## data: df$warmth_PRE[df$condition == "c"] - df$warmth_OUT[df$condition == "c"] and df$warmth_PRE[df$condition == "w"] - df$warmth_OUT[df$condition == "w"]
## t = 0.43141, df = 16.091, p-value = 0.6719
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.5704418 0.8621085
## sample estimates:
## mean of x mean of y
## 0.2500000 0.1041667
t.test(
df$comp_PRE[df$condition=="c"]-df$comp_OUT[df$condition=="c"],
df$comp_PRE[df$condition=="w"]-df$comp_OUT[df$condition=="w"]
)
##
## Welch Two Sample t-test
##
## data: df$comp_PRE[df$condition == "c"] - df$comp_OUT[df$condition == "c"] and df$comp_PRE[df$condition == "w"] - df$comp_OUT[df$condition == "w"]
## t = 0.021049, df = 19.73, p-value = 0.9834
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.7013321 0.7156178
## sample estimates:
## mean of x mean of y
## 0.1571429 0.1500000
####Studying confidence and change of opinion####
#confidence
log$conf <- (50-log$INI_val) %>%
abs
#change of opinion
log <- log %>%
mutate(
change = (INI_val - UPD_val) %>% abs
)
par(mfrow=c(1,1))
log %$%
plot(conf,change)

log %$%
cor.test(conf,change,use="complete.obs")
##
## Pearson's product-moment correlation
##
## data: conf and change
## t = -3.8824, df = 214, p-value = 0.0001378
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.3770938 -0.1273825
## sample estimates:
## cor
## -0.2565135
#Do change and confidence vary with the type of dilemma? (yes, they change quite a bit)
par(mfrow=c(1,1))
plot(log$DIL_val %>% as.factor,log$conf,main="Confidence")

plot(log$DIL_val[log$change!=0] %>% as.factor,log$change[log$change!=0],main="Change (without considering cases with no update)")

#cheking distribution of some important variables through dilemmas
#keeping the same opinion
log$DIL_val[log$change==0] %>% table
## .
## D1 D2 D3 D4 D5 D6 D7 D8
## 18 25 20 22 20 24 22 23
#Is response time (indecision) related to opinion change?
#(...wip...)