easystats/parameters

parameters fails with nestedLogit models

Closed this issue · 7 comments

I see that the nestedLogit package was recently added to parameters. Thank you!

However, it errors in my first test, below. This might be because in v.0.3.0 we revised the predict() method to give standard errors, and this has affected the structure of the result returned.

#' ---
#' title: Parameters package ??
#' ---
#'
library(nestedLogit)
library(parameters)
data(Womenlf, package="carData")
comparisons <- logits(work=dichotomy("not.work", c("parttime", "fulltime")),
                      full=dichotomy("parttime", "fulltime"))
wlf.nested <- nestedLogit(partic ~ hincome + children,
                          dichotomies = comparisons,
                          data=Womenlf)

parameters(wlf.nested)

# > parameters(wlf.nested)
# Error in attr(rval, "reshapeLong") <- undoInfo :
#   attempt to set an attribute on NULL
# > traceback()
# 11: reshapeLong(data, idvar = idvar, timevar = timevar, varying = varying,
#                 v.names = v.names, drop = drop, times = times, ids = ids,
#                 new.row.names = new.row.names)
# 10: stats::reshape(x, idvar = "id", ids = row.names(x), times = columns,
#                    timevar = names_to, v.names = values_to, varying = list(columns),
#                    direction = "long")
# 9: .gather(cf, names_to = "Component", values_to = "Estimate")
# 8: get_parameters.nestedLogit(model, effects = effects, component = component,
# verbose = FALSE)
# 7: insight::get_parameters(model, effects = effects, component = component,
#                            verbose = FALSE)
# ...
# 3: do.call(".model_parameters_generic", args)
# 2: model_parameters.nestedLogit(wlf.nested)
# 1: parameters(wlf.nested)

Note that we now have an as.data.frame.predictNestedLogit() method to do the reshaping to a long format, but it should be called with the same newdata argument as used in predict(). This is probably what you want to use.

# get predicted values for a grid
new <- expand.grid(hincome=seq(0, 45, length=4),
                   children=c("absent", "present"))

pred.nested <- predict(wlf.nested, new)
plotdata <- as.data.frame(pred.nested, newdata=new)
head(plotdata)
  hincome children response       p    se.p   logit se.logit
1       0   absent not.work 0.20820 0.06326 -1.3358   0.3838
2       0   absent parttime 0.02372 0.01775 -3.7176   0.7666
3       0   absent fulltime 0.76809 0.06386  1.1975   0.3585
4      15   absent not.work 0.33155 0.05368 -0.7012   0.2422
5      15   absent parttime 0.08936 0.03356 -2.3214   0.4124
6      15   absent fulltime 0.57909 0.05690  0.3190   0.2334

Are you using the latest version of packages insight and parameters?

Ah, yes, I can see from the traceback that you seem to use latest versions. Will look into this issue.

I'm using:

> packageVersion("parameters")
[1] ‘0.21.1’
> packageVersion("insight")
[1] ‘0.19.2’

Strange, cannot reproduce your issue:

library(nestedLogit)
library(parameters)
data(Womenlf, package = "carData")
comparisons <- logits(
  work = dichotomy("not.work", c("parttime", "fulltime")),
  full = dichotomy("parttime", "fulltime")
)
wlf.nested <- nestedLogit(partic ~ hincome + children,
  dichotomies = comparisons,
  data = Womenlf
)

parameters(wlf.nested)
#> # {not.work} vs. {parttime, fulltime} response
#> 
#> Parameter          | Log-Odds |   SE |         95% CI |     z |      p
#> ----------------------------------------------------------------------
#> (Intercept)        |     1.34 | 0.38 | [ 0.61,  2.12] |  3.48 | < .001
#> hincome            |    -0.04 | 0.02 | [-0.08,  0.00] | -2.14 | 0.032 
#> children [present] |    -1.58 | 0.29 | [-2.16, -1.01] | -5.39 | < .001
#> 
#> # {parttime} vs. {fulltime} response
#> 
#> Parameter          | Log-Odds |   SE |         95% CI |     z |      p
#> ----------------------------------------------------------------------
#> (Intercept)        |     3.48 | 0.77 | [ 2.11,  5.14] |  4.53 | < .001
#> hincome            |    -0.11 | 0.04 | [-0.19, -0.04] | -2.74 | 0.006 
#> children [present] |    -2.65 | 0.54 | [-3.80, -1.66] | -4.90 | < .001
#> 
#> Uncertainty intervals (profile-likelihood) and p-values (two-tailed)
#>   computed using a Wald z-distribution approximation.
#> 
#> The model has a log- or logit-link. Consider using `exponentiate =
#>   TRUE` to interpret coefficients as ratios.

packageVersion("nestedLogit")
#> [1] '0.3.0'
packageVersion("parameters")
#> [1] '0.21.1'
packageVersion("insight")
#> [1] '0.19.2'

Created on 2023-05-30 with reprex v2.0.2

I installed your package using remotes::install_github("friendly/nestedLogit").

Sorry, my problem. Something was wrong in my session.

But this change indeed affects the implementation in ggeffects (which I have already updated to match the new behaviour of version o.3.0 of nestedLogit, see strengejacke/ggeffects#334).