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

View source: R/uni.smooth.const-with-po.r

smooth.construct.cx.smooth.specR Documentation

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 convex 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.

cxBy.smooth.spec works similar to cx.smooth.spec but without applying an identifiability constraint ('zero intercept' constraint). cxBy.smooth.spec should be used when the smooth term has a numeric by variable that takes more than one value. In such cases, the smooth terms are fully identifiable without a 'zero intercept' constraint, so they are left unconstrained. This smooth is specified as s(x,by=z,bs="cxBy"). See an example below.

However a factor by variable requires identifiability constraints, so s(x,by=fac,bs="cx") is used in this case.

Usage

## S3 method for class 'cx.smooth.spec'
smooth.construct(object, data, knots)
## S3 method for class 'cxBy.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", "cxBy.smooth".

Author(s)

Natalya Pya <nat.pya@gmail.com>

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

 ## Not run: 
 
## Convex SCOP-splines example... 
  ## simulating data...
  require(scam)
  set.seed(16)
  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"),family=gaussian,data=dat)
  ## unconstrained fit...
  b1 <- scam(y~s(x,k=15),family=gaussian, data=dat)
  ## 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...
  set.seed(18)
  y <- rpois(n,exp(f))
  dat <- data.frame(x=x,y=y)
  ## fit shape constrained model ...
  b <- scam(y~s(x,k=15,bs="cx"),family=poisson(link="log"),data=dat,optimizer="efs")
  ## unconstrained fit... 
  b1 <- scam(y~s(x,k=15),family=poisson(link="log"), data=dat,optimizer="efs")
  ## 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 

## 'by' factor example... 
  set.seed(9)
  n <- 400
  x <- sort(runif(n,-.5,.5))
  f1 <- .7*x-cos(x)+3
  f2 <- 20*x^2 
  par(mfrow=c(1,2))
  plot(x,f1,type="l");plot(x,f2,type="l")
  e <- rnorm(n, 0, 1.5)
  fac <- as.factor(sample(1:2,n,replace=TRUE))
  fac.1 <- as.numeric(fac==1)
  fac.2 <- as.numeric(fac==2)
  y <- f1*fac.1 + f2*fac.2 + e 
  dat <- data.frame(y=y,x=x,fac=fac,f1=f1,f2=f2)
  b2 <- scam(y ~ fac+s(x,by=fac,bs="cx"),data=dat,optimizer="efs")  
  plot(b2,pages=1,scale=0)
  summary(b2)
  x11()
  vis.scam(b2,theta=50,color="terrain")

 ## numeric 'by' variable example... 
 set.seed(6)
 n <- 100
 x <- sort(2*runif(n)-1)
 z <- runif(n,-2,3)
 f <- 4*x^2
 y <- f*z + rnorm(n)*.6
 dat <- data.frame(x=x,z=z,y=y)
 b <- scam(y~s(x,k=15,by=z,bs="cxBy"),data=dat)
 summary(b)
 par(mfrow=c(1,2))
 plot(b,shade=TRUE)
 ## unconstrained fit...
 b1 <- scam(y~s(x,k=15,by=z),data=dat)
 plot(b1,shade=TRUE)
 summary(b1)
 
## End(Not run)

scam documentation built on June 22, 2024, 10:43 a.m.