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

Support mgcv::gam and scam models? #37

Open
tomwenseleers opened this issue Mar 27, 2018 · 6 comments
Open

Support mgcv::gam and scam models? #37

tomwenseleers opened this issue Mar 27, 2018 · 6 comments

Comments

@tomwenseleers
Copy link

tomwenseleers commented Mar 27, 2018

Would it be possible by any chance for investr to also support scam (https://cran.r-project.org/web/packages/scam/index.html) and mgcv::gam (https://cran.r-project.org/web/packages/mgcv/index.html) models?
I noticed that a constrained monotone scam spline works very nicely for me to do calibration curves, but then of course I would also need SEs on the inverse predictions.

Maybe relevant for calculating confidence intervals:
https://stats.stackexchange.com/questions/33327/confidence-interval-for-gam-model/33328

For prediction intervals for GAMs (scam inherits from gam so recipe for both should be the same) I found this on public threads:
https://stat.ethz.ch/pipermail/r-help/2011-April/275632.html
https://stackoverflow.com/questions/18909234/coding-a-prediction-interval-from-a-generalized-additive-model-with-a-very-large

@bgreenwell
Copy link
Owner

bgreenwell commented Mar 27, 2018

@tomwenseleers I actually wrote some code for someone else a little while back for SCAMs. I'll try to find it and send to you so that you can get started and then hopefully push an update to the package in the next couple weeks. Initially, many years ago, it was my intention to extend the package to support semiparametric models with monotonicity constraints, so this is good motivation to get it done!

@tomwenseleers
Copy link
Author

tomwenseleers commented Mar 27, 2018

Ha that's great - yes please send it over (you can send it to [email protected])! My scam models also use the weights argument btw to take into account unequal variances...

@tomwenseleers
Copy link
Author

Just realised that the scam::predict and mgcv:predict functions also return the SE on the predictions based on the posterior distribution of the parameters if you set se.fit=TRUE - maybe the easiest starting point to calculate confidence intervals?

@jphill01
Copy link

jphill01 commented Apr 4, 2018

@tomwenseleers I believe the person @bgreenwell speaks of is me. We've been corresponding on my interest in employing inverse estimation for GAMs/SCAMs, though it hasn't been without challenges.

investr employs the bisection method in computing the inverse estimate. For my problem, bisection doesn't appear to suffice, due to issue in computing the inverse estimate along with confidence intervals. So, I have decided to use bootstrapping along with GAM/SCAM code for inverse estimation in order to compute estimates of bias and standard error. But, bootstrapping semiparametric models is VERY slow in my case without parallelization. A promising alternative from the literature (according to Simon Wood, the creator of mgcv) seems to be posterior simulation from a Multivariate Normal distribution.

@tomwenseleers
Copy link
Author

tomwenseleers commented Apr 4, 2018

Was wondering if the inbuilt scam or gam se.fit=TRUE in predict together with using the bisection method for the inverse estimate wouldn't be good enough though?
Something like

model.fit = some scam or gam model
preds = data.frame(x = x, predict(model.fit, newdata=list(x = x), 
                                         se=TRUE, weights = weights))
  k <- 2*qt(p=0.95, df=model.fit$df.residual, lower.tail = TRUE) 
  confint <- k*preds$se.fit
  k <- qt(p=0.975, df=model.fit$df.residual)
  predint <- k*sqrt(preds$se.fit^2+weights*model.fit$sig2) 
  preds <- data.frame(x = preds$x, 
                     fit=preds$fit, 
                     confint.lwr=preds$fit-confint, 
                     confint.upr=preds$fit+confint,
                     predint.lwr=preds$fit-predint,
                     predint.upr=preds$fit+predint)

And then just use the bisection method for the inverse estimation?

@bgreenwell
Copy link
Owner

bgreenwell commented Apr 4, 2018

For scams, the bisection method will work just fine since it forces the fitted mean response to be either monotonically increasing or decreasing. The issue with the bisection method comes when you are trying to bootstrap or the fit is not monotonic over the predictor range of interest (same issue with asymptotes as well). I'll put something together soon!

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

No branches or pull requests

3 participants