marginaleffects icon indicating copy to clipboard operation
marginaleffects copied to clipboard

Intercept-only models return `NaN` with `avg_*()`

Open vbrazao opened this issue 4 months ago • 2 comments

Not sure if this is a bug or by design, but I seem to be encountering the same issue @ASKurz reported here: https://github.com/vincentarelbundock/marginaleffects/issues/1021 when using a multinomial model.

library(nnet)
library(marginaleffects)

dat <- data.frame(
  response = sample(c("A", "B", "C"), size = 100, prob = c(.2, .2, .6), replace = TRUE)
)

model <- nnet::multinom(
  formula = response ~ 1,
  data = dat, 
  trace = FALSE
)

marginaleffects::predictions(
  model
)
Results:

 Group Estimate Std. Error    z Pr(>|z|)    S 2.5 % 97.5 %
     A     0.15     0.0357  4.2   <0.001 15.2 0.080  0.220
     A     0.15     0.0357  4.2   <0.001 15.2 0.080  0.220
     A     0.15     0.0357  4.2   <0.001 15.2 0.080  0.220
     A     0.15     0.0357  4.2   <0.001 15.2 0.080  0.220
     A     0.15     0.0357  4.2   <0.001 15.2 0.080  0.220
--- 290 rows omitted. See ?avg_predictions and ?print.marginaleffects --- 
     C     0.59     0.0492 12.0   <0.001 107.7 0.494  0.686
     C     0.59     0.0492 12.0   <0.001 107.7 0.494  0.686
     C     0.59     0.0492 12.0   <0.001 107.7 0.494  0.686
     C     0.59     0.0492 12.0   <0.001 107.7 0.494  0.686
     C     0.59     0.0492 12.0   <0.001 107.7 0.494  0.686
Columns: rowid, group, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high, response 
Type:  probs 

However,

marginaleffects::avg_predictions(
  model
)

Results:

 Group Estimate Std. Error   z Pr(>|z|)   S 2.5 % 97.5 %
     A      NaN        NaN NaN       NA NaN   NaN    NaN
     B      NaN        NaN NaN       NA NaN   NaN    NaN
     C      NaN        NaN NaN       NA NaN   NaN    NaN

Columns: group, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high 
Type:  probs 

My plan was to use the hypothesis argument to compare the predicted proportions to expected proportions (from domain knowledge), but I got stuck here. Not sure if my approach is simply wrong or marginaleffects should be able to produce this...

vbrazao avatar Oct 11 '24 18:10 vbrazao