-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.Rmd
More file actions
400 lines (332 loc) · 11.9 KB
/
Copy pathstats.Rmd
File metadata and controls
400 lines (332 loc) · 11.9 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
---
title: "stats"
output: html_document
---
```{r setup}
if (!requireNamespace("pacman", quietly = TRUE)) install.packages("pacman")
pacman::p_load(tidyverse, here, lme4, lmerTest, stats, kableExtra)
```
# Load Data
```{r, echo = FALSE, results = 'hide', message = FALSE, warning = FALSE}
# (i'm surpressing the output of this code chunk)
version <- "3.0"
role_to_keep <- "assistant"
# Read text stats, filter for 'assistant' role
textstats_df <- read_csv(here("metrics", paste0("v", version, "_text_stats.csv"))) %>%
filter(role == role_to_keep) %>%
rename(level = group)
# Read textdescriptives, filter for 'assistant' role
textdescriptives_df <- read_csv(here("metrics", paste0("v", version, "_textdescriptives.csv"))) %>%
filter(role == role_to_keep) %>%
rename(text_length = doc_length) %>%
rename(level = group)
# Read surprisal, filter for 'assistant' role
surprisal_df <- read_csv(here("metrics", paste0("v", version, "_surprisal.csv"))) %>%
filter(role == role_to_keep) %>%
rename(level = group)
```
```{r}
# check that they are the same
all(textstats_df$id == textdescriptives_df$id)
all(textstats_df$id == surprisal_df$id)
```
## Function DEF for running mixed effects
```{r}
# define the function to fit mixed effects models
# formula is metric ~ group + (1 | id)
fit_lmer_models <- function(data, models, metrics, output_dir) {
# ensure the output directory exists
dir.create(output_dir, recursive = TRUE, showWarnings = FALSE)
# empty data frame to store results
results_df <- data.frame(
model = character(),
metric = character(),
term = character(),
estimate = numeric(),
std_error = numeric(),
t_value = numeric(),
p_value = numeric(),
stringsAsFactors = FALSE
)
# loop through models and metrics to fit the models and extract statistics
for (model_name in models) {
for (metric in metrics) {
model_data <- data %>% filter(model == model_name)
formula <- as.formula(paste(metric, "~ level + (1 | id)"))
model <- lmer(formula, data = model_data)
model_summary <- summary(model)
# extract all coefficients and their statistics
coefficients <- model_summary$coefficients
# loop through terms to store results
for (term in rownames(coefficients)) {
results_df <- rbind(results_df, data.frame(
model = model_name,
metric = metric,
term = term,
estimate = coefficients[term, "Estimate"],
std_error = coefficients[term, "Std. Error"],
t_value = coefficients[term, "t value"],
p_value = round(coefficients[term, "Pr(>|t|)"], 4) # round p-value
))
}
# save the model summary to a text file
file_name <- paste0(model_name, "_", metric, "_summary.txt")
file_path <- file.path(output_dir, file_name)
capture.output(model_summary, file = file_path)
}
}
# return the results data frame
return(results_df)
}
```
## Run Mixed Effects
```{r}
models <- c("meta-llama--Llama-3.1-8B-Instruct",
"google--gemma-3-12b-it",
"mistralai--Mistral-7B-Instruct-v0.3",
"Qwen--Qwen2.5-7B-Instruct")
```
### Text Stats Models (Readability Metrics)
```{r}
output_dir <- here("results", paste0("v", version), "mixed_effects", "readability")
metrics <- c("fernandez_huerta", "szigriszt_pazos", "gutierrez_polini")
results_textstats <- fit_lmer_models(textstats_df, models, metrics, output_dir)
# print the p-values
print(results_textstats)
```
### Text Descriptives Models (Structural Features)
```{r}
output_dir <- here("results", paste0("v", version), "mixed_effects", "structural")
metrics <- c("text_length", "dependency_distance_mean")
results_textdescriptives <- fit_lmer_models(textdescriptives_df, models, metrics, output_dir)
# print the p-values
print(results_textdescriptives)
```
### Surprisal Models (Surprisal Metrics)
```{r}
output_dir <- here("results", paste0("v", version), "mixed_effects", "surprisal")
metrics <- c("surprisal_mean")
results_surprisal <- fit_lmer_models(surprisal_df, models, metrics, output_dir)
# print the p-values
print(results_surprisal)
```
## Adjusting P-values for Multiple Comparisons
```{r}
# combine all p-values data frames
all_results <- bind_rows(results_textstats, results_textdescriptives, results_surprisal)
# count total number of tests
total_tests <- nrow(all_results)
# apply bonferroni correction
all_results <- all_results %>%
mutate(
p_value_corrected = pmin(p_value * total_tests, 1), # bonferroni correction
p_value_corrected = round(p_value_corrected, 4), # round to 4 decimals
stars = case_when( # add significance stars
p_value_corrected < 0.001 ~ "***",
p_value_corrected < 0.01 ~ "**",
p_value_corrected < 0.05 ~ "*",
TRUE ~ ""
)
)
# rename the uncorrected p-value column
all_results <- all_results %>%
select(-p_value)
# print or save the corrected p-values
print(all_results)
```
## Order and Save results
Order the dataset in a meaningful way:
```{r}
# define the desired order for metrics and models
metric_order <- c("fernandez_huerta", "szigriszt_pazos", "gutierrez_polini",
"text_length", "dependency_distance_mean", "surprisal_mean")
model_order <- c("meta-llama--Llama-3.1-8B-Instruct",
"google--gemma-3-12b-it",
"mistralai--Mistral-7B-Instruct-v0.3",
"Qwen--Qwen2.5-7B-Instruct")
all_results$metric <- factor(all_results$metric, levels = metric_order)
all_results$model <- factor(all_results$model, levels = model_order)
# sort by metric, model
all_results <- all_results[order(all_results$metric, all_results$model), ]
```
```{r}
all_results
```
```{r}
# save the corrected p-values to a CSV file
output_dir <- here("results", paste0("v", version), "mixed_effects")
output_file <- file.path(output_dir, "results.csv")
write_csv(all_results, output_file)
```
## Make LATEX table
```{r}
output_dir <- here("results", paste0("v", version), "mixed_effects")
```
```{r}
# format data
all_results <- all_results %>%
mutate(
formatted_metric = case_when(
metric == "fernandez_huerta" ~ "Fernandez Huerta",
metric == "szigriszt_pazos" ~ "Szigriszt Pazos",
metric == "gutierrez_polini" ~ "Gutierrez Polini",
metric == "text_length" ~ "Text Length",
metric == "dependency_distance_mean" ~ "Mean Dependency Distance",
metric == "surprisal_mean" ~ "Message Surprisal",
TRUE ~ as.character(metric)
),
# Clean up model names for display
display_model = case_when(
model == "meta-llama--Llama-3.1-8B-Instruct" ~ "Llama 3.1 8B Instruct",
model == "google--gemma-3-12b-it" ~ "Gemma 3 12B IT",
model == "mistralai--Mistral-7B-Instruct-v0.3" ~ "Mistral 7B Instruct v0.3",
model == "Qwen--Qwen2.5-7B-Instruct" ~ "Qwen 2.5 7B Instruct",
TRUE ~ as.character(model)
)
)
# use the formatted metric names for display
all_results <- all_results %>%
arrange(metric, model, term) %>%
group_by(metric) %>%
mutate(
# flag the first occurrence of each model within a metric group
first_model_occurrence = !duplicated(model)
) %>%
ungroup()
# create a column that shows the model name only on first occurrence
all_results <- all_results %>%
mutate(
display_model_selective = ifelse(first_model_occurrence, display_model, "")
)
```
```{r}
# create base table
html_table <- all_results %>%
# sort by metric first
arrange(metric, model, term) %>%
# select relevant columns
select(display_model_selective, term, estimate, std_error, t_value, p_value_corrected, stars) %>%
# create kable
kable(
format = "html",
booktabs = TRUE,
digits = 4,
col.names = c("", "Term", "Estimate", "Std. Error", "t-value", "Corrected p-value", "Significance"), # model is first column, but is empty as it does not make sense with metric groupings
align = "lcccccc",
escape = FALSE
) %>%
kable_styling(
bootstrap_options = c("striped", "hover", "condensed"),
full_width = TRUE
)
# group by metrics
metric_rows <- all_results %>%
group_by(metric) %>%
summarize(
count = n(),
formatted_name = first(formatted_metric) # get the formatted name for each metric
) %>%
ungroup()
# apply the grouping
row_index <- 1
for(i in 1:nrow(metric_rows)) {
html_table <- html_table %>%
pack_rows(
group_label = metric_rows$formatted_name[i],
start_row = row_index,
end_row = row_index + metric_rows$count[i] - 1
)
row_index <- row_index + metric_rows$count[i]
}
# display the table
html_table
```
```{r}
html_table %>% save_kable(file = file.path(output_dir, "results_table.html"), self_contained = T)
```
```{r}
# First, create separate dataframes for the two tables
first_three_metrics <- c("fernandez_huerta", "szigriszt_pazos", "gutierrez_polini")
last_three_metrics <- c("text_length", "dependency_distance_mean", "surprisal_mean")
# Filter results for first table
first_table_results <- all_results %>%
filter(metric %in% first_three_metrics)
# Filter results for second table
second_table_results <- all_results %>%
filter(metric %in% last_three_metrics)
# Function to create formatted table with better model grouping
create_formatted_table <- function(data) {
# Create base table
formatted_table <- data %>%
arrange(metric, model, term) %>%
select(display_model_selective, term, estimate, std_error, t_value, p_value_corrected, stars) %>%
kable(
format = "latex",
booktabs = TRUE,
digits = 4,
col.names = c("", "Term", "Est.", "SE", "t", "p (Adj.)", "Sig."),
align = "llrrrrc",
escape = FALSE
) %>%
kable_styling(
latex_options = c("scale_down", "hold_position"),
font_size = 8,
) %>%
column_spec(1, width = "12em") %>%
column_spec(2, width = "6em") %>%
column_spec(3:6, width = "5em")
# Get unique metrics for grouping
metrics <- unique(data$metric)
# For each metric, apply spacing and grouping
current_row <- 1
for (current_metric in metrics) {
# Filter data for the current metric
metric_data <- data %>% filter(metric == current_metric)
# Add the metric header first
metric_name <- metric_data$formatted_metric[1]
metric_count <- nrow(metric_data)
formatted_table <- formatted_table %>%
pack_rows(
group_label = metric_name,
start_row = current_row,
end_row = current_row + metric_count - 1,
bold = TRUE,
italic = FALSE,
hline_after = FALSE
)
# Now handle model grouping within each metric
models <- unique(metric_data$model)
model_row <- current_row
for (current_model in models) {
model_data <- metric_data %>% filter(model == current_model)
model_name <- model_data$display_model_selective[1]
model_count <- nrow(model_data)
# If model appears multiple times with different terms, add subtle grouping
if (model_count > 1) {
formatted_table <- formatted_table %>%
pack_rows(
group_label = "",
start_row = model_row,
end_row = model_row + model_count - 1,
indent = TRUE, # Indent the rows
hline_after = FALSE
)
}
model_row <- model_row + model_count
}
# Add line after this metric group
formatted_table <- formatted_table %>%
row_spec(current_row + metric_count - 1, extra_latex_after = "\\addlinespace")
current_row <- current_row + metric_count
}
return(formatted_table)
}
# Create and save first table (readability metrics)
first_table <- create_formatted_table(first_table_results)
first_table %>% save_kable(file = file.path(output_dir, "readability_table.tex"),
self_contained = TRUE)
# Create and save second table (structural metrics)
second_table <- create_formatted_table(second_table_results)
second_table %>% save_kable(file = file.path(output_dir, "structural_surprisal_table.tex"),
self_contained = TRUE)
```