Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
IndrajeetPatil committed Nov 21, 2023
1 parent 2305c90 commit 46bbf25
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
24 changes: 12 additions & 12 deletions R/tidy_model_expressions.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
#' library(statsExpressions)
#'
#' # extract a tidy data frame
#' df <- tidy_model_parameters(lm(wt ~ am * cyl, mtcars))
#' df_expr <- tidy_model_parameters(lm(wt ~ am * cyl, mtcars))
#'
#' # create a column containing expression; the expression will depend on `statistic`
#' tidy_model_expressions(df, statistic = "t")
#' tidy_model_expressions(df, statistic = "z")
#' tidy_model_expressions(df, statistic = "chi")
#' tidy_model_expressions(df_expr, statistic = "t")
#' tidy_model_expressions(df_expr, statistic = "z")
#' tidy_model_expressions(df_expr, statistic = "chi")
#' @export
tidy_model_expressions <- function(data,
statistic = NULL,
Expand All @@ -45,7 +45,7 @@ tidy_model_expressions <- function(data,
# expression corresponding to that row
#
# convert the necessary columns to character type for expression
df <- data %>%
df_expr <- data %>%
filter(if_all(
.cols = matches("estimate|statistic|std.error|p.value"),
.fns = Negate(is.na)
Expand All @@ -59,24 +59,24 @@ tidy_model_expressions <- function(data,

# nolint start: line_length_linter.
if (statistic == "t") {
df %<>% mutate(
df_expr %<>% mutate(
expression = case_when(
df.error %in% c("NA", "Inf") ~ glue("list({es.text}=='{estimate}', italic(t)=='{statistic}', italic(p)=='{p.value}')"),
.default = glue("list({es.text}=='{estimate}', italic(t)('{df.error}')=='{statistic}', italic(p)=='{p.value}')")
df_expr.error %in% c("NA", "Inf") ~ glue("list({es.text}=='{estimate}', italic(t)=='{statistic}', italic(p)=='{p.value}')"),
.default = glue("list({es.text}=='{estimate}', italic(t)('{df_expr.error}')=='{statistic}', italic(p)=='{p.value}')")
)
)
}

# z-statistic ---------------------------------

if (statistic == "z") {
df %<>% mutate(expression = glue("list({es.text}=='{estimate}', italic(z)=='{statistic}', italic(p)=='{p.value}')"))
df_expr %<>% mutate(expression = glue("list({es.text}=='{estimate}', italic(z)=='{statistic}', italic(p)=='{p.value}')"))
}

# chi^2-statistic -----------------------------

if (statistic == "c") {
df %<>% mutate(expression = glue("list({es.text}=='{estimate}', italic(chi)^2*('{df.error}')=='{statistic}', italic(p)=='{p.value}')"))
df_expr %<>% mutate(expression = glue("list({es.text}=='{estimate}', italic(chi)^2*('{df_expr.error}')=='{statistic}', italic(p)=='{p.value}')"))
}

# f-statistic ---------------------------------
Expand All @@ -85,13 +85,13 @@ tidy_model_expressions <- function(data,
if (effsize.type == "eta") es.text <- list(quote(widehat(italic(eta)[p]^2)))
if (effsize.type == "omega") es.text <- list(quote(widehat(italic(omega)[p]^2)))

df %<>% mutate(expression = glue("list({es.text}=='{estimate}', italic(F)('{df}', '{df.error}')=='{statistic}', italic(p)=='{p.value}')"))
df_expr %<>% mutate(expression = glue("list({es.text}=='{estimate}', italic(F)('{df_expr}', '{df_expr.error}')=='{statistic}', italic(p)=='{p.value}')"))
}

# nolint end

# Replace `NA` with `NULL` to show nothing instead of an empty string ("")
left_join(data, select(df, term, expression), by = "term") %>%
left_join(data, select(df_expr, term, expression), by = "term") %>%
.glue_to_expression()
}

Expand Down
8 changes: 4 additions & 4 deletions man/tidy_model_expressions.Rd

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

1 change: 0 additions & 1 deletion tests/testthat/test-contingency_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ test_that(

# paired data with NAs ---------------------------------------------

# untabling the data frame
paired_data %<>% tidyr::uncount(weights = Freq)

Check warning on line 78 in tests/testthat/test-contingency_table.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=tests/testthat/test-contingency_table.R,line=78,col=5,[one_call_pipe_linter] Expressions with only a single call shouldn't use pipe %<>%.

# deliberately introduce NAs
Expand Down

0 comments on commit 46bbf25

Please sign in to comment.