Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Oct 6, 2024
1 parent 2d22158 commit 3b3a157
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 20 deletions.
2 changes: 1 addition & 1 deletion R/1_model_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ model_parameters.default <- function(model,
standardize = standardize,
exponentiate = exponentiate,
p_adjust = p_adjust,
summary = summary,
include_info = include_info,
keep_parameters = keep,
drop_parameters = drop,
vcov = vcov,
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/_snaps/model_parameters.fixest.new.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# model_parameters.fixest

Code
model_parameters(m1, include_info = TRUE, verbose = FALSE)
Output
# Fixed Effects
Parameter | Coefficient | SE | 95% CI | t(187) | p
-----------------------------------------------------------------
time | 1.09 | 0.67 | [-0.23, 2.41] | 1.63 | 0.106
phq4 | -3.66 | 0.67 | [-4.98, -2.34] | -5.45 | < .001
Model: QoL ~ time + phq4 (564 Observations)
Sigma: 12.365 (df = 561)
r2: 0.743; ar2: 0.613; wr2: 0.180; war2: 0.175
RMSE : 10.069

39 changes: 39 additions & 0 deletions tests/testthat/_snaps/model_parameters.glm.new.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# print digits model_parameters.lm

Code
params
Output
Parameter | Coefficient | SE | 95% CI | t(30) | p
------------------------------------------------------------------
(Intercept) | 37.29 | 1.88 | [33.45, 41.12] | 19.86 | < .001
wt | -5.34 | 0.56 | [-6.49, -4.20] | -9.56 | < .001
Model: mpg ~ wt (32 Observations)
Sigma: 3.046 (df = 30)
R2: 0.753; adjusted R2: 0.745
RMSE : 2.949

---

Code
params
Output
Parameter | Coefficient | SE | 95% CI | t(30) | p
------------------------------------------------------------------
(Intercept) | 37.29 | 1.88 | [33.45, 41.12] | 19.86 | < .001
wt | -5.34 | 0.56 | [-6.49, -4.20] | -9.56 | < .001

# model_parameters.glm - Gamma - print

Code
mp
Output
Parameter | Prevalence Ratio | SE | 95% CI | t(7) | p
---------------------------------------------------------------------------
(Intercept) | 245.48 | 46.72 | [173.66, 351.67] | 28.92 | < .001
u [log] | 0.55 | 0.03 | [ 0.49, 0.61] | -10.88 | < .001
Message
Uncertainty intervals (profile-likelihood) and p-values (two-tailed)
computed using a Wald t-distribution approximation.

72 changes: 72 additions & 0 deletions tests/testthat/_snaps/model_parameters.mixed.new.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# print-model_parameters

Code
model_parameters(model, effects = "fixed")
Output
Parameter | Coefficient | SE | 95% CI | t(558) | p
-------------------------------------------------------------------
(Intercept) | 71.53 | 1.56 | [68.48, 74.59] | 45.98 | < .001
time | 1.09 | 0.64 | [-0.17, 2.34] | 1.70 | 0.089
# Within-Effects
Parameter | Coefficient | SE | 95% CI | t(558) | p
-------------------------------------------------------------------
phq4 within | -3.66 | 0.41 | [-4.46, -2.86] | -8.95 | < .001
# Between-Effects
Parameter | Coefficient | SE | 95% CI | t(558) | p
--------------------------------------------------------------------
phq4 between | -6.28 | 0.50 | [-7.27, -5.30] | -12.53 | < .001
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a Wald t-distribution approximation.

---

Code
model_parameters(m1, effects = "all")
Output
# Fixed Effects
Parameter | Coefficient | SE | 95% CI | t(28) | p
-----------------------------------------------------------------
(Intercept) | 0.65 | 0.50 | [-0.38, 1.68] | 1.29 | 0.206
cyl | 0.40 | 0.08 | [ 0.25, 0.56] | 5.29 | < .001
# Random Effects
Parameter | Coefficient | SE | 95% CI
--------------------------------------------------------
SD (Intercept: gear) | 0.27 | 0.24 | [0.05, 1.54]
SD (Residual) | 0.59 | 0.08 | [0.46, 0.77]
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a Wald t-distribution approximation. Uncertainty intervals for
random effect variances computed using a Wald z-distribution
approximation.

---

Code
model_parameters(m1, effects = "fixed", include_info = TRUE)
Output
# Fixed Effects
Parameter | Coefficient | SE | 95% CI | t(28) | p
-----------------------------------------------------------------
(Intercept) | 0.65 | 0.50 | [-0.38, 1.68] | 1.29 | 0.206
cyl | 0.40 | 0.08 | [ 0.25, 0.56] | 5.29 | < .001
Model: wt ~ cyl (32 Observations)
Sigma: 0.594 (df = 28)
Conditional R2: 0.628; Marginal R2: 0.550
RMSE : 0.564
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a Wald t-distribution approximation.

3 changes: 2 additions & 1 deletion tests/testthat/_snaps/printing.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@
Species [virginica] * Petal Length | 0.45 | 0.29 | [-0.12, 1.03] | 1.56 | 0.120
Model: Sepal.Length ~ Species * Petal.Length (150 Observations)
Residual standard deviation: 0.336 (df = 144)
Sigma: 0.336 (df = 144)
R2: 0.840; adjusted R2: 0.835
RMSE : 0.330
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-model_parameters.fixest.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test_that("model_parameters.fixest", {
# currently, a bug for fixest 10.4 on R >= 4.3
# skip_if_not(getRversion() < "4.2.0")
expect_snapshot(
model_parameters(m1, summary = TRUE, verbose = FALSE)
model_parameters(m1, include_info = TRUE, verbose = FALSE)
)

# Poission, df = Inf
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-model_parameters.glm.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ test_that("print digits model_parameters.lm", {
skip_if_not_installed("performance")
model <- lm(mpg ~ wt, data = mtcars)

params <- model_parameters(model, summary = TRUE, verbose = FALSE)
params <- model_parameters(model, include_info = TRUE, verbose = FALSE)
expect_snapshot(params)

params <- model_parameters(model, summary = FALSE, verbose = FALSE)
params <- model_parameters(model, include_info = FALSE, verbose = FALSE)
expect_snapshot(params)
})

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-model_parameters.mixed.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,5 @@ test_that("print-model_parameters", {
skip_if_not_installed("merDeriv")
expect_snapshot(model_parameters(m1, effects = "all"))

expect_snapshot(model_parameters(m1, effects = "fixed", summary = TRUE))
expect_snapshot(model_parameters(m1, effects = "fixed", include_info = TRUE))
})
4 changes: 2 additions & 2 deletions tests/testthat/test-p_adjust.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test_that("model_parameters, p-adjust after keep/drop", {
)

expect_message(
mp <- model_parameters(model, summary = TRUE, keep = c("wt", "hp"), p_adjust = "bonferroni"),
mp <- model_parameters(model, include_info = TRUE, keep = c("wt", "hp"), p_adjust = "bonferroni"),
"more than 1 element"
)
expect_equal(
Expand All @@ -34,7 +34,7 @@ test_that("model_parameters, p-adjust after keep/drop", {
)

expect_message(
mp <- model_parameters(model, summary = TRUE, keep = c("cyl", "gear"), p_adjust = "bonferroni"),
mp <- model_parameters(model, include_info = TRUE, keep = c("cyl", "gear"), p_adjust = "bonferroni"),
"more than 1 element"
)
expect_equal(
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-printing.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ withr::with_options(
# snapshot breaks between R CMD check "classic" and "strict"
skip_if_not_installed("performance")
model <- lm(Sepal.Length ~ Species * Petal.Length, data = iris)
out <- model_parameters(model, summary = TRUE)
out <- model_parameters(model, include_info = TRUE)
expect_snapshot(print(out))
})

Expand Down
10 changes: 5 additions & 5 deletions vignettes/model_parameters.Rmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "Summary of Model Parameters"
output:
output:
rmarkdown::html_vignette:
toc: true
fig_width: 10.08
Expand All @@ -10,7 +10,7 @@ vignette: >
%\VignetteIndexEntry{Summary of Model Parameters}
\usepackage[utf8]{inputenc}
%\VignetteEngine{knitr::rmarkdown}
editor_options:
editor_options:
chunk_output_type: console
bibliography: bibliography.bib
---
Expand Down Expand Up @@ -150,9 +150,9 @@ glm(vs ~ poly(mpg, 2) + cyl, data = mtcars, family = binomial()) |>
```

```{r}
# show Odds Ratios and include model summary
# show Odds Ratios and include model info
glm(vs ~ poly(mpg, 2) + cyl, data = mtcars, family = binomial()) |>
parameters(exponentiate = TRUE, summary = TRUE)
parameters(exponentiate = TRUE, include_info = TRUE)
```

### Mixed Models
Expand Down Expand Up @@ -269,7 +269,7 @@ parameters(

The **parameters** package extends the support to structural models.

### Principal Component Analysis (PCA) and Exploratory Factor Analysis (EFA)
### Principal Component Analysis (PCA) and Exploratory Factor Analysis (EFA)

```{r eval=all(successfully_loaded[c("psych", "nFactors")])}
psych::pca(mtcars, nfactors = 3) |>
Expand Down
12 changes: 6 additions & 6 deletions vignettes/model_parameters_print.Rmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "Printing Model Parameters"
output:
output:
rmarkdown::html_vignette:
toc: true
fig_width: 10.08
Expand All @@ -10,7 +10,7 @@ vignette: >
%\VignetteIndexEntry{Printing Model Parameters}
\usepackage[utf8]{inputenc}
%\VignetteEngine{knitr::rmarkdown}
editor_options:
editor_options:
chunk_output_type: console
bibliography: bibliography.bib
---
Expand Down Expand Up @@ -122,13 +122,13 @@ mp <- model_parameters(model)
print(mp, split_component = FALSE)
```

## Adding model summaries
## Adding model information

A model summary can be added to the table when `summary = TRUE` in the call to `model_parameters()`:
A model summary can be added to the table when `include_info = TRUE` in the call to `model_parameters()`:

```{r}
model <- lm(Sepal.Length ~ Species * Petal.Length, data = iris)
model_parameters(model, summary = TRUE)
model_parameters(model, include_info = TRUE)
```

## Including the reference level of categorical variables
Expand Down Expand Up @@ -380,6 +380,6 @@ print(tab, table_width = 80)

# More advances tables and markdown / HTML formatting

The `print_md()` as well as `print_html()` functions can be used to create markdown (for knitting to PDF or Word) and HTML tables.
The `print_md()` as well as `print_html()` functions can be used to create markdown (for knitting to PDF or Word) and HTML tables.

Meanwhile, there are a lot of additional packages that allow users to have even more flexibility regarding table layouts. One package we can recommend is the [*modelsummary* package](https://vincentarelbundock.github.io/modelsummary/).

0 comments on commit 3b3a157

Please sign in to comment.