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

posterior_linpred() does not use newdata for stann_gamm4 models #132

Closed
noamross opened this issue Nov 21, 2016 · 4 comments
Closed

posterior_linpred() does not use newdata for stann_gamm4 models #132

noamross opened this issue Nov 21, 2016 · 4 comments
Labels

Comments

@noamross
Copy link

Summary:

posterior_linpred() does not seem to use newdata for stann_gamm4 models

Description:

When I pass a new data frame to posterior_linpred() along with a model fit with stan_gamm4, the result is identical to the case where I don't pass any new data at all.

Reproducible Steps:

library(rstanarm)
library(tidyverse)
gtemp <- read.delim("http://www.metoffice.gov.uk/hadobs/hadcrut4/data/current/time_series/HadCRUT.4.5.0.0.annual_ns_avg.txt",
                    sep="", header = FALSE) %>%
  select(1,2) %>%
  set_names(c("Year", "Temperature")) %>%
  mutate(Year = as.double(Year))

st_mod2 <- stan_gamm4(Temperature ~ s(Year), data = gtemp)
preds2 <- posterior_linpred(st_mod2)
preds2_d <- posterior_linpred(st_mod2, newdata=data.frame(Year=(1850:2016) + 0.1))
identical(preds2, preds2_d)

This results in TRUE

RStanARM Version:

2.13.1 (installed from github)

R Version:

3.3.1

Operating System:

OS X 10.12.1

@noamross noamross changed the title posterior_linpred() does use newdata for stann_gamm4 models posterior_linpred() does use newdata for stann_gamm4 models Nov 21, 2016
@jgabry jgabry changed the title posterior_linpred() does use newdata for stann_gamm4 models posterior_linpred() does not use newdata for stann_gamm4 models Nov 21, 2016
@bgoodri
Copy link
Contributor

bgoodri commented Nov 21, 2016

I believe the issue here has to do with the fact that you are adding a constant (0.1). If you add runif(nrow(gtemp)), then the linear predictors are not identical. I'm not sure that we have the right behavior here, but when mgcv constructs s(Year) it is centered.

@bgoodri
Copy link
Contributor

bgoodri commented Nov 21, 2016

So, I guess we should tack on the original mean(s) multiplied by the coefficients.

@noamross
Copy link
Author

Yeah, I think you need to correct for mgcv's centering if you're reconstructing the gam object when doing this. For my particular application, I need to add a constant in order to estimate the derivatives of the smooths, but in any case we'll want to be able to get predictions on an absolute scale.

@bgoodri
Copy link
Contributor

bgoodri commented Dec 11, 2016

This should be fixed on GitHub now. It goes back to the mgcv-style parameterization, so now Stan is calling predict.gam with type = "lpmatrix".

@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