Skip to content

Commit 53e94da

Browse files
authored
Add call as attribute (#572)
* Add call as attribute * fix test * fix * fix test
1 parent 4c9c8a4 commit 53e94da

6 files changed

Lines changed: 71 additions & 23 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: modelbased
33
Title: Estimation of Model-Based Predictions, Contrasts and Means
4-
Version: 0.13.0.8
4+
Version: 0.13.0.9
55
Authors@R:
66
c(person(given = "Dominique",
77
family = "Makowski",

R/estimate_contrasts.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,11 @@ estimate_contrasts.default <- function(
413413
attr(out, "p_adjust") <- p_adjust
414414
attr(out, "backend") <- backend
415415

416+
# we want to store the generic, not the ".default" method, in the call
417+
cl <- match.call()
418+
cl[[1L]] <- quote(estimate_contrasts)
419+
attr(out, "call") <- cl
420+
416421
# add attributes from workhorse function
417422
attributes(out) <- utils::modifyList(attributes(out), info[.info_elements()])
418423

R/estimate_means.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ estimate_means <- function(model,
470470
attr(means, "ci") <- ci
471471
attr(means, "backend") <- backend
472472
attr(means, "coef_name") <- intersect(.valid_coefficient_names(model), colnames(means))
473+
attr(means, "call") <- match.call()
473474

474475
# add attributes from workhorse function
475476
attributes(means) <- utils::modifyList(attributes(means), info[.info_elements()])

R/estimate_slopes.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ estimate_slopes <- function(model,
186186
attr(trends, "model") <- model
187187
attr(trends, "response") <- insight::find_response(model)
188188
attr(trends, "ci") <- ci
189+
attr(trends, "call") <- match.call()
189190

190191
# add attributes from workhorse function
191192
attributes(trends) <- utils::modifyList(attributes(trends), info[.info_elements()])

tests/testthat/test-attributes_estimatefun.R

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,24 @@ test_that("attributes_means", {
66
model <- lm(Sepal.Length ~ Species + Sepal.Width, data = iris)
77

88
estim <- suppressMessages(estimate_means(model, "Species", backend = "emmeans"))
9+
# fmt: skip
910
expect_named(
1011
attributes(estim),
1112
c(
1213
"names", "row.names", "class", "at", "by", "table_title", "table_footer",
13-
"model", "response", "ci", "backend", "coef_name", "focal_terms",
14+
"model", "response", "ci", "backend", "coef_name", "call", "focal_terms",
1415
"predict", "transform", "keep_iterations"
1516
)
1617
)
1718
estim <- suppressMessages(estimate_means(model, "Species", backend = "marginaleffects"))
19+
# fmt: skip
1820
expect_named(
1921
attributes(estim),
2022
c(
2123
"names", "class", "row.names", "at", "by", "focal_terms", "adjusted_for",
2224
"predict", "estimate", "transform", "datagrid", "preserve_range",
2325
"model_info", "keep_iterations", "joint_test", "vcov", "table_title",
24-
"table_footer", "model", "response", "ci", "backend", "coef_name"
26+
"table_footer", "model", "response", "ci", "backend", "coef_name", "call"
2527
)
2628
)
2729
})
@@ -32,42 +34,55 @@ test_that("attributes_means, contrasts", {
3234
model <- lm(Sepal.Length ~ Species + Sepal.Width, data = iris)
3335

3436
estim <- suppressMessages(estimate_contrasts(model, "Species", backend = "emmeans"))
37+
# fmt: skip
3538
expect_named(
3639
attributes(estim),
3740
c(
3841
"names", "class", "row.names", "table_title", "table_footer",
39-
"model", "response", "ci", "p_adjust", "backend", "predict",
42+
"model", "response", "ci", "p_adjust", "backend", "call", "predict",
4043
"comparison", "contrast", "transform", "keep_iterations", "joint_test"
4144
)
4245
)
4346
estim <- suppressMessages(estimate_contrasts(model, "Species", backend = "marginaleffects"))
47+
# fmt: skip
4448
expect_named(
4549
attributes(estim),
4650
c(
4751
"names", "row.names", "class", "table_title", "table_footer",
48-
"model", "response", "ci", "p_adjust", "backend", "focal_terms",
52+
"model", "response", "ci", "p_adjust", "backend", "call", "focal_terms",
4953
"adjusted_for", "predict", "comparison", "contrast", "estimate",
5054
"transform", "datagrid", "preserve_range", "coef_name", "model_info",
5155
"keep_iterations", "joint_test", "vcov"
5256
)
5357
)
54-
estim <- suppressMessages(estimate_contrasts(model, "Species=c('setosa','virginica')", backend = "marginaleffects"))
58+
estim <- suppressMessages(estimate_contrasts(
59+
model,
60+
"Species=c('setosa','virginica')",
61+
backend = "marginaleffects"
62+
))
63+
# fmt: skip
5564
expect_named(
5665
attributes(estim),
5766
c(
5867
"names", "row.names", "class", "table_title", "table_footer",
59-
"model", "response", "ci", "p_adjust", "backend", "focal_terms",
68+
"model", "response", "ci", "p_adjust", "backend", "call", "focal_terms",
6069
"adjusted_for", "predict", "comparison", "contrast", "estimate",
6170
"transform", "datagrid", "preserve_range", "coef_name", "model_info",
6271
"keep_iterations", "joint_test", "vcov"
6372
)
6473
)
65-
estim <- suppressMessages(estimate_contrasts(model, "Species=c('setosa','virginica')", backend = "marginaleffects", estimate = "average"))
74+
estim <- suppressMessages(estimate_contrasts(
75+
model,
76+
"Species=c('setosa','virginica')",
77+
backend = "marginaleffects",
78+
estimate = "average"
79+
))
80+
# fmt: skip
6681
expect_named(
6782
attributes(estim),
6883
c(
6984
"names", "row.names", "class", "table_title", "table_footer",
70-
"model", "response", "ci", "p_adjust", "backend", "focal_terms",
85+
"model", "response", "ci", "p_adjust", "backend", "call", "focal_terms",
7186
"adjusted_for", "predict", "comparison", "contrast", "estimate",
7287
"transform", "datagrid", "preserve_range", "coef_name", "model_info",
7388
"contrast_filter", "keep_iterations", "joint_test","vcov"
@@ -81,21 +96,23 @@ test_that("attributes_means, slopes", {
8196
model <- lm(Sepal.Length ~ Species + Sepal.Width, data = iris)
8297

8398
estim <- suppressMessages(estimate_slopes(model, "Sepal.Width", backend = "emmeans"))
99+
# fmt: skip
84100
expect_named(
85101
attributes(estim),
86102
c(
87103
"names", "row.names", "class", "table_title", "table_footer",
88-
"model", "response", "ci", "trend", "transform", "coef_name",
104+
"model", "response", "ci", "call", "trend", "transform", "coef_name",
89105
"keep_iterations"
90106
)
91107
)
92108
estim <- suppressMessages(estimate_slopes(model, "Sepal.Width", backend = "marginaleffects"))
109+
# fmt: skip
93110
expect_named(
94111
attributes(estim),
95112
c(
96113
"names", "class", "row.names", "trend", "p_adjust", "transform",
97114
"coef_name", "slope", "ci", "model_info", "keep_iterations",
98-
"vcov", "table_title", "table_footer", "model", "response"
115+
"vcov", "table_title", "table_footer", "model", "response", "call"
99116
)
100117
)
101118
})
@@ -109,6 +126,7 @@ test_that("attributes_means", {
109126
by = c("Species", "Petal.Width = [fivenum]"),
110127
preserve_range = FALSE
111128
)
129+
# fmt: skip
112130
expect_named(
113131
attributes(estim),
114132
c(

tests/testthat/test-keep_iterations.R

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@ test_that("estimate_means() - posterior draws", {
1010
m <- insight::download_model("brms_1")
1111
skip_if(is.null(m))
1212
out <- estimate_means(m, by = "wt", keep_iterations = 5)
13+
# fmt: skip
1314
expect_named(
1415
attributes(out),
1516
c(
1617
"names", "class", "row.names", "at", "by", "focal_terms", "adjusted_for",
1718
"predict", "estimate", "transform", "datagrid", "preserve_range",
1819
"model_info", "keep_iterations", "joint_test", "table_title",
19-
"table_footer", "model", "response", "ci", "backend", "coef_name"
20+
"table_footer", "model", "response", "ci", "backend", "coef_name", "call"
2021
)
2122
)
23+
# fmt: skip
2224
expect_named(
2325
out,
2426
c(
@@ -33,15 +35,17 @@ test_that("estimate_means() - posterior draws", {
3335
expect_identical(dim(out), c(10L, 4009L))
3436

3537
out <- estimate_means(m, by = "wt")
38+
# fmt: skip
3639
expect_named(
3740
attributes(out),
3841
c(
3942
"names", "class", "row.names", "at", "by", "focal_terms", "adjusted_for",
4043
"predict", "estimate", "transform", "datagrid", "preserve_range",
4144
"model_info", "keep_iterations", "joint_test", "table_title",
42-
"table_footer", "model", "response", "ci", "backend", "coef_name"
45+
"table_footer", "model", "response", "ci", "backend", "coef_name", "call"
4346
)
4447
)
48+
# fmt: skip
4549
expect_named(
4650
out,
4751
c(
@@ -57,16 +61,18 @@ test_that("estimate_contrasts() - posterior draws", {
5761
m <- insight::download_model("brms_1")
5862
skip_if(is.null(m))
5963
out <- estimate_contrasts(m, "wt=c(3,4,5)", keep_iterations = 5)
64+
# fmt: skip
6065
expect_named(
6166
attributes(out),
6267
c(
6368
"names", "row.names", "class", "table_title", "table_footer",
64-
"model", "response", "ci", "p_adjust", "backend", "focal_terms",
69+
"model", "response", "ci", "p_adjust", "backend", "call", "focal_terms",
6570
"adjusted_for", "predict", "comparison", "contrast", "estimate",
6671
"transform", "datagrid", "preserve_range", "coef_name", "model_info",
6772
"keep_iterations", "joint_test"
6873
)
6974
)
75+
# fmt: skip
7076
expect_named(
7177
out,
7278
c(
@@ -81,16 +87,18 @@ test_that("estimate_contrasts() - posterior draws", {
8187
expect_identical(dim(out), c(3L, 4010L))
8288

8389
out <- estimate_contrasts(m, "wt=c(3,4,5)")
90+
# fmt: skip
8491
expect_named(
8592
attributes(out),
8693
c(
8794
"names", "row.names", "class", "table_title", "table_footer",
88-
"model", "response", "ci", "p_adjust", "backend", "focal_terms",
95+
"model", "response", "ci", "p_adjust", "backend", "call", "focal_terms",
8996
"adjusted_for", "predict", "comparison", "contrast", "estimate",
9097
"transform", "datagrid", "preserve_range", "coef_name", "model_info",
9198
"keep_iterations", "joint_test"
9299
)
93100
)
101+
# fmt: skip
94102
expect_named(
95103
out,
96104
c(
@@ -106,14 +114,16 @@ test_that("estimate_slopes() - posterior draws", {
106114
m <- insight::download_model("brms_1")
107115
skip_if(is.null(m))
108116
out <- estimate_slopes(m, "wt", keep_iterations = 5)
117+
# fmt: skip
109118
expect_named(
110119
attributes(out),
111120
c(
112121
"names", "class", "row.names", "trend", "p_adjust", "transform",
113122
"coef_name", "slope", "ci", "model_info", "keep_iterations",
114-
"table_title", "table_footer", "model", "response"
123+
"table_title", "table_footer", "model", "response", "call"
115124
)
116125
)
126+
# fmt: skip
117127
expect_named(
118128
out,
119129
c(
@@ -125,14 +135,16 @@ test_that("estimate_slopes() - posterior draws", {
125135
expect_identical(dim(out), c(1L, 13L))
126136

127137
out <- estimate_slopes(m, "wt")
138+
# fmt: skip
128139
expect_named(
129140
attributes(out),
130141
c(
131142
"names", "class", "row.names", "trend", "p_adjust", "transform",
132143
"coef_name", "slope", "ci", "model_info", "keep_iterations",
133-
"table_title", "table_footer", "model", "response"
144+
"table_title", "table_footer", "model", "response", "call"
134145
)
135146
)
147+
# fmt: skip
136148
expect_named(
137149
out,
138150
c(
@@ -148,14 +160,16 @@ test_that("estimate_means() - posterior draws, emmeans", {
148160
m <- insight::download_model("brms_1")
149161
skip_if(is.null(m))
150162
out <- estimate_means(m, by = "wt", keep_iterations = 5, backend = "emmeans")
163+
# fmt: skip
151164
expect_named(
152165
attributes(out),
153166
c(
154167
"names", "class", "row.names", "table_title", "table_footer",
155-
"model", "response", "ci", "backend", "coef_name", "at", "by",
168+
"model", "response", "ci", "backend", "coef_name", "call", "at", "by",
156169
"focal_terms", "predict", "transform", "keep_iterations"
157170
)
158171
)
172+
# fmt: skip
159173
expect_named(
160174
out,
161175
c(
@@ -166,11 +180,12 @@ test_that("estimate_means() - posterior draws, emmeans", {
166180
expect_identical(dim(out), c(10L, 10L))
167181

168182
out <- estimate_means(m, by = "wt", keep_iterations = TRUE, backend = "emmeans")
183+
# fmt: skip
169184
expect_named(
170185
attributes(out),
171186
c(
172187
"names", "class", "row.names", "table_title", "table_footer",
173-
"model", "response", "ci", "backend", "coef_name", "at", "by",
188+
"model", "response", "ci", "backend", "coef_name", "call", "at", "by",
174189
"focal_terms", "predict", "transform", "keep_iterations"
175190
)
176191
)
@@ -182,15 +197,17 @@ test_that("estimate_contrasts() - posterior draws, emmeans", {
182197
m <- insight::download_model("brms_1")
183198
skip_if(is.null(m))
184199
out <- estimate_contrasts(m, by = "wt=c(3,4,5)", keep_iterations = 5, backend = "emmeans")
200+
# fmt: skip
185201
expect_named(
186202
attributes(out),
187203
c(
188204
"names", "class", "row.names", "table_title", "table_footer",
189-
"model", "response", "ci", "p_adjust", "backend", "at", "by",
205+
"model", "response", "ci", "p_adjust", "backend", "call", "at", "by",
190206
"predict", "comparison", "contrast", "transform", "keep_iterations",
191207
"joint_test"
192208
)
193209
)
210+
# fmt: skip
194211
expect_named(
195212
out,
196213
c(
@@ -201,11 +218,12 @@ test_that("estimate_contrasts() - posterior draws, emmeans", {
201218
expect_identical(dim(out), c(3L, 12L))
202219

203220
out <- estimate_contrasts(m, by = "wt=c(3,4,5)", keep_iterations = TRUE, backend = "emmeans")
221+
# fmt: skip
204222
expect_named(
205223
attributes(out),
206224
c(
207225
"names", "class", "row.names", "table_title", "table_footer",
208-
"model", "response", "ci", "p_adjust", "backend", "at", "by",
226+
"model", "response", "ci", "p_adjust", "backend", "call", "at", "by",
209227
"predict", "comparison", "contrast", "transform", "keep_iterations",
210228
"joint_test"
211229
)
@@ -218,14 +236,16 @@ test_that("estimate_slopes() - posterior draws, emmeans", {
218236
m <- insight::download_model("brms_1")
219237
skip_if(is.null(m))
220238
out <- estimate_slopes(m, "wt", keep_iterations = 5, backend = "emmeans")
239+
# fmt: skip
221240
expect_named(
222241
attributes(out),
223242
c(
224243
"names", "class", "row.names", "table_title", "table_footer",
225-
"model", "response", "ci", "trend", "transform", "coef_name",
244+
"model", "response", "ci", "call", "trend", "transform", "coef_name",
226245
"keep_iterations"
227246
)
228247
)
248+
# fmt: skip
229249
expect_named(
230250
out,
231251
c(
@@ -236,11 +256,12 @@ test_that("estimate_slopes() - posterior draws, emmeans", {
236256
expect_identical(dim(out), c(1L, 10L))
237257

238258
out <- estimate_slopes(m, "wt", keep_iterations = TRUE, backend = "emmeans")
259+
# fmt: skip
239260
expect_named(
240261
attributes(out),
241262
c(
242263
"names", "class", "row.names", "table_title", "table_footer",
243-
"model", "response", "ci", "trend", "transform", "coef_name",
264+
"model", "response", "ci", "call", "trend", "transform", "coef_name",
244265
"keep_iterations"
245266
)
246267
)
@@ -252,6 +273,7 @@ test_that("estimate_slopes() - posterior draws, get_predicted", {
252273
m <- insight::download_model("brms_1")
253274
skip_if(is.null(m))
254275
out <- estimate_relation(m, by = "wt", keep_iterations = 5)
276+
# fmt: skip
255277
expect_named(
256278
attributes(out),
257279
c(
@@ -261,6 +283,7 @@ test_that("estimate_slopes() - posterior draws, get_predicted", {
261283
"adjusted_for", "at_specs", "at", "by", "reference", "data"
262284
)
263285
)
286+
# fmt: skip
264287
expect_named(
265288
out,
266289
c(

0 commit comments

Comments
 (0)