Skip to content

Commit

Permalink
updated plots
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasgerstenberg committed Mar 29, 2024
1 parent 3207306 commit 50ec9ef
Show file tree
Hide file tree
Showing 10 changed files with 6,726 additions and 597 deletions.
205 changes: 187 additions & 18 deletions analysis/children_disagree.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@ library("Metrics") # for rmse
library("scales") # for percentage plots
library("broom.mixed") # for model summaries
library("grateful") # for package citations
library("ggeffects") # for marginal predictions
library("scales") # for percentage scales
library("tidyverse") # for everything else
```

# Helper functions

```{r}
# set classic theme
theme_set(theme_classic())
# function for printing out html or latex tables
print_table = function(data, format = "html", digits = 2){
if(format == "html"){
Expand Down Expand Up @@ -196,10 +201,56 @@ for(i in 7:11){

## PLOTS

### Age continuous plot

```{r, fig.width=8, fig.height=6, warning=FALSE, message=FALSE}
# Data
fit.exp1.inference_age = glmer(formula = ambiguous_yes ~ age_continuous * condition_disagree + (1 | participant),
data = df.exp1,
family = binomial(link = "logit"))
ggpredict(fit.exp1.inference_age,
terms = c("age_continuous [all]", "condition_disagree")) %>%
plot() +
scale_x_continuous(breaks = c(7, 8, 9, 10, 11, 12),
labels = c("7", "8","9","10","11", "12")) +
scale_y_continuous(labels = percent) +
coord_cartesian(xlim = c(7, 12),
ylim = c(0, 1)) +
labs(x = "Age",
y = "Infer Ambiguous Utterance",
fill = "Condition",
title = "Experiment 1: Inference") +
scale_color_manual(name = "Trial Type",
labels = c("Agreement", "Disagreement"),
values = c("#009999", "#CC6600"),
guide = guide_legend(reverse = T)) +
scale_fill_manual(name = "Trial Type",
labels = c("Agreement", "Disagreement"),
values = c("#009999", "#CC6600"),
guide = guide_legend(reverse = T)) +
theme_classic() +
theme(plot.title = element_text(hjust = 0.5,
size = 20,
face = "bold"),
strip.text = element_text(size = 18),
strip.background = element_blank(),
axis.title = element_text(size = 18),
axis.text = element_text(size = 16),
legend.title = element_text(size = 20),
legend.text = element_text(size = 16),
legend.position = "right")
ggsave(file = "../figures/plots/exp1_inference_age.pdf",
width = 8,
height = 6)
```


### Bar plot

```{r, fig.width=12, fig.height=6}
# Data for Plot
# Data
df.plot = df.exp1 %>%
group_by(condition_disagree) %>%
count(age_group, selection) %>%
Expand All @@ -226,7 +277,6 @@ ggplot(data = df.plot,
y = "Inferred Utterance",
fill = "Possible Statements",
title = "Experiment 1: Inference") +
theme_classic() +
theme(plot.title = element_text(hjust = 0.5,
size = 20,
face = "bold"),
Expand All @@ -238,7 +288,7 @@ ggplot(data = df.plot,
legend.text = element_text(size = 16),
legend.position = "right")
ggsave(file = "../figures/plots/exp1_figure.pdf",
ggsave(file = "../figures/plots/exp1_inference.pdf",
width = 12,
height = 6)
```
Expand Down Expand Up @@ -409,6 +459,41 @@ for(i in 7:11){
}
```

##### Inference condition: First story only

Examine story 1 (trials 1 and 2) and story 4 (trials 7 and 8) among 7-year-olds.

```{r}
# story 1, 7 year olds
df.exp2.infer.7.1 = df.exp2.infer %>%
filter(age_group == 7 &
(trial == "trial 1" |trial == "trial 2"))
results = fun.regression(
formula = "ambiguous_yes ~ 1 + condition_disagree + (1 | participant)",
data = df.exp2.infer.7.1)
prop.table(table(df.exp2.infer.7.1$condition_disagree, df.exp2.infer.7.1$ambiguous_yes), margin=1)
fun.table(results, type = "confirmatory")
# story 4, 7 year olds
df.exp2.infer.7.4 = df.exp2.infer %>%
filter(age_group == 7 &
(trial == "trial 7" |trial == "trial 8"))
results = fun.regression(
formula = "ambiguous_yes ~ 1 + condition_disagree + (1 | participant)",
data = df.exp2.infer.7.4)
prop.table(table(df.exp2.infer.7.4$condition_disagree, df.exp2.infer.7.4$ambiguous_yes), margin=1)
fun.table(results, type = "confirmatory")
```



### Bootstrapping confidence intervals

#### Prediction condition
Expand Down Expand Up @@ -612,7 +697,6 @@ df.model.softmax.separate = df.inference %>%
mutate(condition = factor(condition,
levels = c("agree", "disagree"),
labels = c("Agreement Trials", "Disagreement Trials")))
```

#### Linear increase in softmax
Expand Down Expand Up @@ -688,13 +772,101 @@ df.model.posterior %>%
print_table()
```


## PLOTS

### Prediction: Age continuous plot

```{r, fig.width=8, fig.height=6, warning=FALSE, message=FALSE}
fit.exp2.prediction_age = glmer(formula = dis_yes ~ 1 + condition_amb * age_continuous + (1 | participant),
data = df.exp2.predict,
family = binomial(link = "logit"))
ggpredict(fit.exp2.prediction_age, terms = c("age_continuous [all]", "condition_amb")) %>%
plot() +
scale_x_continuous(breaks = 7:12,
labels = 7:12) +
scale_y_continuous(labels = percent) +
coord_cartesian(xlim = c(7, 12),
ylim = c(0, 1)) +
labs(x = "Age",
y = "Predict Disagreement",
fill = "Condition",
title = "Experiment 2: Prediction") +
scale_color_manual(name = "Trial Type",
labels = c("Unambiguous", "Ambiguous"),
values = c("#A4F76A", "#EEA49A"),
guide = guide_legend(reverse = T)) +
scale_fill_manual(name = "Trial Type",
labels = c("Unambiguous", "Ambiguous"),
values = c("#A4F76A", "#EEA49A"),
guide = guide_legend(reverse = T)) +
theme_classic() +
theme(plot.title = element_text(hjust = 0.5,
size = 20,
face = "bold"),
strip.text = element_text(size = 18),
strip.background = element_blank(),
axis.title = element_text(size = 18),
axis.text = element_text(size = 16),
legend.title = element_text(size = 20),
legend.text = element_text(size = 16),
legend.position = "right")
ggsave(file = "../figures/plots/exp2_prediction_age.pdf",
width = 8,
height = 6)
```

### Inference: Age continuous plot

```{r, fig.width=8, fig.height=6, warning=FALSE, message=FALSE}
fit.exp2.age_inference = glmer(formula = ambiguous_yes ~ age_continuous * condition_disagree + (1 | participant),
data = df.exp2.infer,
family = binomial(link = "logit"))
ggpredict(fit.exp2.age_inference,
terms = c("age_continuous [all]", "condition_disagree")) %>%
plot() +
scale_x_continuous(breaks = 7:12,
labels = 7:12) +
scale_y_continuous(labels = percent) +
coord_cartesian(xlim = c(7, 12),
ylim = c(0, 1)) +
labs(x = "Age",
y = "Infer Ambiguous Utterance",
fill = "Condition",
title = "Experiment 2: Inference") +
scale_color_manual(name = "Trial Type",
labels = c("Agreement", "Disagreement"),
values = c("#009999", "#CC6600"),
guide = guide_legend(reverse = T)) +
scale_fill_manual(name = "Trial Type",
labels = c("Agreement", "Disagreement"),
values = c("#009999", "#CC6600"),
guide = guide_legend(reverse = T)) +
theme_classic() +
theme(plot.title = element_text(hjust = 0.5,
size = 20,
face = "bold"),
strip.text = element_text(size = 18),
strip.background = element_blank(),
axis.title = element_text(size = 18),
axis.text = element_text(size = 16),
legend.title = element_text(size = 20),
legend.text = element_text(size = 16),
legend.position = "right")
ggsave(file = "../figures/plots/exp2_inference_age.pdf",
width = 8,
height = 6)
```

### Prediction condition

```{r, fig.width=12, fig.height=6, warning=FALSE}
# data for plot
# Data
df.plot = df.exp2.predict %>%
group_by(condition_amb_c) %>%
count(age_group, dis_yes) %>%
Expand All @@ -707,11 +879,11 @@ df.plot = df.exp2.predict %>%
levels = c("Unambiguous Trials", "Ambiguous Trials"))) %>%
ungroup()
df.plot.boot <- df.prediction.boot %>%
# Bootstrapped confidence intervals
df.plot.boot = df.prediction.boot %>%
mutate(condition_amb_c = factor(condition_amb_c,
levels = c("Unambiguous Trials", "Ambiguous Trials")))
# plot
# Plot
ggplot(data = df.plot,
mapping = aes(x = age_group,
y = n,
Expand All @@ -727,13 +899,13 @@ ggplot(data = df.plot,
x = age_group)) +
facet_grid(cols = vars(condition_amb_c),
scales = "free") +
scale_fill_manual(values = c("#A4F76A", "#EEA49A")) +
scale_fill_manual(values = c("#A4F76A", "#EEA49A"),
na.translate = F) +
scale_y_continuous(labels = percent) +
labs(x = "Age",
y = "Predicted Outcome",
fill = "Possible Outcomes") +
ggtitle("Experiment 2: Prediction") +
theme_classic() +
theme(plot.title = element_text(hjust = 0.5, size = 20, face = "bold"),
strip.text = element_text(size = 18),
strip.background = element_blank(),
Expand All @@ -743,16 +915,16 @@ ggplot(data = df.plot,
legend.text = element_text(size = 16),
legend.position = "right")
ggsave(file = "../figures/plots/exp2_pred.pdf",
ggsave(file = "../figures/plots/exp2_prediction.pdf",
width = 12,
height = 6)
```

### Inference condition

```{r, fig.width=12, fig.height=6, warning=FALSE}
# Data for Inference Plot
df.plot.infer <- df.exp2.infer %>%
# Data
df.plot.infer = df.exp2.infer %>%
filter(!is.na(ambiguous_yes)) %>%
rename(condition = condition_disagree_c) %>%
mutate(condition = factor(condition, levels = c("Agreement Trials",
Expand Down Expand Up @@ -781,7 +953,6 @@ df.inference.boot = df.inference.boot %>%
"Disagreement Trials"))) %>%
ungroup()
# Plot
ggplot(data = df.plot.infer,
mapping = aes(x = age_group,
Expand Down Expand Up @@ -817,7 +988,6 @@ ggplot(data = df.plot.infer,
fill = "Possible Statements",
shape = "Model",
title = "Experiment 2: Inference") +
theme_classic() +
theme(plot.title = element_text(hjust = 0.5, size = 20, face = "bold"),
strip.text = element_text(size = 18),
strip.background = element_blank(),
Expand All @@ -827,12 +997,11 @@ ggplot(data = df.plot.infer,
legend.text = element_text(size = 16),
legend.position = "right")
ggsave(str_c("../figures/plots/exp2_infer_models.pdf"),
ggsave(str_c("../figures/plots/exp2_inference_models.pdf"),
width = 12,
height = 6)
```


# Session info

```{r}
Expand Down
5,585 changes: 5,585 additions & 0 deletions analysis/children_disagree.html

Large diffs are not rendered by default.

Loading

0 comments on commit 50ec9ef

Please sign in to comment.