PPolyRep | R Documentation |
This function converts a univariate GeDS fit from its B-spline representation to a piecewise polynomial form.
PPolyRep(object, n = 3)
object |
The |
n |
Integer value (2, 3 or 4) specifying the order ( |
This function converts a selected GeDS fit—stored as an object of class
"GeDS"
and represented using B-splines—into an equivalent representation
using piecewise polynomials.
It wraps the function polySpline
, enabling it to handle
"GeDS"
objects as input. This provides a convenient bridge between the
GeDS and splines packages, allowing users to leverage the
functionality available in splines.
An object that inherits from classes "spline"
and
"polySpline"
. It is a list whose arguments are:
a vector of size k + 2
containing the complete set of
knots (internal knots plus the limits of the interval) of the GeDS fit.
a (k + 2) \times n
matrix containing the
coefficients of the polynomials in the required piecewise polynomial
representation.
Let us note that the first k+1
rows of the matrix contain the
n
coefficients of the k+1
consecutive pieces of the piecewise
polynomial representation. The last (k+2)
-th row is extraneous and it
appears as a result of the use of the function
polySpline
.
# Generate a data sample for the response variable
# Y and the single covariate X
set.seed(123)
N <- 500
f_1 <- function(x) (10*x/(1+100*x^2))*4+4
X <- sort(runif(N, min = -2, max = 2))
# Specify a model for the mean of Y to include only
# a component non-linear in X, defined by the function f_1
means <- f_1(X)
# Add (Normal) noise to the mean of Y
Y <- rnorm(N, means, sd = 0.1)
# Fit a Normal GeDS regression using NGeDS
Gmod <- NGeDS(Y ~ f(X), beta = 0.6, phi = 0.995, Xextr = c(-2,2))
# construct the PP representation of the cubic GeDS fit
# and apply some functions of the package splines
Polymod <- PPolyRep(Gmod, 4)
require(splines)
class(Polymod)
splineKnots(Polymod)
knots(Gmod, n = 4)
plot(Polymod)
# Generate a plot showing the PP representation
# based on the same example
knt <- splineKnots(Polymod)
coeffs <- coef(Polymod)
plot(Gmod, n = 4, legend.pos = FALSE, main = "Cubic Curves")
cols <- sample(heat.colors(length(knt)), length(knt))
for(i in 1:(length(knt))){
curve(coeffs[i,1] + coeffs[i,2]*(x - knt[i])+
coeffs[i,3]*(x - knt[i])^2+
coeffs[i,4]*(x - knt[i])^3,
add = TRUE, col = cols[i])
abline(v = knt[i])
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.