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

ggpredict unable to plot from a natural spline with specified knots #240

Open
ifeomaozo opened this issue Oct 23, 2021 · 10 comments
Open

ggpredict unable to plot from a natural spline with specified knots #240

ifeomaozo opened this issue Oct 23, 2021 · 10 comments
Labels
3 investigators ❔❓ bug 🐛 Something isn't working

Comments

@ifeomaozo
Copy link

@strengejacke, I am reopening this issue since the insight update did not solve the problem. Please see below

> library(glmmTMB)
> library(splines)
> 
> data_e <- data.frame(
+   positives = c(3L, 2L, 0L, 7L, 0L, 5L, 0L, 3L, 6L, 0L, 2L, 0L, 2L, 3L, 2L, 14L, 1L, 3L, 3L, 0L), 
+   values = c(86.1702127659574, 100, 81.0344827586207, 98.7341772151899, 97.5903614457831, 
+              98.1308411214953, 96.551724137931, 100, 100, 96.045197740113, 99.1150442477876, 
+              98.8888888888889, 100, 91.6279069767442, 33.3333333333333, 0, 
+              92.3076923076923, 24, 0, 93.5483870967742))
> 
> m1 <- glmmTMB(
+   positives ~ ns(values, 3, knots = seq(min(values), max(values), length = 4)[2:3]),
+   data = data_e,
+   ziformula =  ~ 1,
+   family = poisson
+ )
> ggeffects::ggpredict(m1, terms="values [all]", type ='zero_inflated')

# Predicted counts of positives
# x = values

Error in cat(insight::format_table(dd, digits = digits, protect_integers = TRUE)) : 
  argument 1 (type 'list') cannot be handled by 'cat'
@strengejacke
Copy link
Owner

strange, maybe you updated at a time where the new binaries weren't properly built? For me, it works:

library(glmmTMB)
library(splines)

data_e <- data.frame(
  positives = c(3L, 2L, 0L, 7L, 0L, 5L, 0L, 3L, 6L, 0L, 2L, 0L, 2L, 3L, 2L, 14L, 1L, 3L, 3L, 0L), 
  values = c(86.1702127659574, 100, 81.0344827586207, 98.7341772151899, 97.5903614457831, 
             98.1308411214953, 96.551724137931, 100, 100, 96.045197740113, 99.1150442477876, 
             98.8888888888889, 100, 91.6279069767442, 33.3333333333333, 0, 
             92.3076923076923, 24, 0, 93.5483870967742))
m1 <- glmmTMB(
  positives ~ ns(values, 3, knots = seq(min(values), max(values), length = 4)[2:3]),
  data = data_e,
  ziformula =  ~ 1,
  family = poisson
)

ggeffects::ggpredict(m1, terms="values [all]", type ='zero_inflated')
#> # Predicted counts of positives
#> 
#> values | Predicted |        95% CI
#> ----------------------------------
#>   0.00 |      6.41 | [2.65, 10.18]
#>  33.33 |      1.52 | [0.00,  3.04]
#>  86.17 |      1.33 | [0.00,  2.65]
#>  92.31 |      1.82 | [0.52,  3.11]
#>  93.55 |      1.94 | [0.73,  3.15]
#>  96.55 |      2.29 | [1.19,  3.39]
#>  98.13 |      2.50 | [1.36,  3.65]
#> 100.00 |      2.78 | [1.37,  4.18]

Created on 2021-10-24 by the reprex package (v2.0.1)

@ifeomaozo
Copy link
Author

ifeomaozo commented Oct 23, 2021

hmmm...I just updated 2 hours ago. Do you have any troubleshooting suggestions? I uninstalled the previous version of insight on my windows computer and tried to reinstall and got the same error. I have also tried to replicate the problem on a mac but I can't install the updated package. My session info on the mac and windows is below. I will really appreciate any help.

install.packages("insight", repos = "https://easystats.r-universe.dev")
Warning in install.packages :
  unable to access index for repository https://easystats.r-universe.dev/src/contrib:
  cannot open URL 'https://easystats.r-universe.dev/src/contrib/PACKAGES'
Warning in install.packages :
  package ‘insight’ is not available for this version of R

A version of this package for your version of R might be available elsewhere,
see the ideas at
https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
Warning in install.packages :
  unable to access index for repository https://easystats.r-universe.dev/bin/macosx/contrib/4.1:
  cannot open URL 'https://easystats.r-universe.dev/bin/macosx/contrib/4.1/PACKAGES'
> 
sessionInfo(package = NULL)
R version 4.1.1 (2021-08-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Mojave 10.14.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] splines   stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] glmmTMB_1.1.2.3

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.7          lattice_0.20-44     MASS_7.3-54         grid_4.1.1          nlme_3.1-152        minqa_1.2.4        
 [7] nloptr_1.2.2.2      Matrix_1.3-4        boot_1.3-28         ggeffects_1.1.1     lme4_1.1-27.1       tools_4.1.1        
[13] TMB_1.7.22          numDeriv_2016.8-1.1 compiler_4.1.1      insight_0.14.5   
sessionInfo(package = NULL)
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17134)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] splines   stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] glmmTMB_1.1.2.3

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.7          nloptr_1.2.2.2      pillar_1.4.6        compiler_4.0.2      TMB_1.7.22          class_7.3-17       
 [7] tools_4.0.2         boot_1.3-25         statmod_1.4.34      lme4_1.1-23         digest_0.6.27       packrat_0.6.0      
[13] nlme_3.1-148        evaluate_0.14       lifecycle_1.0.0     tibble_3.0.3        lattice_0.20-41     pkgconfig_2.0.3    
[19] rlang_0.4.10        Matrix_1.2-18       DBI_1.1.0           yaml_2.2.1          xfun_0.21           e1071_1.7-3        
[25] dplyr_1.0.0         knitr_1.29          generics_0.0.2      vctrs_0.3.6         ggeffects_0.16.0    classInt_0.4-3     
[31] sjlabelled_1.1.7    grid_4.0.2          tidyselect_1.1.0    glue_1.4.2          sf_0.9-5            R6_2.4.1           
[37] rmarkdown_2.5       minqa_1.2.4         purrr_0.3.4         magrittr_2.0.1      ellipsis_0.3.1      htmltools_0.5.1.1  
[43] units_0.6-7         MASS_7.3-51.6       insight_0.14.5.1    numDeriv_2016.8-1.1 KernSmooth_2.23-17  crayon_1.4.1  

@ifeomaozo
Copy link
Author

ifeomaozo commented Oct 24, 2021

I was able to solve the issue by updating my R version and reinstalling all packages. The problem I am having now is that when I fit my final model, I end up with a new error message. See below. The data can be downloaded here. Again, I will appreciate any help.

m2 <- glmmTMB(positives~ns(edu_a, 3)+ ns(wealth, 3)+ns(pop_density_0m, 2) + ns(median_age, 2)+
                ns(med_treat_fever, knots = seq(min(med_treat_fever),max(med_treat_fever),length =4)[2:3])+
                ns(precipitation_monthly_0m, 3) + ns(EVI_0m, 3)+
                + offset(log(child_6_59_tested_malaria)) +
                mat(pos + 0 | ID) + ar1(month_year + 0 | ID2), data=map2,  ziformula=~1,family=poisson)

ggeffects::ggpredict(m2,terms="edu_a[all]", type ='zero_inflated')
Error in get_predictions_glmmTMB(model, data_grid, ci.lvl, linv, type,  : 
  'list' object cannot be coerced to type 'double'

@strengejacke
Copy link
Owner

Could you also please upload the fitted model object? It takes hours to fit the model on my machine ;-)

@ifeomaozo
Copy link
Author

ifeomaozo commented Nov 7, 2021

@strengejacke, thanks for returning to this! The model rds file can be downloaded here

@strengejacke
Copy link
Owner

Not sure what the issue here exactly is, but the function fails when computing the model matrix for the count component of the model:

library(splines)
library(glmmTMB)
library(ggeffects)

map2 <- readr::read_csv("D:/Downloads/all_data.csv", show_col_types = FALSE)
#> New names:
#> * `` -> ...1
m2 <- readRDS("D:/Downloads/multivariate_model.rds")
nd <- new_data(m2, "edu_a [all]")

condformula <- lme4::nobars(stats::formula(m2)[-2])
stats::model.matrix(condformula, nd)
#> Error in qr.default(t(const)): NA/NaN/Inf in foreign function call (arg 1)

Created on 2021-11-07 by the reprex package (v2.0.1)

@strengejacke strengejacke added 3 investigators ❔❓ bug 🐛 Something isn't working labels Nov 7, 2021
@nicholasgrebe
Copy link

I am having the same issue as @ifeomaozo -- I get this error when trying to use the ggpredict() call and "zero_inflated" type on a fitted glmmTMB object:

"Error in get_predictions_glmmTMB(model, data_grid, ci.lvl, linv, type, :
'list' object cannot be coerced to type 'double'"

Just moving this to the front in case there's been some resolution on it. Thank you!

@jml-science
Copy link

@nicholasgrebe Could you resolve the issue by any chance? I am trying to do the same thing (ggpredict with "zero_inflated" type on a fitted glmmTMB object) and receive the same error. Curiously, it just happens with one model specification - for other models, the same command works...

@nicholasgrebe
Copy link

@jml-science I never had any luck with this package, but using the marginaleffects package was a good substitute (e.g. the "type" argument under the plot_predictions function did what I needed): https://marginaleffects.com/articles/reference/plot_predictions.html

@strengejacke
Copy link
Owner

Note that this issue (#240 (comment)) stems from how standard errors for zero-inflated models are calculated (or better: should be calculated, see https://journal.r-project.org/archive/2017/RJ-2017-066/index.html), so I'm not sure how accurate SE's returned by marginaleffects or predict() are. ggemmeans() still may work, though.

library(ggeffects)
library(marginaleffects)
library(glmmTMB)
data(Salamanders)
m1 <- glmmTMB(count ~ mined + (1 | site),
  zi = ~mined,
  family = poisson, data = Salamanders
)
ggpredict(m1, terms = "mined", type = "zero_inflated")
#> # Predicted counts of count
#> 
#> mined | Predicted |       95% CI
#> --------------------------------
#> yes   |      0.26 | [0.10, 0.43]
#> no    |      2.21 | [1.77, 2.64]
#> 
#> Adjusted for:
#> * site = NA (population-level)
avg_predictions(m1, variables = "mined", type = "response", vcov = vcov(m1))
#> 
#>  mined Estimate Std. Error     z Pr(>|z|)     S 2.5 % 97.5 %
#>    yes    0.272     0.0442  6.15   <0.001  30.2 0.185  0.359
#>    no     2.265     0.1583 14.31   <0.001 151.8 1.955  2.576
#> 
#> Columns: mined, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high 
#> Type:  response

Created on 2023-10-06 with reprex v2.0.2

Another issue is that there were some changes in glmmTMB that changed the behaviour of predict for model with truncated family, see easystats/insight#679 (comment). Maybe when I try to fix #319, this issue here can also be resolved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3 investigators ❔❓ bug 🐛 Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants