easystats / parameters

:bar_chart: Computation and processing of models' parameters
https://easystats.github.io/parameters/
GNU General Public License v3.0
428 stars 36 forks source link

PR: Support of model_parameters for multcomp::glht #264

Closed iago-pssjd closed 4 years ago

iago-pssjd commented 4 years ago

As suggested by @strengejacke in #141 I open a new issue for this PR.

It seems that model_parameters does not support multcomp::glht output:

> m1<- glmmTMB(am ~ disp + hp + (1|carb), data = mtcars)
> glht(m1, linfct = matrix(c(0,1,-1),nrow=1))

     General Linear Hypotheses

Linear Hypotheses:
        Estimate
1 == 0 -0.008652

> model_parameters(glht(m1, linfct = matrix(c(0,1,-1),nrow=1)))
Error in cs[, column_index] : incorrect number of dimensions

Would be this a possible feature?

strengejacke commented 4 years ago

I can't reproduce your example:

library(glmmTMB)
library(multcomp)
data(mtcars)

m1 <- glmmTMB(am ~ disp + hp + (1 | carb), data = mtcars)
mc <- glht(m1, linfct = matrix(c(0, 1, -1), nrow = 1))
#> Error in modelparm.default(model, ...): dimensions of coefficients and covariance matrix don't match

Created on 2020-06-28 by the reprex package (v0.3.0)

strengejacke commented 4 years ago
library(multcomp)
library(parameters)

### multiple linear model, swiss data
lmod <- lm(Fertility ~ ., data = swiss)

### test of H_0: all regression coefficients are zero 
### (ignore intercept)

### define coefficients of linear function directly
K <- diag(length(coef(lmod)))[-1,]
rownames(K) <- names(coef(lmod))[-1]

### set up general linear hypothesis
m <- glht(lmod, linfct = K)
model_parameters(m)
#> Parameter             |   SE |         95% CI |     t | df |      p
#> -------------------------------------------------------------------
#> Agriculture == 0      | 0.07 | [-0.36,  0.01] | -2.45 | 41 | 0.079 
#> Examination == 0      | 0.25 | [-0.93,  0.41] | -1.02 | 41 | 0.785 
#> Education == 0        | 0.18 | [-1.36, -0.39] | -4.76 | 41 | < .001
#> Catholic == 0         | 0.04 | [ 0.01,  0.20] |  2.95 | 41 | 0.024 
#> Infant.Mortality == 0 | 0.38 | [ 0.07,  2.09] |  2.82 | 41 | 0.033

Created on 2020-06-28 by the reprex package (v0.3.0)

iago-pssjd commented 4 years ago

Dear Daniel,

You are right. At the time I posted the issue I was no conscious that a specific glmmTMB method for multcomp was necessary (it seems that previously was not exported: https://github.com/glmmTMB/glmmTMB/commit/aec62d8fc7b71100fbe6061e87f9d145a1a41813). So my example needs to define previously the next function:

modelparm.glmmTMB <- function (model, 
                               coef. = function(x) fixef(x)[[component]],
                               vcov. = function(x) vcov(x)[[component]],
                               df = NULL, component="cond", ...) {
  multcomp:::modelparm.default(model, coef. = coef., vcov. = vcov.,
                               df = df, ...)
}

Thank you!

strengejacke commented 4 years ago

Could you update insight and parameters from GitHub and check if it now works with your example?

iago-pssjd commented 4 years ago

I updated both packages and also glmmTMB from github and the code produces yet an error:

> library(glmmTMB)
> library(multcomp)
> library(parameters)
> m1 <- glmmTMB(am ~ disp + hp + (1 | carb), data = mtcars)
> mc <- glht(m1, linfct = matrix(c(0, 1, -1), nrow = 1))
> model_parameters(mc)
Error in cs[, column_index] : incorrect number of dimensions
> model_parameters(glht(m1, linfct = matrix(c(0,1,-1),nrow=1)))
Error in cs[, column_index] : incorrect number of dimensions