I am trying to plot scatter plots showing the partial effects of the significant variables from my linear mixed effects model. The model is examining the association between various behavioural variables and hunting success (the outcome variable), where data is grouped by dyad and participant (2 participants per dyad). The formula for the model looks like this:
Hunting.original ~ Distance.between.self.and.prey + Distance.between.self.and.other +
Scaled.self.facing.prey + Scaled.self.facing.other + Lag.Score +
Heart.Rate.self + Breathing.Rate.self + Time + (1 | Dyad) +
(1 | Participant)
The outputs of the model look like this, showing that there are 4 variables which are significantly associated with hunting success:
Fixed effects:
Estimate Std. Error df t value Pr(>|t|)
(Intercept) 41.78983 2.22696 14.12751 18.765 2.19e-11 ***
Distance.between.self.and.prey 3.19081 1.21620 177.88594 2.624 0.00946 **
Distance.between.self.and.other -0.18885 1.26110 174.68755 -0.150 0.88113
Scaled.self.facing.prey 2.34387 1.16867 171.29215 2.006 0.04647 *
Scaled.self.facing.other 2.33858 1.18028 172.39715 1.981 0.04914 *
Lag.Score -5.71666 1.22094 171.67142 -4.682 5.74e-06 ***
Heart.Rate.self 0.95826 1.29081 166.28060 0.742 0.45891
Breathing.Rate.self -0.09401 1.23793 177.50389 -0.076 0.93955
Time -1.04489 1.09477 165.52462 -0.954 0.34125
---
I want to create one correlation plot for each of the 4 significant variables (x-axis) against hunting success (y-axis), resulting in the following 4 plots:
- Distance between self and prey vs. hunting success
- Scaled self facing prey vs. hunting success
- Scaled self facing other vs. hunting success
- Lag Score vs. hunting success
I want to generate these plots using the raw data grouped by dyad and averaged, which is stored in the variable raw_df, so that there is one data point per dyad. The goal is to plot the raw data points, and on top of that, a line of best fit that accounts for the associations of the other variables with hunting success — essentially a partial correlation line of best fit. I have tried to achieve this using ggpredict, but as this is a novel method for me, I am unsure as to whether I am achieving what I want to achieve. I have attached an example plot for one of the variables. I have pasted the code snippet below.
I would be very grateful for a helping hand!
Full code snippet:
raw_df <- read.csv('/Users/macbook/Desktop/UCL PhD Work/fNIRSfMRIminecraft/Minecraftanalysis/Analysispipeline/Behaviouraldata/PREZSCOREFRAMECHASE.csv')
raw_df <- aggregate(raw_df,list(raw_df$Dyad),FUN=mean)
vars <- c(
"Distance.between.self.and.prey",
"Lag.Score",
"Scaled.self.facing.prey",
"Scaled.self.facing.other"
)
pretty_labels <- c(
"Distance.between.self.and.prey" = "Distance between self and prey",
"Lag.Score" = "Speed synchrony between self and other",
"Scaled.self.facing.prey" = "Heading direction from self to prey",
"Scaled.self.facing.other" = "Heading direction from self to other"
)
for (v in vars) {
x_min <- min(raw_df[[v]], na.rm = TRUE)
x_max <- max(raw_df[[v]], na.rm = TRUE)
eff <- ggpredict(combined_model_mixed,
terms = paste0(v, "[", x_min, ",", x_max, "]"))
p <- ggplot(raw_df, aes(x = !!sym(v), y = Hunting.Success)) +
geom_point(color = "black", alpha = 0.5) +
geom_line(data = eff, aes(x = x, y = predicted), color = "blue", size = 1.2) +
coord_cartesian(ylim = c(25, 62)) +
theme_minimal() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.x = element_text(face = "bold", size = 12),
axis.title.y = element_text(face = "bold", size = 12)
) +
labs(
title = "",
x = pretty_labels[v],
y = "Hunting Success"
)
print(p)
}
First few columns of raw_df:
Group.1 Dyad Participant Event Hunting.Success Time Length.of.trial
1 1 1 1.5 NA 38.20000 3.0 38.20000
2 2 2 3.5 NA 29.42857 4.0 29.42857
3 3 3 5.5 NA 47.42857 4.0 47.42857
4 4 4 7.5 NA 50.42857 4.0 50.42857
5 5 5 9.5 NA 47.28571 4.0 47.28571
6 6 6 11.5 NA 48.83333 3.5 48.83333
7 7 7 13.5 NA 44.50000 3.5 44.50000
8 8 8 15.5 NA 28.57143 4.0 28.57143
9 9 9 17.5 NA 36.00000 4.0 36.00000
10 10 10 19.5 NA 31.00000 4.0 31.00000
11 11 11 21.5 NA 52.50000 3.5 52.50000
12 12 12 23.5 NA 60.00000 4.0 60.00000
13 13 13 25.5 NA 38.40000 3.0 38.40000
14 14 14 27.5 NA 40.14286 4.0 40.14286
15 15 15 29.5 NA 36.00000 3.0 36.00000
16 16 16 31.5 NA 54.71429 4.0 54.71429
Proportion.of.successful.trials....
1 0.8000000
2 1.0000000
3 0.5714286
4 0.4285714
5 0.5714286
6 0.5000000
7 0.5000000
8 0.8571429
9 0.7142857
10 0.8571429
11 0.3333333
12 0.0000000
13 0.8000000
14 0.5714286
15 0.6000000
16 0.2857143
Average.second.to.second.change.in.intersection.angle.between.predators.and.prey
1 1.55878862
2 1.46489613
3 1.42389344
4 0.36396349
5 0.52398034
6 1.23139280
7 -0.32201037
8 4.71448952
9 1.11413374
10 0.59904027
11 1.03479994
12 0.09726681
13 0.35182617
14 1.66739219
15 0.91641518
16 5.21208923
Example plot:
