scam (version 1.2-5)

smooth.construct.cx.smooth.spec: Constructor for convex P-splines in SCAMs

Description

This is a special method function for creating smooths subject to convexity constraint which is built by the mgcv constructor function for smooth terms, smooth.construct. It is constructed using the mixed constrained P-splines. This smooth is specified via model terms such as s(x,k,bs="cx",m=2), where k denotes the basis dimension and m+1 is the order of the B-spline basis.

Usage

# S3 method for cx.smooth.spec
smooth.construct(object, data, knots)

Arguments

object

A smooth specification object, generated by an s term in a GAM formula.

data

A data frame or list containing the data required by this term, with names given by object$term. The by variable is the last element.

knots

An optional list containing the knots supplied for basis setup. If it is NULL then the knot locations are generated automatically.

Value

An object of class "cx.smooth".

References

Pya, N. and Wood, S.N. (2015) Shape constrained additive models. Statistics and Computing, 25(3), 543-559

Pya, N. (2010) Additive models with shape constraints. PhD thesis. University of Bath. Department of Mathematical Sciences

See Also

smooth.construct.cv.smooth.spec, smooth.construct.mpi.smooth.spec, smooth.construct.mdcv.smooth.spec, smooth.construct.mdcx.smooth.spec, smooth.construct.micv.smooth.spec, smooth.construct.mpd.smooth.spec

Examples

Run this code
# NOT RUN {
 
# }
# NOT RUN {
 
## Convex P-splines example 
  ## simulating data...
   set.seed(1)
   n <- 100
   x <- sort(2*runif(n)-1)
   f <- 4*x^2
   y <- f + rnorm(n)*0.4
   dat <- data.frame(x=x,y=y)
   b <- scam(y~s(x,k=15,bs="cx",m=2),family=gaussian,data=dat,not.exp=FALSE)
   # UNCONSTRAINED FIT *****************
   b1 <- scam(y~s(x,k=15,bs="cr",m=2),family=gaussian, data=dat,not.exp=FALSE)

## plot results ...
   plot(x,y,xlab="x",ylab="y")
   lines(x,f)      ## the true function
   lines(x,b$fitted,col=2)  ## constrained fit 
   lines(x,b1$fitted,col=3) ## unconstrained fit 

## Poisson version...
   y <- rpois(n,exp(f))
   dat <- data.frame(x=x,y=y)
 ## fit model ...
   b <- scam(y~s(x,k=15,bs="cx",m=2),family=poisson(link="log"),data=dat,not.exp=FALSE)

# UNCONSTRAINED FIT *****************
   b1 <- scam(y~s(x,k=15,bs="cr",m=2),family=poisson(link="log"), data=dat,not.exp=FALSE)

## plot results ...
   plot(x,y,xlab="x",ylab="y")
   lines(x,exp(f))      ## the true function
   lines(x,b$fitted,col=2)  ## constrained fit 
   lines(x,b1$fitted,col=3) ## unconstrained fit 
 
 
# }

Run the code above in your browser using DataLab