Skip to content

Commit

Permalink
subfigures for interaction plots
Browse files Browse the repository at this point in the history
  • Loading branch information
phelps-sg committed Dec 26, 2023
1 parent 19d6d71 commit bd1a563
Showing 1 changed file with 60 additions and 2 deletions.
62 changes: 60 additions & 2 deletions jupyter-book/R-MixedModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
library(coda)
library(effects)
library(ggeffects)
library(gridExtra)

# %%
options(repr.plot.width = 20, repr.plot.height = 10)
Expand Down Expand Up @@ -188,8 +189,6 @@
# %%
texreg(model.pd, caption="Fitted model for Prisoners Dilemma", label="table:pd-estimates", file="pd-estimates.tex", single.row=TRUE, fontsize="small")

# %%

# %%
xtable(lme4::formatVC(summary(model.pd)$varcor$cond))

Expand Down Expand Up @@ -225,6 +224,61 @@
plot(predicted.plot)
dev.off()

# %%
interaction.plot <- function(model, participant.group) {
model.term <- sprintf("Model [%s]", model)
participant.group.term <- sprintf("Participant_group [%s]", participant.group)

predicted <- ggpredict(model.pd, c("Partner_condition", model.term, participant.group.term))

p <- plot(predicted) + geom_line(aes(x = x, y = predicted), color = "blue") +
geom_point(aes(x = x, y = predicted), color = "red") +
labs(title=participant.group)

# pdf(sprintf("figs/glmm-interaction-%s-%s.pdf", model, participant.group))
# print(p)
# dev.off()

return(p)
}

interaction.plots <- function(model) {
do.call(grid.arrange,
c(top=model, mclapply(
levels(results.pd$Participant_group),
function(g) interaction.plot(model, g),
mc.cores=6
))
)
}

pdf.interaction.plots <- function(model) {
pdf(sprintf("figs/interaction-plots-%s", model))
result <- interaction.plots(model)
print(result)
dev.off()
return(result)
}

all.interaction.plots <- function() {
lapply(levels(results.pd$Model), pdf.interaction.plots)
}

# %%
all.interaction.plots()

# %%
predicted <- ggpredict(model.pd, c("Participant_group", "Model [gpt-3.5-turbo-1106]", "Partner_condition [unconditional cooperate]"))

# Plot with ggeffects and ggplot2
p <- plot(predicted)

# Modify the plot to use geom_line for straight lines
p + geom_line(aes(x = x, y = predicted), color = "blue") +
geom_point(aes(x = x, y = predicted), color = "red") # Add points for actual data

# %%

# %%
plot(ggpredict(model.pd, c("Participant_group", "Model")))

Expand All @@ -242,6 +296,10 @@
predicted <- ggpredict(model.pd.1, "Temperature")
predicted

# %%
#effects.1 <- allEffects(model.pd.1)
#plot(effects.1)

# %%
plot(allEffects(model.pd))

Expand Down

0 comments on commit bd1a563

Please sign in to comment.