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

Does stan_gamm4 work with a basis other than the default? #136

Closed
jgellar opened this issue Dec 1, 2016 · 2 comments
Closed

Does stan_gamm4 work with a basis other than the default? #136

jgellar opened this issue Dec 1, 2016 · 2 comments
Labels

Comments

@jgellar
Copy link

jgellar commented Dec 1, 2016

Summary:

Weird-looking results when using stan_gamm4 with a non-default basis.

Description:

I have recently discovered the stan_gamm4 function to allow for GAMs. I'd like to fit some models with different bases, however I've tried some that give weird results. See below.

Reproducible Steps:

library(dplyr)
library(tidyr)
library(ggplot2)
library(rstanarm)
options(mc.cores = parallel::detectCores())

# Simulate some data
N <- 1000
dat <- data.frame(x = rnorm(N)) %>%
  mutate(y = sin(pi*x) + rnorm(N, sd = .5))
x0 <- seq(min(dat$x), max(dat$x), by = .1)

# Fit a model with default basis
fit1 <- stan_gamm4(y ~ s(x), data = dat)
plot1 <- plot_nonlinear(fit1)
plot1 +
  geom_point(aes(x,y), data = dat, inherit.aes = FALSE, alpha = .25, size = .25) +
  geom_line(aes(x0, y0), data = data.frame(x0 = x0, y0 = sin(pi*x0)),
            inherit.aes = FALSE, colour = "red")

This plot looks great. Note that the red line is the "truth", and the black dots are the observed points:
image

However, when we change the basis...

# Fit a model with a p-spline basis of order 8
fit2 <- stan_gamm4(y ~ s(x, bs = "ps", k = 8), data = dat)
plot2 <- plot_nonlinear(fit2)
plot2

image

Now this doesn't look right. In particular, what's with the range of x values (from about -0.2 to 0.4), which is far tighter than the range of simulated x values (closer to -2 to 2)? I don't think this is just a problem with plot_nonlinear(), because the following also gives a weird result:

yhat <- posterior_predict(fit_rsag4_2, newdata = data.frame(x = x0)) %>% colMeans()
plot(x0, yhat, type = "l")

image

Here, the estimate isn't even smooth... it's really bumpy. What's going on?

RStanARM Version:

2.13.1

R Version:

MRO 3.3.1

Operating System:

Windows 10

@bgoodri
Copy link
Contributor

bgoodri commented Dec 2, 2016

We don't change the basis (at least not on purpose) but there is a bug in the prediction code with newdata following stan_gamm4: #132 . Basically, we are not accounting for the centering and scaling done by the mgcv package..

@jgabry jgabry added the question label Dec 5, 2016
@bgoodri
Copy link
Contributor

bgoodri commented Dec 11, 2016

The underlying bug was fixed, so this should be fine now. Please install the master branch from GitHub.

@bgoodri bgoodri closed this as completed Dec 11, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants