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 · 3 comments
Closed
Assignees
Labels
bug

Comments

@tomand-uio
Copy link

@tomand-uio 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

@gavinsimpson gavinsimpson commented Dec 6, 2019

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 label Dec 6, 2019
gavinsimpson added a commit that referenced this issue Dec 6, 2019
@gavinsimpson
Copy link
Owner

@gavinsimpson gavinsimpson commented Dec 6, 2019

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 tomand-uio commented Dec 7, 2019

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

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.