You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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
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:
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..
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:
This plot looks great. Note that the red line is the "truth", and the black dots are the observed points:
However, when we change the basis...
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: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
The text was updated successfully, but these errors were encountered: