Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds tidy-method form summary_emm-objects. #691

Merged
merged 6 commits into from Aug 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions DESCRIPTION
Expand Up @@ -235,6 +235,11 @@ Authors@R:
family = "Szoecs",
role = "ctb",
email = "eduardszoecs@gmail.com"),
person(given = "Frederik",
family = "Aust",
role = "ctb",
email = "frederik.aust@uni-koeln.de",
comment = c(ORCID = "0000-0003-4900-788X")),
person(given = "Angus",
family = "Moore",
role = "ctb",
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Expand Up @@ -205,6 +205,7 @@ S3method(tidy,summary.glht)
S3method(tidy,summary.lm)
S3method(tidy,summary.lm.beta)
S3method(tidy,summary.manova)
S3method(tidy,summary_emm)
S3method(tidy,survdiff)
S3method(tidy,survexp)
S3method(tidy,survfit)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Expand Up @@ -127,6 +127,8 @@ TODO: sort out what happens to `glance.aov()`

- `tidy.lmodel2()` now returns a `p.value` column (#570)

- Added `tidy.summary_emm()` (#691 by @crsh)

- `tidy.zoo()` now doesn't change column names that have spaces or other
special characters (previously they were converted to data.frame friendly
column names by `make.names`)
Expand Down
61 changes: 54 additions & 7 deletions R/emmeans-tidiers.R
Expand Up @@ -62,6 +62,9 @@
#' ggplot(tidy(by_price), aes(price2, estimate, color = day)) +
#' geom_line() +
#' geom_errorbar(aes(ymin = conf.low, ymax = conf.high))
#'
#' # joint_tests
#' tidy(joint_tests(oranges_lm1))
#'
#' @aliases emmeans_tidiers
#' @export
Expand Down Expand Up @@ -126,9 +129,43 @@ tidy.emmGrid <- function(x, ...) {
tidy_emmeans(x, ...)
}

#' @templateVar class summary_emm
#' @template title_desc_tidy
#'
#' @param x An `summary_emm` object.
#' @inherit tidy.lsmobj params examples details
#'
crsh marked this conversation as resolved.
Show resolved Hide resolved
#' @evalRd return_tidy(
#' "std.error",
#' "df",
#' "num.df",
#' "den.df",
#' "conf.low",
#' "conf.high",
#' level1 = "One level of the factor being contrasted",
#' level2 = "The other level of the factor being contrasted",
#' "contrast",
#' term = "Model term in joint tests",
#' "p.value",
#' statistic = "T-ratio statistic or F-ratio statistic",
#' estimate = "Estimated least-squares mean."
#' )
#'
#' @export
#' @family emmeans tidiers
#' @seealso [tidy()], [emmeans::ref_grid()], [emmeans::emmeans()],
#' [emmeans::contrast()]
tidy.summary_emm <- function(x, ...) {
tidy_emmeans_summary(x, ...)
}

tidy_emmeans <- function(x, ...) {
s <- summary(x, ...)
ret <- as.data.frame(s)
tidy_emmeans_summary(s)
}

tidy_emmeans_summary <- function(x) {
ret <- as.data.frame(x)
repl <- list(
"lsmean" = "estimate",
"emmean" = "estimate",
Expand All @@ -137,17 +174,27 @@ tidy_emmeans <- function(x, ...) {
"SE" = "std.error",
"lower.CL" = "conf.low",
"upper.CL" = "conf.high",
"t.ratio" = "statistic"
"t.ratio" = "statistic",
"F.ratio" = "statistic",
"df1" = "num.df",
"df2" = "den.df",
"model term" = "term"
)

if ("contrast" %in% colnames(ret) &&
all(stringr::str_detect(ret$contrast, " - "))) {
all(stringr::str_detect(ret$contrast, " - "))) {
ret <- tidyr::separate_(ret, "contrast",
c("level1", "level2"),
sep = " - "
c("level1", "level2"),
sep = " - "
)
}

colnames(ret) <- dplyr::recode(colnames(ret), !!!(repl))

if("term" %in% colnames(ret)) {
ret <- ret %>%
mutate(term = stringr::str_trim(term))
}

as_tibble(ret)
}
3 changes: 3 additions & 0 deletions man/broom.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion man/tidy.emmGrid.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion man/tidy.lsmobj.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion man/tidy.ref.grid.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

96 changes: 96 additions & 0 deletions man/tidy.summary_emm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion tests/testthat/test-emmeans.R
Expand Up @@ -11,6 +11,10 @@ rg <- ref.grid(fit)

marginal <- lsmeans(rg, "day")

marginal_summary <- summary(marginal)

joint_tests_summary <- joint_tests(fit)

# generate dataset with dashes
marginal_dashes <- tibble(
y = rnorm(100),
Expand All @@ -30,7 +34,7 @@ test_that("tidy.lsmobj", {
tdm <- tidy(marginal)
tdmd <- tidy(marginal_dashes)
tdc <- tidy(contrast(marginal, method = "pairwise"))

check_tidy_output(tdm, strict = FALSE)
check_tidy_output(tdmd, strict = FALSE)
check_tidy_output(tdc, strict = FALSE)
Expand All @@ -45,3 +49,14 @@ test_that("ref.grid tidiers work", {
check_tidy_output(td, strict = FALSE)
check_dims(td, 36, 7)
})

test_that("summary_emm tidiers work", {
tdm <- tidy(marginal)
tdms <- tidy(marginal_summary)

expect_identical(tdm, tdms)

tdjt <- tidy(joint_tests_summary)
check_tidy_output(tdjt)
check_dims(tdjt, 2, 5)
})