Skip to content

Commit 6ad5da7

Browse files
tests
1 parent 96b5085 commit 6ad5da7

File tree

5 files changed

+42
-36
lines changed

5 files changed

+42
-36
lines changed

R/get_modeldata.R

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ get_modeldata <- function(model, additional_variables = FALSE, modeldata = NULL,
4949
additional_variables <- c(additional_variables, fwts)
5050
}
5151

52+
# always need weights in this model class
53+
if (any(c("svylm", "svyglm") %in% class(model))) {
54+
wts <- TRUE
55+
}
56+
5257
# after by
5358
if (isTRUE(checkmate::check_flag(additional_variables))) {
5459
out <- hush(insight::get_data(

R/sanitize_variables.R

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ sanitize_variables <- function(variables,
3131
predictors <- list(conditional = predictors)
3232
} else {
3333
predictors <- insight::find_variables(model, verbose = FALSE)
34+
predictors <- predictors[!names(predictors) %in% "response"]
3435
}
3536

3637
# unsupported models like pytorch

inst/tinytest/test-elasticity.R

+15-11
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,22 @@ expect_equivalent(dyex_a, dyex_b)
7070

7171
# Issue #1113: avg_slopes(slope = "eyex") should skip dedup
7272
requiet("nnet")
73-
dat <- structure(list(y_region2 = c(1, 1, 1, 2, 2, 2, 2, 2, 1, 2, 2,
74-
2, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 1, 2, 1, 2, 2, 3, 3, 3, 3,
75-
3, 3, 1, 3, 3, 3, 1, 3, 3, 1, 3, 1, 3, 3, 3, 1, 3, 3), male = c(0,
76-
1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0,
77-
1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
78-
1, 0, 0, 1, 0, 0, 1), frenchlanguage = c(1, 1, 1, 1, 1, 1, 1,
79-
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1,
80-
1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
81-
1)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
82-
-50L))
73+
dat <- structure(list(y_region2 = c(
74+
1, 1, 1, 2, 2, 2, 2, 2, 1, 2, 2,
75+
2, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 1, 2, 1, 2, 2, 3, 3, 3, 3,
76+
3, 3, 1, 3, 3, 3, 1, 3, 3, 1, 3, 1, 3, 3, 3, 1, 3, 3), male = c(
77+
0,
78+
1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0,
79+
1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
80+
1, 0, 0, 1, 0, 0, 1), frenchlanguage = c(
81+
1, 1, 1, 1, 1, 1, 1,
82+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1,
83+
1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
84+
1)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(
85+
NA,
86+
-50L))
8387
dat$male = dat$male + 1e-6
84-
mod <- multinom(y_region2 ~ male + frenchlanguage, data = dat, trace = FALSE)
88+
mod <- nnet::multinom(y_region2 ~ male + frenchlanguage, data = dat, Hess = TRUE, trace = FALSE)
8589
s1 <- avg_slopes(mod, slope = "dydx")
8690
s2 <- avg_slopes(mod, slope = "dyex")
8791
s3 <- avg_slopes(mod, slope = "eyex")

inst/tinytest/test-pkg-fixest.R

+13-15
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ mod2 <- suppressMessages(feglm(am ~ mpg | gear, family = binomial(link = "logit"
2525
mod3 <- suppressMessages(feglm(am ~ mpg + mpg^2 | gear, family = binomial(link = "logit"), data = mtcars, warn = FALSE))
2626
mod4 <- suppressMessages(feglm(am ~ mpg | gear, family = binomial(link = "logit"), data = mtcars, warn = FALSE))
2727

28-
#skip_if_not_installed("fixest", minimum_version = "0.10.2")
28+
# skip_if_not_installed("fixest", minimum_version = "0.10.2")
2929
expect_inherits(insight::get_data(mod1), "data.frame")
3030
expect_inherits(insight::get_data(mod2), "data.frame")
3131
expect_inherits(insight::get_data(mod3), "data.frame")
@@ -66,9 +66,10 @@ expect_equivalent(mfx$std.error, mfx$std.errorstata, tolerance = .001)
6666

6767
# fixest::feols: predictions
6868
data(trade, package = "fixest")
69-
model <- feols(Euros ~ dist_km | Destination + Origin, data = trade)
69+
dat <- trade
70+
model <- feols(Euros ~ dist_km | Destination + Origin, data = dat)
7071
pred1 <- predictions(model)
71-
pred2 <- predictions(model, newdata = head(trade))
72+
pred2 <- predictions(model, newdata = head(dat))
7273
expect_predictions(pred1)
7374
expect_predictions(pred2, n_row = 6)
7475

@@ -138,8 +139,8 @@ expect_predictions(pred5, se = FALSE)
138139

139140
# bug stay dead: insight::get_data doesn't get all columns
140141
reg <- feols(
141-
Sepal.Width ~ Petal.Length | Species | Sepal.Length ~ Petal.Width,
142-
data = iris)
142+
Sepal.Width ~ Petal.Length | Species | Sepal.Length ~ Petal.Width,
143+
data = iris)
143144
mfx1 <- slopes(reg, newdata = iris)
144145
mfx2 <- slopes(reg)
145146
expect_inherits(mfx1, "marginaleffects")
@@ -163,11 +164,11 @@ expect_inherits(mfx3, "marginaleffects")
163164
dat <- data.table(mtcars)
164165
m <- feols(mpg ~ cyl * disp, dat)
165166
m1 <- slopes(m)
166-
m2 <- slopes(m, newdata = datagrid(disp = 0))
167+
m2 <- slopes(m, newdata = datagrid(disp = 0))
167168
expect_inherits(m1, "marginaleffects")
168169
expect_inherits(m2, "marginaleffects")
169170
m1 <- comparisons(m)
170-
m2 <- comparisons(m, newdata = datagrid(disp = 0))
171+
m2 <- comparisons(m, newdata = datagrid(disp = 0))
171172
expect_inherits(m1, "comparisons")
172173
expect_inherits(m2, "comparisons")
173174

@@ -258,11 +259,11 @@ expect_false(anyNA(p$std.error))
258259

259260
# Issue #705
260261
data(trade, package = "fixest")
261-
mod1 <- fepois(data = trade, Euros ~ 1 | Origin, offset = ~ log(dist_km))
262-
mod2 <- fepois(data = trade, Euros ~ 1 | Origin)
263-
264-
mfx1 <- avg_slopes(mod1)
265-
mfx2 <- avg_slopes(mod2)
262+
dat <- trade
263+
mod1 <- fepois(data = dat, Euros ~ 1 | Origin, offset = ~ log(dist_km))
264+
mod2 <- fepois(data = dat, Euros ~ 1 | Origin)
265+
mfx1 <- avg_slopes(mod1, vcov = FALSE)
266+
mfx2 <- avg_slopes(mod2, vcov = FALSE)
266267
expect_inherits(mfx1, "slopes")
267268
expect_inherits(mfx2, "slopes")
268269

@@ -311,6 +312,3 @@ expect_inherits(res, "comparisons") # should be slopes but can't figure out infe
311312

312313

313314
rm(list = ls())
314-
315-
316-

inst/tinytest/test-pkg-survey.R

+8-10
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ svyd <- survey::svydesign(
1717
data = fpc,
1818
nest = TRUE)
1919
mod <- survey::svyglm(x ~ nh, design = svyd)
20-
res <- slopes(mod)
20+
res <- slopes(mod, wts = "(weights)")
2121
mar <- suppressMessages(data.frame(margins(mod, unit_ses = TRUE)))
2222
expect_equivalent(res$estimate, as.numeric(mar$dydx_nh))
2323
expect_equivalent(res$std.error, as.numeric(mar$SE_dydx_nh), tolerance = 0.001)
@@ -39,15 +39,15 @@ expect_inherits(p, "data.frame")
3939
# Issue #1161
4040
dat <- "https://vincentarelbundock.github.io/Rdatasets/csv/AER/SmokeBan.csv"
4141
dat <- read.csv(dat, na.strings = c("*", ""))
42-
dat$weights <- runif(n=nrow(dat), min=1, max=100)
42+
dat$weights <- runif(n = nrow(dat), min = 1, max = 100)
4343
dat$smoker <- factor(dat$smoker)
44-
design1=svydesign(ids=~1, weights=~weights, data=dat)
45-
m <- suppressWarnings(svyglm(smoker ~ ban*education*gender+age, design=design1, family=binomial(), data = dat))
44+
design1 = svydesign(ids = ~1, weights = ~weights, data = dat)
45+
m <- suppressWarnings(svyglm(smoker ~ ban * education * gender + age, design = design1, family = binomial(), data = dat))
4646
cmp <- avg_comparisons(m,
47-
variables = "education",
48-
by = c("ban","gender"),
49-
wts = "weights",
50-
hypothesis = ~reference)
47+
variables = "education",
48+
by = c("ban", "gender"),
49+
wts = "weights",
50+
hypothesis = ~reference)
5151
expect_false(anyNA(cmp$estimate))
5252

5353

@@ -65,5 +65,3 @@ expect_false(anyNA(cmp$estimate))
6565

6666

6767
rm(list = ls())
68-
69-

0 commit comments

Comments
 (0)