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

derivatives can't handle by variable smooths #47

Closed
bart-larsen opened this issue Sep 6, 2019 · 3 comments
Closed

derivatives can't handle by variable smooths #47

bart-larsen opened this issue Sep 6, 2019 · 3 comments
Assignees
Labels

Comments

@bart-larsen
Copy link

@bart-larsen bart-larsen commented Sep 6, 2019

I really like the derivatives function, but I have been running into this issue.
The issue occurs whether I use ordered or unordered factors in the by-factor smooths. I have been mostly testing this with gamm, but it seems to not work for gam either.
For example:

modobj <- gamm(y ~ s(age,by = sex),random=list(ID=~1)
df <- derivatives(modobj,'age)

This returns the derivative for the first level of the by factor smooth, but returns all zeros for the derivative and standard error for the next level.

If I run this in the ordered factor case to test for interactions,
modobj <- gamm(y ~ s(age) +oSex + s(age,by = oSex),random=list(ID=~1)
df <- derivatives(modobj,'age)

It returns the derivative for the reference smooth (s(age)), but all zeros for the derivative and standard error of the by factor smooth.

@gavinsimpson
Copy link
Owner

@gavinsimpson gavinsimpson commented Sep 12, 2019

Here's a reproducible example using gam()

set.seed(1)
dat <- gamSim(4, n = 400, verbose = FALSE)
m <- gam(y ~ fac + s(x2, by = fac), data = dat)
d <- derivatives(m)
draw(d) # clearly shows the problem

and another one for testing

m2 <- gam(y ~ x1 + s(x2) + fac + s(x0, by = fac), data = dat)

and another one for testing, this time ordered factor

dat <- transform(dat, ofac = ordered(fac))
m3 <- gam(y ~ x1 + s(x2) + ofac + s(x0) + s(x0, by = ofac), data = dat)
@gavinsimpson
Copy link
Owner

@gavinsimpson gavinsimpson commented Sep 12, 2019

An immediate issue is that derivative_data() is only returning data for the first level of the factor. This needs fixing.

@gavinsimpson
Copy link
Owner

@gavinsimpson gavinsimpson commented Sep 13, 2019

This is now working for both ordered and ordinary factor by smooths for gam() and gamm() models.

Thanks for the report.

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.