Skip to content

Commit

Permalink
fix merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Anderson committed Aug 8, 2020
2 parents e8d2265 + f5c2623 commit 5048993
Show file tree
Hide file tree
Showing 61 changed files with 189 additions and 682 deletions.
9 changes: 4 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: equatiomatic
Title: Transform Models into LaTeX Equations
Title: Transform Models into 'LaTeX' Equations
Version: 0.1.0
Authors@R: c(
person(given = "Daniel",
Expand All @@ -24,14 +24,13 @@ Authors@R: c(
comment = c(ORCID = "https://orcid.org/0000-0002-4222-1819"))
)
Description: The goal of equatiomatic is to reduce the pain associated with
writing LaTeX formulas from fitted models. The primary function of
the package, extract_eq, takes a fitted model object as its input
and returns the corresponding LaTeX code for the model.
writing 'LaTeX' formulas from fitted models. The primary function of
the package, extract_eq(), takes a fitted model object as its input
and returns the corresponding 'LaTeX' code for the model.
License: MIT + file LICENSE
Depends: R (>= 3.3.0)
Imports:
broom (>= 0.7.0),
palmerpenguins,
stats
Suggests:
texPreview,
Expand Down
22 changes: 20 additions & 2 deletions R/create_eq.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ create_eq <- function(lhs,...) {
#' \code{extract_rhs}.
#'
#' @inheritParams extract_eq
#' @noRd

create_eq.default <- function(lhs, rhs, ital_vars, use_coefs, coef_digits,
fix_signs, model, intercept, greek, raw_tex) {
Expand All @@ -34,6 +35,7 @@ create_eq.default <- function(lhs, rhs, ital_vars, use_coefs, coef_digits,
}

#' @export
#' @noRd
create_eq.polr <- function(lhs, rhs, ital_vars, use_coefs, coef_digits,
fix_signs, model, ...) {
rhs$final_terms <- create_term(rhs, ital_vars)
Expand All @@ -53,6 +55,7 @@ create_eq.polr <- function(lhs, rhs, ital_vars, use_coefs, coef_digits,
}

#' @export
#' @noRd
create_eq.clm <- function(lhs, rhs, ital_vars, use_coefs, coef_digits,
fix_signs, model, ...) {
rhs$final_terms <- create_term(rhs, ital_vars)
Expand Down Expand Up @@ -80,6 +83,7 @@ create_eq.clm <- function(lhs, rhs, ital_vars, use_coefs, coef_digits,
#' \code{extract_rhs}.
#'
#' @inheritParams extract_eq
#' @noRd

create_term <- function(rhs, ital_vars) {
prim_escaped <- lapply(rhs$primary, function(x) {
Expand Down Expand Up @@ -112,6 +116,7 @@ create_term <- function(rhs, ital_vars) {
#' @param term A character string to escape
#'
#' @return A character string
#' @noRd

escape_tex <- function(term) {
unescaped <- c(" ", "&", "%", "$", "#", "_", "{", "}", "~", "^", "\\")
Expand Down Expand Up @@ -143,6 +148,7 @@ escape_tex <- function(term) {
#' @param ital_vars Passed from \code{extract_eq}
#'
#' @return A character string
#' @noRd

add_tex_ital <- function(term, ital_vars) {
if (any(nchar(term) == 0, ital_vars)) {
Expand All @@ -160,6 +166,7 @@ add_tex_ital <- function(term, ital_vars) {
#' @keywords internal
#'
#' @return A vector of characters
#' @noRd

add_tex_ital_v <- function(term_v, ital_vars) {
vapply(term_v, add_tex_ital, ital_vars, FUN.VALUE = character(1))
Expand All @@ -175,6 +182,7 @@ add_tex_ital_v <- function(term_v, ital_vars) {
#' @param term A character string to TeXify
#'
#' @return A character string
#' @noRd

add_tex_subscripts <- function(term) {
if (any(nchar(term) == 0)) {
Expand All @@ -191,6 +199,7 @@ add_tex_subscripts <- function(term) {
#' @keywords internal
#'
#' @return A vector of characters
#' @noRd

add_tex_subscripts_v <- function(term_v) {
vapply(term_v, add_tex_subscripts, FUN.VALUE = character(1))
Expand All @@ -200,6 +209,7 @@ add_tex_subscripts_v <- function(term_v) {
#' Add multiplication symbol for interaction terms
#'
#' @keywords internal
#' @noRd

add_tex_mult <- function(term) {
paste(term, collapse = " \\times ")
Expand All @@ -214,6 +224,7 @@ add_coefs <- function(rhs, ...) {
#'
#' @export
#' @keywords internal
#' @noRd

add_coefs.default <- function(rhs, term, coef_digits) {
ests <- round(rhs$estimate, coef_digits)
Expand All @@ -226,6 +237,7 @@ add_coefs.default <- function(rhs, term, coef_digits) {

#' @export
#' @keywords internal
#' @noRd

add_coefs.polr <- function(rhs, term, coef_digits) {
ests <- round(rhs$estimate, coef_digits)
Expand All @@ -238,6 +250,7 @@ add_coefs.polr <- function(rhs, term, coef_digits) {

#' @export
#' @keywords internal
#' @noRd

add_coefs.clm <- function(rhs, term, coef_digits) {
ests <- round(rhs$estimate, coef_digits)
Expand All @@ -256,6 +269,8 @@ add_greek <- function(rhs, ...) {
#'
#' @export
#' @keywords internal
#' @noRd

add_greek.default <- function(rhs, terms, greek = "beta", intercept = "alpha",
raw_tex = FALSE) {
int <- switch(intercept,
Expand All @@ -273,6 +288,7 @@ add_greek.default <- function(rhs, terms, greek = "beta", intercept = "alpha",

#' @export
#' @keywords internal
#' @noRd

add_greek.polr <- function(rhs, terms, ...) {
rhs$idx <- unlist(lapply(split(rhs, rhs$coef.type), function(x) {
Expand All @@ -287,6 +303,7 @@ add_greek.polr <- function(rhs, terms, ...) {

#' @export
#' @keywords internal
#' @noRd

add_greek.clm <- function(rhs, terms, ...) {
rhs$idx <- unlist(lapply(split(rhs, rhs$coef.type), function(x) {
Expand All @@ -302,6 +319,7 @@ add_greek.clm <- function(rhs, terms, ...) {
#' Intermediary function to wrap text in `\\beta_{}`
#'
#' @keywords internal
#' @noRd

anno_greek <- function(greek, nums, terms = NULL, raw_tex = FALSE) {
if (raw_tex) {
Expand All @@ -323,8 +341,8 @@ anno_greek <- function(greek, nums, terms = NULL, raw_tex = FALSE) {
#' @keywords internal
#'
#' @param eq String containing a LaTeX equation
#'
#'
#' @noRd

fix_coef_signs <- function(eq) {
# Side-by-side + -
eq_clean <- gsub("\\+ -", "- ", eq)
Expand Down
1 change: 0 additions & 1 deletion R/extract_eq.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
#' extract_eq(mod2)
#'
#' # Works for categorical variables too, putting levels as subscripts
#' library(palmerpenguins)
#' mod3 <- lm(body_mass_g ~ bill_length_mm + species, penguins)
#' extract_eq(mod3)
#'
Expand Down
13 changes: 12 additions & 1 deletion R/extract_lhs.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#'
#' @param model A fitted model
#' @param \dots additional arguments passed to the specific extractor
#' @noRd

extract_lhs <- function(model, ...) {
UseMethod("extract_lhs", model)
Expand All @@ -15,6 +16,7 @@ extract_lhs <- function(model, ...) {
#'
#' @param model A fitted model
#' @param \dots additional arguments passed to the specific extractor
#' @noRd

extract_lhs2 <- function(model, ...) {
UseMethod("extract_lhs2", model)
Expand All @@ -31,6 +33,7 @@ extract_lhs2 <- function(model, ...) {
#' @inheritParams extract_eq
#'
#' @return A character string
#' @noRd

extract_lhs.lm <- function(model, ital_vars, ...) {
lhs <- rownames(attr(model$terms, "factors"))[1]
Expand All @@ -51,6 +54,7 @@ extract_lhs.lm <- function(model, ital_vars, ...) {
#' @inheritParams extract_eq
#'
#' @return A character string
#' @noRd

extract_lhs.glm <- function(model, ital_vars, show_distribution, ...) {
if (show_distribution) {
Expand Down Expand Up @@ -80,6 +84,8 @@ extract_lhs.glm <- function(model, ital_vars, show_distribution, ...) {

#' @export
#' @keywords internal
#' @noRd

extract_lhs2.glm <- function(model, ital_vars, ...) {
outcome <- all.vars(formula(model))[1]
n <- unique(model$model$`(weights)`)
Expand Down Expand Up @@ -136,7 +142,7 @@ extract_lhs2.glm <- function(model, ital_vars, ...) {
#' @inheritParams extract_eq
#'
#' @return A character string
#'
#' @noRd

extract_lhs.polr <- function(model, ital_vars, ...) {
tidied <- broom::tidy(model)
Expand Down Expand Up @@ -164,6 +170,7 @@ extract_lhs.polr <- function(model, ital_vars, ...) {
#' @inheritParams extract_eq
#'
#' @return A character string
#' @noRd

extract_lhs.clm <- function(model, ital_vars, ...) {
tidied <- broom::tidy(model)
Expand All @@ -182,12 +189,14 @@ extract_lhs.clm <- function(model, ital_vars, ...) {

#' modifies lhs of equations that include a link function
#' @keywords internal
#' @noRd
modify_lhs_for_link <- function(model, ...) {
UseMethod("modify_lhs_for_link", model)
}

#' @export
#' @keywords internal
#' @noRd
modify_lhs_for_link.glm <- function(model, lhs) {
if (!(any(grepl(model$family$link, link_function_df$link_name)))) { # is this logical operator not ideal?
message("This link function is not presently supported; using an identity
Expand All @@ -201,6 +210,7 @@ modify_lhs_for_link.glm <- function(model, lhs) {

#' @export
#' @keywords internal
#' @noRd
modify_lhs_for_link.polr <- function(model, lhs) {
matched_row_bool <- grepl(model$method, link_function_df$link_name)
filtered_link_formula <- link_function_df[matched_row_bool, "link_formula"]
Expand All @@ -210,6 +220,7 @@ modify_lhs_for_link.polr <- function(model, lhs) {

#' @export
#' @keywords internal
#' @noRd
modify_lhs_for_link.clm <- function(model, lhs) {
if (!(any(grepl(model$info$link, link_function_df$link_name)))) {
message("This link function is not presently supported; using an identity
Expand Down
12 changes: 11 additions & 1 deletion R/extract_rhs.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
#' #> ..$ : Named chr "Gentoo" ""
#' #> .. ..- attr(*, "names")= chr [1:2] "species" "flipper_length_mm"
#' }
#'
#' @noRd

extract_rhs <- function(model) {
# Extract RHS from formula
Expand Down Expand Up @@ -101,6 +101,7 @@ extract_rhs <- function(model) {
#'
#' extract_primary_term(primaries, full_terms)
#' }
#' @noRd

extract_primary_term <- function(primary_term_v, all_terms) {
detected <- lapply(all_terms, detect_primary, primary_term_v)
Expand All @@ -125,6 +126,7 @@ extract_primary_term <- function(primary_term_v, all_terms) {
#' detect_primary("age", c("partyid", "age", "race"))
#' detect_primary("raceBlack", c("partyid", "age", "race"))
#' }
#' @noRd

detect_primary <- function(full_term, primary_term_v) {
vapply(primary_term_v, function(indiv_term) {
Expand Down Expand Up @@ -158,6 +160,7 @@ detect_primary <- function(full_term, primary_term_v) {
#'
#' extract_all_subscripts(p_list, ft_list)
#' }
#' @noRd

extract_all_subscripts <- function(primary_list, full_term_list) {
Map(extract_subscripts, primary_list, full_term_list)
Expand All @@ -177,6 +180,7 @@ extract_all_subscripts <- function(primary_list, full_term_list) {
#' c("partyidDon't know", "partyidOther party",
#' "partyidNot str democrat"))
#' }
#' @noRd

extract_subscripts <- function(primary, full_term_v) {
out <- switch(as.character(length(primary)),
Expand All @@ -198,19 +202,22 @@ extract_subscripts <- function(primary, full_term_v) {
#' @param tex The TeX version of the RHS of the model (as character), built as
#' \code{rhs_combined} or \code{eq_raw$rhs} in \code{extract_eq()}
#' @param \dots additional arguments passed to the specific extractor
#' @noRd

wrap_rhs <- function(model, tex, ...) {
UseMethod("wrap_rhs", model)
}

#' @export
#' @keywords internal
#' @noRd
wrap_rhs.default <- function(model, tex, ...) {
return(tex)
}

#' @export
#' @keywords internal
#' @noRd
wrap_rhs.glm <- function(model, tex, ...) {
if (model$family$link == "probit") {
rhs <- probitify(tex)
Expand All @@ -223,6 +230,7 @@ wrap_rhs.glm <- function(model, tex, ...) {

#' @export
#' @keywords internal
#' @noRd
wrap_rhs.polr <- function(model, tex, ...) {
if (model$method == "probit") {
rhs <- probitify(tex)
Expand All @@ -235,6 +243,7 @@ wrap_rhs.polr <- function(model, tex, ...) {

#' @export
#' @keywords internal
#' @noRd
wrap_rhs.clm <- function(model, tex, ...) {
if (model$info$link == "probit") {
rhs <- probitify(tex)
Expand All @@ -246,6 +255,7 @@ wrap_rhs.clm <- function(model, tex, ...) {
}

#' @keywords internal
#' @noRd
probitify <- function(tex) {
# Replace existing beginning-of-line \quad space with `\\qquad\` to account for \Phi
tex <- gsub("&\\\\quad", "&\\\\qquad\\\\", tex)
Expand Down
23 changes: 23 additions & 0 deletions R/penguins.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#' Size measurements for adult foraging penguins near Palmer Station, Antarctica
#'
#' Data originally from \code{\link[palmerpenguins]{penguins}}. Includes
#' measurements for penguin species, island in Palmer Archipelago,
#' size (flipper length, body mass, bill dimensions), and sex.
#' This is a subset of \code{\link[palmerpenguins]{penguins_raw}}.
#'
#' @format A tibble with 344 rows and 8 variables:
#' \describe{
#' \item{species}{a factor denoting penguin species (Adélie, Chinstrap and Gentoo)}
#' \item{island}{a factor denoting island in Palmer Archipelago, Antarctica (Biscoe, Dream or Torgersen)}
#' \item{bill_length_mm}{a number denoting bill length (millimeters)}
#' \item{bill_depth_mm}{a number denoting bill depth (millimeters)}
#' \item{flipper_length_mm}{an integer denoting flipper length (millimeters)}
#' \item{body_mass_g}{an integer denoting body mass (grams)}
#' \item{sex}{a factor denoting penguin sex (female, male)}
#' \item{year}{an integer denoting the study year (2007, 2008, or 2009)}
#' }
#' @source {Adélie penguins: Palmer Station Antarctica LTER and K. Gorman. 2020. Structural size measurements and isotopic signatures of foraging among adult male and female Adélie penguins (Pygoscelis adeliae) nesting along the Palmer Archipelago near Palmer Station, 2007-2009 ver 5. Environmental Data Initiative} \url{https://doi.org/10.6073/pasta/98b16d7d563f265cb52372c8ca99e60f}
#' @source {Gentoo penguins: Palmer Station Antarctica LTER and K. Gorman. 2020. Structural size measurements and isotopic signatures of foraging among adult male and female Gentoo penguin (Pygoscelis papua) nesting along the Palmer Archipelago near Palmer Station, 2007-2009 ver 5. Environmental Data Initiative} \url{https://doi.org/10.6073/pasta/7fca67fb28d56ee2ffa3d9370ebda689}
#' @source {Chinstrap penguins: Palmer Station Antarctica LTER and K. Gorman. 2020. Structural size measurements and isotopic signatures of foraging among adult male and female Chinstrap penguin (Pygoscelis antarcticus) nesting along the Palmer Archipelago near Palmer Station, 2007-2009 ver 6. Environmental Data Initiative} \url{https://doi.org/10.6073/pasta/c14dfcfada8ea13a17536e73eb6fbe9e}
#' @source {Originally published in: Gorman KB, Williams TD, Fraser WR (2014) Ecological Sexual Dimorphism and Environmental Variability within a Community of Antarctic Penguins (Genus Pygoscelis). PLoS ONE 9(3): e90081. doi:10.1371/journal.pone.0090081}
"penguins"
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#'
#' @return A vector of the corresponding type, \code{chr} = character,
#' \code{dbl} = double, \code{lgl} = logical, and \code{int} = logical
#'
#' @noRd
mapply_chr <- function(...) {
out <- mapply(...)
stopifnot(is.character(out))
Expand Down
Loading

0 comments on commit 5048993

Please sign in to comment.