fitFixedKnots: Functions to Fit Univariate Break Point Regression Models

Description Usage Arguments Details Value Note Author(s) References Examples

View source: R/freeKnots-18-06-13.R

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

1
2
3
4
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"), ...)

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

Details

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

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

Note

The prediction function in piecewise polynomials using the B-spline basis is tricky because by adding the newdata for x to the current one the B-basis function for the piecewise polynomials changes. This does not seems to be the case with the truncated basis, that is, when the option base="turn" is used (see the example below).

If the newdata are outside the range of the old x then there could a considerable discrepancies between the all fitted values and the predicted ones if the option base="Bbase" is used. The prediction function for the objects FixBreakPointsReg or FreeBreakPointsReg has the option old.x.range=TRUE which allow the user two choices:

The first is to use the old end-points for the creation of the new B-basis which were determine from the original range of x. This choice is implemented as a default in the predict method for FixBreakPointsReg and FreeBreakPointsReg objects with the argument old.x.range=TRUE.

The second is to create new end-points from the new and old data x values. In this case the range of x will be bigger that the original one if the newdata has values outside the original x range. In this case (old.x.range=FALSE) the prediction could be possible better outside the x range but would not coincide with the original predictions i.e. fitted(model) since basis have changed.

Author(s)

Mikis Stasinopoulos d.stasinopoulos@londonmet.ac.uk

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, Vol. 23, Issue 7, Dec 2007, http://www.jstatsoft.org/v23/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 http://www.gamlss.com/).

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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")

Example output

Loading required package: gamlss.dist
Loading required package: MASS
Loading required package: gamlss
Loading required package: splines
Loading required package: gamlss.data
Loading required package: nlme
Loading required package: parallel
 **********   GAMLSS Version 5.1-2  ********** 
For more on GAMLSS look at http://www.gamlss.org/
Type gamlssNews() to see new features/changes/bug fixes.

Loading required package: mgcv
This is mgcv 1.8-25. For overview type 'help("mgcv-package")'.
Loading required package: nnet

Attaching package: 'nnet'

The following object is masked from 'package:mgcv':

    multinom

Loading required package: rpart
BP1 
  5 
     BP1 
5.017862 

Call:
fitFreeKnots(y, x, knots = 5, degree = 1)

             Estimate  Std. Error   t value Pr(>|t|)
(Intercept) 4.9815025 0.044945239 110.83493        0
x           0.4942054 0.015530709  31.82118        0
XatBP1      1.0071104 0.022129506  45.50984        0
BP1         5.0178625 0.063745330  78.71734        0
sigma       0.2275262 0.004597433  49.48984        0
[1] -297.325
[1] -297.325
Warning message:
In predict.FixBreakPointsReg(m21, newdata = -2:13) :
  There is a discrepancy  between the original prediction and the re-fit 
 used to achieve 'safe' predictions 
 
Warning message:
In predict.FixBreakPointsReg(m21, newdata = -2:13, old.x.range = FALSE) :
  There is a discrepancy  between the original prediction and the re-fit 
 used to achieve 'safe' predictions 
 

gamlss.add documentation built on Feb. 4, 2020, 9:08 a.m.