library(dplyr)
library(apa7)
library(tidyr)
library(ggplot2)
library(ggbeeswarm)
library(forcats)
apa7_defaults(font_family = my_font, line_spacing = 1.5)
# Mean and 95% confidence interval, as ggplot2::mean_cl_normal() computes
# them, but without needing the Hmisc package
mean_ci <- function(x) {
x <- x[!is.na(x)]
m <- mean(x)
half <- qt(.975, length(x) - 1) * sd(x) / sqrt(length(x))
data.frame(y = m, ymin = m - half, ymax = m + half)
}The apa_htest function runs common hypothesis tests on the variables in a data frame and makes an APA-style table with one row per test. Each row has descriptive statistics, the test statistic and its degrees of freedom, the p-value, and an effect size with its confidence interval. The tests come from the stats package (e.g., t.test), and the effect sizes are computed from the data by the effectsize package.
| Test | test |
Formula | Default effect size |
|---|---|---|---|
| Welch’s or Student’s t test | "t" |
y ~ group |
Cohen’s d |
| One-sample t test | "t" |
y ~ 1 |
Cohen’s d |
| Paired t test | "t" |
Pair(pre, post) ~ 1 |
dav |
| Wilcoxon rank-sum test | "wilcoxon" |
y ~ group |
Rank-biserial r |
| Wilcoxon signed-rank test | "wilcoxon" |
y ~ 1 or Pair(pre, post) ~ 1
|
Rank-biserial r |
| Correlation test | "cor" |
y ~ x |
The correlation itself |
| Chi-square test of independence | "chisq" |
y ~ group |
Cramér’s V |
Several outcomes on the left side of the formula, joined with +, give one row each. Test results that were already computed (e.g., by t.test) can also be formatted (see Test Results Already Computed). In the console, apa_htest returns an ordinary tibble. In Quarto and R Markdown documents, the tibble is displayed as an APA-style table automatically, and apa_flextable customizes it further.
Comparing Two Groups
Welch’s t test
In the studies in the affect data set in the psychTools package (Rafaeli & Revelle, 2006), participants were randomly assigned to watch one of four 9-minute film clips, and their mood was measured afterward. Did people who watched a sad film (a documentary about the liberation of the Bergen-Belsen concentration camp) feel different from people who watched a comedy (Parenthood)? The grouping variable must have exactly two values, so keep these two films, make the film a factor with readable labels, and give the mood scales names that read well in a table. Also, to make the arbitrary scales of the variables comparable to each other, each was standardized.
d_film <- psychTools::affect |>
filter(Film %in% c(1, 4)) |>
transmute(
Film = factor(Film, levels = c(1, 4), labels = c("Sad", "Comedy")),
`Positive Affect` = PA2,
`Negative Affect` = NA2,
`Energetic Arousal` = EA2,
`Tense Arousal` = TA2,
`State Anxiety` = state2
) |>
mutate(across(-Film, \(x) scale(x) |> as.numeric()))Code
d_film |>
pivot_longer(-Film, names_to = "Mood", values_to = "Mood Intensity") |>
filter_out(is.na(`Mood Intensity`)) |>
mutate(m = mean(`Mood Intensity`, na.rm = TRUE),
.by = c(Mood, Film)) |>
arrange(Film, -m) |>
mutate(Mood = fct_inorder(Mood)) |>
ggplot(aes(Film, `Mood Intensity`)) +
ggbeeswarm::geom_quasirandom(aes(color = Film),pch = 16, size = .5) +
stat_summary(aes(color = Film), fun.data = mean_ci) +
stat_summary(
fun.data = mean_ci,
geom = "label",
aes(
# color = Film,
hjust = ((Film == "Sad") - .5) * 1.7 + .5,
label = after_stat(signs::signs(
y, accuracy = .01, trim_leading_zeros = TRUE
))
),
size = 14 * .8 / ggplot2::.pt,
border.color = NA,
label.padding = margin()
) +
geom_violin(aes(fill = Film),
scale = "width",
colour = NA,
alpha = .4) +
facet_grid(cols = vars(Mood),
labeller = ggplot2::label_wrap_gen(12)) +
theme_light(14, my_font) +
theme(legend.position = "none",
axis.text.x = element_text(hjust = c(.7, .3))) +
scale_fill_viridis_d(begin = .3, end = .7) +
scale_color_viridis_d(begin = .3, end = .7) +
scale_x_discrete(expand = expansion(add = 1.3))Figure 1
Comparing Mean Mood After a Sad Film and a Comedy
apa_htest(
d_film,
`Positive Affect` + `Negative Affect` + `Energetic Arousal` +
`Tense Arousal` + `State Anxiety` ~ Film
) |>
mutate(`*df*` = as.integer(as.numeric(gsub("[^0-9.]", "", `*df*`)))) |>
apa_flextable()Table 1
Welch’s t Tests Comparing Mood After a Sad Film and a Comedy
Variable |
Sad |
Comedy |
t |
df |
p |
Cohen’s d |
95% CI |
|||
|---|---|---|---|---|---|---|---|---|---|---|
M |
SD |
M |
SD |
|||||||
Positive Affect |
−0.35 |
0.91 |
0.34 |
0.97 |
−4.74 |
164 |
<.001 |
−0.73 |
[−1.05, −0.42] |
|
Negative Affect |
0.63 |
0.97 |
−0.62 |
0.52 |
10.32 |
125 |
<.001 |
1.60 |
[ 1.24, 1.96] |
|
Energetic Arousal |
−0.25 |
0.90 |
0.24 |
1.04 |
−3.27 |
162 |
.001 |
−0.51 |
[−0.81, −0.20] |
|
Tense Arousal |
0.43 |
1.07 |
−0.42 |
0.71 |
6.00 |
142 |
<.001 |
0.93 |
[ 0.61, 1.25] |
|
State Anxiety |
0.59 |
0.96 |
−0.53 |
0.70 |
6.12 |
72 |
<.001 |
1.33 |
[ 0.85, 1.80] |
|
By default, the table shows each group’s mean (M) and standard deviation (SD) under a spanner with the group’s name. Then come Welch’s t test, its p-value, and Cohen’s d with its 95% confidence interval. Welch’s test does not assume equal variances, so its degrees of freedom are not whole numbers and differ from row to row. They get their own column (df). When all rows have the same degrees of freedom, as in Student’s t test below, they appear in the column header instead (e.g., t(165)).
Differences are the first group (in factor level order) minus the second, so a positive d means that people who watched the sad film had the larger mean. After the sad film, people felt much more negative affect and tension, and after the comedy, much more positive affect and energy. To flip the direction, reorder the factor levels (e.g., with forcats::fct_rev).
Student’s t test and Hedges’ g
With var_equal = TRUE, the table shows Student’s t test, which assumes equal variances, and Cohen’s d uses the pooled standard deviation. Hedges’ g corrects d for its small-sample bias.
apa_htest(
d_film,
`Positive Affect` + `Energetic Arousal` ~ Film,
var_equal = TRUE,
effect_size = "hedges_g"
)Table 2
Student’s t Tests Comparing Positive Affect and Energetic Arousal After a Sad Film and a Comedy With Hedges’ g
Variable |
Sad |
Comedy |
t(165) |
p |
Hedges’ g |
95% CI |
|||
|---|---|---|---|---|---|---|---|---|---|
M |
SD |
M |
SD |
||||||
Positive Affect |
−0.35 |
0.91 |
0.34 |
0.97 |
−4.73 |
<.001 |
−0.73 |
[−1.04, −0.42] |
|
Energetic Arousal |
−0.25 |
0.90 |
0.24 |
1.04 |
−3.27 |
.001 |
−0.50 |
[−0.81, −0.20] |
|
One-sided tests and other test options
Other arguments are passed to the test function. For example, alternative = "greater" makes a one-sided test, here of whether people felt more negative affect after the sad film than after the comedy. Effect size intervals stay two-sided, and the table note says that the p-values are one-sided.
tbl_greater <- apa_htest(
d_film,
`Negative Affect` ~ Film,
alternative = "greater"
)```{r}
#| label: tbl-one-sided
#| apa7-width: landscape
#| tbl-cap: "One-Sided Welch's *t* Test of Negative Affect"
#| apa-note: !expr 'apa_note(tbl_greater)'
tbl_greater
```Table 3
One-Sided Welch’s t Test of Negative Affect
Variable |
Sad |
Comedy |
t(125.57) |
p |
Cohen’s d |
95% CI |
|||
|---|---|---|---|---|---|---|---|---|---|
M |
SD |
M |
SD |
||||||
Negative Affect |
0.63 |
0.97 |
−0.62 |
0.52 |
10.32 |
<.001 |
1.60 |
[1.24, 1.96] |
|
One-Sample Tests
To compare a mean with a fixed value, put 1 on the right side of the formula and give the value as mu. The sat.act data set in the psych package (Revelle, 2026) has the self-reported SAT scores of 700 people. SAT section scores were scaled to have a mean of 500. Is the mean SAT Verbal score in sat.act different from 500?
Paired Tests
One pair
In the sleep data set, 10 patients each took two sleep drugs. The outcome variable represents the number of hours of extra sleep compared to a control condition (not shown here). Paired data must be in wide format, with one column per measurement.
d_sleep <- sleep |>
tidyr::pivot_wider(names_from = group, values_from = extra, names_prefix = "Drug ")
d_sleep# A tibble: 10 × 3
ID `Drug 1` `Drug 2`
<fct> <dbl> <dbl>
1 1 0.7 1.9
2 2 -1.6 0.8
3 3 -0.2 1.1
4 4 -1.2 0.1
5 5 -0.1 -0.1
6 6 3.4 4.4
7 7 3.7 5.5
8 8 0.8 1.6
9 9 0 4.6
10 10 2 3.4
Code
sleep |>
rename(Drug = group,
Hours = extra) |>
ggplot(aes(Drug, Hours)) +
geom_line(aes(group = ID), color = "gray") +
geom_violin(aes(fill = Drug), color = NA, scale = "area", width = .9, alpha = .2) +
geom_point(aes(color = Drug), pch = 16) +
stat_summary(fun.data = mean_ci, position = position_nudge(c(-.05, .05)), aes(color = Drug)) +
stat_summary(
fun.data = mean_ci,
geom = "text",
aes(
color = Drug,
hjust = ((Drug == 1) - .5) * 2.7 + .5,
label = after_stat(signs::signs(
y, accuracy = .01
))
),
size = 14 * .8 / ggplot2::.pt
)+
theme_minimal(16, my_font) +
theme(legend.position = "none") +
scale_color_viridis_d(begin = .3, end = .7) +
scale_fill_viridis_d(begin = .3, end = .7)Figure 2
Mean Additional Hours of Sleep by Drug Condition
To test whether the means of the paired variables are significantly different, put the two variables of each pair in Pair(), with 1 on the right side of the formula, just as would be done in the t.test function:
The table note extracted with apa_note says how d was standardized:
```{r}
#| label: tbl-paired
#| tbl-cap: "Paired-Samples *t* test"
#| apa-note: !expr 'apa7::apa_note(tbl_sleep)'
tbl_sleep
```Table 5
Paired-Samples t test
Variable |
Drug 1 |
Drug 2 |
t(9) |
p |
dav |
95% CI |
|||
|---|---|---|---|---|---|---|---|---|---|
M |
SD |
M |
SD |
||||||
Drug 1 vs. Drug 2 |
0.75 |
1.79 |
2.33 |
2.00 |
−4.06 |
.003 |
−0.83 |
[−1.23, −0.43] |
|
With one pair, the variable names head their descriptive statistics, and differences are the first variable minus the second.
Effect sizes for paired data
Paired data have several versions of Cohen’s d, which differ in the standard deviation that the mean difference is divided by (Lakens, 2013):
effect_size |
Effect size | Mean difference divided by |
|---|---|---|
"d_av" (default) |
dav | The average of the two variables’ SDs |
"d_z" |
dz | The SD of the differences |
"d_rm" |
drm | The SD of the differences, corrected for the correlation between the variables |
"g_av", "g_z", "g_rm"
|
gav, gz, grm | As above, with Hedges’ small-sample correction |
The default, dav, is on the same scale as d for two independent groups, so it can be compared with between-subjects studies. dz is often much larger, because the SD of the differences is small when the two variables are highly correlated. It describes how consistent the change is rather than how large it is relative to individual differences. "cohens_d" is the same as "d_av", and "hedges_g" is the same as "g_av". All of them come from effectsize::repeated_measures_d.
Several pairs
In this made-up study, anxiety and depression were measured before and after an intervention designed specifically to reduce anxiety:
Code
d_prepost |>
mutate(ID = row_number()) |>
pivot_longer(-ID, values_to = "Intensity") |>
separate(name, c("Mood", "Time")) |>
mutate(Time = factor(Time, levels = c("Pre", "Post"))) |>
ggplot(aes(Time, Intensity)) +
geom_line(aes(group = ID), color = "gray") +
geom_violin(
aes(fill = Time),
color = NA,
scale = "area",
width = .9,
alpha = .2
) +
geom_point(aes(color = Time), pch = 16) +
stat_summary(fun.data = mean_ci,
position = position_nudge(c(-.1, .1)),
aes(color = Time)) +
stat_summary(
fun.data = mean_ci,
geom = "text",
aes(
color = Time,
hjust = ((Time == "Pre") - .5) * 2.5 + .5,
label = after_stat(signs::signs(y, accuracy = .1))
),
size = 0.8 * 16 / ggplot2::.pt
) +
facet_grid(cols = vars(Mood)) +
theme_light(16, my_font) +
theme(legend.position = "none") +
scale_color_viridis_d(begin = .3, end = .7) +
scale_fill_viridis_d(begin = .3, end = .7)Figure 3
Mean Anxiety and Depression Before and After Intervention
To test several pairs of variables, join paired variables with + to get one row per pair. Their descriptive statistics are headed “First” and “Second” unless you set pair_labels.
apa_htest(
d_prepost,
Pair(`Anxiety Pre`, `Anxiety Post`) + Pair(`Depression Pre`, `Depression Post`) ~ 1,
pair_labels = c("Pretest", "Posttest")
) |>
mutate(Variable = apa7::hanging_indent(Variable, width = 20)) |>
apa_flextable(no_format_columns = Variable) |>
flextable::width(width = c(1.58,.5,.5, .02,.5,.5, rep(.6, 3), 1.1)) Table 7
Paired-Samples t Tests of Anxiety and Depression Before and After Treatment
Variable |
Pretest |
Posttest |
t(39) |
p |
dav |
95% CI |
|||
|---|---|---|---|---|---|---|---|---|---|
M |
SD |
M |
SD |
||||||
Anxiety Pre vs. |
50.92 |
8.87 |
44.20 |
9.32 |
7.65 |
<.001 |
0.74 |
[ 0.55, 0.93] |
|
Depression Pre vs. |
51.17 |
8.53 |
51.10 |
8.90 |
0.15 |
.88 |
0.01 |
[−0.09, 0.11] |
|
Each pair uses the rows in which both of its variables are observed.
Wilcoxon Tests
With test = "wilcoxon", the table shows each group’s median (Mdn), the Wilcoxon test statistic (W for the rank-sum test, V for the signed-rank test), and the rank-biserial correlation (rrb). Like other correlations, rrb ranges from −1 to 1 and has no leading zero.
apa_htest(
d_film,
`Negative Affect` + `Tense Arousal` ~ Film,
test = "wilcoxon",
exact = FALSE
) |>
rename(`*Mdn*_Sad` = `Sad_*Mdn*`,
`*Mdn*_Comedy` = `Comedy_*Mdn*`)Table 8
Wilcoxon Rank-Sum Tests Comparing Negative Affect and Tense Arousal After a Sad Film and a Comedy
Variable |
Mdn |
W |
p |
rrb |
95% CI |
|
|---|---|---|---|---|---|---|
Sad |
Comedy |
|||||
Negative Affect |
0.51 |
−0.95 |
6,259.50 |
<.001 |
.80 |
[.72, .85] |
Tense Arousal |
0.54 |
−0.36 |
5,175.00 |
<.001 |
.48 |
[.34, .61] |
With ties in the data, wilcox.test cannot compute exact p-values and warns about it. exact = FALSE asks for the normal approximation without the warning.
Signed-rank tests use the same formulas as one-sample and paired t tests:
apa_htest(
d_sleep,
Pair(`Drug 1`, `Drug 2`) ~ 1,
test = "wilcoxon",
exact = FALSE
) |>
rename(`*Mdn*_Drug 1` = `Drug 1_*Mdn*`,
`*Mdn*_Drug 2` = `Drug 2_*Mdn*`)Table 9
Wilcoxon Signed-Rank Test of Extra Sleep Under Two Drugs
Variable |
Mdn |
V |
p |
rrb |
95% CI |
|
|---|---|---|---|---|---|---|
Drug 1 |
Drug 2 |
|||||
Drug 1 vs. Drug 2 |
0.35 |
1.75 |
0 |
.009 |
−1.00 |
[−1.00, −1.00] |
Every patient slept more with Drug 2 (or the same with both), so rrb is −1.00.
Correlation Tests
With test = "cor", each variable on the left is correlated with the variable on the right. The table shows the sample size, the correlation with its confidence interval, and the t test of the correlation. The epi.bfi data in the psychTools package (Revelle, 2019) has personality, depression, and anxiety scores from 231 undergraduates. How strongly do depression, anxiety, and extraversion correlate with neuroticism?
d_personality <- psychTools::epi.bfi |>
as_tibble() |>
select(
Neuroticism = bfneur,
Extraversion = bfext,
Depression = bdi,
Anxiety = traitanx
)
apa_htest(
d_personality,
Depression + Anxiety + Extraversion ~ Neuroticism,
test = "cor"
)Table 10
Pearson Correlations of Depression, Anxiety, and Extraversion With Neuroticism
Variable |
n |
r |
95% CI |
t(229) |
p |
|---|---|---|---|---|---|
Depression |
231 |
.47 |
[ .36, .56] |
7.97 |
<.001 |
Anxiety |
231 |
.59 |
[ .50, .67] |
11.14 |
<.001 |
Extraversion |
231 |
.04 |
[−.09, .17] |
0.56 |
.57 |
method = "spearman" or method = "kendall" gives rank correlations, which have no confidence interval:
apa_htest(
d_personality,
Depression + Anxiety ~ Neuroticism,
test = "cor",
method = "spearman",
exact = FALSE
)Table 11
Spearman Correlations of Depression and Anxiety With Neuroticism
Variable |
n |
ρ |
p |
|---|---|---|---|
Depression |
231 |
.47 |
<.001 |
Anxiety |
231 |
.62 |
<.001 |
To display all the correlations among a set of variables, use apa_cor (see the correlations article).
Chi-Square Tests of Independence
With test = "chisq", each variable on the left is cross-tabulated with the variable on the right and tested for independence. The default effect size is Cramér’s V. For 2 × 2 tables, effect_size = "phi" gives φ.
The survey data in the MASS package (Venables & Ripley, 2002) has the answers of 237 statistics students to questions about themselves, including which hand they write with, which hand is on top when they clap, and which arm is on top when they fold their arms. Do left-handed and right-handed writers differ in how they clap, how they fold their arms, or whether they smoke?
d_survey <- MASS::survey |>
as_tibble() |>
transmute(
`Writing Hand` = W.Hnd,
Clapping = factor(Clap,
levels = c("Left", "Right", "Neither"),
labels = c("Left hand on top", "Right hand on top", "Neither")),
`Arm Folding` = factor(Fold,
levels = c("L on R", "R on L", "Neither"),
labels = c("Left arm on top", "Right arm on top", "Neither")),
Smoker = factor(Smoke != "Never", labels = c("No", "Yes"))
)Descriptive statistics can be seen in Table 12.
d_survey |>
apa_descriptives(by = `Writing Hand`, missing = "row")Table 12
Variable Frequencies and Percentages by Writing Hand
Variable |
Left (n = 18) |
Right (n = 218) |
Total (N = 236) |
|---|---|---|---|
Clapping, n (%) |
|||
Left hand on top |
9 (50.0) |
29 (13.4) |
38 (16.2) |
Right hand on top |
4 (22.2) |
143 (65.9) |
147 (62.6) |
Neither |
5 (27.8) |
45 (20.7) |
50 (21.3) |
Missing |
0 |
1 |
1 |
Arm Folding, n (%) |
|||
Left arm on top |
10 (55.6) |
88 (40.4) |
98 (41.5) |
Right arm on top |
7 (38.9) |
113 (51.8) |
120 (50.8) |
Neither |
1 (5.6) |
17 (7.8) |
18 (7.6) |
Smoker, n (%) |
|||
No |
13 (72.2) |
175 (80.6) |
188 (80.0) |
Yes |
5 (27.8) |
42 (19.4) |
47 (20.0) |
Missing |
0 |
1 |
1 |
The formal significance tests are seen in Table 13.
apa_htest(
d_survey,
Clapping + `Arm Folding` + Smoker ~ `Writing Hand`,
test = "chisq"
) |>
apa_flextable(note = TRUE)Table 13
Chi-Square Tests of Clapping, Arm Folding, and Smoking by Writing Hand
Variable |
N |
χ2 |
df |
p |
Cramér’s V |
95% CI |
|---|---|---|---|---|---|---|
Clapping |
235 |
19.25 |
2 |
<.001 |
.29 |
[.15, .41] |
Arm Folding |
236 |
1.58 |
2 |
.45 |
.08 |
[.00, .19] |
Smoker |
235 |
0.30 |
1 |
.58 |
.06 |
[.00, .18] |
Note. Chi-square tests of independence with Writing Hand. 2 × 2 tables use Yates’s continuity correction. CI = confidence interval for Cramér’s V. | ||||||
Left-handed writers were much more likely than right-handed writers to clap with the left hand on top, but the two groups did not differ in how they folded their arms or in how many smoked. As in chisq.test, 2 × 2 tables (here, Smoker × Writing Hand) use Yates’s continuity correction, and the note says so. Use correct = FALSE to turn it off. If some expected counts are small, chisq.test warns that the chi-square approximation may be incorrect. Here, only 18 students wrote with their left hand, so it warns for every table (the warnings are hidden with the warning: false chunk option). With so few cases in a group, Fisher’s exact test, which apa_chisq offers, is safer. To display the cell counts of one contingency table, use apa_chisq (see the contingency tables article).
Customizing Hypothesis Test Tables
Notes
apa_htest records how the tests were done, and apa_note turns that record into note text. The note names the test, explains the effect size and the abbreviations, and, when used, explains stars and bold values. The text is in markdown, and it does not start with “Note.” because apaquarto adds that.
tbl_film <- apa_htest(
d_film,
`Positive Affect` + `Negative Affect` ~ Film,
starred = "t"
)
apa_note(tbl_film)[1] "Welch's *t* tests compare the groups of Film without assuming equal variances. Cohen's *d* uses the average of the two groups' variances. CI = confidence interval for Cohen's *d*. ^\\* ^*p* ≤ .05. ^\\*\\* ^*p* ≤ .01. ^\\*\\*\\* ^*p* ≤ .001."
With apaquarto, use apa_note in the apa-note chunk option with !expr. Chunk options are evaluated before the chunk runs, so make the table in an earlier chunk (as tbl_film was made above) and display it in the chunk with the note:
```{r}
#| label: tbl-film-note
#| apa7-width: landscape
#| column: page-right
#| tbl-cap: Welch's *t* Tests Comparing Affect After a Sad Film and a Comedy
#| apa-note: !expr apa7::apa_note(tbl_film)
tbl_film
```Table 14
Welch’s t Tests Comparing Affect After a Sad Film and a Comedy
Variable |
Sad |
Comedy |
t |
df |
p |
Cohen’s d |
95% CI |
|||
|---|---|---|---|---|---|---|---|---|---|---|
M |
SD |
M |
SD |
|||||||
Positive Affect |
−0.35 |
0.91 |
0.34 |
0.97 |
−4.74*** |
164.57 |
<.001 |
−0.73 |
[−1.05, −0.42] |
|
Negative Affect |
0.63 |
0.97 |
−0.62 |
0.52 |
10.32*** |
125.57 |
<.001 |
1.60 |
[ 1.24, 1.96] |
|
Without apaquarto, apa_flextable(tbl, note = TRUE) puts the same note below the table. Do not combine it with the apa-note chunk option, or the note will appear twice.
apa_flextable(tbl_film, note = TRUE)Table 15
Welch’s t Tests Comparing Affect After a Sad Film and a Comedy With a Generated Note
Variable |
Sad |
Comedy |
t |
df |
p |
Cohen’s d |
95% CI |
|||
|---|---|---|---|---|---|---|---|---|---|---|
M |
SD |
M |
SD |
|||||||
Positive Affect |
−0.35 |
0.91 |
0.34 |
0.97 |
−4.74*** |
164.57 |
<.001 |
−0.73 |
[−1.05, −0.42] |
|
Negative Affect |
0.63 |
0.97 |
−0.62 |
0.52 |
10.32*** |
125.57 |
<.001 |
1.60 |
[ 1.24, 1.96] |
|
Note. Welch’s t tests compare the groups of Film without assuming equal variances. Cohen’s d uses the average of the two groups’ variances. CI = confidence interval for Cohen’s d. * p ≤ .05. ** p ≤ .01. *** p ≤ .001. | ||||||||||
The generated note is a starting point. With collapse = NULL, apa_note returns one sentence per element, so you can drop, reword, or add sentences.
Stars and bold values
starred puts significance stars on the columns you name, and bolded bolds significant values:
apa_htest(
d_film,
`Energetic Arousal` + `Tense Arousal` ~ Film,
bolded = "p"
)Table 16
Welch’s t Tests With Significant p-Values in Bold
Variable |
Sad |
Comedy |
t |
df |
p |
Cohen’s d |
95% CI |
|||
|---|---|---|---|---|---|---|---|---|---|---|
M |
SD |
M |
SD |
|||||||
Energetic Arousal |
−0.25 |
0.90 |
0.24 |
1.04 |
−3.27 |
162.32 |
.001 |
−0.51 |
[−0.81, −0.20] |
|
Tense Arousal |
0.43 |
1.07 |
−0.42 |
0.71 |
6.00 |
142.09 |
<.001 |
0.93 |
[ 0.61, 1.25] |
|
Fewer columns
descriptives = FALSE drops the descriptive statistics, and effect_size = "none" drops the effect size:
apa_htest(
d_film,
`Energetic Arousal` + `Tense Arousal` ~ Film,
descriptives = FALSE,
effect_size = "none"
)Table 17
Welch’s t Tests Without Descriptive Statistics or Effect Sizes
Variable |
t |
df |
p |
|---|---|---|---|
Energetic Arousal |
−3.27 |
162.32 |
.001 |
Tense Arousal |
6.00 |
142.09 |
<.001 |
Labels
Variable names are converted to title case (e.g., bdi becomes Bdi). For better labels, rename the variables before calling apa_htest, as was done throughout this article. Because the output is a tibble, you can also change the labels afterward.
Missing data
Each test uses the rows in which the outcome and the grouping (or predictor) variable are observed, so rows can have different sample sizes. In affect, for example, state anxiety after the film was measured in only one of the two studies, so the test of State Anxiety in the first table uses fewer people than the tests of the other mood scales. Paired tests use the rows in which both variables of the pair are observed.
Test Results Already Computed
Functions like t.test return htest objects, which have the test statistic and p-value but not the data. apa_htest also accepts these objects, or a list of objects of the same kind, with one row per test. The names of the list label the rows:
neuroticism_tests <- list(
Depression = cor.test(d_personality$Neuroticism, d_personality$Depression),
Anxiety = cor.test(d_personality$Neuroticism, d_personality$Anxiety),
Extraversion = cor.test(d_personality$Neuroticism, d_personality$Extraversion)
)
apa_htest(neuroticism_tests) |>
apa_flextable(note = TRUE)Table 18
Correlations of Neuroticism With Depression, Anxiety, and Extraversion
Variable |
n |
r |
95% CI |
t(229) |
p |
|---|---|---|---|---|---|
Depression |
231 |
.47 |
[ .36, .56] |
7.97 |
<.001 |
Anxiety |
231 |
.59 |
[ .50, .67] |
11.14 |
<.001 |
Extraversion |
231 |
.04 |
[−.09, .17] |
0.56 |
.57 |
Note. Results are from Pearson correlation tests. CI = confidence interval for r. | |||||
Effect sizes, however, are computed from what the objects keep, and that is not always enough:
| Test | Effect size from the object |
|---|---|
| One-sample t test | Cohen’s d = t/√n, exactly |
| Paired t test | dz = t/√n, exactly (not dav, which needs the data) |
| Welch’s or Student’s t test | Cohen’s d ≈ 2t/√df, which assumes groups of equal size |
| Correlation test | The correlation itself |
| Chi-square test of independence | Cramér’s V (or φ, or Cohen’s w), exactly, from the observed table |
| Wilcoxon tests | None, because the group sizes are not kept |
For two groups, the approximation can be noticeably off when the groups differ in size or variance. Negative affect varied much more after the sad film (SD = 5.35) than after the comedy (SD = 2.88), so the approximate d here is 1.84, rather than the 1.60 computed from the data in the first table. The note says that it is an approximation:
t.test(`Negative Affect` ~ Film, data = d_film) |>
apa_htest() |>
apa_flextable(note = TRUE)Table 19
Welch’s t Test of Negative Affect by Film
Variable |
t(125.57) |
p |
Cohen’s d |
95% CI |
|---|---|---|---|---|
Negative Affect by Film |
10.32 |
<.001 |
1.84 |
[1.42, 2.26] |
Note. The result is from Welch’s t test. Cohen’s d is approximated from t and its degrees of freedom (2t/√df), which assumes groups of equal size. CI = confidence interval for Cohen’s d. | ||||
When the data are at hand, giving them to apa_htest is better: the effect sizes are then computed from the data rather than approximated from the test statistic, and the table has descriptive statistics.