Skip to content

Commit

Permalink
added flexibility for intercept notation
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Anderson committed Jul 24, 2020
1 parent bedbf0d commit 20caf32
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 56 deletions.
15 changes: 7 additions & 8 deletions R/create_eq.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ create_eq <- function(lhs,...) {
#'
#' @inheritParams extract_eq

create_eq.default <- function(lhs, rhs, ital_vars, use_coefs, coef_digits, fix_signs, model) {
create_eq.default <- function(lhs, rhs, ital_vars, use_coefs, coef_digits, fix_signs, model, intercept) {
rhs$final_terms <- create_term(rhs, ital_vars)

if (use_coefs) {
rhs$final_terms <- add_coefs(rhs, rhs$final_terms, coef_digits)
} else {
rhs$final_terms <- add_greek(rhs, rhs$final_terms)
rhs$final_terms <- add_greek(rhs, rhs$final_terms, intercept)
}

# Add error row
Expand All @@ -45,7 +45,7 @@ create_eq.polr <- function(lhs, rhs, ital_vars, use_coefs, coef_digits,
rhs_final <- lapply(splt$zeta$final_terms, function(x) {
c(x, splt$coefficient$final_terms, "\\epsilon")
})
attributes(lhs) <- NULL
attributes(lhs) <- NULL
list(lhs = lhs, rhs = rhs_final)
}

Expand Down Expand Up @@ -210,22 +210,22 @@ add_greek <- function(rhs, ...) {
#'
#' @keywords internal

add_greek.default <- function(rhs, terms) {
add_greek.default <- function(rhs, terms, intercept = "alpha") {
if (any(grepl("(Intercept)", terms))) {
anno_greek("beta", seq_len(nrow(rhs)), terms)
} else {
ifelse(rhs$term == "(Intercept)",
"\\alpha",
ifelse(intercept == "alpha", "\\alpha", "\\beta_{0}"),
anno_greek("beta", seq_len(nrow(rhs)) - 1, terms)
)
}
}

add_greek.polr <- function(rhs, terms) {
ifelse(rhs$coefficient_type == "zeta",
anno_greek("alpha",
anno_greek("alpha",
rev(seq_along(grep("zeta", rhs$coefficient_type)))),
anno_greek("beta",
anno_greek("beta",
seq_along(grep("coefficient", rhs$coefficient_type)),
terms)
)
Expand All @@ -252,7 +252,6 @@ anno_greek <- function(greek, nums, terms = NULL) {
#'
#' @param eq String containing a LaTeX equation
#'
#' @inheritParams extract_eq
#'
fix_coef_signs <- function(eq) {
# Side-by-side + -
Expand Down
24 changes: 14 additions & 10 deletions R/extract_eq.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#' [broom::tidy][broom::tidy].
#'
#' @param model A fitted model
#' @param intercept How should the intercept be displayed? Default is \code{"alpha"},
#' but can also accept \code{"beta"}, in which case the it will be displayed
#' as beta zero.
#' @param ital_vars Logical, defaults to \code{FALSE}. Should the variable names
#' not be wrapped in the \code{\\text{}} command?
#' @param wrap Logical, defaults to \code{FALSE}. Should the terms on the
Expand Down Expand Up @@ -79,9 +82,9 @@
#' mod5 <- glm(out ~ ., data = d, family = binomial(link = "logit"))
#' extract_eq(mod5, wrap = TRUE)

extract_eq <- function(model, ital_vars = FALSE,
wrap = FALSE, terms_per_line = 4, operator_location = "end",
align_env = "aligned",
extract_eq <- function(model, intercept = "alpha", ital_vars = FALSE,
wrap = FALSE, terms_per_line = 4,
operator_location = "end", align_env = "aligned",
use_coefs = FALSE, coef_digits = 2, fix_signs = TRUE) {

lhs <- extract_lhs(model, ital_vars)
Expand All @@ -93,8 +96,9 @@ extract_eq <- function(model, ital_vars = FALSE,
use_coefs,
coef_digits,
fix_signs,
model)

model,
intercept)

if (wrap) {
if (operator_location == "start") {
line_end <- "\\\\\n&\\quad + "
Expand Down Expand Up @@ -123,11 +127,11 @@ extract_eq <- function(model, ital_vars = FALSE,
.rhs,
sep = " &= "),
"\n\\end{", align_env, "}")
},
.lhs = eq_raw$lhs,
},
.lhs = eq_raw$lhs,
.rhs = rhs_combined)


} else {
eq <- Map(function(.lhs, .rhs) {
paste(.lhs,
Expand All @@ -141,7 +145,7 @@ extract_eq <- function(model, ital_vars = FALSE,
if (use_coefs && fix_signs) {
eq <- lapply(eq, fix_coef_signs)
}

if(length(eq) > 1) {
eq <- paste(eq, collapse = "\\\\")
} else {
Expand Down
2 changes: 1 addition & 1 deletion R/extract_rhs.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
#' #> ..$ : Named chr "virginica:Petal.Length" "Speciesvirginica:"
#' #> .. ..- attr(*, "names")= chr "Species" "Petal.Length"
#' }
#'
#'

extract_rhs <- function(model) {
# Extract RHS from formula
Expand Down
22 changes: 7 additions & 15 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,42 +1,34 @@
#' Strict versions of \code{base::\link[base]{mapply}}
#'
#'
#' Return a vector of the corresponding type
#'
#'
#' @keywords internal
#'
#'
#' @param \dots arguments passed to \code{base::\link[base]{mapply}}
#'
#' @return A vector of the corresponding type, \code{chr} = character,
#'
#' @return A vector of the corresponding type, \code{chr} = character,
#' \code{dbl} = double, \code{lgl} = logical, and \code{int} = logical
#'

#' @describeIn mapply_chr Return a character vector
#'
mapply_chr <- function(...) {
out <- mapply(...)
stopifnot(is.character(out))
out
}

#' @inheritParams mapply_chr
#' @describeIn mapply_chr Return a logical vector
mapply_lgl <- function(...) {
out <- mapply(...)
stopifnot(is.logical(out))
out
}

#' @inheritParams mapply_chr
#' @describeIn mapply_chr Return an integer vector
mapply_int <- function(...) {
out <- mapply(...)
stopifnot(is.integer(out))
out
}

#' @inheritParams mapply_chr
#' @describeIn mapply_chr Return a double vector
mapply_dbl <- function(...) {
out <- mapply(...)
stopifnot(is.double(out))
out
}
}
2 changes: 1 addition & 1 deletion man/add_greek.default.Rd

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

15 changes: 14 additions & 1 deletion man/create_eq.default.Rd

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

5 changes: 5 additions & 0 deletions man/extract_eq.Rd

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

20 changes: 0 additions & 20 deletions man/mapply_chr.Rd

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

0 comments on commit 20caf32

Please sign in to comment.