File: predict.R

package info (click to toggle)
r-cran-splines2 0.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,512 kB
  • sloc: cpp: 1,988; ansic: 165; sh: 13; makefile: 2
file content (145 lines) | stat: -rw-r--r-- 4,501 bytes parent folder | download
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
##
## R package splines2 by Wenjie Wang and Jun Yan
## Copyright (C) 2016-2021
##
## This file is part of the R package splines2.
##
## The R package splines2 is free software: You can redistribute it and/or
## modify it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or any later
## version (at your option). See the GNU General Public License at
## <https://www.gnu.org/licenses/> for details.
##
## The R package splines2 is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
##

##' Evaluate a Spline Basis at specified points
##'
##' This function evaluates a predefined spline basis at a (new) given \code{x}.
##'
##' These are methods for the generic function \code{predict} for objects
##' inheriting from class \code{bSpline2}, \code{ibs}, \code{mSpline},
##' \code{iSpline}, \code{cSpline}, or \code{bernsteinPoly}.  If \code{newx} is
##' not given, the function returns the input object.
##'
##' @name predict
##' @param object Objects of class \code{bSpline2}, \code{ibs}, \code{mSpline},
##'     \code{iSpline}, \code{cSpline}, \code{bernsteinPoly} or
##'     \code{naturalSpline} with attributes describing \code{knots},
##'     \code{degree}, etc.
##' @param newx The \code{x} values at which evaluations are required.
##' @param ... Optional argument that are not used.
##'
##' @return
##' An object just like the \code{object} input, except evaluated at
##' the new values of \code{x}.
##'
##' @example inst/examples/ex-predict.R
##'
##' @importFrom stats predict
NULL


##' @rdname predict
##' @export
predict.bSpline2 <- function(object, newx, ...)
{
    if (missing(newx))
        return(object)
    ## checks if key attributes still exist
    check_attr(object, c("x", "degree", "knots", "Boundary.knots", "intercept"))
    do.call(bSpline, c(list(x = newx), pred_attr(object)))
}


##' @rdname predict
##' @export
predict.dbs <- function(object, newx, ...)
{
    if (missing(newx))
        return(object)
    ## checks if key attributes still exist
    ## checks if key attributes still exist
    check_attr(object, c("x", "degree", "derivs",
                         "knots", "Boundary.knots", "intercept"))
    do.call(dbs, c(list(x = newx), pred_attr(object)))
}


##' @rdname predict
##' @export
predict.ibs <- function(object, newx, ...)
{
    if (missing(newx))
        return(object)
    ## checks if key attributes still exist
    check_attr(object, c("x", "degree", "knots", "Boundary.knots", "intercept"))
    do.call(ibs, c(list(x = newx), pred_attr(object)))
}


##' @rdname predict
##' @export
predict.mSpline <- function(object, newx, ...)
{
    if (missing(newx))
        return(object)
    ## checks if key attributes still exist
    check_attr(object, c("x", "degree", "derivs", "integral", "periodic",
                         "knots", "Boundary.knots", "intercept"))
    do.call(mSpline, c(list(x = newx), pred_attr(object)))
}


##' @rdname predict
##' @export
predict.iSpline <- function(object, newx, ...)
{
    if (missing(newx))
        return(object)
    ## checks if key attributes still exist
    check_attr(object, c("x", "degree", "derivs",
                         "knots", "Boundary.knots", "intercept"))
    do.call(iSpline, c(list(x = newx), pred_attr(object)))
}


##' @rdname predict
##' @export
predict.cSpline <- function(object, newx, ...)
{
    if (missing(newx))
        return(object)
    ## checks if key attributes still exist
    check_attr(object, c("x", "degree", "derivs",
                         "knots", "Boundary.knots", "intercept"))
    do.call(cSpline, c(list(x = newx), pred_attr(object)))
}


##' @rdname predict
##' @export
predict.bernsteinPoly <- function(object, newx, ...)
{
    if (missing(newx))
        return(object)
    ## checks if key attributes still exist
    check_attr(object, c("x", "degree", "derivs", "integral",
                         "Boundary.knots", "intercept"))
    do.call(bernsteinPoly, c(list(x = newx), pred_attr(object)))
}


##' @rdname predict
##' @export
predict.naturalSpline <- function(object, newx, ...)
{
    if (missing(newx))
        return(object)
    ## checks if key attributes still exist
    check_attr(object, c("x", "derivs", "integral",
                         "knots", "Boundary.knots", "intercept"))
    do.call(naturalSpline, c(list(x = newx), pred_attr(object)))
}