Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intercept-only models return NaN with avg_*() #1230

Closed
vbrazao opened this issue Oct 11, 2024 · 3 comments
Closed

Intercept-only models return NaN with avg_*() #1230

vbrazao opened this issue Oct 11, 2024 · 3 comments

Comments

@vbrazao
Copy link

vbrazao commented Oct 11, 2024

Not sure if this is a bug or by design, but I seem to be encountering the same issue @ASKurz reported here: #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
Copy link
Author

vbrazao commented Oct 12, 2024

just for a test, I tried the same with a simpler model:

dat_bin <- dat |> 
  dplyr::mutate(
    A = ifelse(response == "A", 1, 0)
  )

model_bin <- glm(
  formula = A ~ 1,
  data = dat_bin
)

and the pattern was the same! predictions() generates predictions, but avg_predictions outputs NaNs. (In this particular case, even the estimate was NaN). So it seems to be generalized behavior for intercept-only models!

@vincentarelbundock vincentarelbundock changed the title avg_predictions produces NaNs for intercept-only nnet::multinom model Intercept-only models return NaN Oct 12, 2024
@vincentarelbundock vincentarelbundock changed the title Intercept-only models return NaN Intercept-only models return NaN with avg_*() Oct 12, 2024
@vincentarelbundock
Copy link
Owner

Thanks for the report @vbrazao

I tried to look at this a bit and can't quite figure out what is going on with intercept-only model. I'll come back to it when I find some time. Sorry for the problems!

vincentarelbundock added a commit that referenced this issue Nov 21, 2024
vincentarelbundock added a commit that referenced this issue Nov 21, 2024
@vincentarelbundock
Copy link
Owner

Thanks a lot for the report. Should be fixed on Github now. I plan to release to CRAN in the next few weeks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants