Last data update: 2014.03.03

R: Monotonic Spline
makesplineR Documentation

Monotonic Spline

Description

Fits a monotonic cubic spline to the data provided, using the penalized constrained least squares method from the mgcv package.

Usage

makespline(x, y, newX=NULL, nKnots = 6, lower_bound = 10^-3)

Arguments

x

The predictor variable.

y

The response variable. Must be the same length as x.

newX

The points at which to return the value on the fitted spline. If not specified x is used.

nKnots

The number of knots to use in fitting the spline.

lower_bound

The spline cannot drop below this value.

Details

This uses the pcls function from the mgcv package to produce the fit. The montonicity constraint is enforced using mono.con from the same package. The lower_bound argument is only used on the rare occasions when the fitting function becomes negative or arbitrarily close to zero. If this does occur lower_bound is added everywhere to ensure that no one length is given essentially infinite weighting.

Value

Returns a vector of values containing the value of the fit at each point newX.

Author(s)

Matthew D. Young myoung@wehi.edu.au.

References

Package mgcv. In particular this function is a modification of an example given in the man page for pcls.

Examples


y <- c( rbinom(50,p=0.4,size=1), rbinom(50,p=0.6,size=1) )
x <- 1:100
plot(x,y)
p <- makespline(x,y)
lines(x,p)

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(goseq)
Loading required package: BiasedUrn
Loading required package: geneLenDataBase

> png(filename="/home/ddbj/snapshot/RGM3/R_BC/result/goseq/makespline.Rd_%03d_medium.png", width=480, height=480)
> ### Name: makespline
> ### Title: Monotonic Spline
> ### Aliases: makespline
> 
> ### ** Examples
> 
> 
> y <- c( rbinom(50,p=0.4,size=1), rbinom(50,p=0.6,size=1) )
> x <- 1:100
> plot(x,y)
> p <- makespline(x,y)
Warning message:
In pcls(G) : initial point very close to some inequality constraints
> lines(x,p)
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>