Learn R Programming

gamlss.add (version 5.1-13)

fitFixedKnots: Functions to Fit Univariate Break Point Regression Models

Description

There are two main functions here. The functions fitFixedKnots allows the fit a univariate regression using piecewise polynomials with "known" break points while the function fitFreeKnots estimates the break points.

Usage

fitFixedKnots(y, x, weights = NULL, knots = NULL, data = NULL, degree = 3, 
             fixed = NULL, base=c("trun","Bbase"), ...)
fitFreeKnots(y, x, weights = NULL, knots = NULL, degree = 3, fixed =
                 NULL, trace = 0, data = NULL, base=c("trun","Bbase"), ...)

Value

The functions fitFixedKnots and fitFreeKnots return an object FixBreakPointsReg and FreeBreakPointsReg respectively with the following items:

fitted.values

the fitted values of the model

residuals

the residuals of the model

df

the degrees of freedom fitted in the model

rss

the residuals sum of squares

knots

the knots used in creating the beta-function base

fixed

the fixed break points if any

breakPoints

the interior (estimated) break points (or knots)

coef

the coefficients of the linear part of the model

degree

the degree of the piecewise polynomial

y

the y variable

x

the x variable

w

the prior weights

Arguments

x

the x variable (explanatory)

y

the response variable

weights

the prior weights

knots

the position of the interior knots for fitFixedKnots or starting values for fitFreeKnots

data

the data frame

degree

the degree if the piecewise polynomials

fixed

this is to be able to fit fixed break points

base

The basis for the piecewise polynomials, turn for truncated (default) and Bbase for B-base piecewise polynomials

trace

controlling the trace of of optim()

...

for extra arguments

Author

Mikis Stasinopoulos d.stasinopoulos@londonmet.ac.uk

Details

The functions fitFreeKnots() is loosely based on the curfit.free.knot() function of package DierckxSpline of Sundar Dorai-Raj and Spencer Graves.

References

Dierckx, P. (1991) Curve and Surface Fitting with Splines, Oxford Science Publications

Rigby, R. A. and Stasinopoulos D. M. (2005). Generalized additive models for location, scale and shape,(with discussion), Appl. Statist., 54, part 3, pp 507-554.

Rigby R.A., Stasinopoulos D. M., Heller G., and De Bastiani F., (2019) Distributions for Modeling Location, Scale and Shape: Using GAMLSS in R, Chapman and Hall/CRC.

Stasinopoulos D. M. Rigby R.A. (2007) Generalized additive models for location scale and shape (GAMLSS) in R. Journal of Statistical Software, 23(7), 1--46, tools:::Rd_expr_doi("10.18637/jss.v023.i07")

Stasinopoulos D. M., Rigby R.A., Heller G., Voudouris V., and De Bastiani F., (2017) Flexible Regression and Smoothing: Using GAMLSS in R, Chapman and Hall/CRC.

(see also https://www.gamlss.com/).

Examples

Run this code
# creating  a linear + linear function
   x <- seq(0,10, length.out=201)
knot <- 5
 set.seed(12543)
 mu <- ifelse(x<=knot,5+0.5*x,5+0.5*x+(x-knot))
  y <- rNO(201, mu=mu, sigma=.5)
# plot the data
 plot(y~x, xlim=c(-1,13), ylim=c(3,18))

# fit model using fixed break points
 m1 <- fitFixedKnots(y, x, knots=5, degree=1)
knots(m1)
lines(fitted(m1)~x, col="red")

# now estimating the knot
m2 <- fitFreeKnots(y, x, knots=5, degree=1)
knots(m2)
summary(m2)

# now predicting 
plot(y~x, xlim=c(-5,13), ylim=c(3,18))
lines(fitted(m2)~x, col="green", lwd=3)
points(-2:13,predict(m2, newdata=-2:13), col="red",pch = 21, bg="blue")
points(-2:13,predict(m2, newdata=-2:13, old.x.range=FALSE), col="red",pch = 21, bg="grey")

# fit different basis 
m21 <- fitFreeKnots(y, x, knots=5, degree=1, base="Bbase")
deviance(m2) 
deviance(m21) # should be identical

# predicting with m21 
 plot(y~x, xlim=c(-5,13), ylim=c(3,18))
lines(fitted(m21)~x, col="green", lwd=3)
points(-2:13,predict(m21, newdata=-2:13), col="red",pch = 21, bg="blue")
points(-2:13,predict(m21, newdata=-2:13, old.x.range=FALSE), col="red",pch = 21, bg="grey")

Run the code above in your browser using DataLab