Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upParametric term evaluation for ziplss models #45
Comments
|
Hi Philipp, yeah, handling these extended formula models (distributional regression models) is not something gratia does at the moment; Things work for the smooths as Simon just stores them as if they were just a single model with many smooths. The parametric terms code was written without considering those more general models. This is on the ToDo list that I hope to get to once I get a new course written/start of term, whichever happened first. |
|
I worked out what was going on; a combination of This should now work for all LSS models, but I have thus far only check the ziplss examples you provided. Thanks for reporting this. |
|
Hi Gavin, Unfortunately I discovered that there's a further complication in my use case that wasn't covered by my original reprex. It appears that categorical predictors cause additional problems. Amended reprex: #gratia error reprex
library(mgcv)
library(gratia)
## simulate some data...
f0 <- function(x) 2 * sin(pi * x); f1 <- function(x) exp(2 * x)
f2 <- function(x) 0.2 * x^11 * (10 * (1 - x))^6 + 10 *
(10 * x)^3 * (1 - x)^10
n <- 500;set.seed(5)
x0 <- runif(n); x1 <- runif(n)
x2 <- runif(n); x3 <- runif(n)
x4 <- sample(factor(c('a','b','c')), size = n, replace = TRUE)
## Simulate probability of potential presence...
eta1 <- f0(x0) + f1(x1) - 3
p <- binomial()$linkinv(eta1)
y <- as.numeric(runif(n)<p) ## 1 for presence, 0 for absence
## Simulate y given potentially present (not exactly model fitted!)...
ind <- y>0
eta2 <- f2(x2[ind])/3
y[ind] <- rpois(exp(eta2),exp(eta2))
## Fit ZIP model...
b <- gam(list(y~s(x2)+x3,~s(x0)+x1),family=ziplss())
draw(b)
#ZIP model with a categorical predictor
b0 <- gam(list(y~s(x2)+x4,~s(x0)+x1),family=ziplss())
draw(b0)# works but warning issued
plot(b0,pages=1, all.terms = TRUE) #works
#ZIP model with linear and categorical predictor
b1 <- gam(list(y~s(x2)+x3+x4,~s(x0)+x1),family=ziplss())
draw(b1) # fails
draw(b1, parametric = FALSE) # works
plot(b1,pages=1, all.terms = TRUE) #worksHaving a single categorical predictor yields a warning, but the partial effect plot appears.
Having a categorical predictor and another term leads to a failure
with the following traceback
which to me appears to indicate that names are being lost as they are being passed from the model frame to the prediction frames, but my knowledge of programming with tibbles is very limited. Any clues? |
|
Thanks for the revised example; I was likely missing some subtleties when implementing this. mgcv can fit such a range of models and this was worked on for simpler models; guess I need to expand the test suites to include some more complex examples. I’ll take a look at why this is failing. |
I'm working with models from the ziplss family that have parametric (i.e. non-smooth) terms, and these cause the
evaluate_parametric_termmethod (and hencedraw) to fail with an error message likeError in evaluate_parametric_term.gam(object, term = terms[i]) : Term is not in the parametric part of model: <x3>,because it does not handle the list structure of the two predictor formulae gracefully.
Reprex collapsed in here
I've traced the issue to
gratia/R/evaluate_smooth.R
Line 546 in 245ac45
which doesn't behave as expected as
ttis now a two-element list with labels"1" "2".My hacky attempt to fix this by changing the above line to
vars <- unlist(lapply(tt, labels))produces reasonable results if there is only a single parameteric predictor in the model, but falls over with a cryptic
plot_griderror that I can trace toalign_margin, but which I don't understand:sessionInfo()