-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanova_behavior_stats.R
89 lines (66 loc) · 3.08 KB
/
anova_behavior_stats.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
setwd("/Users/aantosz/Dysk Google/0 SYNCHRONIZACJA/eksperyment HR/HR behawior/ponowna analiza behawioru 1+2 tura/")
library(openxlsx)
df_wide = read.xlsx("behavior_alldata.xlsx")
library(data.table)
setDT(df_wide)
# delete rows RP_exp due to incomplete recordings
df_wide <- df_wide[!grepl("RP_exp", df_wide$file),]
# delete rows in the CONTR group, for which there are no paired EXP rows (due to lacks of data)
df_wide <- df_wide[!grepl("RP_contr", df_wide$file),]
df_wide <- df_wide[!grepl("RF_contr", df_wide$file),]
df_wide <- df_wide[!grepl("AG_contr", df_wide$file),]
# add index column
df_wide$Index = seq.int(nrow(df_wide))
# delete latency column
library(dplyr)
df_wide <- select(df_wide, -contains("_l"))
# convert 'exploration' into 'cage-exploration'
colnames(df_wide)[7] <- "cage_exploration_n"
colnames(df_wide)[8] <- "cage_exploration_d"
colnames(df_wide)[2] <- "human_id"
### IMPORTANT STEP - SELECTING PHASE
# delete columns with behaviors of no interest - we leave only 'in the cage' phase
df_wide <- df_wide %>% select(matches(c("Index", "file", "human_id", "group", "order", "cage_exploration", "human-exploration-0")))
# delete columns with behaviors of no interest - we leave only 'in the arms' phase
#df_wide <- select(df_wide, -contains(c("cage", "0", "hands.on", "approach", "wait", "avoidance", "tested.rat", "human.interest", "half", "hide.together", "freeze", "rat.interaction")))
# list the behaviors
library(stringr)
behaviors = str_remove(colnames(df_wide)[6:9], "_n") # columns [5:12] in the "in the arms" version of analysis
behaviors = as.data.frame(behaviors)
behaviors <- behaviors [!grepl("_d", behaviors$behaviors),]
df_long <-melt(df_wide, id=c("Index", "file", "human_id", "group", "order"),
measure=patterns("_d$", "_n$"),
value.name=c("duration", "number"),
variable.name = "behavior")
#one_to_ten = unique(df_long$behavior)
library(plyr)
#df_long$behavior <- mapvalues(df_long$behavior, from=1:4, to=c("armpit-hide-1", "human-exploration-1", "armpit-hide-2",
# "human-exploration-2"))
df_long$behavior <- mapvalues(df_long$behavior, from=1:2, to=c("cage_exploration", "human-exploration-0")) # "in the cage" version
# DURATION
# normality plots
library(ggpubr)
ggqqplot(df_long, "duration", facet.by = c("group", "behavior"))
ggplot(df_long, aes(x=behavior, y=duration, fill=group)) + geom_boxplot()
# mixed model ANOVA
library(afex)
a1 <- aov_ez("file", "duration", df_long, between = c("group", "human_id"),
within = "behavior")
knitr::kable(nice(a1))
# For a significant interaction:
# Effect of group for each behavior
library(rstatix)
one.way <- df_long %>%
group_by(behavior) %>%
anova_test(dv = duration, wid = Index, between = group) %>%
get_anova_table() %>%
adjust_pvalue(method = "bonferroni")
one.way
# Pairwise comparisons between group levels
pwc <- df_long %>%
group_by(behavior) %>%
pairwise_t_test(duration ~ group, p.adjust.method = "bonferroni", detailed = TRUE)
pwc
library(emmeans)
m1 <- emmeans(a1, ~ behavior)
p1 <- pairs(m1)