constrained splines

198 views
Skip to first unread message

Andrew Azman

unread,
Feb 19, 2018, 1:56:02 PM2/19/18
to brms-users
Has anyone implemented splines from the Shape Constrained Additive Models (scam) package with brms? I am trying to fit a model and enforce monotonicity in a spline. Any help would be greatly appreciated. 

Paul Buerkner

unread,
Feb 19, 2018, 2:05:20 PM2/19/18
to Andrew Azman, brms-users
I just tested it and surprisingly the model fitting seems to work. But when trying to plot the smooths, an error happens inside mgcv. Here is an example from the scam documentation:

library(scam)

set.seed(0)
n <- 200
x1 <- runif(n)*6-3
f1 <- 3*exp(-x1^2) # unconstrained term
f1 <- (f1-min(f1))/(max(f1)-min(f1)) # function scaled to have range [0,1]
x2 <- runif(n)*4-1;
f2 <- exp(4*x2)/(1+exp(4*x2)) # monotone increasing smooth
f2 <- (f2-min(f2))/(max(f2)-min(f2)) # function scaled to have range [0,1]
f <- f1+f2
y <- f+rnorm(n)*0.1
dat <- data.frame(x1=x1,x2=x2,y=y)
b <- scam(y~s(x1,k=15,bs="cr",m=2)+s(x2,k=25,bs="mpi",m=2),
          family=gaussian(link="identity"),data=dat,not.exp=FALSE)
print(b)
summary(b)
plot(b,pages=1)

library(brms)
fit <- brm(y~s(x1,k=15,bs="cr",m=2)+s(x2,k=25,bs="mpi",m=2), dat)
summary(fit)
marginal_smooths(fit)  # fails

I will report this error the the maintainer of scam. In the hope that he can fix this.

2018-02-19 19:56 GMT+01:00 Andrew Azman <andre...@gmail.com>:
Has anyone implemented splines from the Shape Constrained Additive Models (scam) package with brms? I am trying to fit a model and enforce monotonicity in a spline. Any help would be greatly appreciated. 

--
You received this message because you are subscribed to the Google Groups "brms-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brms-users+unsubscribe@googlegroups.com.
To post to this group, send email to brms-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brms-users/e64af74d-27a5-41ce-9f3d-16b9fcf0f34b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andrew Azman

unread,
Feb 19, 2018, 2:27:27 PM2/19/18
to brms-users
Wow, thanks! Surprised it just worked.
To unsubscribe from this group and stop receiving emails from it, send an email to brms-users+...@googlegroups.com.

Andrew Azman

unread,
Feb 19, 2018, 3:07:31 PM2/19/18
to brms-users
@Paul - It seems like that package has quite a few bugs actually. Any thoughts on enforcing montonicity through a strong prior instead?

Paul Buerkner

unread,
Feb 19, 2018, 3:09:02 PM2/19/18
to Andrew Azman, brms-users
Can you describe your covariate, for which you want to enfore monotonicity?

To unsubscribe from this group and stop receiving emails from it, send an email to brms-users+unsubscribe@googlegroups.com.

To post to this group, send email to brms-...@googlegroups.com.

Andrew Azman

unread,
Feb 19, 2018, 3:20:43 PM2/19/18
to brms-users
Sure, it is describing 'decay' within a biological system ranging from Inf to 0. 

Paul Buerkner

unread,
Feb 19, 2018, 3:35:02 PM2/19/18
to Andrew Azman, brms-users
If your covariate can only take on not too many discrete values, you could use monotonic effects in brms (see vignette("brms_monotonic")), but your variable does rather look very much continuous.

To unsubscribe from this group and stop receiving emails from it, send an email to brms-users+unsubscribe@googlegroups.com.

To post to this group, send email to brms-...@googlegroups.com.

Andrew Azman

unread,
Feb 19, 2018, 4:08:50 PM2/19/18
to Paul Buerkner, brms-users

I had seen that vignette but its more of a continuous variable.


andrew azman

Paul Buerkner

unread,
Mar 6, 2018, 3:30:45 PM3/6/18
to brms-users
After some email exchange with the author of the scam package, she sent me an updated version of one of the scam methods, which after being load into the global environment, will fix this problem. Just run the attached R script,
and post-processing of mpi splines should work.

Unfortunately, this updated version is unlikely to make it into the CRAN version of scam, since, for some reason, it interferes with the way post-processing is done in scam itself.

I hope this still helps and thanks to Natalya Pya for the fix!
predict-mpi.r
Reply all
Reply to author
Forward
0 new messages