diff --git a/DESCRIPTION b/DESCRIPTION index 8b799db17..711d5018f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: insight Title: Easy Access to Model Information for Various Model Objects -Version: 0.19.7.5 +Version: 0.19.7.6 Authors@R: c(person(given = "Daniel", family = "Lüdecke", diff --git a/NAMESPACE b/NAMESPACE index d4a62fff5..2dc5c45e2 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -69,6 +69,7 @@ S3method(find_algorithm,mixed) S3method(find_algorithm,nestedLogit) S3method(find_algorithm,rlmerMod) S3method(find_algorithm,rq) +S3method(find_algorithm,rqs) S3method(find_algorithm,rqss) S3method(find_algorithm,scam) S3method(find_algorithm,speedglm) @@ -1118,6 +1119,7 @@ S3method(link_function,psm) S3method(link_function,riskRegression) S3method(link_function,robmixglm) S3method(link_function,rq) +S3method(link_function,rqs) S3method(link_function,rqss) S3method(link_function,speedglm) S3method(link_function,speedlm) @@ -1241,6 +1243,7 @@ S3method(link_inverse,psm) S3method(link_inverse,riskRegression) S3method(link_inverse,robmixglm) S3method(link_inverse,rq) +S3method(link_inverse,rqs) S3method(link_inverse,rqss) S3method(link_inverse,speedglm) S3method(link_inverse,speedlm) @@ -1389,6 +1392,7 @@ S3method(model_info,riskRegression) S3method(model_info,rma) S3method(model_info,robmixglm) S3method(model_info,rq) +S3method(model_info,rqs) S3method(model_info,rqss) S3method(model_info,speedglm) S3method(model_info,speedlm) @@ -1500,6 +1504,7 @@ S3method(n_obs,poissonmfx) S3method(n_obs,probitmfx) S3method(n_obs,riskRegression) S3method(n_obs,rq) +S3method(n_obs,rqs) S3method(n_obs,rqss) S3method(n_obs,selection) S3method(n_obs,sem) diff --git a/NEWS.md b/NEWS.md index f01f0b75b..379b552e2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,8 @@ * Removed deprecated arguments in `get_data.mmrm()`. +* Improved support for models of class `rqs` (package *quantreg*). + ## Bug fixes * Fixed issue in `get_loglikelihood()` for glm-models with binary outcome, where diff --git a/R/find_algorithm.R b/R/find_algorithm.R index 69f0d543b..1af6bab63 100644 --- a/R/find_algorithm.R +++ b/R/find_algorithm.R @@ -192,6 +192,12 @@ find_algorithm.rq <- function(x, ...) { list(algorithm = x$method) } +#' @export +find_algorithm.rqs <- find_algorithm.rq + +#' @export +find_algorithm.rqss <- find_algorithm.rq + #' @export find_algorithm.bayesx <- function(x, ...) { @@ -209,12 +215,6 @@ find_algorithm.crq <- function(x, ...) { } -#' @export -find_algorithm.rqss <- function(x, ...) { - list(algorithm = x$method) -} - - #' @export find_algorithm.glm <- function(x, ...) { list(algorithm = "ML") diff --git a/R/link_function.R b/R/link_function.R index 4740b7a60..cfcdaeb87 100644 --- a/R/link_function.R +++ b/R/link_function.R @@ -111,6 +111,9 @@ link_function.gls <- link_function.lm #' @export link_function.rq <- link_function.lm +#' @export +link_function.rqs <- link_function.lm + #' @export link_function.rqss <- link_function.lm diff --git a/R/link_inverse.R b/R/link_inverse.R index 749cfde77..051abaa4f 100644 --- a/R/link_inverse.R +++ b/R/link_inverse.R @@ -147,6 +147,9 @@ link_inverse.lme <- link_inverse.lm #' @export link_inverse.rq <- link_inverse.lm +#' @export +link_inverse.rqs <- link_inverse.lm + #' @export link_inverse.rqss <- link_inverse.lm diff --git a/R/model_info.R b/R/model_info.R index 0837f86d5..612b57914 100644 --- a/R/model_info.R +++ b/R/model_info.R @@ -205,6 +205,9 @@ model_info.bayesx <- model_info.maxLik #' @export model_info.rq <- model_info.maxLik +#' @export +model_info.rqs <- model_info.maxLik + #' @export model_info.crq <- model_info.maxLik diff --git a/R/n_obs.R b/R/n_obs.R index 32ee7853e..3461b5a0d 100644 --- a/R/n_obs.R +++ b/R/n_obs.R @@ -100,11 +100,13 @@ n_obs.glm <- function(x, disaggregate = FALSE, ...) { #' @export n_obs.censReg <- n_obs.default + #' @export n_obs.nestedLogit <- function(x, disaggregate = FALSE, ...) { lapply(x$models, n_obs) } + #' @rdname n_obs #' @export n_obs.svyolr <- function(x, weighted = FALSE, ...) { @@ -121,7 +123,6 @@ n_obs.svy_vglm <- function(x, ...) { n_obs(x$fit) } - #' @export n_obs.model_fit <- n_obs.svy_vglm @@ -141,6 +142,7 @@ n_obs.gam <- function(x, ...) { } } + #' @export n_obs.gamm <- function(x, ...) { if (object_has_names(x, "gam")) { @@ -190,14 +192,12 @@ n_obs.merModList <- function(x, ...) { } - #' @export n_obs.summary.lm <- function(x, ...) { length(x$residuals) } - #' @export n_obs.mediate <- function(x, ...) { x$nobs @@ -216,63 +216,54 @@ n_obs.garch <- function(x, ...) { } - #' @export n_obs.bayesx <- function(x, ...) { length(x$response) } - #' @export n_obs.flexsurvreg <- function(x, ...) { x$N } - #' @export n_obs.SemiParBIV <- function(x, ...) { x$n } - #' @export n_obs.ivprobit <- function(x, ...) { nrow(x$mr1) } - #' @export n_obs.mvord <- function(x, ...) { x$rho$n } - #' @export n_obs.bamlss <- function(x, ...) { nrow(x$model.frame) } - #' @export n_obs.coeftest <- function(x, ...) { attributes(x)$nobs } - #' @export n_obs.lmRob <- function(x, ...) { length(x$fitted.values) } - #' @export n_obs.lqmm <- function(x, ...) { x$nobs @@ -282,8 +273,6 @@ n_obs.lqmm <- function(x, ...) { n_obs.lqm <- n_obs.lqmm - - #' @export n_obs.sem <- function(x, ...) { if (!.is_semLme(x)) { @@ -293,28 +282,24 @@ n_obs.sem <- function(x, ...) { } - #' @export n_obs.LORgee <- function(x, ...) { x$nobs } - #' @export n_obs.crr <- function(x, ...) { x$n } - #' @export n_obs.mcmc <- function(x, ...) { nrow(as.data.frame(x)) } - #' @export n_obs.biglm <- function(x, ...) { x$n @@ -339,7 +324,10 @@ n_obs.zerotrunc <- n_obs.biglm n_obs.zeroinfl <- n_obs.biglm - +#' @export +n_obs.rqs <- function(x, ...) { + length(x$y) +} #' @export @@ -351,7 +339,6 @@ n_obs.cgam <- function(x, ...) { n_obs.cglm <- n_obs.cgam - #' @export n_obs.gbm <- function(x, ...) { length(x$fit) @@ -382,7 +369,6 @@ n_obs.glimML <- function(x, ...) { } - #' @export n_obs.mle2 <- function(x, ...) { n <- .safe(x@nobs) @@ -396,42 +382,36 @@ n_obs.mle2 <- function(x, ...) { n_obs.mle <- n_obs.mle2 - #' @export n_obs.glmRob <- function(x, ...) { length(x$fitted.values) } - #' @export n_obs.gmnl <- function(x, ...) { x$logLik$nobs } - #' @export n_obs.multinom <- function(x, ...) { nrow(x$fitted.values) } - #' @export n_obs.cpglmm <- function(x, ...) { nrow(x@frame) } - #' @export n_obs.lmodel2 <- function(x, ...) { nrow(get_data(x, verbose = FALSE)) } - #' @export n_obs.cpglm <- function(x, ...) { nrow(x$model.frame) @@ -461,7 +441,6 @@ n_obs.BBreg <- function(x, ...) { n_obs.BBmm <- n_obs.BBreg - #' @export n_obs.crq <- function(x, ...) { n <- nrow(x$residuals) @@ -502,35 +481,30 @@ n_obs.MANOVA <- function(x, ...) { n_obs.RM <- n_obs.MANOVA - #' @export n_obs.nlrq <- function(x, ...) { length(stats::fitted(x)) } - #' @export n_obs.survfit <- function(x, ...) { length(x$n.event) } - #' @export n_obs.mhurdle <- function(x, ...) { nrow(x$model) } - #' @export n_obs.survreg <- function(x, ...) { length(x$linear.predictors) } - #' @export n_obs.aareg <- function(x, ...) { max(x$n) @@ -556,28 +530,24 @@ n_obs.felm <- function(x, ...) { } - #' @export n_obs.feis <- function(x, ...) { length(x$fitted.values) } - #' @export n_obs.averaging <- function(x, ...) { attr(x, "nobs") } - #' @export n_obs.fixest <- function(x, ...) { x$nobs } - #' @export n_obs.feglm <- function(x, ...) { x$nobs[["nobs"]] @@ -596,7 +566,6 @@ n_obs.aovlist <- function(x, ...) { } - #' @rdname n_obs #' @export n_obs.stanmvreg <- function(x, select = NULL, ...) { @@ -621,7 +590,6 @@ n_obs.stanmvreg <- function(x, select = NULL, ...) { } - #' @export n_obs.blrm <- function(x, ...) { x$N @@ -633,11 +601,9 @@ n_obs.mlogit <- function(x, ...) { nrow(x$model) } - #' @export n_obs.Glm <- n_obs.mlogit - #' @export n_obs.maxLik <- n_obs.mlogit @@ -648,7 +614,6 @@ n_obs.wbm <- function(x, ...) { } - #' @export n_obs.wbgee <- function(x, ...) { stats::nobs(x) diff --git a/tests/testthat/test-rq.R b/tests/testthat/test-rq.R index e6ec908d6..6c7dcb8fc 100644 --- a/tests/testthat/test-rq.R +++ b/tests/testthat/test-rq.R @@ -37,19 +37,16 @@ test_that("find_response", { }) test_that("get_response", { - expect_equal(get_response(m1), stackloss$stack.loss) + expect_identical(get_response(m1), stackloss$stack.loss) }) test_that("get_predictors", { - expect_equal(colnames(get_predictors(m1)), c("Air.Flow", "Water.Temp")) + expect_named(get_predictors(m1), c("Air.Flow", "Water.Temp")) }) test_that("get_data", { - expect_equal(nrow(get_data(m1)), 21) - expect_equal( - colnames(get_data(m1)), - c("stack.loss", "Air.Flow", "Water.Temp") - ) + expect_identical(nrow(get_data(m1)), 21L) + expect_named(get_data(m1), c("stack.loss", "Air.Flow", "Water.Temp")) }) test_that("find_formula", { @@ -62,21 +59,21 @@ test_that("find_formula", { }) test_that("find_terms", { - expect_equal( + expect_identical( find_terms(m1), list( response = "stack.loss", conditional = c("Air.Flow", "Water.Temp") ) ) - expect_equal( + expect_identical( find_terms(m1, flatten = TRUE), c("stack.loss", "Air.Flow", "Water.Temp") ) }) test_that("n_obs", { - expect_equal(n_obs(m1), 21) + expect_identical(n_obs(m1), 21L) }) test_that("link_function", { @@ -88,14 +85,14 @@ test_that("link_inverse", { }) test_that("find_parameters", { - expect_equal( + expect_identical( find_parameters(m1), list(conditional = c( "(Intercept)", "Air.Flow", "Water.Temp" )) ) - expect_equal(nrow(get_parameters(m1)), 3) - expect_equal( + expect_identical(nrow(get_parameters(m1)), 3L) + expect_identical( get_parameters(m1)$Parameter, c("(Intercept)", "Air.Flow", "Water.Temp") ) @@ -106,7 +103,7 @@ test_that("is_multivariate", { }) test_that("find_algorithm", { - expect_equal(find_algorithm(m1), list(algorithm = "br")) + expect_identical(find_algorithm(m1), list(algorithm = "br")) }) test_that("find_statistic", { diff --git a/tests/testthat/test-rqs.R b/tests/testthat/test-rqs.R new file mode 100644 index 000000000..1d5c49189 --- /dev/null +++ b/tests/testthat/test-rqs.R @@ -0,0 +1,116 @@ +skip_if_not(getRversion() >= "4.2.0") +skip_if_not_installed("quantreg") + +data(stackloss) +m1 <- quantreg::rq( + stack.loss ~ Air.Flow + Water.Temp, + data = stackloss, + tau = c(0.25, 0.5, 0.75) +) + +test_that("model_info", { + expect_true(model_info(m1)$is_linear) +}) + +test_that("find_predictors", { + expect_identical( + find_predictors(m1), + list(conditional = c("Air.Flow", "Water.Temp")) + ) + expect_identical( + find_predictors(m1, flatten = TRUE), + c("Air.Flow", "Water.Temp") + ) + expect_null(find_predictors(m1, effects = "random")) +}) + +test_that("find_random", { + expect_null(find_random(m1)) +}) + +test_that("get_random", { + expect_warning(get_random(m1)) +}) + +test_that("find_response", { + expect_identical(find_response(m1), "stack.loss") +}) + +test_that("get_response", { + expect_identical(get_response(m1), stackloss$stack.loss) +}) + +test_that("get_predictors", { + expect_named(get_predictors(m1), c("Air.Flow", "Water.Temp")) +}) + +test_that("get_data", { + expect_identical(nrow(get_data(m1)), 21L) + expect_named(get_data(m1), c("stack.loss", "Air.Flow", "Water.Temp")) +}) + +test_that("find_formula", { + expect_length(find_formula(m1), 1) + expect_equal( + find_formula(m1), + list(conditional = as.formula("stack.loss ~ Air.Flow + Water.Temp")), + ignore_attr = TRUE + ) +}) + +test_that("find_terms", { + expect_identical( + find_terms(m1), + list( + response = "stack.loss", + conditional = c("Air.Flow", "Water.Temp") + ) + ) + expect_identical( + find_terms(m1, flatten = TRUE), + c("stack.loss", "Air.Flow", "Water.Temp") + ) +}) + +test_that("n_obs", { + expect_identical(n_obs(m1), 21L) +}) + +test_that("link_function", { + expect_equal(link_function(m1)(0.2), 0.2, tolerance = 1e-5) +}) + +test_that("link_inverse", { + expect_equal(link_inverse(m1)(0.2), 0.2, tolerance = 1e-5) +}) + +test_that("find_parameters", { + expect_identical( + find_parameters(m1), + list(conditional = c( + "(Intercept)", "Air.Flow", "Water.Temp" + )) + ) + expect_identical(nrow(get_parameters(m1)), 9L) + expect_identical( + get_parameters(m1)$Parameter, + rep(c("(Intercept)", "Air.Flow", "Water.Temp"), 3) + ) + expect_equal( + get_parameters(m1)$Estimate, + c(-36, 0.5, 1, -44.08065, 0.79032, 0.66129, -54.18966, 0.87069, 0.98276), + tolerance = 1e-3 + ) +}) + +test_that("is_multivariate", { + expect_false(is_multivariate(m1)) +}) + +test_that("find_algorithm", { + expect_identical(find_algorithm(m1), list(algorithm = "br")) +}) + +test_that("find_statistic", { + expect_identical(find_statistic(m1), "t-statistic") +})