Skip to content

Commit

Permalink
Add support for "at" argument in sim_slopes
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-long committed Jul 28, 2024
1 parent 465a4e7 commit 2f28eaf
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 22 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ Enhances:
rstanarm
VignetteBuilder: knitr
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2.9000
50 changes: 33 additions & 17 deletions R/int_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -354,18 +354,19 @@ auto_mod_vals <-
## Centering

center_ss <- function(d, weights, facvars = NULL, fvars, pred, resp, modx,
survey, design = NULL, mod2, wname, offname, centered) {
survey, design = NULL, mod2, wname, offname, centered,
at = NULL) {

# Just need to pick a helper function based on survey vs no survey
if (survey == TRUE) {

out <- center_ss_survey(d, weights, facvars, fvars, pred, resp, modx,
survey, design, mod2, wname, offname, centered)
survey, design, mod2, wname, offname, centered, at)

} else {

out <- center_ss_non_survey(d, weights, facvars, fvars, pred, resp, modx,
mod2, wname, offname, centered)
mod2, wname, offname, centered, at)

}

Expand All @@ -377,7 +378,7 @@ center_ss <- function(d, weights, facvars = NULL, fvars, pred, resp, modx,
## If not svydesign, centering is fairly straightforward

center_ss_non_survey <- function(d, weights, facvars = NULL, fvars, pred,
resp, modx, mod2, wname, offname, centered) {
resp, modx, mod2, wname, offname, centered, at) {

omitvars <- c(pred, resp, modx, mod2, wname, offname)

Expand All @@ -389,8 +390,8 @@ center_ss_non_survey <- function(d, weights, facvars = NULL, fvars, pred,
if (centered[1] != "all" && centered[1] != "none") {

if (any(omitvars %in% centered)) {
warning("Moderators, outcome variables, and weights/offsets",
" cannot be centered.")
warn_wrap("Moderators, outcome variables, and weights/offsets
cannot be centered.")
centered <- centered[centered %nin% omitvars]
}
if (length(centered) > 0) {
Expand Down Expand Up @@ -431,8 +432,9 @@ center_ss_non_survey <- function(d, weights, facvars = NULL, fvars, pred,

}

# Fixes a data type error with predict() later
d <- as.data.frame(d)
if (!is.null(at)) {
d <- set_at(at = at, d = d)
}

out <- list(d = d, facvars = facvars, fvars = fvars, design = NULL)

Expand All @@ -444,9 +446,9 @@ center_ss_non_survey <- function(d, weights, facvars = NULL, fvars, pred,

center_ss_survey <- function(d, weights, facvars = NULL, fvars, pred, resp,
modx, survey, design, mod2, wname, offname,
centered) {
centered, at) {

omitvars <- c(pred, resp, modx, mod2, wname, offname)
omitvars <- c(pred, resp, modx, mod2, wname, offname, names(at))

# Dealing with two-level factors that aren't part of an interaction
# /focal pred
Expand All @@ -456,8 +458,8 @@ center_ss_survey <- function(d, weights, facvars = NULL, fvars, pred, resp,
if (centered[1] != "all" && centered[1] != "none") {

if (any(omitvars %in% centered)) {
warning("Moderators, outcome variables, and weights/offsets",
" cannot be centered.")
warn_wrap("Moderators, outcome variables, and weights/offsets cannot be
centered.")
centered <- centered[centered %nin% omitvars]
}
design <- gscale(vars = centered, data = design, center.only = TRUE)
Expand All @@ -475,33 +477,48 @@ center_ss_survey <- function(d, weights, facvars = NULL, fvars, pred, resp,
}

} else if (centered == "none") {

# Dealing with two-level factors that aren't part
# of an interaction/focal pred
for (v in fv2) {
if (is.factor(d[[v]]) && length(unique(d[[v]])) == 2) {

facvars <- c(facvars, v)

}
}

} else if (centered == "all") {

# Center all non-focal
ndfvars <- fvars[fvars %nin% omitvars]

if (length(ndfvars) > 0) {
design <- gscale(vars = ndfvars, data = design, center.only = TRUE)
d <- design$variables
}
}

if (!is.null(at)) {
d <- set_at(at = at, d = d)
}

out <- list(d = d, design = design, facvars = facvars, fvars = fvars)

return(out)
}

#### Deal with at variables #################################################
set_at <- function(at, d) {
for (v in names(at)) {
if (v %nin% names(d)) stop_wrap("`at` variable ", v, " not found in data.")
if (!is.numeric(d[[v]])) {
warn_wrap("Inclusion of non-numeric variable ", v, " in `at` argument
is not currently supported. As an alternative, treat the
variable as a factor and use the relevel() function to
set this value as its reference level before fitting your
model.")
} else {
d[[v]] <- d[[v]] - at[[v]]
}
}
return(d)
}

#### Send deprecation warnings ##############################################
Expand Down Expand Up @@ -923,7 +940,6 @@ drop_factor_levels <- function(d, var, values, labels) {

}


# get_contrasts <- function(model) {
# form <- as.formula(formula(model))
# as.data.frame(t(attr(terms(form), "factors")))
Expand Down
16 changes: 12 additions & 4 deletions R/simple_slopes.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
#' also use "none" to base all predictions on variables set at 0.
#' The response variable, `modx`, and `mod2` variables are never
#' centered.
#'
#' @param at If you want to manually set the values of other variables in the
#' model, do so by providing a named list where the names are the variables and
#' the list values are vectors of the values. Note that you cannot alter the
#' values of the `pred`, `modx`, or `mod2` variables and this will take
#' precedence over the `centered` argument (but any variables unmentioned by
#' `at` will be centered as specified by `centered`). For linear models,
#' this will only change the output of the conditional intercepts.
#'
#' @param cond.int Should conditional intercepts be printed in addition to the
#' slopes? Default is \code{FALSE}.
Expand Down Expand Up @@ -125,9 +133,9 @@
#'

sim_slopes <- function(model, pred, modx, mod2 = NULL, modx.values = NULL,
mod2.values = NULL, centered = "all", data = NULL,
cond.int = FALSE, johnson_neyman = TRUE, jnplot = FALSE,
jnalpha = .05, robust = FALSE,
mod2.values = NULL, centered = "all", at = NULL,
data = NULL, cond.int = FALSE, johnson_neyman = TRUE,
jnplot = FALSE, jnalpha = .05, robust = FALSE,
digits = getOption("jtools-digits", default = 2),
pvals = TRUE, confint = FALSE, ci.width = .95,
cluster = NULL, modx.labels = NULL, mod2.labels = NULL,
Expand Down Expand Up @@ -271,7 +279,7 @@ sim_slopes <- function(model, pred, modx, mod2 = NULL, modx.values = NULL,
fvars = fvars, pred = pred,
resp = resp, modx = modx, survey = is_survey,
design = design, mod2 = mod2, wname = wname,
offname = offname, centered = centered)
offname = offname, centered = centered, at = at)

design <- c_out$design
d <- c_out$d
Expand Down
9 changes: 9 additions & 0 deletions man/sim_slopes.Rd

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

0 comments on commit 2f28eaf

Please sign in to comment.