-
Notifications
You must be signed in to change notification settings - Fork 1
/
printing.R
147 lines (122 loc) · 4.15 KB
/
printing.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
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
# Tidy p values for printing in text
tidy_p_values <- function(df) {
nbsp <- "\u00A0"
df %<>% mutate(p = case_when(
p.value <= .001 ~ str_c("p", nbsp, "<", nbsp, ".001"),
p.value <= .01 ~ str_c("p", nbsp, "<", nbsp, ".01"),
p.value <= .05 ~ str_c("p", nbsp, "<", nbsp, ".05"),
p.value > .05 ~ str_c(
"p", nbsp, "=", nbsp,
(p.value %>% round(2) %>% format(nsmall = 2) %>% str_remove("^0"))
)
))
return(df)
}
# Tidy means/SD for printing in text
tidy_means <- function(df) {
# Convert means to % for text
df %<>% mutate(percent = mean * 100) # %>%
# Round mean/sd to 2 digits and percent to whole number
df %<>% mutate(across(c(mean, sd), ~format(., digits = 2, nsmall = 2))) %>%
mutate(percent = format(percent, digits = 2, nsmall = 0) %>% str_c("%"))
# Row labels
if ("Pronoun" %in% colnames(df) && "M_Acc" %in% colnames(df)) {
df %<>% ungroup() %>%
mutate(
label = case_when(.default = Pronoun,
Pronoun == "he/him" ~ "H",
Pronoun == "she/her" ~ "S",
Pronoun == "they/them" ~ "T"
),
label = case_when(
M_Acc == 0 ~ str_c(label, " Wrong"),
M_Acc == 1 ~ str_c(label, " Right")
)) %>%
select(-Pronoun, -M_Acc) %>%
column_to_rownames("label")
}
else if ("PSA" %in% colnames(df)) {
df %<>% ungroup() %>%
mutate(label = str_c(PSA, Biography, sep = " ")) %>%
select(-PSA, -Biography) %>%
column_to_rownames("label")
}
else if ("Pronoun" %in% colnames(df)) {
df %<>% mutate(
.keep = c("unused"),
label = case_when(
.default = Pronoun,
Pronoun == "he/him" ~ "H",
Pronoun == "she/her" ~ "S",
Pronoun == "they/them" ~ "T"
)) %>%
column_to_rownames("label")
}
return(df)
}
# Tidy model results for printing in text
tidy_model_results <- function(model) {
require(broom.mixed)
beta <- "\u03B2"
nbsp <- "\u00A0"
z <- "\u007A"
# Get model estimates
df <- model %>% tidy() %>% select(-std.error)
# Drop random effects rows
if ("effect" %in% colnames(df)) {
df %<>% filter(effect == "fixed") %>% select(-effect, -group)
}
# Rename columns from broom
df %<>% rename(
"Predictor" = "term",
"Beta" = "estimate",
"z" = "statistic"
)
# Just two cases where models aren't logistic regression:
# Sentence naturalness ratings (Exp3&4) and RT (Exp4)
if ("Type=Name_Indefinite" %in% df$Predictor || max(df$Beta) > 100) {
df %<>% rename("t" = "z")
}
# Convert p values into < .001 etc
df %<>% tidy_p_values()
# Round beta estimates, add symbol and equals sign
df$Beta %<>% round(2) %>%
format(digits = 2, nsmall = 2, trim = TRUE, justify = "none") %>%
str_c("*", beta, "*", nbsp, "=", nbsp, .)
# Round z/t estimates, add symbol and equals sign
# Then make column with beta, z/t, and p combined with commas between
if ("z" %in% colnames(df)) {
df$z %<>% round(2) %>%
format(digits = 2, nsmall = 2, trim = TRUE, justify = "none") %>%
str_c("*z*", nbsp, "=", nbsp, .)
df %<>% mutate(Text = str_c(Beta, z, p, sep = ", "))
}
if ("t" %in% colnames(df)) {
df$t %<>% round(2) %>%
format(digits = 2, nsmall = 2, trim = TRUE, justify = "none") %>%
str_c("*t*", nbsp, "=", nbsp, .)
df %<>% mutate(Text = str_c(Beta, t, p, sep = ", "))
}
# Remove some extra characters from predictor names
df$Predictor %<>% str_remove_all("\\(|\\)") %>%
str_remove_all("1|0|=1") %>%
str_replace("OrderOrder", "Order") %>%
str_remove_all("_Centered") %>%
str_remove_all("_C") %>%
str_remove_all("_Factor")
# Make predictor column row names so that df can be accessed with [row, col]
df %<>% column_to_rownames(var = "Predictor")
return(df)
}
# Tidy t-test results for printing in text
tidy_t_results <- function(t_test) {
nbsp <- "\u00A0"
df <- t_test %>%
tidy() %>%
select(contains("estimate"), parameter, statistic, p.value) %>%
rename_with(~str_replace(., "estimate", "mean")) %>%
rename("df" = "parameter", "t" = "statistic") %>%
tidy_p_values() %>%
modify_if(is.numeric, format, digits = 2, nsmall = 2)
return(df)
}