rconstraintsgam

How to add a monotonic constraint in a GAM model


I use the mgcv package on R, and I have a model like this :

gamm_model <- gam(Y ~ s(X, bs = "ps") + B + C +  s(D, bs = 're') + s(E, bs = 're'),  data = df)
summary(gamm_model)

I would like to add a positive monotonic constraint on X. Is this possible, and if so, how?

I saw that there was a mono.con() function but I don't understand how to use it.

Thanks


Solution

  • It might be easier to use the scam package, although it doesn't have as much downstream support as mgcv: but e.g. gratia::draw() does appear to work OK ...

    data("sleepstudy", package = "lme4")
    library(scam)
    model <- scam(Reaction ~ s(Days, bs = "mpi") + s(Subject, bs = "re"),
       data = sleepstudy)
    

    (Not a great fit, but it shows it can be done ...)

    From ?smooth.construct.mpi.smooth.spec:

    This is a special method function for creating smooths subject to a monotone increasing constraint which is built by the ‘mgcv’ constructor function for smooth terms, ‘smooth.construct’. It is constructed using monotonic P-splines.