Skip to content

Commit

Permalink
initial changes to allow for raw tex for coefs
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Anderson committed Jul 25, 2020
1 parent 5330c56 commit eb76da5
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 18 deletions.
27 changes: 16 additions & 11 deletions R/create_eq.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ create_eq <- function(lhs,...) {
#'
#' @inheritParams extract_eq

create_eq.default <- function(lhs, rhs, ital_vars, use_coefs, coef_digits, fix_signs, model, intercept) {
create_eq.default <- function(lhs, rhs, ital_vars, use_coefs, coef_digits,
fix_signs, model, intercept, greek, raw_tex) {
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, intercept)
rhs$final_terms <- add_greek(rhs, rhs$final_terms, greek, intercept, raw_tex)
}

# Add error row
Expand All @@ -38,7 +39,7 @@ create_eq.polr <- function(lhs, rhs, ital_vars, use_coefs, coef_digits,
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, greek, intercept, raw_tex)
}

splt <- split(rhs, rhs$coefficient_type)
Expand Down Expand Up @@ -209,18 +210,18 @@ add_greek <- function(rhs, ...) {
#' Adds greek symbols to the equation
#'
#' @keywords internal

add_greek.default <- function(rhs, terms, intercept = "alpha") {
add_greek.default <- function(rhs, terms, greek = "beta", intercept = "alpha",
raw_tex = FALSE) {
int <- switch(intercept,
"alpha" = "\\alpha",
"beta" = "\\beta_{0}")

if (any(grepl("(Intercept)", terms))) {
anno_greek("beta", seq_len(nrow(rhs)), terms)
anno_greek(greek, seq_len(nrow(rhs)), terms)
} else {
ifelse(rhs$term == "(Intercept)",
int,
anno_greek("beta", seq_len(nrow(rhs)) - 1, terms)
anno_greek(greek, seq_len(nrow(rhs)) - 1, terms, raw_tex)
)
}
}
Expand All @@ -239,12 +240,16 @@ add_greek.polr <- function(rhs, terms) {
#'
#' @keywords internal

anno_greek <- function(greek, nums, terms = NULL) {
greek <- paste0("\\", greek, "_{", nums,"}")
anno_greek <- function(greek, nums, terms = NULL, raw_tex = FALSE) {
if(raw_tex) {
out <- paste0(greek, "_{", nums,"}")
} else {
out <- paste0("\\", greek, "_{", nums,"}")
}
if(!is.null(terms)) {
greek <- paste0(greek, "(", terms, ")")
out <- paste0(out, "(", terms, ")")
}
greek
out
}


Expand Down
17 changes: 13 additions & 4 deletions R/extract_eq.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
#'
#' @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.
#' but can also accept \code{"beta"}, in which case the it will be displayed
#' as beta zero.
#' @param greek What notation should be used for
#' coefficients? Currently only accepts \code{"beta"} (with plans for future
#' development). Can be used in combination with \code{raw_tex} to use any
#' notation, e.g., \code{"\\hat{\\beta}"}.
#' @param raw_tex Logical. Is the greek code being passed to denote coefficients
#' raw tex code?
#' @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 @@ -82,7 +88,8 @@
#' mod5 <- glm(out ~ ., data = d, family = binomial(link = "logit"))
#' extract_eq(mod5, wrap = TRUE)

extract_eq <- function(model, intercept = "alpha", ital_vars = FALSE,
extract_eq <- function(model, intercept = "alpha", greek = "beta",
raw_tex = FALSE, ital_vars = FALSE,
wrap = FALSE, terms_per_line = 4,
operator_location = "end", align_env = "aligned",
use_coefs = FALSE, coef_digits = 2, fix_signs = TRUE) {
Expand All @@ -97,7 +104,9 @@ extract_eq <- function(model, intercept = "alpha", ital_vars = FALSE,
coef_digits,
fix_signs,
model,
intercept)
intercept,
greek,
raw_tex)

if (wrap) {
if (operator_location == "start") {
Expand Down
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.

2 changes: 1 addition & 1 deletion man/anno_greek.Rd

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

12 changes: 11 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.

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

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

0 comments on commit eb76da5

Please sign in to comment.