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

gratia::derivatives: "Subscript out of bounds"-error on bs="fs" model #57

Closed
tomand-uio opened this issue Dec 6, 2019 · 7 comments
Closed
Assignees
Labels
bug Something isn't working

Comments

@tomand-uio
Copy link

tomand-uio commented Dec 6, 2019

I am trying to use hierarchical GAMs for analyzing microalgal growth curves in 96-well plates, so I fit a "S-type" GAM (cf. Pedersen et al. 2019) and want to extract derivatives to calculate growth rates. The following code shows an example that leads to the error message "Error in .subset2(x, i) : subscript out of bounds" when I try to apply derivatives() to the GAM object.

I can imagine that one possible answer could be that this was never supposed to work, in which case I guess I'll use som combination of predict() and lapply() or similar produce rates of change for each unit. Or maybe I have just misunderstood something, and there is already a simple way to get this right? Or maybe it's an actual bug...

logistic.growth <- function(t, y0, K, r) {
  return(K * (y0 / (y0 + (K - y0) * exp(-r * t))))
}

N <- 16
n <- 12

y0 <- 0.5
r  <- 0.25
K  <- rnorm(N, mean=5, sd=1)

d <- data.frame(
  unit=factor(rep(1:N, each=n)), 
  t=rep(seq(0, 20, length=n), N))
d$y <- logistic.growth(d$t, y0, K[d$unit], r)

S  <- 0.25
d$y.obs <- d$y + rnorm(nrow(d), sd=S)

library(mgcv)
library(gratia)

m <- gam(y.obs ~ s(t, unit, k=5, bs="fs", m=2), data=d, method="REML")
dm <- derivatives(m)
@gavinsimpson
Copy link
Owner

With your code I see this:

> dm <- derivatives(m)
Error in Summary.factor(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,  : 
  ‘min’ not meaningful for factors

with the current version on github (not CRAN). This is because an internal function used by derivatives() to generate data at which to compute the derivative is treating the spline as 2-d and trying to generate a sequence over the factor specified in the smooth.

So, yes, this is a bug.

@gavinsimpson gavinsimpson self-assigned this Dec 6, 2019
@gavinsimpson gavinsimpson added the bug Something isn't working label Dec 6, 2019
gavinsimpson added a commit that referenced this issue Dec 6, 2019
@gavinsimpson
Copy link
Owner

Thanks for informing me about this @tomand-uio. I have now fixed this issue in the latest commit to the github version.

gavinsimpson added a commit that referenced this issue Dec 6, 2019
@tomand-uio
Copy link
Author

tomand-uio commented Dec 7, 2019 via email

@mahonmb
Copy link

mahonmb commented Jul 22, 2024

Following up on this bug. I found a secondary issue with using derivatives() and a factor smooth, which appears when using linear covariates. Taking the example from above and adding a randomly generated variable as a linear covariate:

d$linearterm = runif(N)
m <- gam(y.obs ~ s(t, unit, k=5, bs="fs", m=2) + linearterm, data=d, method="REML")
dm <- derivatives(m)

This returns the following error:

> dm <- derivatives(m)
Error in `str_detect()`:
! Can't recycle `string` (size 2) to match `pattern` (size 3).

I think the issue comes from stringr::str_detect(smooth_vars, m_vars) within the helper derivatives_data().

@gavinsimpson
Copy link
Owner

@mahonmb Confirmed in my latest GitHub version. Will start a new issue for this.

@gavinsimpson
Copy link
Owner

Thanks for the report @mahonmb; this is now fixed in the GH version

@mahonmb
Copy link

mahonmb commented Jul 28, 2024

Thanks, @gavinsimpson! Looking forward to using it.

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

No branches or pull requests

3 participants