Skip to content

Commit

Permalink
Tidying and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-long committed Jan 11, 2024
1 parent d577253 commit 465a4e7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion R/int_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ center_ss_non_survey <- function(d, weights, facvars = NULL, fvars, pred,
# 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) {
if (is.factor(d[[v]]) && length(unique(d[[v]])) == 2) {

facvars <- c(facvars, v)

Expand Down
6 changes: 3 additions & 3 deletions R/johnson_neyman.R
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ johnson_neyman <- function(model, pred, modx, vmat = NULL, alpha = 0.05,
# Construct interaction term
## Create helper function to use either fixef() or coef() depending on input
get_coef <- function(mod) {
if (inherits(mod, "merMod") | inherits(mod, "brmsfit")) {
if (inherits(mod, "merMod") || inherits(mod, "brmsfit")) {
coef <- lme4::fixef(model)
if (inherits(mod, "brmsfit")) {
coefs <- coef[,1, drop = TRUE]
Expand Down Expand Up @@ -307,7 +307,7 @@ johnson_neyman <- function(model, pred, modx, vmat = NULL, alpha = 0.05,
test <- 0
i <- 1 + length(marginal_effects)

while (test == 0 & i > 1) {
while (test == 0 && i > 1) {

i <- i - 1
test <- min(ps[ps_o][1:i] <= multipliers[i] * (alpha * 2))
Expand Down Expand Up @@ -557,7 +557,7 @@ johnson_neyman <- function(model, pred, modx, vmat = NULL, alpha = 0.05,
ggplot2::geom_segment(ggplot2::aes(x = modrangeo[1], xend = modrangeo[2],
y = 0, yend = 0,
linetype = "Range of\nobserved\ndata"),
lineend = "square", size = 1.25)
lineend = "square", linewidth = 1.25)
}

# Adding this scale allows me to have consistent ordering
Expand Down
16 changes: 7 additions & 9 deletions R/simple_slopes.R
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ sim_slopes <- function(model, pred, modx, mod2 = NULL, modx.values = NULL,
of model. If you are using a package like glmmTMB or other mixed modeling
packages, install and load the broom.mixed package and try again. Make sure
you have the broom package installed and loaded otherwise."))

pred_names <- pred_names %just% tidied$term
if (length(pred_names) == 0) {
stop_wrap("Could not find the focal predictor in the model. If it was
Expand Down Expand Up @@ -283,14 +284,14 @@ sim_slopes <- function(model, pred, modx, mod2 = NULL, modx.values = NULL,
modx.labels = modx.labels,
any.mod2 = !is.null(mod2), sims = TRUE)

if ((pred_factor | !is.numeric(d[[modx]])) & johnson_neyman == TRUE) {
if ((pred_factor || !is.numeric(d[[modx]])) && johnson_neyman == TRUE) {
warn_wrap("Johnson-Neyman intervals are not available for factor
predictors or moderators.", call. = FALSE)
johnson_neyman <- FALSE
}

# Now specify def or not (for labeling w/ print method)
if (is.character(modx.values) | is.null(modx.values) | !is.null(modx.labels)) {
if (is.character(modx.values) || is.null(modx.values) || !is.null(modx.labels)) {

ss <- structure(ss, def = TRUE)

Expand Down Expand Up @@ -326,7 +327,7 @@ sim_slopes <- function(model, pred, modx, mod2 = NULL, modx.values = NULL,
}

# Now specify def or not
if (is.character(mod2.values) | is.null(mod2.values) | !is.null(mod2.labels)) {
if (is.character(mod2.values) || is.null(mod2.values) || !is.null(mod2.labels)) {

ss <- structure(ss, def2 = TRUE)

Expand Down Expand Up @@ -411,7 +412,7 @@ sim_slopes <- function(model, pred, modx, mod2 = NULL, modx.values = NULL,

}

# Looping through (perhaps non-existent)
# Looping through (perhaps non-existent) second moderator values
for (j in seq_len(mod2val_len)) {

# We don't want to do the J-N interval with the 1st moderator adjusted,
Expand Down Expand Up @@ -456,7 +457,7 @@ sim_slopes <- function(model, pred, modx, mod2 = NULL, modx.values = NULL,
}

# Getting SEs, robust or otherwise
if (robust != FALSE & is.null(v.cov)) {
if (robust != FALSE && is.null(v.cov)) {
# For J-N
covmat <- get_robust_se(newmod, robust, cluster, dt)$vcov
} else if (is.null(v.cov)) {
Expand Down Expand Up @@ -545,7 +546,7 @@ sim_slopes <- function(model, pred, modx, mod2 = NULL, modx.values = NULL,
} else {
covmat <- NULL
}
# Use j_summ to get the coefficients
# Use summ to get the coefficients
if (has_summ) {
sum <- summ(newmod, robust = robust, model.fit = FALSE,
confint = TRUE, ci.width = ci.width, vifs = FALSE,
Expand All @@ -554,8 +555,6 @@ sim_slopes <- function(model, pred, modx, mod2 = NULL, modx.values = NULL,
} else {
sum <- generics::tidy(newmod, conf.int = TRUE, conf.level = ci.width)
}


} else {
if (is.null(v.cov)) {
# For J-N
Expand Down Expand Up @@ -587,7 +586,6 @@ sim_slopes <- function(model, pred, modx, mod2 = NULL, modx.values = NULL,
intp <- summat[summat$term == "(Intercept)", ]
}


# Have to account for variable amount of rows needed due to factor
# predictors
rows <- split(1:nrow(retmat),
Expand Down

0 comments on commit 465a4e7

Please sign in to comment.