Skip to content

Commit

Permalink
Change remaining FUN args in hypotheses
Browse files Browse the repository at this point in the history
  • Loading branch information
Tristan-Siegfried committed Sep 21, 2023
1 parent c9a97c6 commit 69180dd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
16 changes: 8 additions & 8 deletions book/articles/reference/hypotheses.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ package website, or scroll down this page for a full list of vignettes:
equivalence = NULL,
joint = FALSE,
joint_test = "f",
FUN = NULL,
hypoth_fun = NULL,
numderiv = "fdforward",
...
)
Expand Down Expand Up @@ -190,7 +190,7 @@ package website, or scroll down this page for a full list of vignettes:
<td>
<p>A character string specifying the type of test, either &quot;f&quot; or &quot;chisq&quot;. The null hypothesis is set by the <code>hypothesis</code> argument, with default null equal to 0 for all parameters.</p>
</td></tr>
<tr style="vertical-align: top;"><td style = "white-space: nowrap; font-family: monospace; vertical-align: top"><code>FUN</code></td>
<tr style="vertical-align: top;"><td style = "white-space: nowrap; font-family: monospace; vertical-align: top"><code>hypoth_fun</code></td>
<td>
<p><code>NULL</code> or function.
</p>
Expand Down Expand Up @@ -305,7 +305,7 @@ library(marginaleffects)
library(marginaleffects)
mod <- lm(mpg ~ hp + wt + factor(cyl), data = mtcars)
# When `FUN` and `hypotheses` are `NULL`, `hypotheses()` returns a data.frame of parameters
# When `hypoth_fun` and `hypotheses` are `NULL`, `hypotheses()` returns a data.frame of parameters
hypotheses(mod)
# Test of equality between coefficients
Expand All @@ -318,7 +318,7 @@ hypotheses(mod, hypothesis = "exp(hp + wt) = 0.1")
hypotheses(mod, hypothesis = "hp = wt", vcov = "HC3")
# b1, b2, ... shortcuts can be used to identify the position of the
# parameters of interest in the output of FUN
# parameters of interest in the output of hypoth_fun
hypotheses(mod, hypothesis = "b2 = b3")
# wildcard
Expand All @@ -341,15 +341,15 @@ hypotheses(cmp, hypothesis = "b2 = 0.2")
pre <- predictions(mod, newdata = datagrid(hp = 110, mpg = c(30, 35)))
hypotheses(pre, hypothesis = "b1 = b2")
# The `FUN` argument can be used to compute standard errors for fitted values
# The `hypoth_fun` argument can be used to compute standard errors for fitted values
mod <- glm(am ~ hp + mpg, data = mtcars, family = binomial)
f <- function(x) predict(x, type = "link", newdata = mtcars)
p <- hypotheses(mod, FUN = f)
p <- hypotheses(mod, hypoth_fun = f)
head(p)
f <- function(x) predict(x, type = "response", newdata = mtcars)
p <- hypotheses(mod, FUN = f)
p <- hypotheses(mod, hypoth_fun = f)
head(p)
# Complex aggregation
Expand All @@ -370,7 +370,7 @@ aggregation_fun <- function(model) {
rename(term = cyl)
}
hypotheses(mod, FUN = aggregation_fun)
hypotheses(mod, hypoth_fun = aggregation_fun)
# Equivalence, non-inferiority, and non-superiority tests
mod <- lm(mpg ~ hp + factor(gear), data = mtcars)
Expand Down
2 changes: 1 addition & 1 deletion inst/tinytest/test-equivalence.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ FUN <- function(model, ...) {
e1 <- tost(dat$y[dat$x == 0], dat$y[dat$x == 1], epsilon = .05)
e2 <- hypotheses(
mod,
FUN = FUN,
hypoth_fun = FUN,
equivalence = c(-.05, .05),
df = e1$parameter)
expect_true(e1$tost.p.value > .5 && e1$tost.p.value < .9)
Expand Down
14 changes: 7 additions & 7 deletions inst/tinytest/test-hypotheses.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ requiet("car")



# When `FUN` and `hypotheses` are `NULL`, `hypotheses()` returns a data.frame of parameters
# When `hypoth_fun` and `hypotheses` are `NULL`, `hypotheses()` returns a data.frame of parameters
dat <- mtcars
mod <- lm(mpg ~ hp + wt + factor(cyl), data = dat)
dmm <- hypotheses(mod)
Expand All @@ -25,32 +25,32 @@ expect_inherits(dmm, "data.frame")
dmm <- hypotheses(mod, "hp = wt", vcov = "HC3")
expect_inherits(dmm, "data.frame")

# b1, b2, ... shortcuts can be used to identify rows in the output of FUN
# b1, b2, ... shortcuts can be used to identify rows in the output of hypoth_fun
dmm <- hypotheses(mod, "b2 = b3")
expect_inherits(dmm, "data.frame")

# term names with special characters have to be enclosed in backticks
dmm <- hypotheses(mod, "`factor(cyl)6` = `factor(cyl)8`")
expect_inherits(dmm, "data.frame")

# The `FUN` argument can be used to compute standard errors for fitted values
# The `hypoth_fun` argument can be used to compute standard errors for fitted values
mod <- glm(am ~ hp + mpg, data = mtcars, family = binomial)

f <- function(x) predict(x, type = "link", newdata = mtcars)
p <- hypotheses(mod, FUN = f)
p <- hypotheses(mod, hypoth_fun = f)
expect_inherits(p, "data.frame")
expect_true(all(p$std.error > 0))

f <- function(x) predict(x, type = "response", newdata = mtcars)
p <- hypotheses(mod, FUN = f)
p <- hypotheses(mod, hypoth_fun = f)
expect_inherits(p, "data.frame")
expect_true(all(p$std.error > 0))

# equality between predictions: 1 and 2 equal, 2 and 3 different
f <- function(x) predict(x, type = "link", newdata = mtcars)
dmm <- hypotheses(mod, FUN = f, hypothesis = "b1 = b2")
dmm <- hypotheses(mod, hypoth_fun = f, hypothesis = "b1 = b2")
expect_equivalent(dmm$estimate, 0)
dmm <- hypotheses(mod, FUN = f, hypothesis = "b3 = b2")
dmm <- hypotheses(mod, hypoth_fun = f, hypothesis = "b3 = b2")
expect_equivalent(dmm$estimate, 1.33154848763268)

# named matrix
Expand Down

0 comments on commit 69180dd

Please sign in to comment.