I want to produce multiple reports (using the report
R package) based on my d
result, which is an output when using nest
and unnest
.
library(broom)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(tidyr)
library(report)
library(purrr)
df <- tibble(id = c(1,2,3,4,5,6,7,8),
group = c(1,1,1,1,2,2,2,2),
before = c(2,1,3,1,2,1,2,4),
after = c(1,3,4,7,3,9,3,7))
t_test <- function(df, mu = 0, alt = "two.sided", paired = T, conf.level = .99) {
tidy(t.test(df$before,
df$after,
mu = mu,
alt = alt,
paired = paired,
conf.level = conf.level))
}
d <- df %>%
group_by(group) %>%
nest() %>%
mutate(ttest = map(data, t_test)) %>%
unnest(ttest, .drop = T)
#> Warning: The `.drop` argument of `unnest()` is deprecated as of tidyr 1.0.0.
#> ℹ All list-columns are now preserved.
d[1, 3:10]
#> # A tibble: 1 × 8
#> estimate statistic p.value parameter conf.low conf.high method altern…¹
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
#> 1 -2 -1.36 0.267 3 -10.6 6.60 Paired t-test two.sid…
#> # … with abbreviated variable name ¹alternative
report(d[1, 3:10]) # gives an error
#> Warning in sqrt((6 * (n - 2))/((n 1) * (n 3))): NaNs produced
#> Warning in sqrt((6 * (n - 2))/((n 1) * (n 3))): NaNs produced
#> Warning in sqrt((6 * (n - 2))/((n 1) * (n 3))): NaNs produced
#> Warning in sqrt((6 * (n - 2))/((n 1) * (n 3))): NaNs produced
#> Warning in sqrt((6 * (n - 2))/((n 1) * (n 3))): NaNs produced
#> Warning in sqrt((6 * (n - 2))/((n 1) * (n 3))): NaNs produced
#> Error in names(n_char) <- c("Entry", "n_Entry"): 'names' attribute [2] must be the same length as the vector [1]
Created on 2023-01-16 with reprex v2.0.2
CodePudding user response:
The issue is that you already converted your test results to a tidy dataframe using broom::tidy
. Instead, apply report
on the "raw" test object returned by t.test
:
library(dplyr)
library(tidyr)
library(report)
library(purrr)
t_test <- function(df, mu = 0, alt = "two.sided", paired = T, conf.level = .99) {
t.test(df$before,
df$after,
mu = mu,
alt = alt,
paired = paired,
conf.level = conf.level)
}
d <- df %>%
group_by(group) %>%
nest() %>%
mutate(ttest = map(data, t_test),
report = map(ttest, report))
d$report
#> [[1]]
#> Effect sizes were labelled following Cohen's (1988) recommendations.
#>
#> The Paired t-test testing the difference between df$before and df$after (mean
#> difference = -2.00) suggests that the effect is negative, statistically not
#> significant, and large (difference = -2.00, 99% CI [-10.60, 6.60], t(3) =
#> -1.36, p = 0.267; Cohen's d = -0.88, 99% CI [-1.96, 0.22])
#>
#> [[2]]
#> Effect sizes were labelled following Cohen's (1988) recommendations.
#>
#> The Paired t-test testing the difference between df$before and df$after (mean
#> difference = -3.25) suggests that the effect is negative, statistically not
#> significant, and large (difference = -3.25, 99% CI [-12.90, 6.40], t(3) =
#> -1.97, p = 0.144; Cohen's d = -0.88, 99% CI [-1.96, 0.22])