Title: | Routines for Logspline Density Estimation |
---|---|
Description: | Contains routines for logspline density estimation. The function oldlogspline() uses the same algorithm as the logspline package version 1.0.x; i.e. the Kooperberg and Stone (1992) algorithm (with an improved interface). The recommended routine logspline() uses an algorithm from Stone et al (1997) <DOI:10.1214/aos/1031594728>. |
Authors: | Charles Kooperberg [aut, cre], Cleve Moler [ctb] (LINPACK routines in src), Jack Dongarra [ctb] (LINPACK routines in src) |
Maintainer: | Charles Kooperberg <[email protected]> |
License: | Apache License 2.0 |
Version: | 2.1.22 |
Built: | 2024-12-07 06:26:56 UTC |
Source: | CRAN |
Density (dlogspline
), cumulative probability (plogspline
), quantiles
(qlogspline
), and random samples (rlogspline
) from
a logspline density that was fitted using
the 1997 knot addition and deletion algorithm (logspline
).
The 1992 algorithm is available using the oldlogspline
function.
dlogspline(q, fit, log = FALSE) plogspline(q, fit) qlogspline(p, fit) rlogspline(n, fit)
dlogspline(q, fit, log = FALSE) plogspline(q, fit) qlogspline(p, fit) rlogspline(n, fit)
q |
vector of quantiles. Missing values (NAs) are allowed. |
p |
vector of probabilities. Missing values (NAs) are allowed. |
n |
sample size. If |
fit |
|
log |
should dlogspline return densities (TRUE) or log-densities (FALSE) |
Elements of q
or p
that are missing will cause the
corresponding elements of the result to be missing.
Densities (dlogspline
), probabilities (plogspline
), quantiles (qlogspline
),
or a random sample (rlogspline
) from a logspline
density that was fitted using
knot addition and deletion.
Charles Kooperberg [email protected].
Charles Kooperberg and Charles J. Stone. Logspline density estimation for censored data (1992). Journal of Computational and Graphical Statistics, 1, 301–328.
Charles J. Stone, Mark Hansen, Charles Kooperberg, and Young K. Truong. The use of polynomial splines and their tensor products in extended linear modeling (with discussion) (1997). Annals of Statistics, 25, 1371–1470.
logspline
,
plot.logspline
,
summary.logspline
,
oldlogspline
.
x <- rnorm(100) fit <- logspline(x) qq <- qlogspline((1:99)/100, fit) plot(qnorm((1:99)/100), qq) # qq plot of the fitted density pp <- plogspline((-250:250)/100, fit) plot((-250:250)/100, pp, type = "l") lines((-250:250)/100,pnorm((-250:250)/100)) # asses the fit of the distribution dd <- dlogspline((-250:250)/100, fit) plot((-250:250)/100, dd, type = "l") lines((-250:250)/100, dnorm((-250:250)/100)) # asses the fit of the density rr <- rlogspline(100, fit) # random sample from fit
x <- rnorm(100) fit <- logspline(x) qq <- qlogspline((1:99)/100, fit) plot(qnorm((1:99)/100), qq) # qq plot of the fitted density pp <- plogspline((-250:250)/100, fit) plot((-250:250)/100, pp, type = "l") lines((-250:250)/100,pnorm((-250:250)/100)) # asses the fit of the distribution dd <- dlogspline((-250:250)/100, fit) plot((-250:250)/100, dd, type = "l") lines((-250:250)/100, dnorm((-250:250)/100)) # asses the fit of the density rr <- rlogspline(100, fit) # random sample from fit
Probability density function (doldlogspline
), distribution
function (poldlogspline
), quantiles
(qoldlogspline
), and random samples (roldlogspline
) from
a logspline density that was fitted using
the 1992 knot deletion algorithm (oldlogspline
).
The 1997 algorithm using knot
deletion and addition is available using the logspline
function.
doldlogspline(q, fit) poldlogspline(q, fit) qoldlogspline(p, fit) roldlogspline(n, fit)
doldlogspline(q, fit) poldlogspline(q, fit) qoldlogspline(p, fit) roldlogspline(n, fit)
q |
vector of quantiles. Missing values (NAs) are allowed. |
p |
vector of probabilities. Missing values (NAs) are allowed. |
n |
sample size. If |
fit |
|
Elements of q
or p
that are missing will cause the
corresponding elements of the result to be missing.
Densities (doldlogspline
), probabilities (poldlogspline
), quantiles (qoldlogspline
),
or a random sample (roldlogspline
)
from an oldlogspline
density that was fitted using
knot deletion.
Charles Kooperberg [email protected].
Charles Kooperberg and Charles J. Stone. Logspline density estimation for censored data (1992). Journal of Computational and Graphical Statistics, 1, 301–328.
Charles J. Stone, Mark Hansen, Charles Kooperberg, and Young K. Truong. The use of polynomial splines and their tensor products in extended linear modeling (with discussion) (1997). Annals of Statistics, 25, 1371–1470.
logspline
,
oldlogspline
,
plot.oldlogspline
,
summary.oldlogspline
x <- rnorm(100) fit <- oldlogspline(x) qq <- qoldlogspline((1:99)/100, fit) plot(qnorm((1:99)/100), qq) # qq plot of the fitted density pp <- poldlogspline((-250:250)/100, fit) plot((-250:250)/100, pp, type = "l") lines((-250:250)/100, pnorm((-250:250)/100)) # asses the fit of the distribution dd <- doldlogspline((-250:250)/100, fit) plot((-250:250)/100, dd, type = "l") lines((-250:250)/100, dnorm((-250:250)/100)) # asses the fit of the density rr <- roldlogspline(100, fit) # random sample from fit
x <- rnorm(100) fit <- oldlogspline(x) qq <- qoldlogspline((1:99)/100, fit) plot(qnorm((1:99)/100), qq) # qq plot of the fitted density pp <- poldlogspline((-250:250)/100, fit) plot((-250:250)/100, pp, type = "l") lines((-250:250)/100, pnorm((-250:250)/100)) # asses the fit of the distribution dd <- doldlogspline((-250:250)/100, fit) plot((-250:250)/100, dd, type = "l") lines((-250:250)/100, dnorm((-250:250)/100)) # asses the fit of the density rr <- roldlogspline(100, fit) # random sample from fit
Fits a logspline
density using splines to approximate the log-density
using
the 1997 knot addition and deletion algorithm (logspline
).
The 1992 algorithm is available using the oldlogspline
function.
logspline(x, lbound, ubound, maxknots = 0, knots, nknots = 0, penalty, silent = TRUE, mind = -1, error.action = 2)
logspline(x, lbound, ubound, maxknots = 0, knots, nknots = 0, penalty, silent = TRUE, mind = -1, error.action = 2)
x |
data vector. The data needs to be uncensored. |
lbound , ubound
|
lower/upper bound for the support of the density. For example, if there
is a priori knowledge that the density equals zero to the left of 0,
and has a discontinuity at 0,
the user could specify |
maxknots |
the maximum number of knots. The routine stops adding knots when this number of knots is reached. The method has an automatic rule for selecting maxknots if this parameter is not specified. |
knots |
ordered vector of values (that should cover the complete range of the
observations), which forces the method to start with these knots.
Overrules knots.
If |
nknots |
forces the method to start with |
penalty |
the parameter to be used in the AIC criterion. The method chooses
the number of knots that minimizes
|
silent |
should diagnostic output be printed? |
mind |
minimum distance, in order statistics, between knots. |
error.action |
how should |
Object of the class logspline
, that is intended as input for
plot.logspline
(summary plots),
summary.logspline
(fitting summary),
dlogspline
(densities),
plogspline
(probabilities),
qlogspline
(quantiles),
rlogspline
(random numbers from the fitted distribution).
The object has the following members:
call |
the command that was executed. |
nknots |
the number of knots in the model that was selected. |
coef.pol |
coefficients of the polynomial part of the spline. The first coefficient is the constant term and the second is the linear term. |
coef.kts |
coefficients of the knots part of the spline.
The |
knots |
vector of the locations of the knots in the |
maxknots |
the largest number of knots minus one considered during fitting
(i.e. with |
penalty |
the penalty that was used. |
bound |
first element: 0 - |
samples |
the sample size. |
logl |
matrix with 3 columns. Column one: number of knots; column two: model fitted during addition (1) or deletion (2); column 3: log-likelihood. |
range |
range of the input data. |
mind |
minimum distance in order statistics between knots required during fitting (the actual minimum distance may be much larger). |
Charles Kooperberg [email protected].
Charles Kooperberg and Charles J. Stone. Logspline density estimation for censored data (1992). Journal of Computational and Graphical Statistics, 1, 301–328.
Charles J. Stone, Mark Hansen, Charles Kooperberg, and Young K. Truong. The use of polynomial splines and their tensor products in extended linear modeling (with discussion) (1997). Annals of Statistics, 25, 1371–1470.
plot.logspline
,
summary.logspline
,
dlogspline
,
plogspline
,
qlogspline
, rlogspline
,
oldlogspline,
oldlogspline.to.logspline
.
y <- rnorm(100) fit <- logspline(y) plot(fit) # # as (4 == length(-2, -1, 0, 1, 2) -1), this forces these initial knots, # and does no knot selection fit <- logspline(y, knots = c(-2, -1, 0, 1, 2), maxknots = 4, penalty = 0) # # the following example give one of the rare examples where logspline # crashes, and this shows the use of error.action = 2. # set.seed(118) zz <- rnorm(300) zz[151:300] <- zz[151:300]+5 zz <- round(zz) fit <- logspline(zz) # # you could rerun this with # fit <- logspline(zz, error.action=0) # or # fit <- logspline(zz, error.action=1)
y <- rnorm(100) fit <- logspline(y) plot(fit) # # as (4 == length(-2, -1, 0, 1, 2) -1), this forces these initial knots, # and does no knot selection fit <- logspline(y, knots = c(-2, -1, 0, 1, 2), maxknots = 4, penalty = 0) # # the following example give one of the rare examples where logspline # crashes, and this shows the use of error.action = 2. # set.seed(118) zz <- rnorm(300) zz[151:300] <- zz[151:300]+5 zz <- round(zz) fit <- logspline(zz) # # you could rerun this with # fit <- logspline(zz, error.action=0) # or # fit <- logspline(zz, error.action=1)
Fits a logspline
density using splines to approximate the log-density
using
the 1992 knot deletion algorithm (oldlogspline
).
The 1997 algorithm using knot
deletion and addition is available using the logspline
function.
oldlogspline(uncensored, right, left, interval, lbound, ubound, nknots, knots, penalty, delete = TRUE)
oldlogspline(uncensored, right, left, interval, lbound, ubound, nknots, knots, penalty, delete = TRUE)
uncensored |
vector of uncensored observations from the distribution whose density is
to be estimated. If there are no uncensored observations, this argument can
be omitted. However, either |
right |
vector of right censored observations from the distribution whose density is to be estimated. If there are no right censored observations, this argument can be omitted. |
left |
vector of left censored observations from the distribution whose density is to be estimated. If there are no left censored observations, this argument can be omitted. |
interval |
two column matrix of lower and upper bounds of observations that are interval censored from the distribution whose density is to be estimated. If there are no interval censored observations, this argument can be omitted. |
lbound , ubound
|
lower/upper bound for the support of the density. For example, if there
is a priori knowledge that the density equals zero to the left of 0,
and has a discontinuity at 0,
the user could specify |
nknots |
forces the method to start with nknots knots ( |
knots |
ordered vector of values (that should cover the complete range of the
observations), which forces the method to start with these knots ( |
penalty |
the parameter to be used in the AIC criterion. The method chooses
the number of knots that minimizes |
delete |
should stepwise knot deletion be employed? |
Object of the class oldlogspline
, that is intended as input for
plot.oldlogspline
,
summary.oldlogspline
,
doldlogspline
(densities),
poldlogspline
(probabilities),qoldlogspline
(quantiles),
roldlogspline
(random numbers from the fitted distribution).
The function oldlogspline.to.logspline
can translate an object of the class
oldlogspline
to an object of the class logspline
.
The object has the following members:
call |
the command that was executed. |
knots |
vector of the locations of the knots in the |
coef |
coefficients of the spline. The first coefficient is the constant term,
the second is the linear term and the k-th |
bound |
first element: 0 - |
logl |
the |
penalty |
the penalty that was used. |
sample |
the sample size that was used. |
delete |
was stepwise knot deletion employed? |
Charles Kooperberg [email protected].
Charles Kooperberg and Charles J. Stone. Logspline density estimation for censored data (1992). Journal of Computational and Graphical Statistics, 1, 301–328.
Charles J. Stone, Mark Hansen, Charles Kooperberg, and Young K. Truong. The use of polynomial splines and their tensor products in extended linear modeling (with discussion) (1997). Annals of Statistics, 25, 1371–1470.
logspline
,
oldlogspline
,
plot.oldlogspline
,
summary.oldlogspline
,doldlogspline
,
poldlogspline
,
qoldlogspline
,
roldlogspline
,
oldlogspline.to.logspline
.
# A simple example y <- rnorm(100) fit <- oldlogspline(y) plot(fit) # An example involving censoring and a lower bound y <- rlnorm(1000) censoring <- rexp(1000) * 4 delta <- 1 * (y <= censoring) y[delta == 0] <- censoring[delta == 0] fit <- oldlogspline(y[delta == 1], y[delta == 0], lbound = 0)
# A simple example y <- rnorm(100) fit <- oldlogspline(y) plot(fit) # An example involving censoring and a lower bound y <- rlnorm(1000) censoring <- rexp(1000) * 4 delta <- 1 * (y <= censoring) y[delta == 0] <- censoring[delta == 0] fit <- oldlogspline(y[delta == 1], y[delta == 0], lbound = 0)
Translates an oldlogspline
object in an
logspline
object. This routine is mostly used in logspline
,
as it allows the routine to use oldlogspline
for some situations
where logspline
crashes. The other use is when you have censored data,
and thus have to use oldlogspline
to fit, but wish to use the
auxiliary routines from logspline
.
oldlogspline.to.logspline(obj, data)
oldlogspline.to.logspline(obj, data)
obj |
object of class |
data |
the original data. Used to compute the |
object of the class logspline
. The call
component
of the new object is not useful. The delete
component of the old
object is ignored.
Charles Kooperberg [email protected].
Charles Kooperberg and Charles J. Stone. Logspline density estimation for censored data (1992). Journal of Computational and Graphical Statistics, 1, 301–328.
Charles J. Stone, Mark Hansen, Charles Kooperberg, and Young K. Truong. The use of polynomial splines and their tensor products in extended linear modeling (with discussion) (1997). Annals of Statistics, 25, 1371–1470.
x <- rnorm(100) fit.old <- oldlogspline(x) fit.translate <- oldlogspline.to.logspline(fit.old,x) fit.new <- logspline(x) plot(fit.new) plot(fit.old,add=TRUE,col=2) # # should look almost the same, the differences are the # different fitting routines #
x <- rnorm(100) fit.old <- oldlogspline(x) fit.translate <- oldlogspline.to.logspline(fit.old,x) fit.new <- logspline(x) plot(fit.new) plot(fit.old,add=TRUE,col=2) # # should look almost the same, the differences are the # different fitting routines #
Plots a logspline
density, distribution function, hazard
function or survival function
from
a logspline density that was fitted using
the 1997 knot addition and deletion algorithm (logspline
).
The 1992 algorithm is available using the oldlogspline
function.
## S3 method for class 'logspline' plot(x, n = 100, what = "d", add = FALSE, xlim, xlab = "", ylab = "", type = "l", ...)
## S3 method for class 'logspline' plot(x, n = 100, what = "d", add = FALSE, xlim, xlab = "", ylab = "", type = "l", ...)
x |
|
n |
the number of equally spaced points at which to plot the density. |
what |
what should be plotted:
|
add |
should the plot be added to an existing plot. |
xlim |
range of data on which to plot. Default is from the 1th to the 99th percentile of the density, extended by 10% on each end. |
xlab , ylab
|
labels plotted on the axes. |
type |
type of plot. |
... |
other plotting options, as desired |
This function produces a plot of a logspline
fit at n
equally
spaced points roughly covering the support of the density. (Use
xlim = c(from, to)
to change the range of these points.)
Charles Kooperberg [email protected].
Charles Kooperberg and Charles J. Stone. Logspline density estimation for censored data (1992). Journal of Computational and Graphical Statistics, 1, 301–328.
Charles J. Stone, Mark Hansen, Charles Kooperberg, and Young K. Truong. The use of polynomial splines and their tensor products in extended linear modeling (with discussion) (1997). Annals of Statistics, 25, 1371–1470.
logspline
,
summary.logspline
,
dlogspline
,
plogspline
,
qlogspline
,
rlogspline
,
y <- rnorm(100) fit <- logspline(y) plot(fit)
y <- rnorm(100) fit <- logspline(y) plot(fit)
Plots an oldlogspline
density, distribution function, hazard
function or survival function
from
a logspline density that was fitted using
the 1992 knot deletion algorithm.
The 1997 algorithm using knot
deletion and addition is available using the logspline
function.
## S3 method for class 'oldlogspline' plot(x, n = 100, what = "d", xlim, xlab = "", ylab = "", type = "l", add = FALSE, ...)
## S3 method for class 'oldlogspline' plot(x, n = 100, what = "d", xlim, xlab = "", ylab = "", type = "l", add = FALSE, ...)
x |
|
n |
the number of equally spaced points at which to plot the density. |
what |
what should be plotted:
|
xlim |
range of data on which to plot. Default is from the 1th to the 99th percentile of the density, extended by 10% on each end. |
xlab , ylab
|
labels plotted on the axes. |
type |
type of plot. |
add |
should the plot be added to an existing plot. |
... |
other plotting options, as desired |
This function produces a plot of a oldlogspline
fit at n
equally
spaced points roughly covering the support of the density. (Use
xlim=c(from,to)
to change the range of these points.)
Charles Kooperberg [email protected].
Charles Kooperberg and Charles J. Stone. Logspline density estimation for censored data (1992). Journal of Computational and Graphical Statistics, 1, 301–328.
Charles J. Stone, Mark Hansen, Charles Kooperberg, and Young K. Truong. The use of polynomial splines and their tensor products in extended linear modeling (with discussion) (1997). Annals of Statistics, 25, 1371–1470.
logspline
,
oldlogspline
,
summary.oldlogspline
,
doldlogspline
,
poldlogspline
,qoldlogspline
,
roldlogspline
.
y <- rnorm(100) fit <- oldlogspline(y) plot(fit)
y <- rnorm(100) fit <- oldlogspline(y) plot(fit)
This function summarizes both the stepwise selection process of the
model fitting by logspline
, as well as the final model
that was selected using AIC/BIC. A
logspline
object was fit using the 1997 knot addition and deletion algorithm.
The 1992 algorithm is available using the oldlogspline
function.
## S3 method for class 'logspline' summary(object, ...) ## S3 method for class 'logspline' print(x, ...)
## S3 method for class 'logspline' summary(object, ...) ## S3 method for class 'logspline' print(x, ...)
object , x
|
|
... |
other arguments are ignored. |
These function produce identical printed output. The main body is a table with five columns: the first column is a possible number of knots for the fitted model;
the second column is the log-likelihood for the fit;
the third column is -2 * loglikelihood + penalty * (number of knots - 1)
,
which is the AIC criterion; logspline
selected the model with
the smallest value of AIC;
the fourth and fifth columns give the
endpoints of the interval of values of penalty that would yield the
model with the indicated number of knots. (NA
s imply that the model is
not optimal for any choice of penalty
.) At the bottom of the table the
number of knots corresponding to the selected model is reported, as is
the value of penalty that was used.
Charles Kooperberg [email protected].
Charles Kooperberg and Charles J. Stone. Logspline density estimation for censored data (1992). Journal of Computational and Graphical Statistics, 1, 301–328.
Charles J. Stone, Mark Hansen, Charles Kooperberg, and Young K. Truong. The use of polynomial splines and their tensor products in extended linear modeling (with discussion) (1997). Annals of Statistics, 25, 1371–1470.
logspline
,
plot.logspline
,
dlogspline
,
plogspline
,
qlogspline
,
rlogspline
,oldlogspline
.
y <- rnorm(100) fit <- logspline(y) summary(fit)
y <- rnorm(100) fit <- logspline(y) summary(fit)
This function summarizes both the stepwise selection process of the
model fitting by oldlogspline
, as well as the final model
that was selected using AIC/BIC. A
logspline
object was fit using
the 1992 knot deletion algorithm (oldlogspline
).
The 1997 algorithm using knot
deletion and addition is available using the logspline
function.
## S3 method for class 'oldlogspline' summary(object, ...) ## S3 method for class 'oldlogspline' print(x, ...)
## S3 method for class 'oldlogspline' summary(object, ...) ## S3 method for class 'oldlogspline' print(x, ...)
object , x
|
|
... |
other arguments are ignored. |
These function produces the same printed output. The main body is a table with five columns: the first column is a possible number of knots for the fitted model;
the second column is the log-likelihood for the fit;
the third column is -2 * loglikelihood + penalty * (number of knots - 1)
,
which is the AIC criterion; logspline
selected the model with
the smallest value of AIC;
the fourth and fifth columns give the
endpoints of the interval of values of penalty that would yield the
model with the indicated number of knots. (NA
s imply that the model is
not optimal for any choice of penalty
.) At the bottom of the table the
number of knots corresponding to the selected model is reported, as is
the value of penalty that was used.
Charles Kooperberg [email protected].
Charles Kooperberg and Charles J. Stone. Logspline density estimation for censored data (1992). Journal of Computational and Graphical Statistics, 1, 301–328.
Charles J. Stone, Mark Hansen, Charles Kooperberg, and Young K. Truong. The use of polynomial splines and their tensor products in extended linear modeling (with discussion) (1997). Annals of Statistics, 25, 1371–1470.
logspline
,
oldlogspline
,
plot.oldlogspline
,
doldlogspline
,
poldlogspline
,qoldlogspline
,
roldlogspline
.
y <- rnorm(100) fit <- oldlogspline(y) summary(fit)
y <- rnorm(100) fit <- oldlogspline(y) summary(fit)
This function tries to convert a date.frame or a matrix to a no-frills matrix without labels, and a vector or time-series to a no-frills vector without labels.
unstrip(x)
unstrip(x)
x |
one- or two-dimensional object. |
Many of the functions for logspline
, oldlogspline
,
were written in the “before data.frame” era;
unstrip
attempts to keep all these functions useful with more advanced input objects.
In particular, many of these functions call unstrip
before doing anything else.
If x
is two-dimensional a matrix
without names, if x
is one-dimensional a numerical vector
Charles Kooperberg [email protected].
data(co2) unstrip(co2) data(iris) unstrip(iris)
data(co2) unstrip(co2) data(iris) unstrip(iris)