Title: | The R Stats Package |
---|---|
Description: | R statistical functions. |
Authors: | R Core Team and contributors worldwide |
Maintainer: | R Core Team <[email protected]> |
License: | Part of R 4.4.1 |
Version: | 4.4.1 |
Built: | 2024-06-15 17:27:26 UTC |
Source: | base |
R statistical functions
This package contains functions for statistical calculations and random number generation.
For a complete
list of functions, use library(help = "stats")
.
R Core Team and contributors worldwide
Maintainer: R Core Team [email protected]
.checkMFClasses
checks if the variables used in a predict
method agree in type with those used for fitting.
.MFclass
categorizes variables for this purpose.
.getXlevels()
extracts factor levels from factor
or
character
variables.
.checkMFClasses(cl, m, ordNotOK = FALSE) .MFclass(x) .getXlevels(Terms, m)
.checkMFClasses(cl, m, ordNotOK = FALSE) .MFclass(x) .getXlevels(Terms, m)
cl |
a character vector of class descriptions to match. |
m |
a model frame ( |
x |
any R object. |
ordNotOK |
logical: are ordered factors different? |
Terms |
a |
For applications involving model.matrix()
such as linear models
we do not need to differentiate between ordered factors and factors as
although these affect the coding, the coding used in the fit is
already recorded and imposed during prediction. However, other
applications may treat ordered factors differently:
rpart
does, for example.
.checkMFClasses()
checks and either signals an error calling
stop()
or returns NULL
invisibly.
.MFclass()
returns a character string, one of "logical"
,
"ordered"
, "factor"
, "numeric"
, "nmatrix.*"
(a numeric matrix with a number of columns appended) or "other"
.
.getXlevels
returns a named list
of character
vectors, possibly empty, or NULL
.
sapply(warpbreaks, .MFclass) # "numeric" plus 2 x "factor" sapply(iris, .MFclass) # 4 x "numeric" plus "factor" mf <- model.frame(Sepal.Width ~ Species, iris) mc <- model.frame(Sepal.Width ~ Sepal.Length, iris) .checkMFClasses("numeric", mc) # nothing else .checkMFClasses(c("numeric", "factor"), mf) ## simple .getXlevels() cases : (xl <- .getXlevels(terms(mf), mf)) # a list with one entry " $ Species" with 3 levels: stopifnot(exprs = { identical(xl$Species, levels(iris$Species)) identical(.getXlevels(terms(mc), mc), xl[0]) # a empty named list, as no factors is.null(.getXlevels(terms(x~x), list(x=1))) })
sapply(warpbreaks, .MFclass) # "numeric" plus 2 x "factor" sapply(iris, .MFclass) # 4 x "numeric" plus "factor" mf <- model.frame(Sepal.Width ~ Species, iris) mc <- model.frame(Sepal.Width ~ Sepal.Length, iris) .checkMFClasses("numeric", mc) # nothing else .checkMFClasses(c("numeric", "factor"), mf) ## simple .getXlevels() cases : (xl <- .getXlevels(terms(mf), mf)) # a list with one entry " $ Species" with 3 levels: stopifnot(exprs = { identical(xl$Species, levels(iris$Species)) identical(.getXlevels(terms(mc), mc), xl[0]) # a empty named list, as no factors is.null(.getXlevels(terms(x~x), list(x=1))) })
The function acf
computes (and by default plots) estimates of
the autocovariance or autocorrelation function. Function pacf
is the function used for the partial autocorrelations. Function
ccf
computes the cross-correlation or cross-covariance of two
univariate series.
acf(x, lag.max = NULL, type = c("correlation", "covariance", "partial"), plot = TRUE, na.action = na.fail, demean = TRUE, ...) pacf(x, lag.max, plot, na.action, ...) ## Default S3 method: pacf(x, lag.max = NULL, plot = TRUE, na.action = na.fail, ...) ccf(x, y, lag.max = NULL, type = c("correlation", "covariance"), plot = TRUE, na.action = na.fail, ...) ## S3 method for class 'acf' x[i, j]
acf(x, lag.max = NULL, type = c("correlation", "covariance", "partial"), plot = TRUE, na.action = na.fail, demean = TRUE, ...) pacf(x, lag.max, plot, na.action, ...) ## Default S3 method: pacf(x, lag.max = NULL, plot = TRUE, na.action = na.fail, ...) ccf(x, y, lag.max = NULL, type = c("correlation", "covariance"), plot = TRUE, na.action = na.fail, ...) ## S3 method for class 'acf' x[i, j]
x , y
|
a univariate or multivariate (not |
lag.max |
maximum lag at which to calculate the acf.
Default is |
type |
character string giving the type of acf to be computed.
Allowed values are
|
plot |
logical. If |
na.action |
function to be called to handle missing
values. |
demean |
logical. Should the covariances be about the sample means? |
... |
further arguments to be passed to |
i |
a set of lags (time differences) to retain. |
j |
a set of series (names or numbers) to retain. |
For type
= "correlation"
and "covariance"
, the
estimates are based on the sample covariance. (The lag 0 autocorrelation
is fixed at 1 by convention.)
By default, no missing values are allowed. If the na.action
function passes through missing values (as na.pass
does), the
covariances are computed from the complete cases. This means that the
estimate computed may well not be a valid autocorrelation sequence,
and may contain missing values. Missing values are not allowed when
computing the PACF of a multivariate time series.
The partial correlation coefficient is estimated by fitting
autoregressive models of successively higher orders up to
lag.max
.
The generic function plot
has a method for objects of class
"acf"
.
The lag is returned and plotted in units of time, and not numbers of observations.
There are print
and subsetting methods for objects of class
"acf"
.
An object of class "acf"
, which is a list with the following
elements:
lag |
A three dimensional array containing the lags at which the acf is estimated. |
acf |
An array with the same dimensions as |
type |
The type of correlation (same as the |
n.used |
The number of observations in the time series. |
series |
The name of the series |
snames |
The series names for a multivariate time series. |
The lag k
value returned by ccf(x, y)
estimates the
correlation between x[t+k]
and y[t]
.
The result is returned invisibly if plot
is TRUE
.
Original: Paul Gilbert, Martyn Plummer.
Extensive modifications and univariate case of pacf
by
B. D. Ripley.
Venables, W. N. and Ripley, B. D. (2002) Modern Applied Statistics with S. Fourth Edition. Springer-Verlag.
(This contains the exact definitions used.)
plot.acf
, ARMAacf
for the exact
autocorrelations of a given ARMA process.
require(graphics) ## Examples from Venables & Ripley acf(lh) acf(lh, type = "covariance") pacf(lh) acf(ldeaths) acf(ldeaths, ci.type = "ma") acf(ts.union(mdeaths, fdeaths)) ccf(mdeaths, fdeaths, ylab = "cross-correlation") # (just the cross-correlations) presidents # contains missing values acf(presidents, na.action = na.pass) pacf(presidents, na.action = na.pass)
require(graphics) ## Examples from Venables & Ripley acf(lh) acf(lh, type = "covariance") pacf(lh) acf(ldeaths) acf(ldeaths, ci.type = "ma") acf(ts.union(mdeaths, fdeaths)) ccf(mdeaths, fdeaths, ylab = "cross-correlation") # (just the cross-correlations) presidents # contains missing values acf(presidents, na.action = na.pass) pacf(presidents, na.action = na.pass)
Compute an AR process exactly fitting an autocorrelation function.
acf2AR(acf)
acf2AR(acf)
acf |
An autocorrelation or autocovariance sequence. |
A matrix, with one row for the computed AR(p) coefficients for
1 <= p <= length(acf)
.
ARMAacf
, ar.yw
which does this from an
empirical ACF.
(Acf <- ARMAacf(c(0.6, 0.3, -0.2))) acf2AR(Acf)
(Acf <- ARMAacf(c(0.6, 0.3, -0.2))) acf2AR(Acf)
Compute all the single terms in the scope
argument that can be
added to or dropped from the model, fit those models and compute a
table of the changes in fit.
add1(object, scope, ...) ## Default S3 method: add1(object, scope, scale = 0, test = c("none", "Chisq"), k = 2, trace = FALSE, ...) ## S3 method for class 'lm' add1(object, scope, scale = 0, test = c("none", "Chisq", "F"), x = NULL, k = 2, ...) ## S3 method for class 'glm' add1(object, scope, scale = 0, test = c("none", "Rao", "LRT", "Chisq", "F"), x = NULL, k = 2, ...) drop1(object, scope, ...) ## Default S3 method: drop1(object, scope, scale = 0, test = c("none", "Chisq"), k = 2, trace = FALSE, ...) ## S3 method for class 'lm' drop1(object, scope, scale = 0, all.cols = TRUE, test = c("none", "Chisq", "F"), k = 2, ...) ## S3 method for class 'glm' drop1(object, scope, scale = 0, test = c("none", "Rao", "LRT", "Chisq", "F"), k = 2, ...)
add1(object, scope, ...) ## Default S3 method: add1(object, scope, scale = 0, test = c("none", "Chisq"), k = 2, trace = FALSE, ...) ## S3 method for class 'lm' add1(object, scope, scale = 0, test = c("none", "Chisq", "F"), x = NULL, k = 2, ...) ## S3 method for class 'glm' add1(object, scope, scale = 0, test = c("none", "Rao", "LRT", "Chisq", "F"), x = NULL, k = 2, ...) drop1(object, scope, ...) ## Default S3 method: drop1(object, scope, scale = 0, test = c("none", "Chisq"), k = 2, trace = FALSE, ...) ## S3 method for class 'lm' drop1(object, scope, scale = 0, all.cols = TRUE, test = c("none", "Chisq", "F"), k = 2, ...) ## S3 method for class 'glm' drop1(object, scope, scale = 0, test = c("none", "Rao", "LRT", "Chisq", "F"), k = 2, ...)
object |
a fitted model object. |
scope |
a formula giving the terms to be considered for adding or dropping. |
scale |
an estimate of the residual mean square to be
used in computing |
test |
should the results include a test statistic relative to the
original model? The F test is only appropriate for |
k |
the penalty constant in AIC / |
trace |
if |
x |
a model matrix containing columns for the fitted model and all
terms in the upper scope. Useful if |
all.cols |
(Provided for compatibility with S.) Logical to specify
whether all columns of the design matrix should be used. If
|
... |
further arguments passed to or from other methods. |
For drop1
methods, a missing scope
is taken to be all
terms in the model. The hierarchy is respected when considering terms
to be added or dropped: all main effects contained in a second-order
interaction must remain, and so on.
In a scope
formula .
means ‘what is already there’.
The methods for lm
and glm
are more
efficient in that they do not recompute the model matrix and call the
fit
methods directly.
The default output table gives AIC, defined as minus twice log
likelihood plus where
is the rank of the model (the
number of effective parameters). This is only defined up to an
additive constant (like log-likelihoods). For linear Gaussian models
with fixed scale, the constant is chosen to give Mallows'
,
. Where
is used,
the column is labelled as
Cp
rather than AIC
.
The F tests for the "glm"
methods are based on analysis of
deviance tests, so if the dispersion is estimated it is based on the
residual deviance, unlike the F tests of anova.glm
.
An object of class "anova"
summarizing the differences in fit
between the models.
The model fitting must apply the models to the same dataset. Most
methods will attempt to use a subset of the data with no missing
values for any of the variables if na.action = na.omit
, but
this may give biased results. Only use these functions with data
containing missing values with great care.
The default methods make calls to the function nobs
to
check that the number of observations involved in the fitting process
remained unchanged.
These are not fully equivalent to the functions in S. There is no
keep
argument, and the methods used are not quite so
computationally efficient.
Their authors' definitions of Mallows' and Akaike's AIC
are used, not those of the authors of the models chapter of S.
The design was inspired by the S functions of the same names described in Chambers (1992).
Chambers, J. M. (1992) Linear models. Chapter 4 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
step
, aov
, lm
,
extractAIC
, anova
require(graphics); require(utils) ## following example(swiss) lm1 <- lm(Fertility ~ ., data = swiss) add1(lm1, ~ I(Education^2) + .^2) drop1(lm1, test = "F") # So called 'type II' anova ## following example(glm) drop1(glm.D93, test = "Chisq") drop1(glm.D93, test = "F") add1(glm.D93, scope = ~outcome*treatment, test = "Rao") ## Pearson Chi-square
require(graphics); require(utils) ## following example(swiss) lm1 <- lm(Fertility ~ ., data = swiss) add1(lm1, ~ I(Education^2) + .^2) drop1(lm1, test = "F") # So called 'type II' anova ## following example(glm) drop1(glm.D93, test = "Chisq") drop1(glm.D93, test = "F") add1(glm.D93, scope = ~outcome*treatment, test = "Rao") ## Pearson Chi-square
For a given table one can specify which of the classifying factors to expand by one or more levels to hold margins to be calculated. One may for example form sums and means over the first dimension and medians over the second. The resulting table will then have two extra levels for the first dimension and one extra level for the second. The default is to sum over all margins in the table. Other possibilities may give results that depend on the order in which the margins are computed. This is flagged in the printed output from the function.
addmargins(A, margin = seq_along(dim(A)), FUN = sum, quiet = FALSE)
addmargins(A, margin = seq_along(dim(A)), FUN = sum, quiet = FALSE)
A |
table or array. The function uses the presence of the
|
margin |
vector of dimensions over which to form margins. Margins
are formed in the order in which dimensions are specified in
|
FUN |
|
quiet |
logical which suppresses the message telling the order in which the margins were computed. |
If the functions used to form margins are not commutative, the result
depends on the order in which margins are computed. Annotation
of margins is done via naming the FUN
list.
A table
or array
with the same number of
dimensions as A
, but
with extra levels of the dimensions mentioned in margin
. The
number of levels added to each dimension is the length of the entries
in FUN
. A message with the order of computation of margins is
printed.
Bendix Carstensen, Steno Diabetes Center & Department of Biostatistics, University of Copenhagen, https://BendixCarstensen.com, autumn 2003. Margin naming enhanced by Duncan Murdoch.
Aye <- sample(c("Yes", "Si", "Oui"), 177, replace = TRUE) Bee <- sample(c("Hum", "Buzz"), 177, replace = TRUE) Sea <- sample(c("White", "Black", "Red", "Dead"), 177, replace = TRUE) (A <- table(Aye, Bee, Sea)) (aA <- addmargins(A)) ftable(A) ftable(aA) # Non-commutative functions - note differences between resulting tables: ftable( addmargins(A, c(3, 1), FUN = list(list(Min = min, Max = max), Sum = sum))) ftable( addmargins(A, c(1, 3), FUN = list(Sum = sum, list(Min = min, Max = max)))) # Weird function needed to return the N when computing percentages sqsm <- function(x) sum(x)^2/100 B <- table(Sea, Bee) round(sweep(addmargins(B, 1, list(list(All = sum, N = sqsm))), 2, apply(B, 2, sum)/100, `/`), 1) round(sweep(addmargins(B, 2, list(list(All = sum, N = sqsm))), 1, apply(B, 1, sum)/100, `/`), 1) # A total over Bee requires formation of the Bee-margin first: mB <- addmargins(B, 2, FUN = list(list(Total = sum))) round(ftable(sweep(addmargins(mB, 1, list(list(All = sum, N = sqsm))), 2, apply(mB, 2, sum)/100, `/`)), 1) ## Zero.Printing table+margins: set.seed(1) x <- sample( 1:7, 20, replace = TRUE) y <- sample( 1:7, 20, replace = TRUE) tx <- addmargins( table(x, y) ) print(tx, zero.print = ".")
Aye <- sample(c("Yes", "Si", "Oui"), 177, replace = TRUE) Bee <- sample(c("Hum", "Buzz"), 177, replace = TRUE) Sea <- sample(c("White", "Black", "Red", "Dead"), 177, replace = TRUE) (A <- table(Aye, Bee, Sea)) (aA <- addmargins(A)) ftable(A) ftable(aA) # Non-commutative functions - note differences between resulting tables: ftable( addmargins(A, c(3, 1), FUN = list(list(Min = min, Max = max), Sum = sum))) ftable( addmargins(A, c(1, 3), FUN = list(Sum = sum, list(Min = min, Max = max)))) # Weird function needed to return the N when computing percentages sqsm <- function(x) sum(x)^2/100 B <- table(Sea, Bee) round(sweep(addmargins(B, 1, list(list(All = sum, N = sqsm))), 2, apply(B, 2, sum)/100, `/`), 1) round(sweep(addmargins(B, 2, list(list(All = sum, N = sqsm))), 1, apply(B, 1, sum)/100, `/`), 1) # A total over Bee requires formation of the Bee-margin first: mB <- addmargins(B, 2, FUN = list(list(Total = sum))) round(ftable(sweep(addmargins(mB, 1, list(list(All = sum, N = sqsm))), 2, apply(mB, 2, sum)/100, `/`)), 1) ## Zero.Printing table+margins: set.seed(1) x <- sample( 1:7, 20, replace = TRUE) y <- sample( 1:7, 20, replace = TRUE) tx <- addmargins( table(x, y) ) print(tx, zero.print = ".")
Splits the data into subsets, computes summary statistics for each, and returns the result in a convenient form.
aggregate(x, ...) ## Default S3 method: aggregate(x, ...) ## S3 method for class 'data.frame' aggregate(x, by, FUN, ..., simplify = TRUE, drop = TRUE) ## S3 method for class 'formula' aggregate(x, data, FUN, ..., subset, na.action = na.omit) ## S3 method for class 'ts' aggregate(x, nfrequency = 1, FUN = sum, ndeltat = 1, ts.eps = getOption("ts.eps"), ...)
aggregate(x, ...) ## Default S3 method: aggregate(x, ...) ## S3 method for class 'data.frame' aggregate(x, by, FUN, ..., simplify = TRUE, drop = TRUE) ## S3 method for class 'formula' aggregate(x, data, FUN, ..., subset, na.action = na.omit) ## S3 method for class 'ts' aggregate(x, nfrequency = 1, FUN = sum, ndeltat = 1, ts.eps = getOption("ts.eps"), ...)
x |
an R object. For the |
by |
a list of grouping elements, each as long as the variables
in the data frame |
FUN |
a function to compute the summary statistics which can be applied to all data subsets. |
simplify |
a logical indicating whether results should be simplified to a vector or matrix if possible. |
drop |
a logical indicating whether to drop unused combinations
of grouping values. The non-default case |
data |
a data frame (or list) from which the variables in the formula should be taken. |
subset |
an optional vector specifying a subset of observations to be used. |
na.action |
a function which indicates what should happen when
the data contain |
nfrequency |
new number of observations per unit of time; must
be a divisor of the frequency of |
ndeltat |
new fraction of the sampling period between
successive observations; must be a divisor of the sampling
interval of |
ts.eps |
tolerance used to decide if |
... |
further arguments passed to or used by methods. |
aggregate
is a generic function with methods for data frames
and time series.
The default method, aggregate.default
, uses the time series
method if x
is a time series, and otherwise coerces x
to a data frame and calls the data frame method.
aggregate.data.frame
is the data frame method. If x
is
not a data frame, it is coerced to one, which must have a non-zero
number of rows. Then, each of the variables (columns) in x
is
split into subsets of cases (rows) of identical combinations of the
components of by
, and FUN
is applied to each such subset
with further arguments in ...
passed to it. The result is
reformatted into a data frame containing the variables in by
and x
. The ones arising from by
contain the unique
combinations of grouping values used for determining the subsets, and
the ones arising from x
the corresponding summaries for the
subset of the respective variables in x
. If simplify
is
true, summaries are simplified to vectors or matrices if they have a
common length of one or greater than one, respectively; otherwise,
lists of summary results according to subsets are obtained. Rows with
missing values in any of the by
variables will be omitted from
the result. (Note that versions of R prior to 2.11.0 required
FUN
to be a scalar function.)
The formula method provides a standard formula interface to
aggregate.data.frame
.
The latter invokes the formula method if by
is a formula,
in which case aggregate(x, by, FUN)
is the same as
aggregate(by, x, FUN)
for a data frame x
.
aggregate.ts
is the time series method, and requires FUN
to be a scalar function. If x
is not a time series, it is
coerced to one. Then, the variables in x
are split into
appropriate blocks of length frequency(x) / nfrequency
, and
FUN
is applied to each such block, with further (named)
arguments in ...
passed to it. The result returned is a time
series with frequency nfrequency
holding the aggregated values.
Note that this make most sense for a quarterly or yearly result when
the original series covers a whole number of quarters or years: in
particular aggregating a monthly series to quarters starting in
February does not give a conventional quarterly series.
FUN
is passed to match.fun
, and hence it can be a
function or a symbol or character string naming a function.
For the time series method, a time series of class "ts"
or
class c("mts", "ts")
.
For the data frame method, a data frame with columns
corresponding to the grouping variables in by
followed by
aggregated columns from x
. If the by
has names, the
non-empty times are used to label the columns in the results, with
unnamed grouping variables being named Group.i
for
by[[i]]
.
The first argument of the "formula"
method was named
formula
rather than x
prior to R 4.2.0. Portable uses
should not name that argument.
Kurt Hornik, with contributions by Arni Magnusson.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
## Compute the averages for the variables in 'state.x77', grouped ## according to the region (Northeast, South, North Central, West) that ## each state belongs to. aggregate(state.x77, list(Region = state.region), mean) ## Compute the averages according to region and the occurrence of more ## than 130 days of frost. aggregate(state.x77, list(Region = state.region, Cold = state.x77[,"Frost"] > 130), mean) ## (Note that no state in 'South' is THAT cold.) ## example with character variables and NAs testDF <- data.frame(v1 = c(1,3,5,7,8,3,5,NA,4,5,7,9), v2 = c(11,33,55,77,88,33,55,NA,44,55,77,99) ) by1 <- c("red", "blue", 1, 2, NA, "big", 1, 2, "red", 1, NA, 12) by2 <- c("wet", "dry", 99, 95, NA, "damp", 95, 99, "red", 99, NA, NA) aggregate(x = testDF, by = list(by1, by2), FUN = "mean") # and if you want to treat NAs as a group fby1 <- factor(by1, exclude = "") fby2 <- factor(by2, exclude = "") aggregate(x = testDF, by = list(fby1, fby2), FUN = "mean") ## Formulas, one ~ one, one ~ many, many ~ one, and many ~ many: aggregate(weight ~ feed, data = chickwts, mean) aggregate(breaks ~ wool + tension, data = warpbreaks, mean) aggregate(cbind(Ozone, Temp) ~ Month, data = airquality, mean) aggregate(cbind(ncases, ncontrols) ~ alcgp + tobgp, data = esoph, sum) ## "complete cases" vs. "available cases" colSums(is.na(airquality)) # NAs in Ozone but not Temp ## the default is to summarize *complete cases*: aggregate(cbind(Ozone, Temp) ~ Month, data = airquality, FUN = mean) ## to handle missing values *per variable*: aggregate(cbind(Ozone, Temp) ~ Month, data = airquality, FUN = mean, na.action = na.pass, na.rm = TRUE) ## Dot notation: aggregate(. ~ Species, data = iris, mean) aggregate(len ~ ., data = ToothGrowth, mean) ## Often followed by xtabs(): ag <- aggregate(len ~ ., data = ToothGrowth, mean) xtabs(len ~ ., data = ag) ## Formula interface via 'by' (for pipe operations) ToothGrowth |> aggregate(len ~ ., FUN = mean) ## Compute the average annual approval ratings for American presidents. aggregate(presidents, nfrequency = 1, FUN = mean) ## Give the summer less weight. aggregate(presidents, nfrequency = 1, FUN = weighted.mean, w = c(1, 1, 0.5, 1))
## Compute the averages for the variables in 'state.x77', grouped ## according to the region (Northeast, South, North Central, West) that ## each state belongs to. aggregate(state.x77, list(Region = state.region), mean) ## Compute the averages according to region and the occurrence of more ## than 130 days of frost. aggregate(state.x77, list(Region = state.region, Cold = state.x77[,"Frost"] > 130), mean) ## (Note that no state in 'South' is THAT cold.) ## example with character variables and NAs testDF <- data.frame(v1 = c(1,3,5,7,8,3,5,NA,4,5,7,9), v2 = c(11,33,55,77,88,33,55,NA,44,55,77,99) ) by1 <- c("red", "blue", 1, 2, NA, "big", 1, 2, "red", 1, NA, 12) by2 <- c("wet", "dry", 99, 95, NA, "damp", 95, 99, "red", 99, NA, NA) aggregate(x = testDF, by = list(by1, by2), FUN = "mean") # and if you want to treat NAs as a group fby1 <- factor(by1, exclude = "") fby2 <- factor(by2, exclude = "") aggregate(x = testDF, by = list(fby1, fby2), FUN = "mean") ## Formulas, one ~ one, one ~ many, many ~ one, and many ~ many: aggregate(weight ~ feed, data = chickwts, mean) aggregate(breaks ~ wool + tension, data = warpbreaks, mean) aggregate(cbind(Ozone, Temp) ~ Month, data = airquality, mean) aggregate(cbind(ncases, ncontrols) ~ alcgp + tobgp, data = esoph, sum) ## "complete cases" vs. "available cases" colSums(is.na(airquality)) # NAs in Ozone but not Temp ## the default is to summarize *complete cases*: aggregate(cbind(Ozone, Temp) ~ Month, data = airquality, FUN = mean) ## to handle missing values *per variable*: aggregate(cbind(Ozone, Temp) ~ Month, data = airquality, FUN = mean, na.action = na.pass, na.rm = TRUE) ## Dot notation: aggregate(. ~ Species, data = iris, mean) aggregate(len ~ ., data = ToothGrowth, mean) ## Often followed by xtabs(): ag <- aggregate(len ~ ., data = ToothGrowth, mean) xtabs(len ~ ., data = ag) ## Formula interface via 'by' (for pipe operations) ToothGrowth |> aggregate(len ~ ., FUN = mean) ## Compute the average annual approval ratings for American presidents. aggregate(presidents, nfrequency = 1, FUN = mean) ## Give the summer less weight. aggregate(presidents, nfrequency = 1, FUN = weighted.mean, w = c(1, 1, 0.5, 1))
Generic function calculating Akaike's ‘An Information Criterion’ for
one or several fitted model objects for which a log-likelihood value
can be obtained, according to the formula
,
where
represents the number of parameters in the
fitted model, and
for the usual AIC, or
(
being the number of observations) for the so-called BIC or SBC
(Schwarz's Bayesian criterion).
AIC(object, ..., k = 2) BIC(object, ...)
AIC(object, ..., k = 2) BIC(object, ...)
object |
a fitted model object for which there exists a
|
... |
optionally more fitted model objects. |
k |
numeric, the penalty per parameter to be used; the
default |
When comparing models fitted by maximum likelihood to the same data, the smaller the AIC or BIC, the better the fit.
The theory of AIC requires that the log-likelihood has been maximized: whereas AIC can be computed for models not fitted by maximum likelihood, their AIC values should not be compared.
Examples of models not ‘fitted to the same data’ are where the response is transformed (accelerated-life models are fitted to log-times) and where contingency tables have been used to summarize data.
These are generic functions (with S4 generics defined in package
stats4): however methods should be defined for the
log-likelihood function logLik
rather than these
functions: the action of their default methods is to call logLik
on all the supplied objects and assemble the results. Note that in
several common cases logLik
does not return the value at
the MLE: see its help page.
The log-likelihood and hence the AIC/BIC is only defined up to an
additive constant. Different constants have conventionally been used
for different purposes and so extractAIC
and AIC
may give different values (and do for models of class "lm"
: see
the help for extractAIC
). Particular care is needed
when comparing fits of different classes (with, for example, a
comparison of a Poisson and gamma GLM being meaningless since one has
a discrete response, the other continuous).
BIC
is defined as
AIC(object, ..., k = log(nobs(object)))
.
This needs the number of observations to be known: the default method
looks first for a "nobs"
attribute on the return value from the
logLik
method, then tries the nobs
generic, and if neither succeed returns BIC as NA
.
If just one object is provided, a numeric value with the corresponding
AIC (or BIC, or ..., depending on k
).
If multiple objects are provided, a data.frame
with rows
corresponding to the objects and columns representing the number of
parameters in the model (df
) and the AIC or BIC.
Originally by José Pinheiro and Douglas Bates, more recent revisions by R-core.
Sakamoto, Y., Ishiguro, M., and Kitagawa G. (1986). Akaike Information Criterion Statistics. D. Reidel Publishing Company.
extractAIC
, logLik
, nobs
.
lm1 <- lm(Fertility ~ . , data = swiss) AIC(lm1) stopifnot(all.equal(AIC(lm1), AIC(logLik(lm1)))) BIC(lm1) lm2 <- update(lm1, . ~ . -Examination) AIC(lm1, lm2) BIC(lm1, lm2)
lm1 <- lm(Fertility ~ . , data = swiss) AIC(lm1) stopifnot(all.equal(AIC(lm1), AIC(logLik(lm1)))) BIC(lm1) lm2 <- update(lm1, . ~ . -Examination) AIC(lm1, lm2) BIC(lm1, lm2)
Find aliases (linearly dependent terms) in a linear model specified by a formula.
alias(object, ...) ## S3 method for class 'formula' alias(object, data, ...) ## S3 method for class 'lm' alias(object, complete = TRUE, partial = FALSE, partial.pattern = FALSE, ...)
alias(object, ...) ## S3 method for class 'formula' alias(object, data, ...) ## S3 method for class 'lm' alias(object, complete = TRUE, partial = FALSE, partial.pattern = FALSE, ...)
object |
A fitted model object, for example from |
data |
Optionally, a data frame to search for the objects in the formula. |
complete |
Should information on complete aliasing be included? |
partial |
Should information on partial aliasing be included? |
partial.pattern |
Should partial aliasing be presented in a schematic way? If this is done, the results are presented in a more compact way, usually giving the deciles of the coefficients. |
... |
further arguments passed to or from other methods. |
Although the main method is for class "lm"
, alias
is
most useful for experimental designs and so is used with fits from
aov
.
Complete aliasing refers to effects in linear models that cannot be estimated
independently of the terms which occur earlier in the model and so
have their coefficients omitted from the fit. Partial aliasing refers
to effects that can be estimated less precisely because of
correlations induced by the design.
Some parts of the "lm"
method require recommended package
MASS to be installed.
A list (of class
"listof"
) containing components
Model |
Description of the model; usually the formula. |
Complete |
A matrix with columns corresponding to effects that are linearly dependent on the rows. |
Partial |
The correlations of the estimable effects, with a zero
diagonal. An object of class |
The aliasing pattern may depend on the contrasts in use: Helmert contrasts are probably most useful.
The defaults are different from those in S.
The design was inspired by the S function of the same name described in Chambers et al. (1992).
Chambers, J. M., Freeny, A and Heiberger, R. M. (1992) Analysis of variance; designed experiments. Chapter 5 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
op <- options(contrasts = c("contr.helmert", "contr.poly")) npk.aov <- aov(yield ~ block + N*P*K, npk) alias(npk.aov) options(op) # reset
op <- options(contrasts = c("contr.helmert", "contr.poly")) npk.aov <- aov(yield ~ block + N*P*K, npk) alias(npk.aov) options(op) # reset
Compute analysis of variance (or deviance) tables for one or more fitted model objects.
anova(object, ...)
anova(object, ...)
object |
an object containing the results returned by a model
fitting function (e.g., |
... |
additional objects of the same type. |
This (generic) function returns an object of class anova
.
These objects represent analysis-of-variance and analysis-of-deviance tables.
When given a single argument it produces a table which
tests whether the model terms are significant.
When given a sequence of objects, anova
tests
the models against one another in the order specified.
The print method for anova
objects prints tables in a
‘pretty’ form.
The comparison between two or more models will only be valid if they
are fitted to the same dataset. This may be a problem if there are
missing values and R's default of na.action = na.omit
is used.
Chambers, J. M. and Hastie, T. J. (1992) Statistical Models in S, Wadsworth & Brooks/Cole.
coefficients
, effects
,
fitted.values
, residuals
,
summary
, drop1
, add1
.
Compute an analysis of deviance table for one or more generalized linear model fits.
## S3 method for class 'glm' anova(object, ..., dispersion = NULL, test = NULL)
## S3 method for class 'glm' anova(object, ..., dispersion = NULL, test = NULL)
object , ...
|
objects of class |
dispersion |
the dispersion parameter for the fitting family. By default it is obtained from the object(s). |
test |
a character string, (partially) matching one of |
Specifying a single object gives a sequential analysis of deviance table for that fit. That is, the reductions in the residual deviance as each term of the formula is added in turn are given in as the rows of a table, plus the residual deviances themselves.
If more than one object is specified, the table has a row for the residual degrees of freedom and deviance for each model. For all but the first model, the change in degrees of freedom and deviance is also given. (This only makes statistical sense if the models are nested.) It is conventional to list the models from smallest to largest, but this is up to the user.
The table will optionally contain test statistics (and P values)
comparing the reduction in deviance for the row to the residuals.
For models with known dispersion (e.g., binomial and Poisson fits)
the chi-squared test is most appropriate, and for those with
dispersion estimated by moments (e.g., gaussian
,
quasibinomial
and quasipoisson
fits) the F test is
most appropriate. If anova.glm
can determine which of these
cases applies then by default it will use one of the above tests.
If the dispersion
argument is supplied, the dispersion is
considered known and the chi-squared test will be used.
Argument test=FALSE
suppresses the test statistics and P values.
Mallows' statistic is the residual
deviance plus twice the estimate of
times
the residual degrees of freedom, which is closely related to AIC (and
a multiple of it if the dispersion is known).
You can also choose
"LRT"
and
"Rao"
for likelihood ratio tests and Rao's efficient score test.
The former is synonymous with "Chisq"
(although both have
an asymptotic chi-square distribution).
The dispersion estimate will be taken from the largest model, using
the value returned by summary.glm
. As this will in most
cases use a Chi-squared-based estimate, the F tests are not based on
the residual deviance in the analysis of deviance table shown.
An object of class "anova"
inheriting from class "data.frame"
.
The comparison between two or more models will only be valid if they
are fitted to the same dataset. This may be a problem if there are
missing values and R's default of na.action = na.omit
is used,
and anova
will detect this with an error.
Hastie, T. J. and Pregibon, D. (1992) Generalized linear models. Chapter 6 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
drop1
for
so-called ‘type II’ ANOVA where each term is dropped one at a
time respecting their hierarchy.
## --- Continuing the Example from '?glm': anova(glm.D93, test = FALSE) anova(glm.D93, test = "Cp") anova(glm.D93, test = "Chisq") glm.D93a <- update(glm.D93, ~treatment*outcome) # equivalent to Pearson Chi-square anova(glm.D93, glm.D93a, test = "Rao")
## --- Continuing the Example from '?glm': anova(glm.D93, test = FALSE) anova(glm.D93, test = "Cp") anova(glm.D93, test = "Chisq") glm.D93a <- update(glm.D93, ~treatment*outcome) # equivalent to Pearson Chi-square anova(glm.D93, glm.D93a, test = "Rao")
Compute an analysis of variance table for one or more linear model fits.
## S3 method for class 'lm' anova(object, ...) ## S3 method for class 'lmlist' anova(object, ..., scale = 0, test = "F")
## S3 method for class 'lm' anova(object, ...) ## S3 method for class 'lmlist' anova(object, ..., scale = 0, test = "F")
object , ...
|
objects of class |
test |
a character string specifying the test statistic to be
used. Can be one of |
scale |
numeric. An estimate of the noise variance
|
Specifying a single object gives a sequential analysis of variance table for that fit. That is, the reductions in the residual sum of squares as each term of the formula is added in turn are given in as the rows of a table, plus the residual sum of squares.
The table will contain F statistics (and P values) comparing the mean square for the row to the residual mean square.
If more than one object is specified, the table has a row for the residual degrees of freedom and sum of squares for each model. For all but the first model, the change in degrees of freedom and sum of squares is also given. (This only make statistical sense if the models are nested.) It is conventional to list the models from smallest to largest, but this is up to the user.
Optionally the table can include test statistics. Normally the
F statistic is most appropriate, which compares the mean square for a
row to the residual sum of squares for the largest model considered.
If scale
is specified chi-squared tests can be used. Mallows'
statistic is the residual sum of squares plus twice the
estimate of
times the residual degrees of freedom.
An object of class "anova"
inheriting from class "data.frame"
.
The comparison between two or more models will only be valid if they
are fitted to the same dataset. This may be a problem if there are
missing values and R's default of na.action = na.omit
is used,
and anova.lmlist
will detect this with an error.
Chambers, J. M. (1992) Linear models. Chapter 4 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
The model fitting function lm
, anova
.
drop1
for
so-called ‘type II’ ANOVA where each term is dropped one at a
time respecting their hierarchy.
## sequential table fit <- lm(sr ~ ., data = LifeCycleSavings) anova(fit) ## same effect via separate models fit0 <- lm(sr ~ 1, data = LifeCycleSavings) fit1 <- update(fit0, . ~ . + pop15) fit2 <- update(fit1, . ~ . + pop75) fit3 <- update(fit2, . ~ . + dpi) fit4 <- update(fit3, . ~ . + ddpi) anova(fit0, fit1, fit2, fit3, fit4, test = "F") anova(fit4, fit2, fit0, test = "F") # unconventional order
## sequential table fit <- lm(sr ~ ., data = LifeCycleSavings) anova(fit) ## same effect via separate models fit0 <- lm(sr ~ 1, data = LifeCycleSavings) fit1 <- update(fit0, . ~ . + pop15) fit2 <- update(fit1, . ~ . + pop75) fit3 <- update(fit2, . ~ . + dpi) fit4 <- update(fit3, . ~ . + ddpi) anova(fit0, fit1, fit2, fit3, fit4, test = "F") anova(fit4, fit2, fit0, test = "F") # unconventional order
Compute a (generalized) analysis of variance table for one or more multivariate linear models.
## S3 method for class 'mlm' anova(object, ..., test = c("Pillai", "Wilks", "Hotelling-Lawley", "Roy", "Spherical"), Sigma = diag(nrow = p), T = Thin.row(Proj(M) - Proj(X)), M = diag(nrow = p), X = ~0, idata = data.frame(index = seq_len(p)), tol = 1e-7)
## S3 method for class 'mlm' anova(object, ..., test = c("Pillai", "Wilks", "Hotelling-Lawley", "Roy", "Spherical"), Sigma = diag(nrow = p), T = Thin.row(Proj(M) - Proj(X)), M = diag(nrow = p), X = ~0, idata = data.frame(index = seq_len(p)), tol = 1e-7)
object |
an object of class |
... |
further objects of class |
test |
choice of test statistic (see below). Can be abbreviated. |
Sigma |
(only relevant if |
T |
transformation matrix. By default computed from |
M |
formula or matrix describing the outer projection (see below). |
X |
formula or matrix describing the inner projection (see below). |
idata |
data frame describing intra-block design. |
tol |
tolerance to be used in deciding if the residuals are
rank-deficient: see |
The anova.mlm
method uses either a multivariate test statistic for
the summary table, or a test based on sphericity assumptions (i.e.
that the covariance is proportional to a given matrix).
For the multivariate test, Wilks' statistic is most popular in the
literature, but the default Pillai–Bartlett statistic is
recommended by Hand and Taylor (1987). See
summary.manova
for further details.
For the "Spherical"
test, proportionality is usually with the
identity matrix but a different matrix can be specified using Sigma
.
Corrections for asphericity known as the Greenhouse–Geisser,
respectively Huynh–Feldt, epsilons are given and adjusted tests are
performed.
It is common to transform the observations prior to testing. This
typically involves
transformation to intra-block differences, but more complicated
within-block designs can be encountered,
making more elaborate transformations necessary. A
transformation matrix T
can be given directly or specified as
the difference between two projections onto the spaces spanned by
M
and X
, which in turn can be given as matrices or as
model formulas with respect to idata
(the tests will be
invariant to parametrization of the quotient space M/X
).
As with anova.lm
, all test statistics use the SSD matrix from
the largest model considered as the (generalized) denominator.
Contrary to other anova
methods, the intercept is not excluded
from the display in the single-model case. When contrast
transformations are involved, it often makes good sense to test for a
zero intercept.
An object of class "anova"
inheriting from class "data.frame"
The Huynh–Feldt epsilon differs from that calculated by SAS (as of v. 8.2) except when the DF is equal to the number of observations minus one. This is believed to be a bug in SAS, not in R.
Hand, D. J. and Taylor, C. C. (1987) Multivariate Analysis of Variance and Repeated Measures. Chapman and Hall.
require(graphics) utils::example(SSD) # Brings in the mlmfit and reacttime objects mlmfit0 <- update(mlmfit, ~0) ### Traditional tests of intrasubj. contrasts ## Using MANOVA techniques on contrasts: anova(mlmfit, mlmfit0, X = ~1) ## Assuming sphericity anova(mlmfit, mlmfit0, X = ~1, test = "Spherical") ### tests using intra-subject 3x2 design idata <- data.frame(deg = gl(3, 1, 6, labels = c(0, 4, 8)), noise = gl(2, 3, 6, labels = c("A", "P"))) anova(mlmfit, mlmfit0, X = ~ deg + noise, idata = idata, test = "Spherical") anova(mlmfit, mlmfit0, M = ~ deg + noise, X = ~ noise, idata = idata, test = "Spherical" ) anova(mlmfit, mlmfit0, M = ~ deg + noise, X = ~ deg, idata = idata, test = "Spherical" ) f <- factor(rep(1:2, 5)) # bogus, just for illustration mlmfit2 <- update(mlmfit, ~f) anova(mlmfit2, mlmfit, mlmfit0, X = ~1, test = "Spherical") anova(mlmfit2, X = ~1, test = "Spherical") # one-model form, eqiv. to previous ### There seems to be a strong interaction in these data plot(colMeans(reacttime))
require(graphics) utils::example(SSD) # Brings in the mlmfit and reacttime objects mlmfit0 <- update(mlmfit, ~0) ### Traditional tests of intrasubj. contrasts ## Using MANOVA techniques on contrasts: anova(mlmfit, mlmfit0, X = ~1) ## Assuming sphericity anova(mlmfit, mlmfit0, X = ~1, test = "Spherical") ### tests using intra-subject 3x2 design idata <- data.frame(deg = gl(3, 1, 6, labels = c(0, 4, 8)), noise = gl(2, 3, 6, labels = c("A", "P"))) anova(mlmfit, mlmfit0, X = ~ deg + noise, idata = idata, test = "Spherical") anova(mlmfit, mlmfit0, M = ~ deg + noise, X = ~ noise, idata = idata, test = "Spherical" ) anova(mlmfit, mlmfit0, M = ~ deg + noise, X = ~ deg, idata = idata, test = "Spherical" ) f <- factor(rep(1:2, 5)) # bogus, just for illustration mlmfit2 <- update(mlmfit, ~f) anova(mlmfit2, mlmfit, mlmfit0, X = ~1, test = "Spherical") anova(mlmfit2, X = ~1, test = "Spherical") # one-model form, eqiv. to previous ### There seems to be a strong interaction in these data plot(colMeans(reacttime))
Performs the Ansari-Bradley two-sample test for a difference in scale parameters.
ansari.test(x, ...) ## Default S3 method: ansari.test(x, y, alternative = c("two.sided", "less", "greater"), exact = NULL, conf.int = FALSE, conf.level = 0.95, ...) ## S3 method for class 'formula' ansari.test(formula, data, subset, na.action, ...)
ansari.test(x, ...) ## Default S3 method: ansari.test(x, y, alternative = c("two.sided", "less", "greater"), exact = NULL, conf.int = FALSE, conf.level = 0.95, ...) ## S3 method for class 'formula' ansari.test(formula, data, subset, na.action, ...)
x |
numeric vector of data values. |
y |
numeric vector of data values. |
alternative |
indicates the alternative hypothesis and must be
one of |
exact |
a logical indicating whether an exact p-value should be computed. |
conf.int |
a logical,indicating whether a confidence interval should be computed. |
conf.level |
confidence level of the interval. |
formula |
a formula of the form |
data |
an optional matrix or data frame (or similar: see
|
subset |
an optional vector specifying a subset of observations to be used. |
na.action |
a function which indicates what should happen when
the data contain |
... |
further arguments to be passed to or from methods. |
Suppose that x
and y
are independent samples from
distributions with densities and
,
respectively, where
is an unknown nuisance parameter and
, the ratio of scales, is the parameter of interest. The
Ansari-Bradley test is used for testing the null that
equals
1, the two-sided alternative being that
(the
distributions differ only in variance), and the one-sided alternatives
being
(the distribution underlying
x
has a larger
variance, "greater"
) or (
"less"
).
By default (if exact
is not specified), an exact p-value
is computed if both samples contain less than 50 finite values and
there are no ties. Otherwise, a normal approximation is used.
Optionally, a nonparametric confidence interval and an estimator for
are computed. If exact p-values are available, an exact
confidence interval is obtained by the algorithm described in Bauer
(1972), and the Hodges-Lehmann estimator is employed. Otherwise, the
returned confidence interval and point estimate are based on normal
approximations.
Note that mid-ranks are used in the case of ties rather than average scores as employed in Hollander & Wolfe (1973). See, e.g., Hajek, Sidak and Sen (1999), pages 131ff, for more information.
A list with class "htest"
containing the following components:
statistic |
the value of the Ansari-Bradley test statistic. |
p.value |
the p-value of the test. |
null.value |
the ratio of scales |
alternative |
a character string describing the alternative hypothesis. |
method |
the string |
data.name |
a character string giving the names of the data. |
conf.int |
a confidence interval for the scale parameter.
(Only present if argument |
estimate |
an estimate of the ratio of scales.
(Only present if argument |
To compare results of the Ansari-Bradley test to those of the F test
to compare two variances (under the assumption of normality), observe
that is the ratio of scales and hence
is the ratio
of variances (provided they exist), whereas for the F test the ratio
of variances itself is the parameter of interest. In particular,
confidence intervals are for
in the Ansari-Bradley test but
for
in the F test.
David F. Bauer (1972). Constructing confidence sets using rank statistics. Journal of the American Statistical Association, 67, 687–690. doi:10.1080/01621459.1972.10481279.
Jaroslav Hajek, Zbynek Sidak and Pranab K. Sen (1999). Theory of Rank Tests. San Diego, London: Academic Press.
Myles Hollander and Douglas A. Wolfe (1973). Nonparametric Statistical Methods. New York: John Wiley & Sons. Pages 83–92.
fligner.test
for a rank-based (nonparametric)
-sample test for homogeneity of variances;
mood.test
for another rank-based two-sample test for a
difference in scale parameters;
var.test
and bartlett.test
for parametric
tests for the homogeneity in variance.
ansari_test
in package coin
for exact and approximate conditional p-values for the
Ansari-Bradley test, as well as different methods for handling ties.
## Hollander & Wolfe (1973, p. 86f): ## Serum iron determination using Hyland control sera ramsay <- c(111, 107, 100, 99, 102, 106, 109, 108, 104, 99, 101, 96, 97, 102, 107, 113, 116, 113, 110, 98) jung.parekh <- c(107, 108, 106, 98, 105, 103, 110, 105, 104, 100, 96, 108, 103, 104, 114, 114, 113, 108, 106, 99) ansari.test(ramsay, jung.parekh) ansari.test(rnorm(10), rnorm(10, 0, 2), conf.int = TRUE) ## try more points - failed in 2.4.1 ansari.test(rnorm(100), rnorm(100, 0, 2), conf.int = TRUE)
## Hollander & Wolfe (1973, p. 86f): ## Serum iron determination using Hyland control sera ramsay <- c(111, 107, 100, 99, 102, 106, 109, 108, 104, 99, 101, 96, 97, 102, 107, 113, 116, 113, 110, 98) jung.parekh <- c(107, 108, 106, 98, 105, 103, 110, 105, 104, 100, 96, 108, 103, 104, 114, 114, 113, 108, 106, 99) ansari.test(ramsay, jung.parekh) ansari.test(rnorm(10), rnorm(10, 0, 2), conf.int = TRUE) ## try more points - failed in 2.4.1 ansari.test(rnorm(100), rnorm(100, 0, 2), conf.int = TRUE)
Fit an analysis of variance model by a call to lm
(for each
stratum if an Error(.)
is used).
aov(formula, data = NULL, projections = FALSE, qr = TRUE, contrasts = NULL, ...)
aov(formula, data = NULL, projections = FALSE, qr = TRUE, contrasts = NULL, ...)
formula |
A formula specifying the model. |
data |
A data frame in which the variables specified in the formula will be found. If missing, the variables are searched for in the standard way. |
projections |
Logical flag: should the projections be returned? |
qr |
Logical flag: should the QR decomposition be returned? |
contrasts |
A list of contrasts to be used for some of the factors
in the formula. These are not used for any |
... |
Arguments to be passed to |
This provides a wrapper to lm
for fitting linear models to
balanced or unbalanced experimental designs.
The main difference from lm
is in the way print
,
summary
and so on handle the fit: this is expressed in the
traditional language of the analysis of variance rather than that of
linear models.
If the formula contains a single Error
term, this is used to
specify error strata, and appropriate models are fitted within each
error stratum.
The formula can specify multiple responses.
Weights can be specified by a weights
argument, but should not
be used with an Error
term, and are incompletely supported
(e.g., not by model.tables
).
An object of class c("aov", "lm")
or for multiple responses
of class c("maov", "aov", "mlm", "lm")
or for multiple error
strata of class c("aovlist", "listof")
. There are
print
and summary
methods available for these.
aov
is designed for balanced designs, and the results can be
hard to interpret without balance: beware that missing values in the
response(s) will likely lose the balance. If there are two or more
error strata, the methods used are statistically inefficient without
balance, and it may be better to use lme
in
package nlme.
Balance can be checked with the replications
function.
The default ‘contrasts’ in R are not orthogonal contrasts, and
aov
and its helper functions will work better with such
contrasts: see the examples for how to select these.
The design was inspired by the S function of the same name described in Chambers et al. (1992).
Chambers, J. M., Freeny, A and Heiberger, R. M. (1992) Analysis of variance; designed experiments. Chapter 5 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
lm
, summary.aov
,
replications
, alias
,
proj
, model.tables
, TukeyHSD
## From Venables and Ripley (2002) p.165. ## Set orthogonal contrasts. op <- options(contrasts = c("contr.helmert", "contr.poly")) ( npk.aov <- aov(yield ~ block + N*P*K, npk) ) summary(npk.aov) coefficients(npk.aov) ## to show the effects of re-ordering terms contrast the two fits aov(yield ~ block + N * P + K, npk) aov(terms(yield ~ block + N * P + K, keep.order = TRUE), npk) ## as a test, not particularly sensible statistically npk.aovE <- aov(yield ~ N*P*K + Error(block), npk) npk.aovE summary(npk.aovE) options(op) # reset to previous
## From Venables and Ripley (2002) p.165. ## Set orthogonal contrasts. op <- options(contrasts = c("contr.helmert", "contr.poly")) ( npk.aov <- aov(yield ~ block + N*P*K, npk) ) summary(npk.aov) coefficients(npk.aov) ## to show the effects of re-ordering terms contrast the two fits aov(yield ~ block + N * P + K, npk) aov(terms(yield ~ block + N * P + K, keep.order = TRUE), npk) ## as a test, not particularly sensible statistically npk.aovE <- aov(yield ~ N*P*K + Error(block), npk) npk.aovE summary(npk.aovE) options(op) # reset to previous
Return a list of points which linearly interpolate given data points, or a function performing the linear (or constant) interpolation.
approx (x, y = NULL, xout, method = "linear", n = 50, yleft, yright, rule = 1, f = 0, ties = mean, na.rm = TRUE) approxfun(x, y = NULL, method = "linear", yleft, yright, rule = 1, f = 0, ties = mean, na.rm = TRUE)
approx (x, y = NULL, xout, method = "linear", n = 50, yleft, yright, rule = 1, f = 0, ties = mean, na.rm = TRUE) approxfun(x, y = NULL, method = "linear", yleft, yright, rule = 1, f = 0, ties = mean, na.rm = TRUE)
x , y
|
numeric vectors giving the coordinates of the points to be
interpolated. Alternatively a single plotting structure can be
specified: see |
xout |
an optional set of numeric values specifying where interpolation is to take place. |
method |
specifies the interpolation method to be used. Choices
are |
n |
If |
yleft |
the value to be returned when input |
yright |
the value to be returned when input |
rule |
an integer (of length 1 or 2) describing how interpolation
is to take place outside the interval [ |
f |
for |
ties |
handling of tied |
na.rm |
logical specifying how missing values ( |
The inputs can contain missing values which are deleted (if na.rm
is true, i.e., by default), so at least
two complete (x, y)
pairs are required (for method =
"linear"
, one otherwise). If there are duplicated (tied) x
values and ties
contains a function it is applied to the y
values for each distinct x
value to produce (x,y)
pairs
with unique x
.
Useful functions in this context include mean
,
min
, and max
.
If ties = "ordered"
the x
values are assumed to be already
ordered (and unique) and ties are not checked but kept if present.
This is the fastest option for large length(x)
.
If ties
is a list
of length two, ties[[2]]
must be a function to be applied to ties, see above, but if
ties[[1]]
is identical to "ordered"
, the x
values
are assumed to be sorted and are only checked for ties. Consequently,
ties = list("ordered", mean)
will be slightly more efficient than
the default ties = mean
in such a case.
The first y
value will be used for interpolation to the left and the last
one for interpolation to the right.
approx
returns a list with components x
and y
,
containing n
coordinates which interpolate the given data
points according to the method
(and rule
) desired.
The function approxfun
returns a function performing (linear or
constant) interpolation of the given data points. For a given set of
x
values, this function will return the corresponding
interpolated values. It uses data stored in its environment when it
was created, the details of which are subject to change.
The value returned by approxfun
contains references to the code
in the current version of R: it is not intended to be saved and
loaded into a different R session. This is safer for R >= 3.0.0.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
spline
and splinefun
for spline
interpolation.
require(graphics) x <- 1:10 y <- rnorm(10) par(mfrow = c(2,1)) plot(x, y, main = "approx(.) and approxfun(.)") points(approx(x, y), col = 2, pch = "*") points(approx(x, y, method = "constant"), col = 4, pch = "*") f <- approxfun(x, y) curve(f(x), 0, 11, col = "green2") points(x, y) is.function(fc <- approxfun(x, y, method = "const")) # TRUE curve(fc(x), 0, 10, col = "darkblue", add = TRUE) ## different extrapolation on left and right side : plot(approxfun(x, y, rule = 2:1), 0, 11, col = "tomato", add = TRUE, lty = 3, lwd = 2) ### Treatment of 'NA's -- are kept if na.rm=FALSE : xn <- 1:4 yn <- c(1,NA,3:4) xout <- (1:9)/2 ## Default behavior (na.rm = TRUE): NA's omitted; extrapolation gives NA data.frame(approx(xn,yn, xout)) data.frame(approx(xn,yn, xout, rule = 2))# -> *constant* extrapolation ## New (2019-2020) na.rm = FALSE: NA's are "kept" data.frame(approx(xn,yn, xout, na.rm=FALSE, rule = 2)) data.frame(approx(xn,yn, xout, na.rm=FALSE, rule = 2, method="constant")) ## NA's in x[] are not allowed: stopifnot(inherits( try( approx(yn,yn, na.rm=FALSE) ), "try-error")) ## Give a nice overview of all possibilities rule * method * na.rm : ## ----------------------------- ==== ====== ===== ## extrapolations "N":= NA; "C":= Constant : rules <- list(N=1, C=2, NC=1:2, CN=2:1) methods <- c("constant","linear") ry <- sapply(rules, function(R) { sapply(methods, function(M) sapply(setNames(,c(TRUE,FALSE)), function(na.) approx(xn, yn, xout=xout, method=M, rule=R, na.rm=na.)$y), simplify="array") }, simplify="array") names(dimnames(ry)) <- c("x = ", "na.rm", "method", "rule") dimnames(ry)[[1]] <- format(xout) ftable(aperm(ry, 4:1)) # --> (4 * 2 * 2) x length(xout) = 16 x 9 matrix ## Show treatment of 'ties' : x <- c(2,2:4,4,4,5,5,7,7,7) y <- c(1:6, 5:4, 3:1) (amy <- approx(x, y, xout = x)$y) # warning, can be avoided by specifying 'ties=': op <- options(warn=2) # warnings would be error stopifnot(identical(amy, approx(x, y, xout = x, ties=mean)$y)) (ay <- approx(x, y, xout = x, ties = "ordered")$y) stopifnot(amy == c(1.5,1.5, 3, 5,5,5, 4.5,4.5, 2,2,2), ay == c(2, 2, 3, 6,6,6, 4, 4, 1,1,1)) approx(x, y, xout = x, ties = min)$y approx(x, y, xout = x, ties = max)$y options(op) # revert 'warn'ing level
require(graphics) x <- 1:10 y <- rnorm(10) par(mfrow = c(2,1)) plot(x, y, main = "approx(.) and approxfun(.)") points(approx(x, y), col = 2, pch = "*") points(approx(x, y, method = "constant"), col = 4, pch = "*") f <- approxfun(x, y) curve(f(x), 0, 11, col = "green2") points(x, y) is.function(fc <- approxfun(x, y, method = "const")) # TRUE curve(fc(x), 0, 10, col = "darkblue", add = TRUE) ## different extrapolation on left and right side : plot(approxfun(x, y, rule = 2:1), 0, 11, col = "tomato", add = TRUE, lty = 3, lwd = 2) ### Treatment of 'NA's -- are kept if na.rm=FALSE : xn <- 1:4 yn <- c(1,NA,3:4) xout <- (1:9)/2 ## Default behavior (na.rm = TRUE): NA's omitted; extrapolation gives NA data.frame(approx(xn,yn, xout)) data.frame(approx(xn,yn, xout, rule = 2))# -> *constant* extrapolation ## New (2019-2020) na.rm = FALSE: NA's are "kept" data.frame(approx(xn,yn, xout, na.rm=FALSE, rule = 2)) data.frame(approx(xn,yn, xout, na.rm=FALSE, rule = 2, method="constant")) ## NA's in x[] are not allowed: stopifnot(inherits( try( approx(yn,yn, na.rm=FALSE) ), "try-error")) ## Give a nice overview of all possibilities rule * method * na.rm : ## ----------------------------- ==== ====== ===== ## extrapolations "N":= NA; "C":= Constant : rules <- list(N=1, C=2, NC=1:2, CN=2:1) methods <- c("constant","linear") ry <- sapply(rules, function(R) { sapply(methods, function(M) sapply(setNames(,c(TRUE,FALSE)), function(na.) approx(xn, yn, xout=xout, method=M, rule=R, na.rm=na.)$y), simplify="array") }, simplify="array") names(dimnames(ry)) <- c("x = ", "na.rm", "method", "rule") dimnames(ry)[[1]] <- format(xout) ftable(aperm(ry, 4:1)) # --> (4 * 2 * 2) x length(xout) = 16 x 9 matrix ## Show treatment of 'ties' : x <- c(2,2:4,4,4,5,5,7,7,7) y <- c(1:6, 5:4, 3:1) (amy <- approx(x, y, xout = x)$y) # warning, can be avoided by specifying 'ties=': op <- options(warn=2) # warnings would be error stopifnot(identical(amy, approx(x, y, xout = x, ties=mean)$y)) (ay <- approx(x, y, xout = x, ties = "ordered")$y) stopifnot(amy == c(1.5,1.5, 3, 5,5,5, 4.5,4.5, 2,2,2), ay == c(2, 2, 3, 6,6,6, 4, 4, 1,1,1)) approx(x, y, xout = x, ties = min)$y approx(x, y, xout = x, ties = max)$y options(op) # revert 'warn'ing level
Fit an autoregressive time series model to the data, by default selecting the complexity by AIC.
ar(x, aic = TRUE, order.max = NULL, method = c("yule-walker", "burg", "ols", "mle", "yw"), na.action, series, ...) ar.burg(x, ...) ## Default S3 method: ar.burg(x, aic = TRUE, order.max = NULL, na.action = na.fail, demean = TRUE, series, var.method = 1, ...) ## S3 method for class 'mts' ar.burg(x, aic = TRUE, order.max = NULL, na.action = na.fail, demean = TRUE, series, var.method = 1, ...) ar.yw(x, ...) ## Default S3 method: ar.yw(x, aic = TRUE, order.max = NULL, na.action = na.fail, demean = TRUE, series, ...) ## S3 method for class 'mts' ar.yw(x, aic = TRUE, order.max = NULL, na.action = na.fail, demean = TRUE, series, var.method = 1, ...) ar.mle(x, aic = TRUE, order.max = NULL, na.action = na.fail, demean = TRUE, series, ...) ## S3 method for class 'ar' predict(object, newdata, n.ahead = 1, se.fit = TRUE, ...)
ar(x, aic = TRUE, order.max = NULL, method = c("yule-walker", "burg", "ols", "mle", "yw"), na.action, series, ...) ar.burg(x, ...) ## Default S3 method: ar.burg(x, aic = TRUE, order.max = NULL, na.action = na.fail, demean = TRUE, series, var.method = 1, ...) ## S3 method for class 'mts' ar.burg(x, aic = TRUE, order.max = NULL, na.action = na.fail, demean = TRUE, series, var.method = 1, ...) ar.yw(x, ...) ## Default S3 method: ar.yw(x, aic = TRUE, order.max = NULL, na.action = na.fail, demean = TRUE, series, ...) ## S3 method for class 'mts' ar.yw(x, aic = TRUE, order.max = NULL, na.action = na.fail, demean = TRUE, series, var.method = 1, ...) ar.mle(x, aic = TRUE, order.max = NULL, na.action = na.fail, demean = TRUE, series, ...) ## S3 method for class 'ar' predict(object, newdata, n.ahead = 1, se.fit = TRUE, ...)
x |
a univariate or multivariate time series. |
aic |
|
order.max |
maximum order (or order) of model to fit. Defaults
to the smaller of |
method |
character string specifying the method to fit the
model. Must be one of the strings in the default argument
(the first few characters are sufficient). Defaults to
|
na.action |
function to be called to handle missing
values. Currently, via |
demean |
should a mean be estimated during fitting? |
series |
names for the series. Defaults to
|
var.method |
the method to estimate the innovations variance (see ‘Details’). |
... |
additional arguments for specific methods. |
object |
a fit from |
newdata |
data to which to apply the prediction. |
n.ahead |
number of steps ahead at which to predict. |
se.fit |
logical: return estimated standard errors of the prediction error? |
For definiteness, note that the AR coefficients have the sign in
ar
is just a wrapper for the functions ar.yw
,
ar.burg
, ar.ols
and ar.mle
.
Order selection is done by AIC if aic
is true. This is
problematic, as of the methods here only ar.mle
performs
true maximum likelihood estimation. The AIC is computed as if the variance
estimate were the MLE, omitting the determinant term from the
likelihood. Note that this is not the same as the Gaussian likelihood
evaluated at the estimated parameter values. In ar.yw
the
variance matrix of the innovations is computed from the fitted
coefficients and the autocovariance of x
.
ar.burg
allows two methods to estimate the innovations
variance and hence AIC. Method 1 is to use the update given by
the Levinson-Durbin recursion
(Brockwell and Davis, 1991, (8.2.6) on page 242),
and follows S-PLUS. Method 2 is the mean of the sum
of squares of the forward and backward prediction errors
(as in Brockwell and Davis, 1996, page 145).
Percival and Walden (1998) discuss both.
In the multivariate case the estimated
coefficients will depend (slightly) on the variance estimation method.
Remember that ar
includes by default a constant in the model, by
removing the overall mean of x
before fitting the AR model,
or (ar.mle
) estimating a constant to subtract.
For ar
and its methods a list of class "ar"
with
the following elements:
order |
The order of the fitted model. This is chosen by
minimizing the AIC if |
ar |
Estimated autoregression coefficients for the fitted model. |
var.pred |
The prediction variance: an estimate of the portion of the variance of the time series that is not explained by the autoregressive model. |
x.mean |
The estimated mean of the series used in fitting and for use in prediction. |
x.intercept |
( |
aic |
The differences in AIC between each model and the
best-fitting model. Note that the latter can have an AIC of |
n.used |
The number of observations in the time series, including missing. |
n.obs |
The number of non-missing observations in the time series. |
order.max |
The value of the |
partialacf |
The estimate of the partial autocorrelation function
up to lag |
resid |
residuals from the fitted model, conditioning on the
first |
method |
The value of the |
series |
The name(s) of the time series. |
frequency |
The frequency of the time series. |
call |
The matched call. |
asy.var.coef |
(univariate case, |
For predict.ar
, a time series of predictions, or if
se.fit = TRUE
, a list with components pred
, the
predictions, and se
, the estimated standard errors. Both
components are time series.
Only the univariate case of ar.mle
is implemented.
Fitting by method="mle"
to long series can be very slow.
If x
contains missing values, see NA
, also consider
using arima()
, possibly with method = "ML"
.
Martyn Plummer. Univariate case of ar.yw
, ar.mle
and C code for univariate case of ar.burg
by B. D. Ripley.
Brockwell, P. J. and Davis, R. A. (1991). Time Series and Forecasting Methods, second edition. Springer, New York. Section 11.4.
Brockwell, P. J. and Davis, R. A. (1996). Introduction to Time Series and Forecasting. Springer, New York. Sections 5.1 and 7.6.
Percival, D. P. and Walden, A. T. (1998). Spectral Analysis for Physical Applications. Cambridge University Press.
Whittle, P. (1963). On the fitting of multivariate autoregressions and the approximate canonical factorization of a spectral density matrix. Biometrika, 40, 129–134. doi:10.2307/2333753.
ar.ols
, arima
for ARMA models;
acf2AR
, for AR construction from the ACF.
arima.sim
for simulation of AR processes.
ar(lh) ar(lh, method = "burg") ar(lh, method = "ols") ar(lh, FALSE, 4) # fit ar(4) (sunspot.ar <- ar(sunspot.year)) predict(sunspot.ar, n.ahead = 25) ## try the other methods too ar(ts.union(BJsales, BJsales.lead)) ## Burg is quite different here, as is OLS (see ar.ols) ar(ts.union(BJsales, BJsales.lead), method = "burg")
ar(lh) ar(lh, method = "burg") ar(lh, method = "ols") ar(lh, FALSE, 4) # fit ar(4) (sunspot.ar <- ar(sunspot.year)) predict(sunspot.ar, n.ahead = 25) ## try the other methods too ar(ts.union(BJsales, BJsales.lead)) ## Burg is quite different here, as is OLS (see ar.ols) ar(ts.union(BJsales, BJsales.lead), method = "burg")
Fit an autoregressive time series model to the data by ordinary least squares, by default selecting the complexity by AIC.
ar.ols(x, aic = TRUE, order.max = NULL, na.action = na.fail, demean = TRUE, intercept = demean, series, ...)
ar.ols(x, aic = TRUE, order.max = NULL, na.action = na.fail, demean = TRUE, intercept = demean, series, ...)
x |
A univariate or multivariate time series. |
aic |
Logical flag. If |
order.max |
Maximum order (or order) of model to fit. Defaults
to |
na.action |
function to be called to handle missing values. |
demean |
should the AR model be for |
intercept |
should a separate intercept term be fitted? |
series |
names for the series. Defaults to
|
... |
further arguments to be passed to or from methods. |
ar.ols
fits the general AR model to a possibly non-stationary
and/or multivariate system of series x
. The resulting
unconstrained least squares estimates are consistent, even if
some of the series are non-stationary and/or co-integrated.
For definiteness, note that the AR coefficients have the sign in
where is zero unless
intercept
is true, and
is the sample mean if
demean
is true, zero
otherwise.
Order selection is done by AIC if aic
is true. This is
problematic, as ar.ols
does not perform
true maximum likelihood estimation. The AIC is computed as if
the variance estimate (computed from the variance matrix of the
residuals) were the MLE, omitting the determinant term from the
likelihood. Note that this is not the same as the Gaussian
likelihood evaluated at the estimated parameter values.
Some care is needed if intercept
is true and demean
is
false. Only use this is the series are roughly centred on
zero. Otherwise the computations may be inaccurate or fail entirely.
A list of class "ar"
with the following elements:
order |
The order of the fitted model. This is chosen by
minimizing the AIC if |
ar |
Estimated autoregression coefficients for the fitted model. |
var.pred |
The prediction variance: an estimate of the portion of the variance of the time series that is not explained by the autoregressive model. |
x.mean |
The estimated mean (or zero if |
x.intercept |
The intercept in the model for
|
aic |
The differences in AIC between each model and the
best-fitting model. Note that the latter can have an AIC of |
n.used |
The number of observations in the time series. |
order.max |
The value of the |
partialacf |
|
resid |
residuals from the fitted model, conditioning on the
first |
method |
The character string |
series |
The name(s) of the time series. |
frequency |
The frequency of the time series. |
call |
The matched call. |
asy.se.coef |
The asymptotic-theory standard errors of the coefficient estimates. |
Adrian Trapletti, Brian Ripley.
Luetkepohl, H. (1991): Introduction to Multiple Time Series Analysis. Springer Verlag, NY, pp. 368–370.
ar(lh, method = "burg") ar.ols(lh) ar.ols(lh, FALSE, 4) # fit ar(4) ar.ols(ts.union(BJsales, BJsales.lead)) x <- diff(log(EuStockMarkets)) ar.ols(x, order.max = 6, demean = FALSE, intercept = TRUE)
ar(lh, method = "burg") ar.ols(lh) ar.ols(lh, FALSE, 4) # fit ar(4) ar.ols(ts.union(BJsales, BJsales.lead)) x <- diff(log(EuStockMarkets)) ar.ols(x, order.max = 6, demean = FALSE, intercept = TRUE)
Fit an ARIMA model to a univariate time series.
arima(x, order = c(0L, 0L, 0L), seasonal = list(order = c(0L, 0L, 0L), period = NA), xreg = NULL, include.mean = TRUE, transform.pars = TRUE, fixed = NULL, init = NULL, method = c("CSS-ML", "ML", "CSS"), n.cond, SSinit = c("Gardner1980", "Rossignol2011"), optim.method = "BFGS", optim.control = list(), kappa = 1e6)
arima(x, order = c(0L, 0L, 0L), seasonal = list(order = c(0L, 0L, 0L), period = NA), xreg = NULL, include.mean = TRUE, transform.pars = TRUE, fixed = NULL, init = NULL, method = c("CSS-ML", "ML", "CSS"), n.cond, SSinit = c("Gardner1980", "Rossignol2011"), optim.method = "BFGS", optim.control = list(), kappa = 1e6)
x |
a univariate time series |
order |
A specification of the non-seasonal part of the ARIMA
model: the three integer components |
seasonal |
A specification of the seasonal part of the ARIMA
model, plus the period (which defaults to |
xreg |
Optionally, a vector or matrix of external regressors,
which must have the same number of rows as |
include.mean |
Should the ARMA model include a mean/intercept term? The
default is |
transform.pars |
logical; if true, the AR parameters are
transformed to ensure that they remain in the region of
stationarity. Not used for |
fixed |
optional numeric vector of the same length as the total number of coefficients to be estimated. It should be of the form
where The entries of the The argument |
init |
optional numeric vector of initial parameter
values. Missing values will be filled in, by zeroes except for
regression coefficients. Values already specified in |
method |
fitting method: maximum likelihood or minimize conditional sum-of-squares. The default (unless there are missing values) is to use conditional-sum-of-squares to find starting values, then maximum likelihood. Can be abbreviated. |
n.cond |
only used if fitting by conditional-sum-of-squares: the number of initial observations to ignore. It will be ignored if less than the maximum lag of an AR term. |
SSinit |
a string specifying the algorithm to compute the
state-space initialization of the likelihood; see
|
optim.method |
The value passed as the |
optim.control |
List of control parameters for |
kappa |
the prior variance (as a multiple of the innovations variance) for the past observations in a differenced model. Do not reduce this. |
Different definitions of ARMA models have different signs for the AR and/or MA coefficients. The definition used here has
and so the MA coefficients differ in sign from those used in
documentation written for S-PLUS. Further, if include.mean
is
true (the default for an ARMA model), this formula applies to rather than
. For ARIMA models with differencing, the
differenced series follows a zero-mean ARMA model. If an
xreg
term is included, a linear regression (with a constant term if
include.mean
is true and there is no differencing) is fitted
with an ARMA model for the error term.
The variance matrix of the estimates is found from the Hessian of the log-likelihood, and so may only be a rough guide.
Optimization is done by optim
. It will work
best if the columns in xreg
are roughly scaled to zero mean
and unit variance, but does attempt to estimate suitable scalings.
A list of class "Arima"
with components:
coef |
a vector of AR, MA and regression coefficients, which can
be extracted by the |
sigma2 |
the MLE of the innovations variance. |
var.coef |
the estimated variance matrix of the coefficients
|
loglik |
the maximized log-likelihood (of the differenced data), or the approximation to it used. |
arma |
A compact form of the specification, as a vector giving the number of AR, MA, seasonal AR and seasonal MA coefficients, plus the period and the number of non-seasonal and seasonal differences. |
aic |
the AIC value corresponding to the log-likelihood. Only
valid for |
residuals |
the fitted innovations. |
call |
the matched call. |
series |
the name of the series |
code |
the convergence value returned by |
n.cond |
the number of initial observations not used in the fitting. |
nobs |
the number of “used” observations for the fitting,
can also be extracted via |
model |
A list representing the Kalman filter used in the
fitting. See |
The exact likelihood is computed via a state-space representation of
the ARIMA process, and the innovations and their variance found by a
Kalman filter. The initialization of the differenced ARMA process uses
stationarity and is based on Gardner et al. (1980). For a
differenced process the non-stationary components are given a diffuse
prior (controlled by kappa
). Observations which are still
controlled by the diffuse prior (determined by having a Kalman gain of
at least 1e4
) are excluded from the likelihood calculations.
(This gives comparable results to arima0
in the absence
of missing values, when the observations excluded are precisely those
dropped by the differencing.)
Missing values are allowed, and are handled exactly in method "ML"
.
If transform.pars
is true, the optimization is done using an
alternative parametrization which is a variation on that suggested by
Jones (1980) and ensures that the model is stationary. For an AR(p)
model the parametrization is via the inverse tanh of the partial
autocorrelations: the same procedure is applied (separately) to the
AR and seasonal AR terms. The MA terms are not constrained to be
invertible during optimization, but they will be converted to
invertible form after optimization if transform.pars
is true.
Conditional sum-of-squares is provided mainly for expositional
purposes. This computes the sum of squares of the fitted innovations
from observation n.cond
on, (where n.cond
is at least
the maximum lag of an AR term), treating all earlier innovations to
be zero. Argument n.cond
can be used to allow comparability
between different fits. The ‘part log-likelihood’ is the first
term, half the log of the estimated mean square. Missing values
are allowed, but will cause many of the innovations to be missing.
When regressors are specified, they are orthogonalized prior to fitting unless any of the coefficients is fixed. It can be helpful to roughly scale the regressors to zero mean and unit variance.
arima
is very similar to arima0
for
ARMA models or for differenced models without missing values,
but handles differenced models with missing values exactly.
It is somewhat slower than arima0
, particularly for seasonally
differenced models.
Brockwell, P. J. and Davis, R. A. (1996). Introduction to Time Series and Forecasting. Springer, New York. Sections 3.3 and 8.3.
Durbin, J. and Koopman, S. J. (2001). Time Series Analysis by State Space Methods. Oxford University Press.
Gardner, G, Harvey, A. C. and Phillips, G. D. A. (1980). Algorithm AS 154: An algorithm for exact maximum likelihood estimation of autoregressive-moving average models by means of Kalman filtering. Applied Statistics, 29, 311–322. doi:10.2307/2346910.
Harvey, A. C. (1993). Time Series Models. 2nd Edition. Harvester Wheatsheaf. Sections 3.3 and 4.4.
Jones, R. H. (1980). Maximum likelihood fitting of ARMA models to time series with missing observations. Technometrics, 22, 389–395. doi:10.2307/1268324.
Ripley, B. D. (2002). “Time series in R 1.5.0”. R News, 2(2), 2–7. https://www.r-project.org/doc/Rnews/Rnews_2002-2.pdf
predict.Arima
, arima.sim
for simulating
from an ARIMA model, tsdiag
, arima0
,
ar
arima(lh, order = c(1,0,0)) arima(lh, order = c(3,0,0)) arima(lh, order = c(1,0,1)) arima(lh, order = c(3,0,0), method = "CSS") arima(USAccDeaths, order = c(0,1,1), seasonal = list(order = c(0,1,1))) arima(USAccDeaths, order = c(0,1,1), seasonal = list(order = c(0,1,1)), method = "CSS") # drops first 13 observations. # for a model with as few years as this, we want full ML arima(LakeHuron, order = c(2,0,0), xreg = time(LakeHuron) - 1920) ## presidents contains NAs ## graphs in example(acf) suggest order 1 or 3 require(graphics) (fit1 <- arima(presidents, c(1, 0, 0))) nobs(fit1) tsdiag(fit1) (fit3 <- arima(presidents, c(3, 0, 0))) # smaller AIC tsdiag(fit3) BIC(fit1, fit3) ## compare a whole set of models; BIC() would choose the smallest AIC(fit1, arima(presidents, c(2,0,0)), arima(presidents, c(2,0,1)), # <- chosen (barely) by AIC fit3, arima(presidents, c(3,0,1))) ## An example of using the 'fixed' argument: ## Note that the period of the seasonal component is taken to be ## frequency(presidents), i.e. 4. (fitSfx <- arima(presidents, order=c(2,0,1), seasonal=c(1,0,0), fixed=c(NA, NA, 0.5, -0.1, 50), transform.pars=FALSE)) ## The partly-fixed & smaller model seems better (as we "knew too much"): AIC(fitSfx, arima(presidents, order=c(2,0,1), seasonal=c(1,0,0))) ## An example of ARIMA forecasting: predict(fit3, 3)
arima(lh, order = c(1,0,0)) arima(lh, order = c(3,0,0)) arima(lh, order = c(1,0,1)) arima(lh, order = c(3,0,0), method = "CSS") arima(USAccDeaths, order = c(0,1,1), seasonal = list(order = c(0,1,1))) arima(USAccDeaths, order = c(0,1,1), seasonal = list(order = c(0,1,1)), method = "CSS") # drops first 13 observations. # for a model with as few years as this, we want full ML arima(LakeHuron, order = c(2,0,0), xreg = time(LakeHuron) - 1920) ## presidents contains NAs ## graphs in example(acf) suggest order 1 or 3 require(graphics) (fit1 <- arima(presidents, c(1, 0, 0))) nobs(fit1) tsdiag(fit1) (fit3 <- arima(presidents, c(3, 0, 0))) # smaller AIC tsdiag(fit3) BIC(fit1, fit3) ## compare a whole set of models; BIC() would choose the smallest AIC(fit1, arima(presidents, c(2,0,0)), arima(presidents, c(2,0,1)), # <- chosen (barely) by AIC fit3, arima(presidents, c(3,0,1))) ## An example of using the 'fixed' argument: ## Note that the period of the seasonal component is taken to be ## frequency(presidents), i.e. 4. (fitSfx <- arima(presidents, order=c(2,0,1), seasonal=c(1,0,0), fixed=c(NA, NA, 0.5, -0.1, 50), transform.pars=FALSE)) ## The partly-fixed & smaller model seems better (as we "knew too much"): AIC(fitSfx, arima(presidents, order=c(2,0,1), seasonal=c(1,0,0))) ## An example of ARIMA forecasting: predict(fit3, 3)
Simulate from an ARIMA model.
arima.sim(model, n, rand.gen = rnorm, innov = rand.gen(n, ...), n.start = NA, start.innov = rand.gen(n.start, ...), ...)
arima.sim(model, n, rand.gen = rnorm, innov = rand.gen(n, ...), n.start = NA, start.innov = rand.gen(n.start, ...), ...)
model |
A list with component |
n |
length of output series, before un-differencing. A strictly positive integer. |
rand.gen |
optional: a function to generate the innovations. |
innov |
an optional times series of innovations. If not
provided, |
n.start |
length of ‘burn-in’ period. If |
start.innov |
an optional times series of innovations to be used
for the burn-in period. If supplied there must be at least
|
... |
additional arguments for |
See arima
for the precise definition of an ARIMA model.
The ARMA model is checked for stationarity.
ARIMA models are specified via the order
component of
model
, in the same way as for arima
. Other
aspects of the order
component are ignored, but inconsistent
specifications of the MA and AR orders are detected. The
un-differencing assumes previous values of zero, and to remind the
user of this, those values are returned.
Random inputs for the ‘burn-in’ period are generated by calling
rand.gen
.
A time-series object of class "ts"
.
require(graphics) arima.sim(n = 63, list(ar = c(0.8897, -0.4858), ma = c(-0.2279, 0.2488)), sd = sqrt(0.1796)) # mildly long-tailed arima.sim(n = 63, list(ar = c(0.8897, -0.4858), ma = c(-0.2279, 0.2488)), rand.gen = function(n, ...) sqrt(0.1796) * rt(n, df = 5)) # An ARIMA simulation ts.sim <- arima.sim(list(order = c(1,1,0), ar = 0.7), n = 200) ts.plot(ts.sim)
require(graphics) arima.sim(n = 63, list(ar = c(0.8897, -0.4858), ma = c(-0.2279, 0.2488)), sd = sqrt(0.1796)) # mildly long-tailed arima.sim(n = 63, list(ar = c(0.8897, -0.4858), ma = c(-0.2279, 0.2488)), rand.gen = function(n, ...) sqrt(0.1796) * rt(n, df = 5)) # An ARIMA simulation ts.sim <- arima.sim(list(order = c(1,1,0), ar = 0.7), n = 200) ts.plot(ts.sim)
Fit an ARIMA model to a univariate time series, and forecast from the fitted model.
arima0(x, order = c(0, 0, 0), seasonal = list(order = c(0, 0, 0), period = NA), xreg = NULL, include.mean = TRUE, delta = 0.01, transform.pars = TRUE, fixed = NULL, init = NULL, method = c("ML", "CSS"), n.cond, optim.control = list()) ## S3 method for class 'arima0' predict(object, n.ahead = 1, newxreg, se.fit = TRUE, ...)
arima0(x, order = c(0, 0, 0), seasonal = list(order = c(0, 0, 0), period = NA), xreg = NULL, include.mean = TRUE, delta = 0.01, transform.pars = TRUE, fixed = NULL, init = NULL, method = c("ML", "CSS"), n.cond, optim.control = list()) ## S3 method for class 'arima0' predict(object, n.ahead = 1, newxreg, se.fit = TRUE, ...)
x |
a univariate time series |
order |
A specification of the non-seasonal part of the ARIMA
model: the three components |
seasonal |
A specification of the seasonal part of the ARIMA
model, plus the period (which defaults to |
xreg |
Optionally, a vector or matrix of external regressors,
which must have the same number of rows as |
include.mean |
Should the ARIMA model include
a mean term? The default is |
delta |
A value to indicate at which point ‘fast recursions’ should be used. See the ‘Details’ section. |
transform.pars |
Logical. If true, the AR parameters are
transformed to ensure that they remain in the region of
stationarity. Not used for |
fixed |
optional numeric vector of the same length as the total
number of parameters. If supplied, only |
init |
optional numeric vector of initial parameter
values. Missing values will be filled in, by zeroes except for
regression coefficients. Values already specified in |
method |
Fitting method: maximum likelihood or minimize conditional sum-of-squares. Can be abbreviated. |
n.cond |
Only used if fitting by conditional-sum-of-squares: the number of initial observations to ignore. It will be ignored if less than the maximum lag of an AR term. |
optim.control |
List of control parameters for |
object |
The result of an |
newxreg |
New values of |
n.ahead |
The number of steps ahead for which prediction is required. |
se.fit |
Logical: should standard errors of prediction be returned? |
... |
arguments passed to or from other methods. |
Different definitions of ARMA models have different signs for the AR and/or MA coefficients. The definition here has
and so the MA coefficients differ in sign from those given by
S-PLUS. Further, if include.mean
is true, this formula
applies to rather than
. For ARIMA models with
differencing, the differenced series follows a zero-mean ARMA model.
The variance matrix of the estimates is found from the Hessian of the log-likelihood, and so may only be a rough guide, especially for fits close to the boundary of invertibility.
Optimization is done by optim
. It will work
best if the columns in xreg
are roughly scaled to zero mean
and unit variance, but does attempt to estimate suitable scalings.
Finite-history prediction is used. This is only statistically
efficient if the MA part of the fit is invertible, so
predict.arima0
will give a warning for non-invertible MA
models.
For arima0
, a list of class "arima0"
with components:
coef |
a vector of AR, MA and regression coefficients, |
sigma2 |
the MLE of the innovations variance. |
var.coef |
the estimated variance matrix of the coefficients
|
loglik |
the maximized log-likelihood (of the differenced data), or the approximation to it used. |
arma |
A compact form of the specification, as a vector giving the number of AR, MA, seasonal AR and seasonal MA coefficients, plus the period and the number of non-seasonal and seasonal differences. |
aic |
the AIC value corresponding to the log-likelihood. Only
valid for |
residuals |
the fitted innovations. |
call |
the matched call. |
series |
the name of the series |
convergence |
the value returned by |
n.cond |
the number of initial observations not used in the fitting. |
For predict.arima0
, a time series of predictions, or if
se.fit = TRUE
, a list with components pred
, the
predictions, and se
, the estimated standard errors. Both
components are time series.
The exact likelihood is computed via a state-space representation of
the ARMA process, and the innovations and their variance found by a
Kalman filter based on Gardner et al. (1980). This has
the option to switch to ‘fast recursions’ (assume an
effectively infinite past) if the innovations variance is close
enough to its asymptotic bound. The argument delta
sets the
tolerance: at its default value the approximation is normally
negligible and the speed-up considerable. Exact computations can be
ensured by setting delta
to a negative value.
If transform.pars
is true, the optimization is done using an
alternative parametrization which is a variation on that suggested by
Jones (1980) and ensures that the model is stationary. For an AR(p)
model the parametrization is via the inverse tanh of the partial
autocorrelations: the same procedure is applied (separately) to the
AR and seasonal AR terms. The MA terms are also constrained to be
invertible during optimization by the same transformation if
transform.pars
is true. Note that the MLE for MA terms does
sometimes occur for MA polynomials with unit roots: such models can be
fitted by using transform.pars = FALSE
and specifying a good
set of initial values (often obtainable from a fit with
transform.pars = TRUE
).
Missing values are allowed, but any missing values
will force delta
to be ignored and full recursions used.
Note that missing values will be propagated by differencing, so the
procedure used in this function is not fully efficient in that case.
Conditional sum-of-squares is provided mainly for expositional
purposes. This computes the sum of squares of the fitted innovations
from observation
n.cond
on, (where n.cond
is at least the maximum lag of
an AR term), treating all earlier innovations to be zero. Argument
n.cond
can be used to allow comparability between different
fits. The ‘part log-likelihood’ is the first term, half the
log of the estimated mean square. Missing values are allowed, but
will cause many of the innovations to be missing.
When regressors are specified, they are orthogonalized prior to fitting unless any of the coefficients is fixed. It can be helpful to roughly scale the regressors to zero mean and unit variance.
This is a preliminary version, and will be replaced by arima
.
The standard errors of prediction exclude the uncertainty in the estimation of the ARMA model and the regression coefficients.
The results are likely to be different from S-PLUS's
arima.mle
, which computes a conditional likelihood and does
not include a mean in the model. Further, the convention used by
arima.mle
reverses the signs of the MA coefficients.
Brockwell, P. J. and Davis, R. A. (1996). Introduction to Time Series and Forecasting. Springer, New York. Sections 3.3 and 8.3.
Gardner, G, Harvey, A. C. and Phillips, G. D. A. (1980). Algorithm AS 154: An algorithm for exact maximum likelihood estimation of autoregressive-moving average models by means of Kalman filtering. Applied Statistics, 29, 311–322. doi:10.2307/2346910.
Harvey, A. C. (1993). Time Series Models. 2nd Edition. Harvester Wheatsheaf. Sections 3.3 and 4.4.
Harvey, A. C. and McKenzie, C. R. (1982). Algorithm AS 182: An algorithm for finite sample prediction from ARIMA processes. Applied Statistics, 31, 180–187. doi:10.2307/2347987.
Jones, R. H. (1980). Maximum likelihood fitting of ARMA models to time series with missing observations. Technometrics, 22, 389–395. doi:10.2307/1268324.
## Not run: arima0(lh, order = c(1,0,0)) arima0(lh, order = c(3,0,0)) arima0(lh, order = c(1,0,1)) predict(arima0(lh, order = c(3,0,0)), n.ahead = 12) arima0(lh, order = c(3,0,0), method = "CSS") # for a model with as few years as this, we want full ML (fit <- arima0(USAccDeaths, order = c(0,1,1), seasonal = list(order=c(0,1,1)), delta = -1)) predict(fit, n.ahead = 6) arima0(LakeHuron, order = c(2,0,0), xreg = time(LakeHuron)-1920) ## Not run: ## presidents contains NAs ## graphs in example(acf) suggest order 1 or 3 (fit1 <- arima0(presidents, c(1, 0, 0), delta = -1)) # avoid warning tsdiag(fit1) (fit3 <- arima0(presidents, c(3, 0, 0), delta = -1)) # smaller AIC tsdiag(fit3) ## End(Not run)
## Not run: arima0(lh, order = c(1,0,0)) arima0(lh, order = c(3,0,0)) arima0(lh, order = c(1,0,1)) predict(arima0(lh, order = c(3,0,0)), n.ahead = 12) arima0(lh, order = c(3,0,0), method = "CSS") # for a model with as few years as this, we want full ML (fit <- arima0(USAccDeaths, order = c(0,1,1), seasonal = list(order=c(0,1,1)), delta = -1)) predict(fit, n.ahead = 6) arima0(LakeHuron, order = c(2,0,0), xreg = time(LakeHuron)-1920) ## Not run: ## presidents contains NAs ## graphs in example(acf) suggest order 1 or 3 (fit1 <- arima0(presidents, c(1, 0, 0), delta = -1)) # avoid warning tsdiag(fit1) (fit3 <- arima0(presidents, c(3, 0, 0), delta = -1)) # smaller AIC tsdiag(fit3) ## End(Not run)
Compute the theoretical autocorrelation function or partial autocorrelation function for an ARMA process.
ARMAacf(ar = numeric(), ma = numeric(), lag.max = r, pacf = FALSE)
ARMAacf(ar = numeric(), ma = numeric(), lag.max = r, pacf = FALSE)
ar |
numeric vector of AR coefficients |
ma |
numeric vector of MA coefficients |
lag.max |
integer. Maximum lag required. Defaults to
|
pacf |
logical. Should the partial autocorrelations be returned? |
The methods used follow
Brockwell & Davis (1991, section 3.3). Their
equations (3.3.8) are solved for the autocovariances at lags
,
and the remaining autocorrelations are given by a recursive filter.
A vector of (partial) autocorrelations, named by the lags.
Brockwell, P. J. and Davis, R. A. (1991) Time Series: Theory and Methods, Second Edition. Springer.
arima
, ARMAtoMA
,
acf2AR
for inverting part of ARMAacf
; further
filter
.
ARMAacf(c(1.0, -0.25), 1.0, lag.max = 10) ## Example from Brockwell & Davis (1991, pp.92-4) ## answer: 2^(-n) * (32/3 + 8 * n) /(32/3) n <- 1:10 a.n <- 2^(-n) * (32/3 + 8 * n) /(32/3) (A.n <- ARMAacf(c(1.0, -0.25), 1.0, lag.max = 10)) stopifnot(all.equal(unname(A.n), c(1, a.n))) ARMAacf(c(1.0, -0.25), 1.0, lag.max = 10, pacf = TRUE) zapsmall(ARMAacf(c(1.0, -0.25), lag.max = 10, pacf = TRUE)) ## Cov-Matrix of length-7 sub-sample of AR(1) example: toeplitz(ARMAacf(0.8, lag.max = 7))
ARMAacf(c(1.0, -0.25), 1.0, lag.max = 10) ## Example from Brockwell & Davis (1991, pp.92-4) ## answer: 2^(-n) * (32/3 + 8 * n) /(32/3) n <- 1:10 a.n <- 2^(-n) * (32/3 + 8 * n) /(32/3) (A.n <- ARMAacf(c(1.0, -0.25), 1.0, lag.max = 10)) stopifnot(all.equal(unname(A.n), c(1, a.n))) ARMAacf(c(1.0, -0.25), 1.0, lag.max = 10, pacf = TRUE) zapsmall(ARMAacf(c(1.0, -0.25), lag.max = 10, pacf = TRUE)) ## Cov-Matrix of length-7 sub-sample of AR(1) example: toeplitz(ARMAacf(0.8, lag.max = 7))
Convert ARMA process to infinite MA process.
ARMAtoMA(ar = numeric(), ma = numeric(), lag.max)
ARMAtoMA(ar = numeric(), ma = numeric(), lag.max)
ar |
numeric vector of AR coefficients |
ma |
numeric vector of MA coefficients |
lag.max |
Largest MA(Inf) coefficient required. |
A vector of coefficients.
Brockwell, P. J. and Davis, R. A. (1991) Time Series: Theory and Methods, Second Edition. Springer.
ARMAtoMA(c(1.0, -0.25), 1.0, 10) ## Example from Brockwell & Davis (1991, p.92) ## answer (1 + 3*n)*2^(-n) n <- 1:10; (1 + 3*n)*2^(-n)
ARMAtoMA(c(1.0, -0.25), 1.0, 10) ## Example from Brockwell & Davis (1991, p.92) ## answer (1 + 3*n)*2^(-n) n <- 1:10; (1 + 3*n)*2^(-n)
"hclust"
Converts objects from other hierarchical clustering functions to
class "hclust"
.
as.hclust(x, ...)
as.hclust(x, ...)
x |
Hierarchical clustering object |
... |
further arguments passed to or from other methods. |
Currently there is only support for converting objects of
class "twins"
as produced by the functions diana
and
agnes
from the package cluster. The default method
throws an error unless passed an "hclust"
object.
An object of class "hclust"
.
hclust
, and from package cluster,
diana
and agnes
x <- matrix(rnorm(30), ncol = 3) hc <- hclust(dist(x), method = "complete") if(require("cluster", quietly = TRUE)) {# is a recommended package ag <- agnes(x, method = "complete") hcag <- as.hclust(ag) ## The dendrograms order slightly differently: op <- par(mfrow = c(1,2)) plot(hc) ; mtext("hclust", side = 1) plot(hcag); mtext("agnes", side = 1) detach("package:cluster") }
x <- matrix(rnorm(30), ncol = 3) hc <- hclust(dist(x), method = "complete") if(require("cluster", quietly = TRUE)) {# is a recommended package ag <- agnes(x, method = "complete") hcag <- as.hclust(ag) ## The dendrograms order slightly differently: op <- par(mfrow = c(1,2)) plot(hc) ; mtext("hclust", side = 1) plot(hcag); mtext("agnes", side = 1) detach("package:cluster") }
Names, calls, expressions (first element), numeric values, and character strings are converted to one-sided formulae associated with the global environment. If the input is a formula, it must be one-sided, in which case it is returned unaltered.
asOneSidedFormula(object)
asOneSidedFormula(object)
object |
a one-sided formula, name, call, expression, numeric value, or character string. |
a one-sided formula representing object
José Pinheiro and Douglas Bates
(form <- asOneSidedFormula("age")) stopifnot(exprs = { identical(form, asOneSidedFormula(form)) identical(form, asOneSidedFormula(as.name("age"))) identical(form, asOneSidedFormula(expression(age))) }) asOneSidedFormula(quote(log(age))) asOneSidedFormula(1)
(form <- asOneSidedFormula("age")) stopifnot(exprs = { identical(form, asOneSidedFormula(form)) identical(form, asOneSidedFormula(as.name("age"))) identical(form, asOneSidedFormula(expression(age))) }) asOneSidedFormula(quote(log(age))) asOneSidedFormula(1)
Subsets of x[]
are averaged, where each subset consist of those
observations with the same factor levels.
ave(x, ..., FUN = mean)
ave(x, ..., FUN = mean)
x |
A numeric. |
... |
Grouping variables, typically factors, all of the same
|
FUN |
Function to apply for each factor level combination. |
A numeric vector, say y
of length length(x)
.
If ...
is g1, g2
, e.g.,
y[i]
is equal to FUN(x[j]
, for all j
with
g1[j] == g1[i]
and g2[j] == g2[i])
.
require(graphics) ave(1:3) # no grouping -> grand mean attach(warpbreaks) ave(breaks, wool) ave(breaks, tension) ave(breaks, tension, FUN = function(x) mean(x, trim = 0.1)) plot(breaks, main = "ave( Warpbreaks ) for wool x tension combinations") lines(ave(breaks, wool, tension ), type = "s", col = "blue") lines(ave(breaks, wool, tension, FUN = median), type = "s", col = "green") legend(40, 70, c("mean", "median"), lty = 1, col = c("blue","green"), bg = "gray90") detach()
require(graphics) ave(1:3) # no grouping -> grand mean attach(warpbreaks) ave(breaks, wool) ave(breaks, tension) ave(breaks, tension, FUN = function(x) mean(x, trim = 0.1)) plot(breaks, main = "ave( Warpbreaks ) for wool x tension combinations") lines(ave(breaks, wool, tension ), type = "s", col = "blue") lines(ave(breaks, wool, tension, FUN = median), type = "s", col = "green") legend(40, 70, c("mean", "median"), lty = 1, col = c("blue","green"), bg = "gray90") detach()
Bandwidth selectors for Gaussian kernels in density
.
bw.nrd0(x) bw.nrd(x) bw.ucv(x, nb = 1000, lower = 0.1 * hmax, upper = hmax, tol = 0.1 * lower) bw.bcv(x, nb = 1000, lower = 0.1 * hmax, upper = hmax, tol = 0.1 * lower) bw.SJ(x, nb = 1000, lower = 0.1 * hmax, upper = hmax, method = c("ste", "dpi"), tol = 0.1 * lower)
bw.nrd0(x) bw.nrd(x) bw.ucv(x, nb = 1000, lower = 0.1 * hmax, upper = hmax, tol = 0.1 * lower) bw.bcv(x, nb = 1000, lower = 0.1 * hmax, upper = hmax, tol = 0.1 * lower) bw.SJ(x, nb = 1000, lower = 0.1 * hmax, upper = hmax, method = c("ste", "dpi"), tol = 0.1 * lower)
x |
numeric vector. |
nb |
number of bins to use. |
lower , upper
|
range over which to minimize. The default is
almost always satisfactory. |
method |
either |
tol |
for method |
bw.nrd0
implements a rule-of-thumb for
choosing the bandwidth of a Gaussian kernel density estimator.
It defaults to 0.9 times the
minimum of the standard deviation and the interquartile range divided by
1.34 times the sample size to the negative one-fifth power
(= Silverman's ‘rule of thumb’, Silverman (1986, page 48, eqn (3.31)))
unless the quartiles coincide when a positive result
will be guaranteed.
bw.nrd
is the more common variation given by Scott (1992),
using factor 1.06.
bw.ucv
and bw.bcv
implement unbiased and
biased cross-validation respectively.
bw.SJ
implements the methods of Sheather & Jones (1991)
to select the bandwidth using pilot estimation of derivatives.
The algorithm for method "ste"
solves an equation (via
uniroot
) and because of that, enlarges the interval
c(lower, upper)
when the boundaries were not user-specified and
do not bracket the root.
The last three methods use all pairwise binned distances: they are of
complexity up to
n = nb/2
and
thereafter. Because of the binning, the results differ slightly when
x
is translated or sign-flipped.
A bandwidth on a scale suitable for the bw
argument
of density
.
Long vectors x
are not supported, but neither are they by
density
and kernel density estimation and for more than
a few thousand points a histogram would be preferred.
B. D. Ripley, taken from early versions of package MASS.
Scott, D. W. (1992) Multivariate Density Estimation: Theory, Practice, and Visualization. New York: Wiley.
Sheather, S. J. and Jones, M. C. (1991). A reliable data-based bandwidth selection method for kernel density estimation. Journal of the Royal Statistical Society Series B, 53, 683–690. doi:10.1111/j.2517-6161.1991.tb01857.x.
Silverman, B. W. (1986). Density Estimation. London: Chapman and Hall.
Venables, W. N. and Ripley, B. D. (2002). Modern Applied Statistics with S. Springer.
bandwidth.nrd
, ucv
,
bcv
and width.SJ
in
package MASS, which are all scaled to the width
argument
of density
and so give answers four times as large.
require(graphics) plot(density(precip, n = 1000)) rug(precip) lines(density(precip, bw = "nrd"), col = 2) lines(density(precip, bw = "ucv"), col = 3) lines(density(precip, bw = "bcv"), col = 4) lines(density(precip, bw = "SJ-ste"), col = 5) lines(density(precip, bw = "SJ-dpi"), col = 6) legend(55, 0.035, legend = c("nrd0", "nrd", "ucv", "bcv", "SJ-ste", "SJ-dpi"), col = 1:6, lty = 1)
require(graphics) plot(density(precip, n = 1000)) rug(precip) lines(density(precip, bw = "nrd"), col = 2) lines(density(precip, bw = "ucv"), col = 3) lines(density(precip, bw = "bcv"), col = 4) lines(density(precip, bw = "SJ-ste"), col = 5) lines(density(precip, bw = "SJ-dpi"), col = 6) legend(55, 0.035, legend = c("nrd0", "nrd", "ucv", "bcv", "SJ-ste", "SJ-dpi"), col = 1:6, lty = 1)
Performs Bartlett's test of the null that the variances in each of the groups (samples) are the same.
bartlett.test(x, ...) ## Default S3 method: bartlett.test(x, g, ...) ## S3 method for class 'formula' bartlett.test(formula, data, subset, na.action, ...)
bartlett.test(x, ...) ## Default S3 method: bartlett.test(x, g, ...) ## S3 method for class 'formula' bartlett.test(formula, data, subset, na.action, ...)
x |
a numeric vector of data values, or a list of numeric data
vectors representing the respective samples, or fitted linear model
objects (inheriting from class |
g |
a vector or factor object giving the group for the
corresponding elements of |
formula |
a formula of the form |
data |
an optional matrix or data frame (or similar: see
|
subset |
an optional vector specifying a subset of observations to be used. |
na.action |
a function which indicates what should happen when
the data contain |
... |
further arguments to be passed to or from methods. |
If x
is a list, its elements are taken as the samples or fitted
linear models to be compared for homogeneity of variances. In this
case, the elements must either all be numeric data vectors or fitted
linear model objects, g
is ignored, and one can simply use
bartlett.test(x)
to perform the test. If the samples are not
yet contained in a list, use bartlett.test(list(x, ...))
.
Otherwise, x
must be a numeric data vector, and g
must
be a vector or factor object of the same length as x
giving the
group for the corresponding elements of x
.
A list of class "htest"
containing the following components:
statistic |
Bartlett's K-squared test statistic. |
parameter |
the degrees of freedom of the approximate chi-squared distribution of the test statistic. |
p.value |
the p-value of the test. |
method |
the character string
|
data.name |
a character string giving the names of the data. |
Bartlett, M. S. (1937). Properties of sufficiency and statistical tests. Proceedings of the Royal Society of London Series A 160, 268–282. doi:10.1098/rspa.1937.0109.
var.test
for the special case of comparing variances in
two samples from normal distributions;
fligner.test
for a rank-based (nonparametric)
-sample test for homogeneity of variances;
ansari.test
and mood.test
for two rank
based two-sample tests for difference in scale.
require(graphics) plot(count ~ spray, data = InsectSprays) bartlett.test(InsectSprays$count, InsectSprays$spray) bartlett.test(count ~ spray, data = InsectSprays)
require(graphics) plot(count ~ spray, data = InsectSprays) bartlett.test(InsectSprays$count, InsectSprays$spray) bartlett.test(count ~ spray, data = InsectSprays)
Density, distribution function, quantile function and random
generation for the Beta distribution with parameters shape1
and
shape2
(and optional non-centrality parameter ncp
).
dbeta(x, shape1, shape2, ncp = 0, log = FALSE) pbeta(q, shape1, shape2, ncp = 0, lower.tail = TRUE, log.p = FALSE) qbeta(p, shape1, shape2, ncp = 0, lower.tail = TRUE, log.p = FALSE) rbeta(n, shape1, shape2, ncp = 0)
dbeta(x, shape1, shape2, ncp = 0, log = FALSE) pbeta(q, shape1, shape2, ncp = 0, lower.tail = TRUE, log.p = FALSE) qbeta(p, shape1, shape2, ncp = 0, lower.tail = TRUE, log.p = FALSE) rbeta(n, shape1, shape2, ncp = 0)
x , q
|
vector of quantiles. |
p |
vector of probabilities. |
n |
number of observations. If |
shape1 , shape2
|
non-negative parameters of the Beta distribution. |
ncp |
non-centrality parameter. |
log , log.p
|
logical; if TRUE, probabilities p are given as log(p). |
lower.tail |
logical; if TRUE (default), probabilities are
|
The Beta distribution with parameters shape1
and
shape2
has density
for ,
and
where the boundary values at
or
are defined as
by continuity (as limits).
The mean is and the variance is
.
If
, (or one of them
), the mode is
.
These and all other distributional properties can be defined as
limits (leading to point masses at 0, 1/2, or 1) when
or
are zero or infinite, and the corresponding
[dpqr]beta()
functions are defined correspondingly.
pbeta
is closely related to the incomplete beta function. As
defined by Abramowitz and Stegun 6.6.1
and 6.6.2 where
is the Beta function (
beta
).
is
pbeta(x, a, b)
.
The noncentral Beta distribution (with ncp
)
is defined (Johnson et al., 1995, pp. 502) as the distribution of
where
and
.
There,
is the noncentral
chi-squared distribution with
degrees of freedom and
non-centrality parameter
, see Chisquare.
dbeta
gives the density, pbeta
the distribution
function, qbeta
the quantile function, and rbeta
generates random deviates.
Invalid arguments will result in return value NaN
, with a warning.
The length of the result is determined by n
for
rbeta
, and is the maximum of the lengths of the
numerical arguments for the other functions.
The numerical arguments other than n
are recycled to the
length of the result. Only the first elements of the logical
arguments are used.
Supplying ncp = 0
uses the algorithm for the non-central
distribution, which is not the same algorithm as when ncp
is
omitted. This is to give consistent behaviour in extreme cases with
values of ncp
very near zero.
The central dbeta
is based on a binomial probability, using code
contributed by Catherine Loader (see dbinom
) if either
shape parameter is larger than one, otherwise directly from the definition.
The non-central case is based on the derivation as a Poisson
mixture of betas (Johnson et al., 1995, pp. 502–3).
The central pbeta
for the default (log_p = FALSE
)
uses a C translation based on
Didonato, A. and Morris, A., Jr, (1992)
Algorithm 708: Significant digit computation of the incomplete beta
function ratios,
ACM Transactions on Mathematical Software, 18, 360–373,
doi:10.1145/131766.131776.
(See also
Brown, B. and Lawrence Levy, L. (1994)
Certification of algorithm 708: Significant digit computation of the
incomplete beta,
ACM Transactions on Mathematical Software, 20, 393–397,
doi:10.1145/192115.192155.)
We have slightly tweaked the original “TOMS 708” algorithm, and
enhanced for log.p = TRUE
. For that (log-scale) case,
underflow to -Inf
(i.e., ) or
0
, (i.e.,
) still happens because the original algorithm was designed
without log-scale considerations. Underflow to
-Inf
now
typically signals a warning
.
The non-central pbeta
uses a C translation of
Lenth, R. V. (1987) Algorithm AS 226: Computing noncentral beta
probabilities. Applied Statistics, 36, 241–244,
doi:10.2307/2347558,
incorporating
Frick, H. (1990)'s AS R84, Applied Statistics, 39,
311–2, doi:10.2307/2347780
and
Lam, M.L. (1995)'s AS R95, Applied Statistics, 44,
551–2, doi:10.2307/2986147.
This computes the lower tail only, so the upper tail suffers from cancellation and a warning will be given when this is likely to be significant.
The central case of qbeta
is based on a C translation of
Cran, G. W., K. J. Martin and G. E. Thomas (1977). Remark AS R19 and Algorithm AS 109, Applied Statistics, 26, 111–114, doi:10.2307/2346887, and subsequent remarks (AS83 and correction).
Enhancements, notably for starting values and switching to a log-scale Newton search, by R Core.
The central case of rbeta
is based on a C translation of
R. C. H. Cheng (1978). Generating beta variates with nonintegral shape parameters. Communications of the ACM, 21, 317–322.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
Abramowitz, M. and Stegun, I. A. (1972) Handbook of Mathematical Functions. New York: Dover. Chapter 6: Gamma and Related Functions.
Johnson, N. L., Kotz, S. and Balakrishnan, N. (1995) Continuous Univariate Distributions, volume 2, especially chapter 25. Wiley, New York.
Distributions for other standard distributions.
beta
for the Beta function.
x <- seq(0, 1, length.out = 21) dbeta(x, 1, 1) pbeta(x, 1, 1) ## Visualization, including limit cases: pl.beta <- function(a,b, asp = if(isLim) 1, ylim = if(isLim) c(0,1.1)) { if(isLim <- a == 0 || b == 0 || a == Inf || b == Inf) { eps <- 1e-10 x <- c(0, eps, (1:7)/16, 1/2+c(-eps,0,eps), (9:15)/16, 1-eps, 1) } else { x <- seq(0, 1, length.out = 1025) } fx <- cbind(dbeta(x, a,b), pbeta(x, a,b), qbeta(x, a,b)) f <- fx; f[fx == Inf] <- 1e100 matplot(x, f, ylab="", type="l", ylim=ylim, asp=asp, main = sprintf("[dpq]beta(x, a=%g, b=%g)", a,b)) abline(0,1, col="gray", lty=3) abline(h = 0:1, col="gray", lty=3) legend("top", paste0(c("d","p","q"), "beta(x, a,b)"), col=1:3, lty=1:3, bty = "n") invisible(cbind(x, fx)) } pl.beta(3,1) pl.beta(2, 4) pl.beta(3, 7) pl.beta(3, 7, asp=1) pl.beta(0, 0) ## point masses at {0, 1} pl.beta(0, 2) ## point mass at 0 ; the same as pl.beta(1, Inf) pl.beta(Inf, 2) ## point mass at 1 ; the same as pl.beta(3, 0) pl.beta(Inf, Inf)# point mass at 1/2
x <- seq(0, 1, length.out = 21) dbeta(x, 1, 1) pbeta(x, 1, 1) ## Visualization, including limit cases: pl.beta <- function(a,b, asp = if(isLim) 1, ylim = if(isLim) c(0,1.1)) { if(isLim <- a == 0 || b == 0 || a == Inf || b == Inf) { eps <- 1e-10 x <- c(0, eps, (1:7)/16, 1/2+c(-eps,0,eps), (9:15)/16, 1-eps, 1) } else { x <- seq(0, 1, length.out = 1025) } fx <- cbind(dbeta(x, a,b), pbeta(x, a,b), qbeta(x, a,b)) f <- fx; f[fx == Inf] <- 1e100 matplot(x, f, ylab="", type="l", ylim=ylim, asp=asp, main = sprintf("[dpq]beta(x, a=%g, b=%g)", a,b)) abline(0,1, col="gray", lty=3) abline(h = 0:1, col="gray", lty=3) legend("top", paste0(c("d","p","q"), "beta(x, a,b)"), col=1:3, lty=1:3, bty = "n") invisible(cbind(x, fx)) } pl.beta(3,1) pl.beta(2, 4) pl.beta(3, 7) pl.beta(3, 7, asp=1) pl.beta(0, 0) ## point masses at {0, 1} pl.beta(0, 2) ## point mass at 0 ; the same as pl.beta(1, Inf) pl.beta(Inf, 2) ## point mass at 1 ; the same as pl.beta(3, 0) pl.beta(Inf, Inf)# point mass at 1/2
Performs an exact test of a simple null hypothesis about the probability of success in a Bernoulli experiment.
binom.test(x, n, p = 0.5, alternative = c("two.sided", "less", "greater"), conf.level = 0.95)
binom.test(x, n, p = 0.5, alternative = c("two.sided", "less", "greater"), conf.level = 0.95)
x |
number of successes, or a vector of length 2 giving the numbers of successes and failures, respectively. |
n |
number of trials; ignored if |
p |
hypothesized probability of success. |
alternative |
indicates the alternative hypothesis and must be
one of |
conf.level |
confidence level for the returned confidence interval. |
Confidence intervals are obtained by a procedure first given in
Clopper and Pearson (1934).
This guarantees that the confidence level
is at least conf.level
, but in general does not give the
shortest-length confidence intervals.
A list with class "htest"
containing the following components:
statistic |
the number of successes. |
parameter |
the number of trials. |
p.value |
the p-value of the test. |
conf.int |
a confidence interval for the probability of success. |
estimate |
the estimated probability of success. |
null.value |
the probability of success under the null,
|
alternative |
a character string describing the alternative hypothesis. |
method |
the character string |
data.name |
a character string giving the names of the data. |
Clopper, C. J. & Pearson, E. S. (1934). The use of confidence or fiducial limits illustrated in the case of the binomial. Biometrika, 26, 404–413. doi:10.2307/2331986.
William J. Conover (1971), Practical nonparametric statistics. New York: John Wiley & Sons. Pages 97–104.
Myles Hollander & Douglas A. Wolfe (1973), Nonparametric Statistical Methods. New York: John Wiley & Sons. Pages 15–22.
prop.test
for a general (approximate) test for equal or
given proportions.
## Conover (1971), p. 97f. ## Under (the assumption of) simple Mendelian inheritance, a cross ## between plants of two particular genotypes produces progeny 1/4 of ## which are "dwarf" and 3/4 of which are "giant", respectively. ## In an experiment to determine if this assumption is reasonable, a ## cross results in progeny having 243 dwarf and 682 giant plants. ## If "giant" is taken as success, the null hypothesis is that p = ## 3/4 and the alternative that p != 3/4. binom.test(c(682, 243), p = 3/4) binom.test(682, 682 + 243, p = 3/4) # The same. ## => Data are in agreement with the null hypothesis.
## Conover (1971), p. 97f. ## Under (the assumption of) simple Mendelian inheritance, a cross ## between plants of two particular genotypes produces progeny 1/4 of ## which are "dwarf" and 3/4 of which are "giant", respectively. ## In an experiment to determine if this assumption is reasonable, a ## cross results in progeny having 243 dwarf and 682 giant plants. ## If "giant" is taken as success, the null hypothesis is that p = ## 3/4 and the alternative that p != 3/4. binom.test(c(682, 243), p = 3/4) binom.test(682, 682 + 243, p = 3/4) # The same. ## => Data are in agreement with the null hypothesis.
Density, distribution function, quantile function and random
generation for the binomial distribution with parameters size
and prob
.
This is conventionally interpreted as the number of ‘successes’
in size
trials.
dbinom(x, size, prob, log = FALSE) pbinom(q, size, prob, lower.tail = TRUE, log.p = FALSE) qbinom(p, size, prob, lower.tail = TRUE, log.p = FALSE) rbinom(n, size, prob)
dbinom(x, size, prob, log = FALSE) pbinom(q, size, prob, lower.tail = TRUE, log.p = FALSE) qbinom(p, size, prob, lower.tail = TRUE, log.p = FALSE) rbinom(n, size, prob)
x , q
|
vector of quantiles. |
p |
vector of probabilities. |
n |
number of observations. If |
size |
number of trials (zero or more). |
prob |
probability of success on each trial. |
log , log.p
|
logical; if TRUE, probabilities p are given as log(p). |
lower.tail |
logical; if TRUE (default), probabilities are
|
The binomial distribution with size
and
prob
has density
for .
Note that binomial coefficients can be computed by
choose
in R.
If an element of x
is not integer, the result of dbinom
is zero, with a warning.
is computed using Loader's algorithm, see the reference below.
The quantile is defined as the smallest value such that
, where
is the distribution function.
dbinom
gives the density, pbinom
gives the distribution
function, qbinom
gives the quantile function and rbinom
generates random deviates.
If size
is not an integer, NaN
is returned.
The length of the result is determined by n
for
rbinom
, and is the maximum of the lengths of the
numerical arguments for the other functions.
The numerical arguments other than n
are recycled to the
length of the result. Only the first elements of the logical
arguments are used.
For dbinom
a saddle-point expansion is used: see
Catherine Loader (2000). Fast and Accurate Computation of Binomial Probabilities; available as https://www.r-project.org/doc/reports/CLoader-dbinom-2002.pdf
pbinom
uses pbeta
.
qbinom
uses the Cornish–Fisher Expansion to include a skewness
correction to a normal approximation, followed by a search.
rbinom
(for size < .Machine$integer.max
) is based on
Kachitvichyanukul, V. and Schmeiser, B. W. (1988) Binomial random variate generation. Communications of the ACM, 31, 216–222.
For larger values it uses inversion.
Distributions for other standard distributions, including
dnbinom
for the negative binomial, and
dpois
for the Poisson distribution.
require(graphics) # Compute P(45 < X < 55) for X Binomial(100,0.5) sum(dbinom(46:54, 100, 0.5)) ## Using "log = TRUE" for an extended range : n <- 2000 k <- seq(0, n, by = 20) plot (k, dbinom(k, n, pi/10, log = TRUE), type = "l", ylab = "log density", main = "dbinom(*, log=TRUE) is better than log(dbinom(*))") lines(k, log(dbinom(k, n, pi/10)), col = "red", lwd = 2) ## extreme points are omitted since dbinom gives 0. mtext("dbinom(k, log=TRUE)", adj = 0) mtext("extended range", adj = 0, line = -1, font = 4) mtext("log(dbinom(k))", col = "red", adj = 1)
require(graphics) # Compute P(45 < X < 55) for X Binomial(100,0.5) sum(dbinom(46:54, 100, 0.5)) ## Using "log = TRUE" for an extended range : n <- 2000 k <- seq(0, n, by = 20) plot (k, dbinom(k, n, pi/10, log = TRUE), type = "l", ylab = "log density", main = "dbinom(*, log=TRUE) is better than log(dbinom(*))") lines(k, log(dbinom(k, n, pi/10)), col = "red", lwd = 2) ## extreme points are omitted since dbinom gives 0. mtext("dbinom(k, log=TRUE)", adj = 0) mtext("extended range", adj = 0, line = -1, font = 4) mtext("log(dbinom(k))", col = "red", adj = 1)
Plot a biplot on the current graphics device.
biplot(x, ...) ## Default S3 method: biplot(x, y, var.axes = TRUE, col, cex = rep(par("cex"), 2), xlabs = NULL, ylabs = NULL, expand = 1, xlim = NULL, ylim = NULL, arrow.len = 0.1, main = NULL, sub = NULL, xlab = NULL, ylab = NULL, ...)
biplot(x, ...) ## Default S3 method: biplot(x, y, var.axes = TRUE, col, cex = rep(par("cex"), 2), xlabs = NULL, ylabs = NULL, expand = 1, xlim = NULL, ylim = NULL, arrow.len = 0.1, main = NULL, sub = NULL, xlab = NULL, ylab = NULL, ...)
x |
The |
y |
The second set of points (a two-column matrix), usually associated with variables. |
var.axes |
If |
col |
A vector of length 2 giving the colours for the first and
second set of points respectively (and the corresponding axes). If a
single colour is specified it will be used for both sets. If
missing the default colour is looked for in the
|
cex |
The character expansion factor used for labelling the points. The labels can be of different sizes for the two sets by supplying a vector of length two. |
xlabs |
A vector of character strings to label the first set of
points: the default is to use the row dimname of |
ylabs |
A vector of character strings to label the second set of
points: the default is to use the row dimname of |
expand |
An expansion factor to apply when plotting the second set of points relative to the first. This can be used to tweak the scaling of the two sets to a physically comparable scale. |
arrow.len |
The length of the arrow heads on the axes plotted in
|
xlim , ylim
|
Limits for the x and y axes in the units of the first set of variables. |
main , sub , xlab , ylab , ...
|
graphical parameters. |
A biplot is plot which aims to represent both the observations and
variables of a matrix of multivariate data on the same plot. There are
many variations on biplots (see the references) and perhaps the most
widely used one is implemented by biplot.princomp
.
The function biplot.default
merely provides the
underlying code to plot two sets of variables on the same figure.
Graphical parameters can also be given to biplot
: the size of
xlabs
and ylabs
is controlled by cex
.
a plot is produced on the current graphics device.
K. R. Gabriel (1971). The biplot graphical display of matrices with application to principal component analysis. Biometrika, 58, 453–467. doi:10.2307/2334381.
J.C. Gower and D. J. Hand (1996). Biplots. Chapman & Hall.
biplot.princomp
, also for examples.
Produces a biplot (in the strict sense) from the output of
princomp
or prcomp
## S3 method for class 'prcomp' biplot(x, choices = 1:2, scale = 1, pc.biplot = FALSE, ...) ## S3 method for class 'princomp' biplot(x, choices = 1:2, scale = 1, pc.biplot = FALSE, ...)
## S3 method for class 'prcomp' biplot(x, choices = 1:2, scale = 1, pc.biplot = FALSE, ...) ## S3 method for class 'princomp' biplot(x, choices = 1:2, scale = 1, pc.biplot = FALSE, ...)
x |
an object of class |
choices |
length 2 vector specifying the components to plot. Only the default is a biplot in the strict sense. |
scale |
The variables are scaled by |
pc.biplot |
If true, use what Gabriel (1971) refers to as a "principal component
biplot", with |
... |
optional arguments to be passed to
|
This is a method for the generic function biplot
. There is
considerable confusion over the precise definitions: those of the
original paper, Gabriel (1971), are followed here. Gabriel and
Odoroff (1990) use the same definitions, but their plots actually
correspond to pc.biplot = TRUE
.
a plot is produced on the current graphics device.
Gabriel, K. R. (1971). The biplot graphical display of matrices with applications to principal component analysis. Biometrika, 58, 453–467. doi:10.2307/2334381.
Gabriel, K. R. and Odoroff, C. L. (1990). Biplots in biomedical research. Statistics in Medicine, 9, 469–485. doi:10.1002/sim.4780090502.
require(graphics) biplot(princomp(USArrests))
require(graphics) biplot(princomp(USArrests))
Computes answers to a generalised birthday paradox problem.
pbirthday
computes the probability of a coincidence and
qbirthday
computes the smallest number of observations needed
to have at least a specified probability of coincidence.
qbirthday(prob = 0.5, classes = 365, coincident = 2) pbirthday(n, classes = 365, coincident = 2)
qbirthday(prob = 0.5, classes = 365, coincident = 2) pbirthday(n, classes = 365, coincident = 2)
classes |
How many distinct categories the people could fall into |
prob |
The desired probability of coincidence |
n |
The number of people |
coincident |
The number of people to fall in the same category |
The birthday paradox is that a very small number of people, 23, suffices to have a 50–50 chance that two or more of them have the same birthday. This function generalises the calculation to probabilities other than 0.5, numbers of coincident events other than 2, and numbers of classes other than 365.
The formula used is approximate for coincident > 2
. The
approximation is very good for moderate values of prob
but less
good for very small probabilities.
qbirthday |
Minimum number of people needed for a probability of at least
|
pbirthday |
Probability of the specified coincidence. |
Diaconis, P. and Mosteller F. (1989). Methods for studying coincidences. Journal of the American Statistical Association, 84, 853–861. doi:10.1080/01621459.1989.10478847.
require(graphics) ## the standard version qbirthday() # 23 ## probability of > 2 people with the same birthday pbirthday(23, coincident = 3) ## examples from Diaconis & Mosteller p. 858. ## 'coincidence' is that husband, wife, daughter all born on the 16th qbirthday(classes = 30, coincident = 3) # approximately 18 qbirthday(coincident = 4) # exact value 187 qbirthday(coincident = 10) # exact value 1181 ## same 4-digit PIN number qbirthday(classes = 10^4) ## 0.9 probability of three or more coincident birthdays qbirthday(coincident = 3, prob = 0.9) ## Chance of 4 or more coincident birthdays in 150 people pbirthday(150, coincident = 4) ## 100 or more coincident birthdays in 1000 people: very rare pbirthday(1000, coincident = 100)
require(graphics) ## the standard version qbirthday() # 23 ## probability of > 2 people with the same birthday pbirthday(23, coincident = 3) ## examples from Diaconis & Mosteller p. 858. ## 'coincidence' is that husband, wife, daughter all born on the 16th qbirthday(classes = 30, coincident = 3) # approximately 18 qbirthday(coincident = 4) # exact value 187 qbirthday(coincident = 10) # exact value 1181 ## same 4-digit PIN number qbirthday(classes = 10^4) ## 0.9 probability of three or more coincident birthdays qbirthday(coincident = 3, prob = 0.9) ## Chance of 4 or more coincident birthdays in 150 people pbirthday(150, coincident = 4) ## 100 or more coincident birthdays in 1000 people: very rare pbirthday(1000, coincident = 100)
Compute the Box–Pierce or Ljung–Box test statistic for examining the null hypothesis of independence in a given time series. These are sometimes known as ‘portmanteau’ tests.
Box.test(x, lag = 1, type = c("Box-Pierce", "Ljung-Box"), fitdf = 0)
Box.test(x, lag = 1, type = c("Box-Pierce", "Ljung-Box"), fitdf = 0)
x |
a numeric vector or univariate time series. |
lag |
the statistic will be based on |
type |
test to be performed: partial matching is used. |
fitdf |
number of degrees of freedom to be subtracted if |
These tests are sometimes applied to the residuals from an
ARMA(p, q)
fit, in which case the references suggest a better
approximation to the null-hypothesis distribution is obtained by
setting fitdf = p+q
, provided of course that lag > fitdf
.
A list with class "htest"
containing the following components:
statistic |
the value of the test statistic. |
parameter |
the degrees of freedom of the approximate chi-squared
distribution of the test statistic (taking |
p.value |
the p-value of the test. |
method |
a character string indicating which type of test was performed. |
data.name |
a character string giving the name of the data. |
Missing values are not handled.
A. Trapletti
Box, G. E. P. and Pierce, D. A. (1970), Distribution of residual correlations in autoregressive-integrated moving average time series models. Journal of the American Statistical Association, 65, 1509–1526. doi:10.2307/2284333.
Ljung, G. M. and Box, G. E. P. (1978), On a measure of lack of fit in time series models. Biometrika, 65, 297–303. doi:10.2307/2335207.
Harvey, A. C. (1993) Time Series Models. 2nd Edition, Harvester Wheatsheaf, NY, pp. 44, 45.
x <- rnorm (100) Box.test (x, lag = 1) Box.test (x, lag = 1, type = "Ljung")
x <- rnorm (100) Box.test (x, lag = 1) Box.test (x, lag = 1, type = "Ljung")
Sets the "contrasts"
attribute for the factor.
C(object, contr, how.many, ...)
C(object, contr, how.many, ...)
object |
a factor or ordered factor |
contr |
which contrasts to use. Can be a matrix with one row for
each level of the factor or a suitable function like
|
how.many |
the number of contrasts to set, by default one less
than |
... |
additional arguments for the function |
For compatibility with S, contr
can be treatment
,
helmert
, sum
or poly
(without quotes) as shorthand
for contr.treatment
and so on.
The factor object
with the "contrasts"
attribute set.
Chambers, J. M. and Hastie, T. J. (1992) Statistical models. Chapter 2 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
## reset contrasts to defaults options(contrasts = c("contr.treatment", "contr.poly")) tens <- with(warpbreaks, C(tension, poly, 1)) attributes(tens) ## tension SHOULD be an ordered factor, but as it is not we can use aov(breaks ~ wool + tens + tension, data = warpbreaks) ## show the use of ... The default contrast is contr.treatment here summary(lm(breaks ~ wool + C(tension, base = 2), data = warpbreaks)) # following on from help(esoph) model3 <- glm(cbind(ncases, ncontrols) ~ agegp + C(tobgp, , 1) + C(alcgp, , 1), data = esoph, family = binomial()) summary(model3)
## reset contrasts to defaults options(contrasts = c("contr.treatment", "contr.poly")) tens <- with(warpbreaks, C(tension, poly, 1)) attributes(tens) ## tension SHOULD be an ordered factor, but as it is not we can use aov(breaks ~ wool + tens + tension, data = warpbreaks) ## show the use of ... The default contrast is contr.treatment here summary(lm(breaks ~ wool + C(tension, base = 2), data = warpbreaks)) # following on from help(esoph) model3 <- glm(cbind(ncases, ncontrols) ~ agegp + C(tobgp, , 1) + C(alcgp, , 1), data = esoph, family = binomial()) summary(model3)
Compute the canonical correlations between two data matrices.
cancor(x, y, xcenter = TRUE, ycenter = TRUE)
cancor(x, y, xcenter = TRUE, ycenter = TRUE)
x |
numeric matrix ( |
y |
numeric matrix ( |
xcenter |
logical or numeric vector of length |
ycenter |
analogous to |
The canonical correlation analysis seeks linear combinations of the
y
variables which are well explained by linear combinations
of the x
variables. The relationship is symmetric as
‘well explained’ is measured by correlations.
A list containing the following components:
cor |
correlations. |
xcoef |
estimated coefficients for the |
ycoef |
estimated coefficients for the |
xcenter |
the values used to adjust the |
ycenter |
the values used to adjust the |
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988). The New S Language. Wadsworth & Brooks/Cole.
Hotelling H. (1936). Relations between two sets of variables. Biometrika, 28, 321–327. doi:10.1093/biomet/28.3-4.321.
Seber, G. A. F. (1984). Multivariate Observations. New York: Wiley. Page 506f.
## signs of results are random pop <- LifeCycleSavings[, 2:3] oec <- LifeCycleSavings[, -(2:3)] cancor(pop, oec) x <- matrix(rnorm(150), 50, 3) y <- matrix(rnorm(250), 50, 5) (cxy <- cancor(x, y)) all(abs(cor(x %*% cxy$xcoef, y %*% cxy$ycoef)[,1:3] - diag(cxy $ cor)) < 1e-15) all(abs(cor(x %*% cxy$xcoef) - diag(3)) < 1e-15) all(abs(cor(y %*% cxy$ycoef) - diag(5)) < 1e-15)
## signs of results are random pop <- LifeCycleSavings[, 2:3] oec <- LifeCycleSavings[, -(2:3)] cancor(pop, oec) x <- matrix(rnorm(150), 50, 3) y <- matrix(rnorm(250), 50, 5) (cxy <- cancor(x, y)) all(abs(cor(x %*% cxy$xcoef, y %*% cxy$ycoef)[,1:3] - diag(cxy $ cor)) < 1e-15) all(abs(cor(x %*% cxy$xcoef) - diag(3)) < 1e-15) all(abs(cor(y %*% cxy$ycoef) - diag(5)) < 1e-15)
Simple utilities returning (non-missing) case names, and (non-eliminated) variable names.
case.names(object, ...) ## S3 method for class 'lm' case.names(object, full = FALSE, ...) variable.names(object, ...) ## S3 method for class 'lm' variable.names(object, full = FALSE, ...)
case.names(object, ...) ## S3 method for class 'lm' case.names(object, full = FALSE, ...) variable.names(object, ...) ## S3 method for class 'lm' variable.names(object, full = FALSE, ...)
object |
an R object, typically a fitted model. |
full |
logical; if |
... |
further arguments passed to or from other methods. |
A character vector.
lm
; further, all.names
,
all.vars
for functions with a similar name but only slightly
related purpose.
x <- 1:20 y <- setNames(x + (x/4 - 2)^3 + rnorm(20, sd = 3), paste("O", x, sep = ".")) ww <- rep(1, 20); ww[13] <- 0 summary(lmxy <- lm(y ~ x + I(x^2)+I(x^3) + I((x-10)^2), weights = ww), correlation = TRUE) variable.names(lmxy) variable.names(lmxy, full = TRUE) # includes the last case.names(lmxy) case.names(lmxy, full = TRUE) # includes the 0-weight case
x <- 1:20 y <- setNames(x + (x/4 - 2)^3 + rnorm(20, sd = 3), paste("O", x, sep = ".")) ww <- rep(1, 20); ww[13] <- 0 summary(lmxy <- lm(y ~ x + I(x^2)+I(x^3) + I((x-10)^2), weights = ww), correlation = TRUE) variable.names(lmxy) variable.names(lmxy, full = TRUE) # includes the last case.names(lmxy) case.names(lmxy, full = TRUE) # includes the 0-weight case
Density, distribution function, quantile function and random
generation for the Cauchy distribution with location parameter
location
and scale parameter scale
.
dcauchy(x, location = 0, scale = 1, log = FALSE) pcauchy(q, location = 0, scale = 1, lower.tail = TRUE, log.p = FALSE) qcauchy(p, location = 0, scale = 1, lower.tail = TRUE, log.p = FALSE) rcauchy(n, location = 0, scale = 1)
dcauchy(x, location = 0, scale = 1, log = FALSE) pcauchy(q, location = 0, scale = 1, lower.tail = TRUE, log.p = FALSE) qcauchy(p, location = 0, scale = 1, lower.tail = TRUE, log.p = FALSE) rcauchy(n, location = 0, scale = 1)
x , q
|
vector of quantiles. |
p |
vector of probabilities. |
n |
number of observations. If |
location , scale
|
location and scale parameters. |
log , log.p
|
logical; if TRUE, probabilities p are given as log(p). |
lower.tail |
logical; if TRUE (default), probabilities are
|
If location
or scale
are not specified, they assume
the default values of 0
and 1
respectively.
The Cauchy distribution with location and scale
has
density
for all .
dcauchy
, pcauchy
, and qcauchy
are respectively
the density, distribution function and quantile function of the Cauchy
distribution. rcauchy
generates random deviates from the
Cauchy.
The length of the result is determined by n
for
rcauchy
, and is the maximum of the lengths of the
numerical arguments for the other functions.
The numerical arguments other than n
are recycled to the
length of the result. Only the first elements of the logical
arguments are used.
dcauchy
, pcauchy
and qcauchy
are all calculated
from numerically stable versions of the definitions.
rcauchy
uses inversion.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
Johnson, N. L., Kotz, S. and Balakrishnan, N. (1995) Continuous Univariate Distributions, volume 1, chapter 16. Wiley, New York.
Distributions for other standard distributions, including
dt
for the t distribution which generalizes
dcauchy(*, l = 0, s = 1)
.
dcauchy(-1:4)
dcauchy(-1:4)
chisq.test
performs chi-squared contingency table tests
and goodness-of-fit tests.
chisq.test(x, y = NULL, correct = TRUE, p = rep(1/length(x), length(x)), rescale.p = FALSE, simulate.p.value = FALSE, B = 2000)
chisq.test(x, y = NULL, correct = TRUE, p = rep(1/length(x), length(x)), rescale.p = FALSE, simulate.p.value = FALSE, B = 2000)
x |
a numeric vector or matrix. |
y |
a numeric vector; ignored if |
correct |
a logical indicating whether to apply continuity
correction when computing the test statistic for 2 by 2 tables: one
half is subtracted from all |
p |
a vector of probabilities of the same length as |
rescale.p |
a logical scalar; if TRUE then |
simulate.p.value |
a logical indicating whether to compute p-values by Monte Carlo simulation. |
B |
an integer specifying the number of replicates used in the Monte Carlo test. |
If x
is a matrix with one row or column, or if x
is a
vector and y
is not given, then a goodness-of-fit test
is performed (x
is treated as a one-dimensional
contingency table). The entries of x
must be non-negative
integers. In this case, the hypothesis tested is whether the
population probabilities equal those in p
, or are all equal if
p
is not given.
If x
is a matrix with at least two rows and columns, it is
taken as a two-dimensional contingency table: the entries of x
must be non-negative integers. Otherwise, x
and y
must
be vectors or factors of the same length; cases with missing values
are removed, the objects are coerced to factors, and the contingency
table is computed from these. Then Pearson's chi-squared test is
performed of the null hypothesis that the joint distribution of the
cell counts in a 2-dimensional contingency table is the product of the
row and column marginals.
If simulate.p.value
is FALSE
, the p-value is computed
from the asymptotic chi-squared distribution of the test statistic;
continuity correction is only used in the 2-by-2 case (if correct
is TRUE
, the default). Otherwise the p-value is computed for a
Monte Carlo test (Hope, 1968) with B
replicates. The default
B = 2000
implies a minimum p-value of about 0.0005 ().
In the contingency table case, simulation is done by random sampling from the set of all contingency tables with given marginals, and works only if the marginals are strictly positive. Continuity correction is never used, and the statistic is quoted without it. Note that this is not the usual sampling situation assumed for the chi-squared test but rather that for Fisher's exact test.
In the goodness-of-fit case simulation is done by random sampling from
the discrete distribution specified by p
, each sample being
of size n = sum(x)
. This simulation is done in R and may be
slow.
A list with class "htest"
containing the following
components:
statistic |
the value the chi-squared test statistic. |
parameter |
the degrees of freedom of the approximate
chi-squared distribution of the test statistic, |
p.value |
the p-value for the test. |
method |
a character string indicating the type of test performed, and whether Monte Carlo simulation or continuity correction was used. |
data.name |
a character string giving the name(s) of the data. |
observed |
the observed counts. |
expected |
the expected counts under the null hypothesis. |
residuals |
the Pearson residuals,
|
stdres |
standardized residuals,
|
The code for Monte Carlo simulation is a C translation of the Fortran algorithm of Patefield (1981).
Hope, A. C. A. (1968). A simplified Monte Carlo significance test procedure. Journal of the Royal Statistical Society Series B, 30, 582–598. doi:10.1111/j.2517-6161.1968.tb00759.x.
Patefield, W. M. (1981). Algorithm AS 159: An efficient method of generating r x c tables with given row and column totals. Applied Statistics, 30, 91–97. doi:10.2307/2346669.
Agresti, A. (2007). An Introduction to Categorical Data Analysis, 2nd ed. New York: John Wiley & Sons. Page 38.
For goodness-of-fit testing, notably of continuous distributions,
ks.test
.
## From Agresti(2007) p.39 M <- as.table(rbind(c(762, 327, 468), c(484, 239, 477))) dimnames(M) <- list(gender = c("F", "M"), party = c("Democrat","Independent", "Republican")) (Xsq <- chisq.test(M)) # Prints test summary Xsq$observed # observed counts (same as M) Xsq$expected # expected counts under the null Xsq$residuals # Pearson residuals Xsq$stdres # standardized residuals ## Effect of simulating p-values x <- matrix(c(12, 5, 7, 7), ncol = 2) chisq.test(x)$p.value # 0.4233 chisq.test(x, simulate.p.value = TRUE, B = 10000)$p.value # around 0.29! ## Testing for population probabilities ## Case A. Tabulated data x <- c(A = 20, B = 15, C = 25) chisq.test(x) chisq.test(as.table(x)) # the same x <- c(89,37,30,28,2) p <- c(40,20,20,15,5) try( chisq.test(x, p = p) # gives an error ) chisq.test(x, p = p, rescale.p = TRUE) # works p <- c(0.40,0.20,0.20,0.19,0.01) # Expected count in category 5 # is 1.86 < 5 ==> chi square approx. chisq.test(x, p = p) # maybe doubtful, but is ok! chisq.test(x, p = p, simulate.p.value = TRUE) ## Case B. Raw data x <- trunc(5 * runif(100)) chisq.test(table(x)) # NOT 'chisq.test(x)'!
## From Agresti(2007) p.39 M <- as.table(rbind(c(762, 327, 468), c(484, 239, 477))) dimnames(M) <- list(gender = c("F", "M"), party = c("Democrat","Independent", "Republican")) (Xsq <- chisq.test(M)) # Prints test summary Xsq$observed # observed counts (same as M) Xsq$expected # expected counts under the null Xsq$residuals # Pearson residuals Xsq$stdres # standardized residuals ## Effect of simulating p-values x <- matrix(c(12, 5, 7, 7), ncol = 2) chisq.test(x)$p.value # 0.4233 chisq.test(x, simulate.p.value = TRUE, B = 10000)$p.value # around 0.29! ## Testing for population probabilities ## Case A. Tabulated data x <- c(A = 20, B = 15, C = 25) chisq.test(x) chisq.test(as.table(x)) # the same x <- c(89,37,30,28,2) p <- c(40,20,20,15,5) try( chisq.test(x, p = p) # gives an error ) chisq.test(x, p = p, rescale.p = TRUE) # works p <- c(0.40,0.20,0.20,0.19,0.01) # Expected count in category 5 # is 1.86 < 5 ==> chi square approx. chisq.test(x, p = p) # maybe doubtful, but is ok! chisq.test(x, p = p, simulate.p.value = TRUE) ## Case B. Raw data x <- trunc(5 * runif(100)) chisq.test(table(x)) # NOT 'chisq.test(x)'!
Density, distribution function, quantile function and random
generation for the chi-squared () distribution with
df
degrees of freedom and optional non-centrality parameter
ncp
.
dchisq(x, df, ncp = 0, log = FALSE) pchisq(q, df, ncp = 0, lower.tail = TRUE, log.p = FALSE) qchisq(p, df, ncp = 0, lower.tail = TRUE, log.p = FALSE) rchisq(n, df, ncp = 0)
dchisq(x, df, ncp = 0, log = FALSE) pchisq(q, df, ncp = 0, lower.tail = TRUE, log.p = FALSE) qchisq(p, df, ncp = 0, lower.tail = TRUE, log.p = FALSE) rchisq(n, df, ncp = 0)
x , q
|
vector of quantiles. |
p |
vector of probabilities. |
n |
number of observations. If |
df |
degrees of freedom (non-negative, but can be non-integer). |
ncp |
non-centrality parameter (non-negative). |
log , log.p
|
logical; if TRUE, probabilities p are given as log(p). |
lower.tail |
logical; if TRUE (default), probabilities are
|
The chi-squared distribution with df
degrees of freedom has density
for , where
, a point mass at zero, is not a density function proper, but
a “
distribution”.
The mean and variance are and
.
The non-central chi-squared distribution with df
degrees of freedom and non-centrality parameter
ncp
has density
for . For integer
, this is the distribution of
the sum of squares of
normals each with variance one,
being the sum of squares of the normal means; further,
,
, and
.
Note that the degrees of freedom df
, can be
non-integer, and also
which is relevant for
non-centrality
,
see Johnson et al. (1995, chapter 29).
In that (noncentral, zero df) case, the distribution is a mixture of a
point mass at
(of size
pchisq(0, df=0, ncp=ncp)
) and
a continuous part, and dchisq()
is not a density with
respect to that mixture measure but rather the limit of the density
for .
Note that ncp
values larger than about 1e5 (and even smaller) may give inaccurate
results with many warnings for pchisq
and qchisq
.
dchisq
gives the density, pchisq
gives the distribution
function, qchisq
gives the quantile function, and rchisq
generates random deviates.
Invalid arguments will result in return value NaN
, with a warning.
The length of the result is determined by n
for
rchisq
, and is the maximum of the lengths of the
numerical arguments for the other functions.
The numerical arguments other than n
are recycled to the
length of the result. Only the first elements of the logical
arguments are used.
Supplying ncp = 0
uses the algorithm for the non-central
distribution, which is not the same algorithm used if ncp
is
omitted. This is to give consistent behaviour in extreme cases with
values of ncp
very near zero.
The code for non-zero ncp
is principally intended to be used
for moderate values of ncp
: it will not be highly accurate,
especially in the tails, for large values.
The central cases are computed via the gamma distribution.
The non-central dchisq
and rchisq
are computed as a
Poisson mixture of central chi-squares (Johnson et al., 1995, p.436).
The non-central pchisq
is for ncp < 80
computed from
the Poisson mixture of central chi-squares and for larger ncp
via a C translation of
Ding, C. G. (1992) Algorithm AS275: Computing the non-central chi-squared distribution function. Applied Statistics, 41 478–482.
which computes the lower tail only (so the upper tail suffers from cancellation and a warning will be given when this is likely to be significant).
The non-central qchisq
is based on inversion of pchisq
.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
Johnson, N. L., Kotz, S. and Balakrishnan, N. (1995) Continuous Univariate Distributions, chapters 18 (volume 1) and 29 (volume 2). Wiley, New York.
Distributions for other standard distributions.
A central chi-squared distribution with degrees of freedom
is the same as a Gamma distribution with
shape
and
scale
. Hence, see
dgamma
for the Gamma distribution.
The central chi-squared distribution with 2 d.f. is identical to the
exponential distribution with rate 1/2: , see
dexp
.
require(graphics) dchisq(1, df = 1:3) pchisq(1, df = 3) pchisq(1, df = 3, ncp = 0:4) # includes the above x <- 1:10 ## Chi-squared(df = 2) is a special exponential distribution all.equal(dchisq(x, df = 2), dexp(x, 1/2)) all.equal(pchisq(x, df = 2), pexp(x, 1/2)) ## non-central RNG -- df = 0 with ncp > 0: Z0 has point mass at 0! Z0 <- rchisq(100, df = 0, ncp = 2.) graphics::stem(Z0) ## visual testing ## do P-P plots for 1000 points at various degrees of freedom L <- 1.2; n <- 1000; pp <- ppoints(n) op <- par(mfrow = c(3,3), mar = c(3,3,1,1)+.1, mgp = c(1.5,.6,0), oma = c(0,0,3,0)) for(df in 2^(4*rnorm(9))) { plot(pp, sort(pchisq(rr <- rchisq(n, df = df, ncp = L), df = df, ncp = L)), ylab = "pchisq(rchisq(.),.)", pch = ".") mtext(paste("df = ", formatC(df, digits = 4)), line = -2, adj = 0.05) abline(0, 1, col = 2) } mtext(expression("P-P plots : Noncentral "* chi^2 *"(n=1000, df=X, ncp= 1.2)"), cex = 1.5, font = 2, outer = TRUE) par(op) ## "analytical" test lam <- seq(0, 100, by = .25) p00 <- pchisq(0, df = 0, ncp = lam) p.0 <- pchisq(1e-300, df = 0, ncp = lam) stopifnot(all.equal(p00, exp(-lam/2)), all.equal(p.0, exp(-lam/2)))
require(graphics) dchisq(1, df = 1:3) pchisq(1, df = 3) pchisq(1, df = 3, ncp = 0:4) # includes the above x <- 1:10 ## Chi-squared(df = 2) is a special exponential distribution all.equal(dchisq(x, df = 2), dexp(x, 1/2)) all.equal(pchisq(x, df = 2), pexp(x, 1/2)) ## non-central RNG -- df = 0 with ncp > 0: Z0 has point mass at 0! Z0 <- rchisq(100, df = 0, ncp = 2.) graphics::stem(Z0) ## visual testing ## do P-P plots for 1000 points at various degrees of freedom L <- 1.2; n <- 1000; pp <- ppoints(n) op <- par(mfrow = c(3,3), mar = c(3,3,1,1)+.1, mgp = c(1.5,.6,0), oma = c(0,0,3,0)) for(df in 2^(4*rnorm(9))) { plot(pp, sort(pchisq(rr <- rchisq(n, df = df, ncp = L), df = df, ncp = L)), ylab = "pchisq(rchisq(.),.)", pch = ".") mtext(paste("df = ", formatC(df, digits = 4)), line = -2, adj = 0.05) abline(0, 1, col = 2) } mtext(expression("P-P plots : Noncentral "* chi^2 *"(n=1000, df=X, ncp= 1.2)"), cex = 1.5, font = 2, outer = TRUE) par(op) ## "analytical" test lam <- seq(0, 100, by = .25) p00 <- pchisq(0, df = 0, ncp = lam) p.0 <- pchisq(1e-300, df = 0, ncp = lam) stopifnot(all.equal(p00, exp(-lam/2)), all.equal(p.0, exp(-lam/2)))
Classical multidimensional scaling (MDS) of a data matrix. Also known as principal coordinates analysis (Gower, 1966).
cmdscale(d, k = 2, eig = FALSE, add = FALSE, x.ret = FALSE, list. = eig || add || x.ret)
cmdscale(d, k = 2, eig = FALSE, add = FALSE, x.ret = FALSE, list. = eig || add || x.ret)
d |
a distance structure such as that returned by |
k |
the maximum dimension of the space which the data are to be
represented in; must be in |
eig |
indicates whether eigenvalues should be returned. |
add |
logical indicating if an additive constant |
x.ret |
indicates whether the doubly centred symmetric distance matrix should be returned. |
list. |
logical indicating if a |
Multidimensional scaling takes a set of dissimilarities and returns a set of points such that the distances between the points are approximately equal to the dissimilarities. (It is a major part of what ecologists call ‘ordination’.)
A set of Euclidean distances on points can be represented
exactly in at most
dimensions.
cmdscale
follows
the analysis of Mardia (1978), and returns the best-fitting
-dimensional representation, where
may be less than the
argument
k
.
The representation is only determined up to location (cmdscale
takes the column means of the configuration to be at the origin),
rotations and reflections. The configuration returned is given in
principal-component axes, so the reflection chosen may differ between
R platforms (see prcomp
).
When add = TRUE
, a minimal additive constant is
computed such that the dissimilarities
are Euclidean and hence can be represented in
n - 1
dimensions. Whereas S (Becker et al., 1988) computes this
constant using an approximation suggested by Torgerson, R uses the
analytical solution of Cailliez (1983), see also Cox and Cox (2001).
Note that because of numerical errors the computed eigenvalues need
not all be non-negative, and even theoretically the representation
could be in fewer than n - 1
dimensions.
If .list
is false (as per default), a matrix with k
columns whose rows give the coordinates of the points chosen to
represent the dissimilarities.
Otherwise, a list
containing the following components.
points |
a matrix with up to |
eig |
the |
x |
the doubly centered distance matrix if |
ac |
the additive constant |
GOF |
a numeric vector of length 2, equal to say
|
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988). The New S Language. Wadsworth & Brooks/Cole.
Cailliez, F. (1983). The analytical solution of the additive constant problem. Psychometrika, 48, 343–349. doi:10.1007/BF02294026.
Cox, T. F. and Cox, M. A. A. (2001). Multidimensional Scaling. Second edition. Chapman and Hall.
Gower, J. C. (1966). Some distance properties of latent root and vector methods used in multivariate analysis. Biometrika, 53, 325–328. doi:10.2307/2333639.
Krzanowski, W. J. and Marriott, F. H. C. (1994). Multivariate Analysis. Part I. Distributions, Ordination and Inference. London: Edward Arnold. (Especially pp. 108–111.)
Mardia, K.V. (1978). Some properties of classical multidimensional scaling. Communications on Statistics – Theory and Methods, A7, 1233–41. doi:10.1080/03610927808827707
Mardia, K. V., Kent, J. T. and Bibby, J. M. (1979). Chapter 14 of Multivariate Analysis, London: Academic Press.
Seber, G. A. F. (1984). Multivariate Observations. New York: Wiley.
Torgerson, W. S. (1958). Theory and Methods of Scaling. New York: Wiley.
dist
.
isoMDS
and sammon
in package MASS provide alternative methods of
multidimensional scaling.
require(graphics) loc <- cmdscale(eurodist) x <- loc[, 1] y <- -loc[, 2] # reflect so North is at the top ## note asp = 1, to ensure Euclidean distances are represented correctly plot(x, y, type = "n", xlab = "", ylab = "", asp = 1, axes = FALSE, main = "cmdscale(eurodist)") text(x, y, rownames(loc), cex = 0.6)
require(graphics) loc <- cmdscale(eurodist) x <- loc[, 1] y <- -loc[, 2] # reflect so North is at the top ## note asp = 1, to ensure Euclidean distances are represented correctly plot(x, y, type = "n", xlab = "", ylab = "", asp = 1, axes = FALSE, main = "cmdscale(eurodist)") text(x, y, rownames(loc), cex = 0.6)
coef
is a generic function which extracts model coefficients
from objects returned by modeling functions. coefficients
is
an alias for it.
coef(object, ...) coefficients(object, ...) ## Default S3 method: coef(object, complete = TRUE, ...) ## S3 method for class 'aov' coef(object, complete = FALSE, ...)
coef(object, ...) coefficients(object, ...) ## Default S3 method: coef(object, complete = TRUE, ...) ## S3 method for class 'aov' coef(object, complete = FALSE, ...)
object |
an object for which the extraction of model coefficients is meaningful. |
complete |
for the default (used for |
... |
other arguments. |
All object classes which are returned by model fitting functions
should provide a coef
method or use the default one.
(Note that the method is for coef
and not coefficients
.)
The "aov"
method does not report aliased coefficients (see
alias
) by default where complete = FALSE
.
The complete
argument also exists for compatibility with
vcov
methods, and coef
and aov
methods for
other classes should typically also keep the complete = *
behavior in sync. By that, with p <- length(coef(obj, complete = TF))
,
dim(vcov(obj, complete = TF)) == c(p,p)
will be fulfilled for both
complete
settings and the default.
Coefficients extracted from the model object object
.
For standard model fitting classes this will be a named numeric vector.
For "maov"
objects (produced by aov
) it will be a matrix.
Chambers, J. M. and Hastie, T. J. (1992) Statistical Models in S. Wadsworth & Brooks/Cole.
fitted.values
and residuals
for related methods;
glm
, lm
for model fitting.
x <- 1:5; coef(lm(c(1:3, 7, 6) ~ x))
x <- 1:5; coef(lm(c(1:3, 7, 6) ~ x))
Return a logical vector indicating which cases are complete, i.e., have no missing values.
complete.cases(...)
complete.cases(...)
... |
a sequence of vectors, matrices and data frames. |
A logical vector specifying which observations/rows have no missing values across the entire sequence.
A current limitation of this function is that it uses low level
functions to determine lengths and missingness, ignoring the
class. This will lead to spurious errors when some columns
have classes with length
or is.na
methods, for example "POSIXlt"
, as described
in PR#16648.
x <- airquality[, -1] # x is a regression design matrix y <- airquality[, 1] # y is the corresponding response stopifnot(complete.cases(y) != is.na(y)) ok <- complete.cases(x, y) sum(!ok) # how many are not "ok" ? x <- x[ok,] y <- y[ok]
x <- airquality[, -1] # x is a regression design matrix y <- airquality[, 1] # y is the corresponding response stopifnot(complete.cases(y) != is.na(y)) ok <- complete.cases(x, y) sum(!ok) # how many are not "ok" ? x <- x[ok,] y <- y[ok]
Computes confidence intervals for one or more parameters in a fitted
model. There is a default and a method for objects inheriting from class
"lm"
.
confint(object, parm, level = 0.95, ...) ## Default S3 method: confint(object, parm, level = 0.95, ...) ## S3 method for class 'lm' confint(object, parm, level = 0.95, ...) ## S3 method for class 'glm' confint(object, parm, level = 0.95, trace = FALSE, test=c("LRT", "Rao"), ...) ## S3 method for class 'nls' confint(object, parm, level = 0.95, ...)
confint(object, parm, level = 0.95, ...) ## Default S3 method: confint(object, parm, level = 0.95, ...) ## S3 method for class 'lm' confint(object, parm, level = 0.95, ...) ## S3 method for class 'glm' confint(object, parm, level = 0.95, trace = FALSE, test=c("LRT", "Rao"), ...) ## S3 method for class 'nls' confint(object, parm, level = 0.95, ...)
object |
a fitted model object. |
parm |
a specification of which parameters are to be given confidence intervals, either a vector of numbers or a vector of names. If missing, all parameters are considered. |
level |
the confidence level required. |
trace |
logical. Should profiling be traced? |
test |
use Likelihood Ratio or Rao Score test in profiling. |
... |
additional argument(s) for methods. |
confint
is a generic function. The default method assumes
normality, and needs suitable coef
and
vcov
methods to be available. The default method can be
called directly for comparison with other methods.
For objects of class "lm"
the direct formulae based on
values are used.
Methods for classes "glm"
and "nls"
call the appropriate profile method,
then find the confidence intervals by interpolation in the profile
traces. If the profile object is already available it can be used
as the main argument rather than the fitted model object itself.
A matrix (or vector) with columns giving lower and upper confidence limits for each parameter. These will be labelled as (1-level)/2 and 1 - (1-level)/2 in % (by default 2.5% and 97.5%).
Venables, W. N. and Ripley, B. D. (2002) Modern Applied Statistics with S. Fourth edition. Springer.
Original versions: confint.glm
and
confint.nls
in package MASS.
fit <- lm(100/mpg ~ disp + hp + wt + am, data = mtcars) confint(fit) confint(fit, "wt") ## from example(glm) counts <- c(18,17,15,20,10,20,25,13,12) outcome <- gl(3, 1, 9); treatment <- gl(3, 3) glm.D93 <- glm(counts ~ outcome + treatment, family = poisson()) confint(glm.D93) confint.default(glm.D93) # based on asymptotic normality
fit <- lm(100/mpg ~ disp + hp + wt + am, data = mtcars) confint(fit) confint(fit, "wt") ## from example(glm) counts <- c(18,17,15,20,10,20,25,13,12) outcome <- gl(3, 1, 9); treatment <- gl(3, 3) glm.D93 <- glm(counts ~ outcome + treatment, family = poisson()) confint(glm.D93) confint.default(glm.D93) # based on asymptotic normality
Minimise a function subject to linear inequality constraints using an adaptive barrier algorithm.
constrOptim(theta, f, grad, ui, ci, mu = 1e-04, control = list(), method = if(is.null(grad)) "Nelder-Mead" else "BFGS", outer.iterations = 100, outer.eps = 1e-05, ..., hessian = FALSE)
constrOptim(theta, f, grad, ui, ci, mu = 1e-04, control = list(), method = if(is.null(grad)) "Nelder-Mead" else "BFGS", outer.iterations = 100, outer.eps = 1e-05, ..., hessian = FALSE)
theta |
numeric (vector) starting value (of length |
f |
function to minimise (see below). |
grad |
gradient of |
ui |
constraint matrix ( |
ci |
constraint vector of length |
mu |
(Small) tuning parameter. |
control , method , hessian
|
passed to |
outer.iterations |
iterations of the barrier algorithm. |
outer.eps |
non-negative number; the relative convergence tolerance of the barrier algorithm. |
... |
Other named arguments to be passed to |
The feasible region is defined by ui %*% theta - ci >= 0
. The
starting value must be in the interior of the feasible region, but the
minimum may be on the boundary.
A logarithmic barrier is added to enforce the constraints and then
optim
is called. The barrier function is chosen so that
the objective function should decrease at each outer iteration. Minima
in the interior of the feasible region are typically found quite
quickly, but a substantial number of outer iterations may be needed
for a minimum on the boundary.
The tuning parameter mu
multiplies the barrier term. Its precise
value is often relatively unimportant. As mu
increases the
augmented objective function becomes closer to the original objective
function but also less smooth near the boundary of the feasible
region.
Any optim
method that permits infinite values for the
objective function may be used (currently all but "L-BFGS-B").
The objective function f
takes as first argument the vector
of parameters over which minimisation is to take place. It should
return a scalar result. Optional arguments ...
will be
passed to optim
and then (if not used by optim
) to
f
. As with optim
, the default is to minimise, but
maximisation can be performed by setting control$fnscale
to a
negative value.
The gradient function grad
must be supplied except with
method = "Nelder-Mead"
. It should take arguments matching
those of f
and return a vector containing the gradient.
As for optim
, but with two extra components:
barrier.value
giving the value of the barrier function at the
optimum and outer.iterations
gives the
number of outer iterations (calls to optim
).
The counts
component contains the sum of all
optim()$counts
.
K. Lange Numerical Analysis for Statisticians. Springer 2001, p185ff
optim
, especially method = "L-BFGS-B"
which
does box-constrained optimisation.
## from optim fr <- function(x) { ## Rosenbrock Banana function x1 <- x[1] x2 <- x[2] 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 } grr <- function(x) { ## Gradient of 'fr' x1 <- x[1] x2 <- x[2] c(-400 * x1 * (x2 - x1 * x1) - 2 * (1 - x1), 200 * (x2 - x1 * x1)) } optim(c(-1.2,1), fr, grr) #Box-constraint, optimum on the boundary constrOptim(c(-1.2,0.9), fr, grr, ui = rbind(c(-1,0), c(0,-1)), ci = c(-1,-1)) # x <= 0.9, y - x > 0.1 constrOptim(c(.5,0), fr, grr, ui = rbind(c(-1,0), c(1,-1)), ci = c(-0.9,0.1)) ## Solves linear and quadratic programming problems ## but needs a feasible starting value # # from example(solve.QP) in 'quadprog' # no derivative fQP <- function(b) {-sum(c(0,5,0)*b)+0.5*sum(b*b)} Amat <- matrix(c(-4,-3,0,2,1,0,0,-2,1), 3, 3) bvec <- c(-8, 2, 0) constrOptim(c(2,-1,-1), fQP, NULL, ui = t(Amat), ci = bvec) # derivative gQP <- function(b) {-c(0, 5, 0) + b} constrOptim(c(2,-1,-1), fQP, gQP, ui = t(Amat), ci = bvec) ## Now with maximisation instead of minimisation hQP <- function(b) {sum(c(0,5,0)*b)-0.5*sum(b*b)} constrOptim(c(2,-1,-1), hQP, NULL, ui = t(Amat), ci = bvec, control = list(fnscale = -1))
## from optim fr <- function(x) { ## Rosenbrock Banana function x1 <- x[1] x2 <- x[2] 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 } grr <- function(x) { ## Gradient of 'fr' x1 <- x[1] x2 <- x[2] c(-400 * x1 * (x2 - x1 * x1) - 2 * (1 - x1), 200 * (x2 - x1 * x1)) } optim(c(-1.2,1), fr, grr) #Box-constraint, optimum on the boundary constrOptim(c(-1.2,0.9), fr, grr, ui = rbind(c(-1,0), c(0,-1)), ci = c(-1,-1)) # x <= 0.9, y - x > 0.1 constrOptim(c(.5,0), fr, grr, ui = rbind(c(-1,0), c(1,-1)), ci = c(-0.9,0.1)) ## Solves linear and quadratic programming problems ## but needs a feasible starting value # # from example(solve.QP) in 'quadprog' # no derivative fQP <- function(b) {-sum(c(0,5,0)*b)+0.5*sum(b*b)} Amat <- matrix(c(-4,-3,0,2,1,0,0,-2,1), 3, 3) bvec <- c(-8, 2, 0) constrOptim(c(2,-1,-1), fQP, NULL, ui = t(Amat), ci = bvec) # derivative gQP <- function(b) {-c(0, 5, 0) + b} constrOptim(c(2,-1,-1), fQP, gQP, ui = t(Amat), ci = bvec) ## Now with maximisation instead of minimisation hQP <- function(b) {sum(c(0,5,0)*b)-0.5*sum(b*b)} constrOptim(c(2,-1,-1), hQP, NULL, ui = t(Amat), ci = bvec, control = list(fnscale = -1))
Return a matrix of contrasts.
contr.helmert(n, contrasts = TRUE, sparse = FALSE) contr.poly(n, scores = 1:n, contrasts = TRUE, sparse = FALSE) contr.sum(n, contrasts = TRUE, sparse = FALSE) contr.treatment(n, base = 1, contrasts = TRUE, sparse = FALSE) contr.SAS(n, contrasts = TRUE, sparse = FALSE)
contr.helmert(n, contrasts = TRUE, sparse = FALSE) contr.poly(n, scores = 1:n, contrasts = TRUE, sparse = FALSE) contr.sum(n, contrasts = TRUE, sparse = FALSE) contr.treatment(n, base = 1, contrasts = TRUE, sparse = FALSE) contr.SAS(n, contrasts = TRUE, sparse = FALSE)
n |
a vector of levels for a factor, or the number of levels. |
contrasts |
a logical indicating whether contrasts should be computed. |
sparse |
logical indicating if the result should be sparse
(of class |
scores |
the set of values over which orthogonal polynomials are to be computed. |
base |
an integer specifying which group is considered the
baseline group. Ignored if |
These functions are used for creating contrast matrices for use in
fitting analysis of variance and regression models. The columns of
the resulting matrices contain contrasts which can be used for coding
a factor with n
levels. The returned value contains the
computed contrasts. If the argument contrasts
is FALSE
a square indicator matrix (the dummy coding) is returned except
for contr.poly
(which includes the 0-degree, i.e. constant,
polynomial when contrasts = FALSE
).
contr.helmert
returns Helmert contrasts, which contrast the
second level with the first, the third with the average of the first
two, and so on. contr.poly
returns contrasts based on
orthogonal polynomials. contr.sum
uses ‘sum to zero
contrasts’.
contr.treatment
contrasts each level with the baseline level
(specified by base
): the baseline level is omitted. Note that
this does not produce ‘contrasts’ as defined in the standard
theory for linear models as they are not orthogonal to the intercept.
contr.SAS
is a wrapper for contr.treatment
that sets
the base level to be the last level of the factor. The coefficients
produced when using these contrasts should be equivalent to those
produced by many (but not all) SAS procedures.
For consistency, sparse
is an argument to all these contrast
functions, however sparse = TRUE
for contr.poly
is typically pointless and is rarely useful for
contr.helmert
.
A matrix with n
rows and k
columns, with k=n-1
if
contrasts
is TRUE
and k=n
if contrasts
is
FALSE
.
Chambers, J. M. and Hastie, T. J. (1992) Statistical models. Chapter 2 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
contrasts
,
C
,
and
aov
,
glm
,
lm
.
(cH <- contr.helmert(4)) apply(cH, 2, sum) # column sums are 0 crossprod(cH) # diagonal -- columns are orthogonal contr.helmert(4, contrasts = FALSE) # just the 4 x 4 identity matrix (cT <- contr.treatment(5)) all(crossprod(cT) == diag(4)) # TRUE: even orthonormal (cT. <- contr.SAS(5)) all(crossprod(cT.) == diag(4)) # TRUE zapsmall(cP <- contr.poly(3)) # Linear and Quadratic zapsmall(crossprod(cP), digits = 15) # orthonormal up to fuzz
(cH <- contr.helmert(4)) apply(cH, 2, sum) # column sums are 0 crossprod(cH) # diagonal -- columns are orthogonal contr.helmert(4, contrasts = FALSE) # just the 4 x 4 identity matrix (cT <- contr.treatment(5)) all(crossprod(cT) == diag(4)) # TRUE: even orthonormal (cT. <- contr.SAS(5)) all(crossprod(cT.) == diag(4)) # TRUE zapsmall(cP <- contr.poly(3)) # Linear and Quadratic zapsmall(crossprod(cP), digits = 15) # orthonormal up to fuzz
Set and view the contrasts associated with a factor.
contrasts(x, contrasts = TRUE, sparse = FALSE) contrasts(x, how.many = NULL) <- value
contrasts(x, contrasts = TRUE, sparse = FALSE) contrasts(x, how.many = NULL) <- value
x |
a factor or a logical variable. |
contrasts |
logical. See ‘Details’. |
sparse |
logical indicating if the result should be sparse
(of class |
how.many |
integer number indicating how many contrasts should be
made. Defaults to one
less than the number of levels of |
value |
either a numeric matrix (or a sparse or dense matrix of a
class extending |
If contrasts are not set for a factor the default functions from
options("contrasts")
are used.
A logical vector x
is converted into a two-level factor with
levels c(FALSE, TRUE)
(regardless of which levels occur in the
variable).
The argument contrasts
is ignored if x
has a matrix
contrasts
attribute set. Otherwise if contrasts = TRUE
it is passed to a contrasts function such as
contr.treatment
and if contrasts = FALSE
an identity matrix is returned. Suitable functions have a first
argument which is the character vector of levels, a named argument
contrasts
(always called with contrasts = TRUE
) and
optionally a logical argument sparse
.
If value
supplies more than how.many
contrasts, the
first how.many
are used. If too few are supplied, a suitable
contrast matrix is created by extending value
after ensuring
its columns are contrasts (orthogonal to the constant term) and not
collinear.
Chambers, J. M. and Hastie, T. J. (1992) Statistical models. Chapter 2 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
C
,
contr.helmert
,
contr.poly
,
contr.sum
,
contr.treatment
;
glm
,
aov
,
lm
.
utils::example(factor) fff <- ff[, drop = TRUE] # reduce to 5 levels. contrasts(fff) # treatment contrasts by default contrasts(C(fff, sum)) contrasts(fff, contrasts = FALSE) # the 5x5 identity matrix contrasts(fff) <- contr.sum(5); contrasts(fff) # set sum contrasts contrasts(fff, 2) <- contr.sum(5); contrasts(fff) # set 2 contrasts # supply 2 contrasts, compute 2 more to make full set of 4. contrasts(fff) <- contr.sum(5)[, 1:2]; contrasts(fff) ## using sparse contrasts: % useful, once model.matrix() works with these : ffs <- fff contrasts(ffs) <- contr.sum(5, sparse = TRUE)[, 1:2]; contrasts(ffs) stopifnot(all.equal(ffs, fff)) contrasts(ffs) <- contr.sum(5, sparse = TRUE); contrasts(ffs)
utils::example(factor) fff <- ff[, drop = TRUE] # reduce to 5 levels. contrasts(fff) # treatment contrasts by default contrasts(C(fff, sum)) contrasts(fff, contrasts = FALSE) # the 5x5 identity matrix contrasts(fff) <- contr.sum(5); contrasts(fff) # set sum contrasts contrasts(fff, 2) <- contr.sum(5); contrasts(fff) # set 2 contrasts # supply 2 contrasts, compute 2 more to make full set of 4. contrasts(fff) <- contr.sum(5)[, 1:2]; contrasts(fff) ## using sparse contrasts: % useful, once model.matrix() works with these : ffs <- fff contrasts(ffs) <- contr.sum(5, sparse = TRUE)[, 1:2]; contrasts(ffs) stopifnot(all.equal(ffs, fff)) contrasts(ffs) <- contr.sum(5, sparse = TRUE); contrasts(ffs)
Use the Fast Fourier Transform to compute the several kinds of convolutions of two sequences.
convolve(x, y, conj = TRUE, type = c("circular", "open", "filter"))
convolve(x, y, conj = TRUE, type = c("circular", "open", "filter"))
x , y
|
numeric sequences of the same length to be convolved. |
conj |
logical; if |
type |
character; partially matched to For |
The Fast Fourier Transform, fft
, is used for efficiency.
The input sequences x
and y
must have the same length if
circular
is true.
Note that the usual definition of convolution of two sequences
x
and y
is given by convolve(x, rev(y), type = "o")
.
If r <- convolve(x, y, type = "open")
and n <- length(x)
, m <- length(y)
, then
where the sum is over all valid indices , for
.
If type == "circular"
, is required, and the above is
true for
when
for
.
Brillinger, D. R. (1981) Time Series: Data Analysis and Theory, Second Edition. San Francisco: Holden-Day.
fft
, nextn
, and particularly
filter
(from the stats package) which may be
more appropriate.
require(graphics) x <- c(0,0,0,100,0,0,0) y <- c(0,0,1, 2 ,1,0,0)/4 zapsmall(convolve(x, y)) # *NOT* what you first thought. zapsmall(convolve(x, y[3:5], type = "f")) # rather x <- rnorm(50) y <- rnorm(50) # Circular convolution *has* this symmetry: all.equal(convolve(x, y, conj = FALSE), rev(convolve(rev(y),x))) n <- length(x <- -20:24) y <- (x-10)^2/1000 + rnorm(x)/8 Han <- function(y) # Hanning convolve(y, c(1,2,1)/4, type = "filter") plot(x, y, main = "Using convolve(.) for Hanning filters") lines(x[-c(1 , n) ], Han(y), col = "red") lines(x[-c(1:2, (n-1):n)], Han(Han(y)), lwd = 2, col = "dark blue")
require(graphics) x <- c(0,0,0,100,0,0,0) y <- c(0,0,1, 2 ,1,0,0)/4 zapsmall(convolve(x, y)) # *NOT* what you first thought. zapsmall(convolve(x, y[3:5], type = "f")) # rather x <- rnorm(50) y <- rnorm(50) # Circular convolution *has* this symmetry: all.equal(convolve(x, y, conj = FALSE), rev(convolve(rev(y),x))) n <- length(x <- -20:24) y <- (x-10)^2/1000 + rnorm(x)/8 Han <- function(y) # Hanning convolve(y, c(1,2,1)/4, type = "filter") plot(x, y, main = "Using convolve(.) for Hanning filters") lines(x[-c(1 , n) ], Han(y), col = "red") lines(x[-c(1:2, (n-1):n)], Han(Han(y)), lwd = 2, col = "dark blue")
Computes the cophenetic distances for a hierarchical clustering.
cophenetic(x) ## Default S3 method: cophenetic(x) ## S3 method for class 'dendrogram' cophenetic(x)
cophenetic(x) ## Default S3 method: cophenetic(x) ## S3 method for class 'dendrogram' cophenetic(x)
x |
an R object representing a hierarchical clustering.
For the default method, an object of class |
The cophenetic distance between two observations that have been clustered is defined to be the intergroup dissimilarity at which the two observations are first combined into a single cluster. Note that this distance has many ties and restrictions.
It can be argued that a dendrogram is an appropriate summary of some data if the correlation between the original distances and the cophenetic distances is high. Otherwise, it should simply be viewed as the description of the output of the clustering algorithm.
cophenetic
is a generic function. Support for classes which
represent hierarchical clusterings (total indexed hierarchies) can be
added by providing an as.hclust()
or, more directly, a
cophenetic()
method for such a class.
The method for objects of class "dendrogram"
requires
that all leaves of the dendrogram object have non-null labels.
An object of class "dist"
.
Robert Gentleman
Sneath, P.H.A. and Sokal, R.R. (1973) Numerical Taxonomy: The Principles and Practice of Numerical Classification, p. 278 ff; Freeman, San Francisco.
require(graphics) d1 <- dist(USArrests) hc <- hclust(d1, "ave") d2 <- cophenetic(hc) cor(d1, d2) # 0.7659 ## Example from Sneath & Sokal, Fig. 5-29, p.279 d0 <- c(1,3.8,4.4,5.1, 4,4.2,5, 2.6,5.3, 5.4) attributes(d0) <- list(Size = 5, diag = TRUE) class(d0) <- "dist" names(d0) <- letters[1:5] d0 utils::str(upgma <- hclust(d0, method = "average")) plot(upgma, hang = -1) # (d.coph <- cophenetic(upgma)) cor(d0, d.coph) # 0.9911
require(graphics) d1 <- dist(USArrests) hc <- hclust(d1, "ave") d2 <- cophenetic(hc) cor(d1, d2) # 0.7659 ## Example from Sneath & Sokal, Fig. 5-29, p.279 d0 <- c(1,3.8,4.4,5.1, 4,4.2,5, 2.6,5.3, 5.4) attributes(d0) <- list(Size = 5, diag = TRUE) class(d0) <- "dist" names(d0) <- letters[1:5] d0 utils::str(upgma <- hclust(d0, method = "average")) plot(upgma, hang = -1) # (d.coph <- cophenetic(upgma)) cor(d0, d.coph) # 0.9911
var
, cov
and cor
compute the variance of x
and the covariance or correlation of x
and y
if these
are vectors. If x
and y
are matrices then the
covariances (or correlations) between the columns of x
and the
columns of y
are computed.
cov2cor
scales a covariance matrix into the corresponding
correlation matrix efficiently.
var(x, y = NULL, na.rm = FALSE, use) cov(x, y = NULL, use = "everything", method = c("pearson", "kendall", "spearman")) cor(x, y = NULL, use = "everything", method = c("pearson", "kendall", "spearman")) cov2cor(V)
var(x, y = NULL, na.rm = FALSE, use) cov(x, y = NULL, use = "everything", method = c("pearson", "kendall", "spearman")) cor(x, y = NULL, use = "everything", method = c("pearson", "kendall", "spearman")) cov2cor(V)
x |
a numeric vector, matrix or data frame. |
y |
|
na.rm |
logical. Should missing values be removed? |
use |
an optional character string giving a
method for computing covariances in the presence
of missing values. This must be (an abbreviation of) one of the strings
|
method |
a character string indicating which correlation
coefficient (or covariance) is to be computed. One of
|
V |
symmetric numeric matrix, usually positive definite such as a covariance matrix. |
For cov
and cor
one must either give a matrix or
data frame for x
or give both x
and y
.
The inputs must be numeric (as determined by is.numeric
:
logical values are also allowed for historical compatibility): the
"kendall"
and "spearman"
methods make sense for ordered
inputs but xtfrm
can be used to find a suitable prior
transformation to numbers.
var
is just another interface to cov
, where
na.rm
is used to determine the default for use
when that
is unspecified. If na.rm
is TRUE
then the complete
observations (rows) are used (use = "na.or.complete"
) to
compute the variance. Otherwise, by default use = "everything"
.
If use
is "everything"
, NA
s will
propagate conceptually, i.e., a resulting value will be NA
whenever one of its contributing observations is NA
.
If use
is "all.obs"
, then the presence of missing
observations will produce an error. If use
is
"complete.obs"
then missing values are handled by casewise
deletion (and if there are no complete cases, that gives an error).
"na.or.complete"
is the same unless there are no complete
cases, that gives NA
.
Finally, if use
has the value "pairwise.complete.obs"
then the correlation or covariance between each pair of variables is
computed using all complete pairs of observations on those variables.
This can result in covariance or correlation matrices which are not positive
semi-definite, as well as NA
entries if there are no complete
pairs for that pair of variables. For cov
and var
,
"pairwise.complete.obs"
only works with the "pearson"
method.
Note that (the equivalent of) var(double(0), use = *)
gives
NA
for use = "everything"
and "na.or.complete"
,
and gives an error in the other cases.
The denominator is used which gives an unbiased estimator
of the (co)variance for i.i.d. observations. These functions return
NA
when there is only one observation.
For cor()
, if method
is "kendall"
or
"spearman"
, Kendall's or Spearman's
statistic is used to estimate a rank-based measure of
association. These are more robust and have been recommended if the
data do not necessarily come from a bivariate normal distribution.
For cov()
, a non-Pearson method is unusual but available for
the sake of completeness. Note that "spearman"
basically
computes cor(R(x), R(y))
(or cov(., .)
) where R(u)
:= rank(u, na.last = "keep")
. In the case of missing values, the
ranks are calculated depending on the value of use
, either
based on complete observations, or based on pairwise completeness with
reranking for each pair.
When there are ties, Kendall's is computed, as
proposed by Kendall (1945).
Scaling a covariance matrix into a correlation one can be achieved in
many ways, mathematically most appealing by multiplication with a
diagonal matrix from left and right, or more efficiently by using
sweep(.., FUN = "/")
twice. The cov2cor
function
is even a bit more efficient, and provided mostly for didactical
reasons.
For r <- cor(*, use = "all.obs")
, it is now guaranteed that
all(abs(r) <= 1)
.
Some people have noted that the code for Kendall's tau is slow for
very large datasets (many more than 1000 cases). It rarely makes
sense to do such a computation, but see function
cor.fk
in package pcaPP.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988). The New S Language. Wadsworth & Brooks/Cole.
Kendall, M. G. (1938). A new measure of rank correlation, Biometrika, 30, 81–93. doi:10.1093/biomet/30.1-2.81.
Kendall, M. G. (1945). The treatment of ties in rank problems. Biometrika, 33 239–251. doi:10.1093/biomet/33.3.239
cor.test
for confidence intervals (and tests).
cov.wt
for weighted covariance computation.
sd
for standard deviation (vectors).
var(1:10) # 9.166667 var(1:5, 1:5) # 2.5 ## Two simple vectors cor(1:10, 2:11) # == 1 ## Correlation Matrix of Multivariate sample: (Cl <- cor(longley)) ## Graphical Correlation Matrix: symnum(Cl) # highly correlated ## Spearman's rho and Kendall's tau symnum(clS <- cor(longley, method = "spearman")) symnum(clK <- cor(longley, method = "kendall")) ## How much do they differ? i <- lower.tri(Cl) cor(cbind(P = Cl[i], S = clS[i], K = clK[i])) ## cov2cor() scales a covariance matrix by its diagonal ## to become the correlation matrix. cov2cor # see the function definition {and learn ..} stopifnot(all.equal(Cl, cov2cor(cov(longley))), all.equal(cor(longley, method = "kendall"), cov2cor(cov(longley, method = "kendall")))) ##--- Missing value treatment: C1 <- cov(swiss) range(eigen(C1, only.values = TRUE)$values) # 6.19 1921 ## swM := "swiss" with 3 "missing"s : swM <- swiss colnames(swM) <- abbreviate(colnames(swiss), minlength=6) swM[1,2] <- swM[7,3] <- swM[25,5] <- NA # create 3 "missing" ## Consider all 5 "use" cases : (C. <- cov(swM)) # use="everything" quite a few NA's in cov.matrix try(cov(swM, use = "all")) # Error: missing obs... C2 <- cov(swM, use = "complete") stopifnot(identical(C2, cov(swM, use = "na.or.complete"))) range(eigen(C2, only.values = TRUE)$values) # 6.46 1930 C3 <- cov(swM, use = "pairwise") range(eigen(C3, only.values = TRUE)$values) # 6.19 1938 ## Kendall's tau doesn't change much: symnum(Rc <- cor(swM, method = "kendall", use = "complete")) symnum(Rp <- cor(swM, method = "kendall", use = "pairwise")) symnum(R. <- cor(swiss, method = "kendall")) ## "pairwise" is closer componentwise, summary(abs(c(1 - Rp/R.))) summary(abs(c(1 - Rc/R.))) ## but "complete" is closer in Eigen space: EV <- function(m) eigen(m, only.values=TRUE)$values summary(abs(1 - EV(Rp)/EV(R.)) / abs(1 - EV(Rc)/EV(R.)))
var(1:10) # 9.166667 var(1:5, 1:5) # 2.5 ## Two simple vectors cor(1:10, 2:11) # == 1 ## Correlation Matrix of Multivariate sample: (Cl <- cor(longley)) ## Graphical Correlation Matrix: symnum(Cl) # highly correlated ## Spearman's rho and Kendall's tau symnum(clS <- cor(longley, method = "spearman")) symnum(clK <- cor(longley, method = "kendall")) ## How much do they differ? i <- lower.tri(Cl) cor(cbind(P = Cl[i], S = clS[i], K = clK[i])) ## cov2cor() scales a covariance matrix by its diagonal ## to become the correlation matrix. cov2cor # see the function definition {and learn ..} stopifnot(all.equal(Cl, cov2cor(cov(longley))), all.equal(cor(longley, method = "kendall"), cov2cor(cov(longley, method = "kendall")))) ##--- Missing value treatment: C1 <- cov(swiss) range(eigen(C1, only.values = TRUE)$values) # 6.19 1921 ## swM := "swiss" with 3 "missing"s : swM <- swiss colnames(swM) <- abbreviate(colnames(swiss), minlength=6) swM[1,2] <- swM[7,3] <- swM[25,5] <- NA # create 3 "missing" ## Consider all 5 "use" cases : (C. <- cov(swM)) # use="everything" quite a few NA's in cov.matrix try(cov(swM, use = "all")) # Error: missing obs... C2 <- cov(swM, use = "complete") stopifnot(identical(C2, cov(swM, use = "na.or.complete"))) range(eigen(C2, only.values = TRUE)$values) # 6.46 1930 C3 <- cov(swM, use = "pairwise") range(eigen(C3, only.values = TRUE)$values) # 6.19 1938 ## Kendall's tau doesn't change much: symnum(Rc <- cor(swM, method = "kendall", use = "complete")) symnum(Rp <- cor(swM, method = "kendall", use = "pairwise")) symnum(R. <- cor(swiss, method = "kendall")) ## "pairwise" is closer componentwise, summary(abs(c(1 - Rp/R.))) summary(abs(c(1 - Rc/R.))) ## but "complete" is closer in Eigen space: EV <- function(m) eigen(m, only.values=TRUE)$values summary(abs(1 - EV(Rp)/EV(R.)) / abs(1 - EV(Rc)/EV(R.)))
Test for association between paired samples, using one of
Pearson's product moment correlation coefficient,
Kendall's or Spearman's
.
cor.test(x, ...) ## Default S3 method: cor.test(x, y, alternative = c("two.sided", "less", "greater"), method = c("pearson", "kendall", "spearman"), exact = NULL, conf.level = 0.95, continuity = FALSE, ...) ## S3 method for class 'formula' cor.test(formula, data, subset, na.action, ...)
cor.test(x, ...) ## Default S3 method: cor.test(x, y, alternative = c("two.sided", "less", "greater"), method = c("pearson", "kendall", "spearman"), exact = NULL, conf.level = 0.95, continuity = FALSE, ...) ## S3 method for class 'formula' cor.test(formula, data, subset, na.action, ...)
x , y
|
numeric vectors of data values. |
alternative |
indicates the alternative hypothesis and must be
one of |
method |
a character string indicating which correlation
coefficient is to be used for the test. One of |
exact |
a logical indicating whether an exact p-value should be
computed. Used for Kendall's |
conf.level |
confidence level for the returned confidence interval. Currently only used for the Pearson product moment correlation coefficient if there are at least 4 complete pairs of observations. |
continuity |
logical: if true, a continuity correction is used
for Kendall's |
formula |
a formula of the form |
data |
an optional matrix or data frame (or similar: see
|
subset |
an optional vector specifying a subset of observations to be used. |
na.action |
a function which indicates what should happen when
the data contain |
... |
further arguments to be passed to or from methods. |
The three methods each estimate the association between paired samples
and compute a test of the value being zero. They use different
measures of association, all in the range with
indicating no association. These are sometimes referred to as tests
of no correlation, but that term is often confined to the
default method.
If method
is "pearson"
, the test statistic is based on
Pearson's product moment correlation coefficient cor(x, y)
and
follows a t distribution with length(x)-2
degrees of freedom
if the samples follow independent normal distributions. If there are
at least 4 complete pairs of observation, an asymptotic confidence
interval is given based on Fisher's Z transform.
If method
is "kendall"
or "spearman"
, Kendall's
or Spearman's
statistic is used to
estimate a rank-based measure of association. These tests may be used
if the data do not necessarily come from a bivariate normal
distribution.
For Kendall's test, by default (if exact
is NULL), an exact
p-value is computed if there are less than 50 paired samples containing
finite values and there are no ties. Otherwise, the test statistic is
the estimate scaled to zero mean and unit variance, and is approximately
normally distributed.
For Spearman's test, p-values are computed using algorithm AS 89 for
and
exact = TRUE
, otherwise via the asymptotic
approximation. Note that these are ‘exact’ for
, and use an Edgeworth series approximation for larger sample
sizes (the cutoff has been changed from the original paper).
A list with class "htest"
containing the following components:
statistic |
the value of the test statistic. |
parameter |
the degrees of freedom of the test statistic in the case that it follows a t distribution. |
p.value |
the p-value of the test. |
estimate |
the estimated measure of association, with name
|
null.value |
the value of the association measure under the
null hypothesis, always |
alternative |
a character string describing the alternative hypothesis. |
method |
a character string indicating how the association was measured. |
data.name |
a character string giving the names of the data. |
conf.int |
a confidence interval for the measure of association. Currently only given for Pearson's product moment correlation coefficient in case of at least 4 complete pairs of observations. |
D. J. Best & D. E. Roberts (1975).
Algorithm AS 89: The Upper Tail Probabilities of Spearman's .
Applied Statistics, 24, 377–379.
doi:10.2307/2347111.
Myles Hollander & Douglas A. Wolfe (1973), Nonparametric Statistical Methods. New York: John Wiley & Sons. Pages 185–194 (Kendall and Spearman tests).
pKendall
and
pSpearman
in package
SuppDists,
spearman.test
in package
pspearman,
which supply different (and often more accurate) approximations.
## Hollander & Wolfe (1973), p. 187f. ## Assessment of tuna quality. We compare the Hunter L measure of ## lightness to the averages of consumer panel scores (recoded as ## integer values from 1 to 6 and averaged over 80 such values) in ## 9 lots of canned tuna. x <- c(44.4, 45.9, 41.9, 53.3, 44.7, 44.1, 50.7, 45.2, 60.1) y <- c( 2.6, 3.1, 2.5, 5.0, 3.6, 4.0, 5.2, 2.8, 3.8) ## The alternative hypothesis of interest is that the ## Hunter L value is positively associated with the panel score. cor.test(x, y, method = "kendall", alternative = "greater") ## => p=0.05972 cor.test(x, y, method = "kendall", alternative = "greater", exact = FALSE) # using large sample approximation ## => p=0.04765 ## Compare this to cor.test(x, y, method = "spearm", alternative = "g") cor.test(x, y, alternative = "g") ## Formula interface. require(graphics) pairs(USJudgeRatings) cor.test(~ CONT + INTG, data = USJudgeRatings)
## Hollander & Wolfe (1973), p. 187f. ## Assessment of tuna quality. We compare the Hunter L measure of ## lightness to the averages of consumer panel scores (recoded as ## integer values from 1 to 6 and averaged over 80 such values) in ## 9 lots of canned tuna. x <- c(44.4, 45.9, 41.9, 53.3, 44.7, 44.1, 50.7, 45.2, 60.1) y <- c( 2.6, 3.1, 2.5, 5.0, 3.6, 4.0, 5.2, 2.8, 3.8) ## The alternative hypothesis of interest is that the ## Hunter L value is positively associated with the panel score. cor.test(x, y, method = "kendall", alternative = "greater") ## => p=0.05972 cor.test(x, y, method = "kendall", alternative = "greater", exact = FALSE) # using large sample approximation ## => p=0.04765 ## Compare this to cor.test(x, y, method = "spearm", alternative = "g") cor.test(x, y, alternative = "g") ## Formula interface. require(graphics) pairs(USJudgeRatings) cor.test(~ CONT + INTG, data = USJudgeRatings)
Returns a list containing estimates of the weighted covariance matrix and the mean of the data, and optionally of the (weighted) correlation matrix.
cov.wt(x, wt = rep(1/nrow(x), nrow(x)), cor = FALSE, center = TRUE, method = c("unbiased", "ML"))
cov.wt(x, wt = rep(1/nrow(x), nrow(x)), cor = FALSE, center = TRUE, method = c("unbiased", "ML"))
x |
a matrix or data frame. As usual, rows are observations and columns are variables. |
wt |
a non-negative and non-zero vector of weights for each
observation. Its length must equal the number of rows of |
cor |
a logical indicating whether the estimated correlation weighted matrix will be returned as well. |
center |
either a logical or a numeric vector specifying the
centers to be used when computing covariances. If |
method |
string specifying how the result is scaled, see ‘Details’ below. Can be abbreviated. |
By default, method = "unbiased"
,
The covariance matrix is divided by one minus the sum of squares of
the weights, so if the weights are the default () the conventional
unbiased estimate of the covariance matrix with divisor
is obtained.
A list containing the following named components:
cov |
the estimated (weighted) covariance matrix |
center |
an estimate for the center (mean) of the data. |
n.obs |
the number of observations (rows) in |
wt |
the weights used in the estimation. Only returned if given as an argument. |
cor |
the estimated correlation matrix. Only returned if
|
(xy <- cbind(x = 1:10, y = c(1:3, 8:5, 8:10))) w1 <- c(0,0,0,1,1,1,1,1,0,0) cov.wt(xy, wt = w1) # i.e. method = "unbiased" cov.wt(xy, wt = w1, method = "ML", cor = TRUE)
(xy <- cbind(x = 1:10, y = c(1:3, 8:5, 8:10))) w1 <- c(0,0,0,1,1,1,1,1,0,0) cov.wt(xy, wt = w1) # i.e. method = "unbiased" cov.wt(xy, wt = w1, method = "ML", cor = TRUE)
Plots a cumulative periodogram.
cpgram(ts, taper = 0.1, main = paste("Series: ", deparse1(substitute(ts))), ci.col = "blue")
cpgram(ts, taper = 0.1, main = paste("Series: ", deparse1(substitute(ts))), ci.col = "blue")
ts |
a univariate time series |
taper |
proportion tapered in forming the periodogram |
main |
main title |
ci.col |
colour for confidence band. |
None.
Plots the cumulative periodogram in a square plot.
From package MASS.
B.D. Ripley
require(graphics) par(pty = "s", mfrow = c(1,2)) cpgram(lh) lh.ar <- ar(lh, order.max = 9) cpgram(lh.ar$resid, main = "AR(3) fit to lh") cpgram(ldeaths)
require(graphics) par(pty = "s", mfrow = c(1,2)) cpgram(lh) lh.ar <- ar(lh, order.max = 9) cpgram(lh.ar$resid, main = "AR(3) fit to lh") cpgram(ldeaths)
Cuts a tree, e.g., as resulting from hclust
, into several
groups either by specifying the desired number(s) of groups or the cut
height(s).
cutree(tree, k = NULL, h = NULL)
cutree(tree, k = NULL, h = NULL)
tree |
a tree as produced by |
k |
an integer scalar or vector with the desired number of groups |
h |
numeric scalar or vector with heights where the tree should be cut. |
At least one of k
or h
must be specified, k
overrides h
if both are given.
Cutting trees at a given height is only possible for ultrametric trees (with monotone clustering heights).
cutree
returns a vector with group memberships if k
or
h
are scalar, otherwise a matrix with group memberships is returned
where each column corresponds to the elements of k
or h
,
respectively (which are also used as column names).
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
hclust
, dendrogram
for cutting trees themselves.
hc <- hclust(dist(USArrests)) cutree(hc, k = 1:5) #k = 1 is trivial cutree(hc, h = 250) ## Compare the 2 and 4 grouping: g24 <- cutree(hc, k = c(2,4)) table(grp2 = g24[,"2"], grp4 = g24[,"4"])
hc <- hclust(dist(USArrests)) cutree(hc, k = 1:5) #k = 1 is trivial cutree(hc, h = 250) ## Compare the 2 and 4 grouping: g24 <- cutree(hc, k = c(2,4)) table(grp2 = g24[,"2"], grp4 = g24[,"4"])
Decompose a time series into seasonal, trend and irregular components using moving averages. Deals with additive or multiplicative seasonal component.
decompose(x, type = c("additive", "multiplicative"), filter = NULL)
decompose(x, type = c("additive", "multiplicative"), filter = NULL)
x |
A time series. |
type |
The type of seasonal component. Can be abbreviated. |
filter |
A vector of filter coefficients in reverse time order (as for
AR or MA coefficients), used for filtering out the seasonal
component. If |
The additive model used is:
The multiplicative model used is:
The function first determines the trend component using a moving
average (if filter
is NULL
, a symmetric window with
equal weights is used), and removes it from the time series. Then,
the seasonal figure is computed by averaging, for each time unit, over
all periods. The seasonal figure is then centered. Finally, the error
component is determined by removing trend and seasonal figure
(recycled as needed) from the original time series.
This only works well if x
covers an integer number of complete
periods.
An object of class "decomposed.ts"
with following components:
x |
The original series. |
seasonal |
The seasonal component (i.e., the repeated seasonal figure). |
figure |
The estimated seasonal figure only. |
trend |
The trend component. |
random |
The remainder part. |
type |
The value of |
The function stl
provides a much more sophisticated
decomposition.
David Meyer [email protected]
M. Kendall and A. Stuart (1983) The Advanced Theory of Statistics, Vol.3, Griffin. pp. 410–414.
require(graphics) m <- decompose(co2) m$figure plot(m) ## example taken from Kendall/Stuart x <- c(-50, 175, 149, 214, 247, 237, 225, 329, 729, 809, 530, 489, 540, 457, 195, 176, 337, 239, 128, 102, 232, 429, 3, 98, 43, -141, -77, -13, 125, 361, -45, 184) x <- ts(x, start = c(1951, 1), end = c(1958, 4), frequency = 4) m <- decompose(x) ## seasonal figure: 6.25, 8.62, -8.84, -6.03 round(decompose(x)$figure / 10, 2)
require(graphics) m <- decompose(co2) m$figure plot(m) ## example taken from Kendall/Stuart x <- c(-50, 175, 149, 214, 247, 237, 225, 329, 729, 809, 530, 489, 540, 457, 195, 176, 337, 239, 128, 102, 232, 429, 3, 98, 43, -141, -77, -13, 125, 361, -45, 184) x <- ts(x, start = c(1951, 1), end = c(1958, 4), frequency = 4) m <- decompose(x) ## seasonal figure: 6.25, 8.62, -8.84, -6.03 round(decompose(x)$figure / 10, 2)
delete.response
returns a terms
object for the same
model but with no response variable.
drop.terms
removes variables from the right-hand side of the
model. There is also a "[.terms"
method to perform the same
function (with keep.response = TRUE
).
reformulate
creates a formula from a character vector. If
length(termlabels) > 1
, its elements are concatenated with +
.
Non-syntactic names (e.g. containing spaces or special characters; see
make.names
) must be protected with backticks (see examples).
A non-parse
able response
still works for now,
back compatibly, with a deprecation warning.
delete.response(termobj) reformulate(termlabels, response = NULL, intercept = TRUE, env = parent.frame()) drop.terms(termobj, dropx = NULL, keep.response = FALSE)
delete.response(termobj) reformulate(termlabels, response = NULL, intercept = TRUE, env = parent.frame()) drop.terms(termobj, dropx = NULL, keep.response = FALSE)
termobj |
A |
termlabels |
character vector giving the right-hand side of a model formula. Cannot be zero-length. |
response |
character string, symbol or call giving the left-hand
side of a model formula, or |
intercept |
logical: should the formula have an intercept? |
env |
the |
dropx |
vector of positions of variables to drop from the right-hand side of the model. |
keep.response |
Keep the response in the resulting object? |
delete.response
and drop.terms
return a terms
object.
reformulate
returns a formula
.
ff <- y ~ z + x + w tt <- terms(ff) tt delete.response(tt) drop.terms(tt, 2:3, keep.response = TRUE) tt[-1] tt[2:3] reformulate(attr(tt, "term.labels")) ## keep LHS : reformulate("x*w", ff[[2]]) fS <- surv(ft, case) ~ a + b reformulate(c("a", "b*f"), fS[[2]]) ## using non-syntactic names: reformulate(c("`P/E`", "`% Growth`"), response = as.name("+-")) x <- c("a name", "another name") tryCatch( reformulate(x), error = function(e) "Syntax error." ) ## rather backquote the strings in x : reformulate(sprintf("`%s`", x)) stopifnot(identical( ~ var, reformulate("var")), identical(~ a + b + c, reformulate(letters[1:3])), identical( y ~ a + b, reformulate(letters[1:2], "y")) )
ff <- y ~ z + x + w tt <- terms(ff) tt delete.response(tt) drop.terms(tt, 2:3, keep.response = TRUE) tt[-1] tt[2:3] reformulate(attr(tt, "term.labels")) ## keep LHS : reformulate("x*w", ff[[2]]) fS <- surv(ft, case) ~ a + b reformulate(c("a", "b*f"), fS[[2]]) ## using non-syntactic names: reformulate(c("`P/E`", "`% Growth`"), response = as.name("+-")) x <- c("a name", "another name") tryCatch( reformulate(x), error = function(e) "Syntax error." ) ## rather backquote the strings in x : reformulate(sprintf("`%s`", x)) stopifnot(identical( ~ var, reformulate("var")), identical(~ a + b + c, reformulate(letters[1:3])), identical( y ~ a + b, reformulate(letters[1:2], "y")) )
Apply function FUN
to each node of a dendrogram
recursively. When y <- dendrapply(x, fn)
, then y
is a
dendrogram of the same graph structure as x
and for each node,
y.node[j] <- FUN( x.node[j], ...)
(where y.node[j]
is an
(invalid!) notation for the j-th node of y).
dendrapply(X, FUN, ...)
dendrapply(X, FUN, ...)
X |
an object of class |
FUN |
an R function to be applied to each dendrogram node,
typically working on its |
... |
potential further arguments passed to |
Usually a dendrogram of the same (graph) structure as X
.
For that, the function must be conceptually of the form
FUN <- function(X) { attributes(X) <- .....; X }
,
i.e., returning the node with some attributes added or changed.
The implementation is somewhat experimental and suggestions for
enhancements (or nice examples of usage) are very welcome. The
current implementation is recursive and inefficient for
dendrograms with many non-leaves. See the ‘Warning’ in
dendrogram
.
Martin Maechler
as.dendrogram
, lapply
for applying
a function to each component of a list
, rapply
for doing so to each non-list component of a nested list.
require(graphics) ## a smallish simple dendrogram dhc <- as.dendrogram(hc <- hclust(dist(USArrests), "ave")) (dhc21 <- dhc[[2]][[1]]) ## too simple: dendrapply(dhc21, function(n) utils::str(attributes(n))) ## toy example to set colored leaf labels : local({ colLab <<- function(n) { if(is.leaf(n)) { a <- attributes(n) i <<- i+1 attr(n, "nodePar") <- c(a$nodePar, list(lab.col = mycols[i], lab.font = i%%3)) } n } mycols <- grDevices::rainbow(attr(dhc21,"members")) i <- 0 }) dL <- dendrapply(dhc21, colLab) op <- par(mfrow = 2:1) plot(dhc21) plot(dL) ## --> colored labels! par(op)
require(graphics) ## a smallish simple dendrogram dhc <- as.dendrogram(hc <- hclust(dist(USArrests), "ave")) (dhc21 <- dhc[[2]][[1]]) ## too simple: dendrapply(dhc21, function(n) utils::str(attributes(n))) ## toy example to set colored leaf labels : local({ colLab <<- function(n) { if(is.leaf(n)) { a <- attributes(n) i <<- i+1 attr(n, "nodePar") <- c(a$nodePar, list(lab.col = mycols[i], lab.font = i%%3)) } n } mycols <- grDevices::rainbow(attr(dhc21,"members")) i <- 0 }) dL <- dendrapply(dhc21, colLab) op <- par(mfrow = 2:1) plot(dhc21) plot(dL) ## --> colored labels! par(op)
Class "dendrogram"
provides general functions for handling
tree-like structures. It is intended as a replacement for similar
functions in hierarchical clustering and classification/regression
trees, such that all of these can use the same engine for plotting or
cutting trees.
as.dendrogram(object, ...) ## S3 method for class 'hclust' as.dendrogram(object, hang = -1, check = TRUE, ...) ## S3 method for class 'dendrogram' as.hclust(x, ...) ## S3 method for class 'dendrogram' plot(x, type = c("rectangle", "triangle"), center = FALSE, edge.root = is.leaf(x) || !is.null(attr(x,"edgetext")), nodePar = NULL, edgePar = list(), leaflab = c("perpendicular", "textlike", "none"), dLeaf = NULL, xlab = "", ylab = "", xaxt = "n", yaxt = "s", horiz = FALSE, frame.plot = FALSE, xlim, ylim, ...) ## S3 method for class 'dendrogram' cut(x, h, ...) ## S3 method for class 'dendrogram' merge(x, y, ..., height, adjust = c("auto", "add.max", "none")) ## S3 method for class 'dendrogram' nobs(object, ...) ## S3 method for class 'dendrogram' print(x, digits, ...) ## S3 method for class 'dendrogram' rev(x) ## S3 method for class 'dendrogram' str(object, max.level = NA, digits.d = 3, give.attr = FALSE, wid = getOption("width"), nest.lev = 0, indent.str = "", last.str = getOption("str.dendrogram.last"), stem = "--", ...) is.leaf(object)
as.dendrogram(object, ...) ## S3 method for class 'hclust' as.dendrogram(object, hang = -1, check = TRUE, ...) ## S3 method for class 'dendrogram' as.hclust(x, ...) ## S3 method for class 'dendrogram' plot(x, type = c("rectangle", "triangle"), center = FALSE, edge.root = is.leaf(x) || !is.null(attr(x,"edgetext")), nodePar = NULL, edgePar = list(), leaflab = c("perpendicular", "textlike", "none"), dLeaf = NULL, xlab = "", ylab = "", xaxt = "n", yaxt = "s", horiz = FALSE, frame.plot = FALSE, xlim, ylim, ...) ## S3 method for class 'dendrogram' cut(x, h, ...) ## S3 method for class 'dendrogram' merge(x, y, ..., height, adjust = c("auto", "add.max", "none")) ## S3 method for class 'dendrogram' nobs(object, ...) ## S3 method for class 'dendrogram' print(x, digits, ...) ## S3 method for class 'dendrogram' rev(x) ## S3 method for class 'dendrogram' str(object, max.level = NA, digits.d = 3, give.attr = FALSE, wid = getOption("width"), nest.lev = 0, indent.str = "", last.str = getOption("str.dendrogram.last"), stem = "--", ...) is.leaf(object)
object |
any R object that can be made into one of class
|
x , y
|
object(s) of class |
hang |
numeric scalar indicating how the height of leaves
should be computed from the heights of their parents; see
|
check |
logical indicating if |
type |
type of plot. |
center |
logical; if |
edge.root |
logical; if true, draw an edge to the root node. |
nodePar |
a |
edgePar |
a |
leaflab |
a string specifying how leaves are labeled. The
default |
dLeaf |
a number specifying the distance in user
coordinates between the tip of a leaf and its label. If |
horiz |
logical indicating if the dendrogram should be drawn horizontally or not. |
frame.plot |
logical indicating if a box around the plot should
be drawn, see |
h |
height at which the tree is cut. |
height |
height at which the two dendrograms should be merged. If not
specified (or |
adjust |
a string determining if the leaf values should be
adjusted. The default, |
xlim , ylim
|
optional x- and y-limits of the plot, passed to
|
... , xlab , ylab , xaxt , yaxt
|
graphical parameters, or arguments for other methods. |
digits |
integer specifying the precision for printing, see
|
max.level , digits.d , give.attr , wid , nest.lev , indent.str
|
arguments
to |
last.str , stem
|
strings used for |
The dendrogram is directly represented as a nested list where each
component corresponds to a branch of the tree. Hence, the first
branch of tree z
is z[[1]]
, the second branch of the
corresponding subtree is z[[1]][[2]]
, or shorter
z[[c(1,2)]]
, etc.. Each node of the tree
carries some information needed for efficient plotting or cutting as
attributes, of which only members
, height
and
leaf
for leaves are compulsory:
members
total number of leaves in the branch
height
numeric non-negative height at which the node is plotted.
midpoint
numeric horizontal distance of the node from
the left border (the leftmost leaf) of the branch (unit 1 between
all leaves). This is used for plot(*, center = FALSE)
.
label
character; the label of the node
x.member
for cut()$upper
,
the number of former members; more generally a substitute
for the members
component used for ‘horizontal’
(when horiz = FALSE
, else ‘vertical’) alignment.
edgetext
character; the label for the edge leading to the node
nodePar
a named list (of length-1 components)
specifying node-specific attributes for points
plotting, see the nodePar
argument above.
edgePar
a named list (of length-1 components)
specifying attributes for segments
plotting of the
edge leading to the node, and drawing of the edgetext
if
available, see the edgePar
argument above.
leaf
logical, if TRUE
, the node is a leaf of
the tree.
cut.dendrogram()
returns a list with components $upper
and $lower
, the first is a truncated version of the original
tree, also of class dendrogram
, the latter a list with the
branches obtained from cutting the tree, each a dendrogram
.
There are [[
, print
, and str
methods for "dendrogram"
objects where the first one
(extraction) ensures that selecting sub-branches keeps the class,
i.e., returns a dendrogram even if only a leaf.
On the other hand, [
(single bracket) extraction
returns the underlying list structure.
Objects of class "hclust"
can be converted to class
"dendrogram"
using method as.dendrogram()
, and since R
2.13.0, there is also a as.hclust()
method as an inverse.
rev.dendrogram
simply returns the dendrogram x
with
reversed nodes, see also reorder.dendrogram
.
The merge(x, y, ...)
method merges two or more
dendrograms into a new one which has x
and y
(and
optional further arguments) as branches. Note that before R 3.1.2,
adjust = "none"
was used implicitly, which is invalid when,
e.g., the dendrograms are from as.dendrogram(hclust(..))
.
nobs(object)
returns the total number of leaves (the
members
attribute, see above).
is.leaf(object)
returns logical indicating if object
is a
leaf (the most simple dendrogram).
plotNode()
and plotNodeLimit()
are helper functions.
Some operations on dendrograms such as merge()
make use of
recursion. For deep trees it may be necessary to increase
options("expressions")
: if you do, you are likely to need
to set the C stack size (Cstack_info()[["size"]]
) larger
than the default where possible.
plot()
:When using type = "triangle"
,
center = TRUE
often looks better.
str(d)
:If you really want to see the internal
structure, use str(unclass(d))
instead.
dendrapply
for applying a function to each node.
order.dendrogram
and reorder.dendrogram
;
further, the labels
method.
require(graphics); require(utils) hc <- hclust(dist(USArrests), "ave") (dend1 <- as.dendrogram(hc)) # "print()" method str(dend1) # "str()" method str(dend1, max.level = 2, last.str = "'") # only the first two sub-levels oo <- options(str.dendrogram.last = "\\") # yet another possibility str(dend1, max.level = 2) # only the first two sub-levels options(oo) # .. resetting them op <- par(mfrow = c(2,2), mar = c(5,2,1,4)) plot(dend1) ## "triangle" type and show inner nodes: plot(dend1, nodePar = list(pch = c(1,NA), cex = 0.8, lab.cex = 0.8), type = "t", center = TRUE) plot(dend1, edgePar = list(col = 1:2, lty = 2:3), dLeaf = 1, edge.root = TRUE) plot(dend1, nodePar = list(pch = 2:1, cex = .4*2:1, col = 2:3), horiz = TRUE) ## simple test for as.hclust() as the inverse of as.dendrogram(): stopifnot(identical(as.hclust(dend1)[1:4], hc[1:4])) dend2 <- cut(dend1, h = 70) ## leaves are wrong horizontally in R 4.0 and earlier: plot(dend2$upper) plot(dend2$upper, nodePar = list(pch = c(1,7), col = 2:1)) ## dend2$lower is *NOT* a dendrogram, but a list of .. : plot(dend2$lower[[3]], nodePar = list(col = 4), horiz = TRUE, type = "tr") ## "inner" and "leaf" edges in different type & color : plot(dend2$lower[[2]], nodePar = list(col = 1), # non empty list edgePar = list(lty = 1:2, col = 2:1), edge.root = TRUE) par(op) d3 <- dend2$lower[[2]][[2]][[1]] stopifnot(identical(d3, dend2$lower[[2]][[c(2,1)]])) str(d3, last.str = "'") ## to peek at the inner structure "if you must", use '[..]' indexing : str(d3[2][[1]]) ## or the full str(d3[]) ## merge() to join dendrograms: (d13 <- merge(dend2$lower[[1]], dend2$lower[[3]])) ## merge() all parts back (using default 'height' instead of original one): den.1 <- Reduce(merge, dend2$lower) ## or merge() all four parts at same height --> 4 branches (!) d. <- merge(dend2$lower[[1]], dend2$lower[[2]], dend2$lower[[3]], dend2$lower[[4]]) ## (with a warning) or the same using do.call : stopifnot(identical(d., do.call(merge, dend2$lower))) plot(d., main = "merge(d1, d2, d3, d4) |-> dendrogram with a 4-split") ## "Zoom" in to the first dendrogram : plot(dend1, xlim = c(1,20), ylim = c(1,50)) nP <- list(col = 3:2, cex = c(2.0, 0.75), pch = 21:22, bg = c("light blue", "pink"), lab.cex = 0.75, lab.col = "tomato") plot(d3, nodePar= nP, edgePar = list(col = "gray", lwd = 2), horiz = TRUE) addE <- function(n) { if(!is.leaf(n)) { attr(n, "edgePar") <- list(p.col = "plum") attr(n, "edgetext") <- paste(attr(n,"members"),"members") } n } d3e <- dendrapply(d3, addE) plot(d3e, nodePar = nP) plot(d3e, nodePar = nP, leaflab = "textlike")
require(graphics); require(utils) hc <- hclust(dist(USArrests), "ave") (dend1 <- as.dendrogram(hc)) # "print()" method str(dend1) # "str()" method str(dend1, max.level = 2, last.str = "'") # only the first two sub-levels oo <- options(str.dendrogram.last = "\\") # yet another possibility str(dend1, max.level = 2) # only the first two sub-levels options(oo) # .. resetting them op <- par(mfrow = c(2,2), mar = c(5,2,1,4)) plot(dend1) ## "triangle" type and show inner nodes: plot(dend1, nodePar = list(pch = c(1,NA), cex = 0.8, lab.cex = 0.8), type = "t", center = TRUE) plot(dend1, edgePar = list(col = 1:2, lty = 2:3), dLeaf = 1, edge.root = TRUE) plot(dend1, nodePar = list(pch = 2:1, cex = .4*2:1, col = 2:3), horiz = TRUE) ## simple test for as.hclust() as the inverse of as.dendrogram(): stopifnot(identical(as.hclust(dend1)[1:4], hc[1:4])) dend2 <- cut(dend1, h = 70) ## leaves are wrong horizontally in R 4.0 and earlier: plot(dend2$upper) plot(dend2$upper, nodePar = list(pch = c(1,7), col = 2:1)) ## dend2$lower is *NOT* a dendrogram, but a list of .. : plot(dend2$lower[[3]], nodePar = list(col = 4), horiz = TRUE, type = "tr") ## "inner" and "leaf" edges in different type & color : plot(dend2$lower[[2]], nodePar = list(col = 1), # non empty list edgePar = list(lty = 1:2, col = 2:1), edge.root = TRUE) par(op) d3 <- dend2$lower[[2]][[2]][[1]] stopifnot(identical(d3, dend2$lower[[2]][[c(2,1)]])) str(d3, last.str = "'") ## to peek at the inner structure "if you must", use '[..]' indexing : str(d3[2][[1]]) ## or the full str(d3[]) ## merge() to join dendrograms: (d13 <- merge(dend2$lower[[1]], dend2$lower[[3]])) ## merge() all parts back (using default 'height' instead of original one): den.1 <- Reduce(merge, dend2$lower) ## or merge() all four parts at same height --> 4 branches (!) d. <- merge(dend2$lower[[1]], dend2$lower[[2]], dend2$lower[[3]], dend2$lower[[4]]) ## (with a warning) or the same using do.call : stopifnot(identical(d., do.call(merge, dend2$lower))) plot(d., main = "merge(d1, d2, d3, d4) |-> dendrogram with a 4-split") ## "Zoom" in to the first dendrogram : plot(dend1, xlim = c(1,20), ylim = c(1,50)) nP <- list(col = 3:2, cex = c(2.0, 0.75), pch = 21:22, bg = c("light blue", "pink"), lab.cex = 0.75, lab.col = "tomato") plot(d3, nodePar= nP, edgePar = list(col = "gray", lwd = 2), horiz = TRUE) addE <- function(n) { if(!is.leaf(n)) { attr(n, "edgePar") <- list(p.col = "plum") attr(n, "edgetext") <- paste(attr(n,"members"),"members") } n } d3e <- dendrapply(d3, addE) plot(d3e, nodePar = nP) plot(d3e, nodePar = nP, leaflab = "textlike")
The (S3) generic function density
computes kernel density
estimates. Its default method does so with the given kernel and
bandwidth for univariate observations.
density(x, ...) ## Default S3 method: density(x, bw = "nrd0", adjust = 1, kernel = c("gaussian", "epanechnikov", "rectangular", "triangular", "biweight", "cosine", "optcosine"), weights = NULL, window = kernel, width, give.Rkern = FALSE, subdensity = FALSE, warnWbw = var(weights) > 0, n = 512, from, to, cut = 3, ext = 4, old.coords = FALSE, na.rm = FALSE, ...)
density(x, ...) ## Default S3 method: density(x, bw = "nrd0", adjust = 1, kernel = c("gaussian", "epanechnikov", "rectangular", "triangular", "biweight", "cosine", "optcosine"), weights = NULL, window = kernel, width, give.Rkern = FALSE, subdensity = FALSE, warnWbw = var(weights) > 0, n = 512, from, to, cut = 3, ext = 4, old.coords = FALSE, na.rm = FALSE, ...)
x |
the data from which the estimate is to be computed. For the default method a numeric vector: long vectors are not supported. |
bw |
the smoothing bandwidth to be used. The kernels are scaled such that this is the standard deviation of the smoothing kernel. (Note this differs from the reference books cited below.)
The specified (or computed) value of |
adjust |
the bandwidth used is actually |
kernel , window
|
a character string giving the smoothing kernel
to be used. This must partially match one of
|
weights |
numeric vector of non-negative observation weights,
hence of same length as Note that weights are not taken into account for automatic
bandwidth rules, i.e., when |
width |
this exists for compatibility with S; if given, and
|
give.Rkern |
logical; if true, no density is estimated, and
the ‘canonical bandwidth’ of the chosen |
subdensity |
used only when |
warnWbw |
|
n |
the number of equally spaced points at which the density is
to be estimated. When |
from , to
|
the left and right-most points of the grid at which the
density is to be estimated; the defaults are |
cut |
by default, the values of |
ext |
a positive extension factor, |
old.coords |
|
na.rm |
logical; if |
... |
further arguments for (non-default) methods. |
The algorithm used in density.default
disperses the mass of the
empirical distribution function over a regular grid of at least 512
points and then uses the fast Fourier transform to convolve this
approximation with a discretized version of the kernel and then uses
linear approximation to evaluate the density at the specified points.
The statistical properties of a kernel are determined by
which is always
for our kernels (and hence the bandwidth
bw
is the standard deviation of the kernel) and
.
MSE-equivalent bandwidths (for different kernels) are proportional to
which is scale invariant and for our
kernels equal to
. This value is returned when
give.Rkern = TRUE
. See the examples for using exact equivalent
bandwidths.
Infinite values in x
are assumed to correspond to a point mass at
+/-Inf
and the density estimate is of the sub-density on
(-Inf, +Inf)
.
If give.Rkern
is true, the number , otherwise
an object with class
"density"
whose
underlying structure is a list containing the following components.
x |
the |
y |
the estimated density values. These will be non-negative, but can be zero. |
bw |
the bandwidth used. |
n |
the sample size after elimination of missing values. |
call |
the call which produced the result. |
data.name |
the deparsed name of the |
has.na |
logical, for compatibility (always |
The print
method reports summary
values on the
x
and y
components.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988). The New S Language. Wadsworth & Brooks/Cole (for S version).
Scott, D. W. (1992). Multivariate Density Estimation. Theory, Practice and Visualization. New York: Wiley.
Sheather, S. J. and Jones, M. C. (1991). A reliable data-based bandwidth selection method for kernel density estimation. Journal of the Royal Statistical Society Series B, 53, 683–690. doi:10.1111/j.2517-6161.1991.tb01857.x.
Silverman, B. W. (1986). Density Estimation. London: Chapman and Hall.
Venables, W. N. and Ripley, B. D. (2002). Modern Applied Statistics with S. New York: Springer.
bw.nrd
,
plot.density
, hist
;
fft
and convolve
for the computational short
cut used.
require(graphics) plot(density(c(-20, rep(0,98), 20)), xlim = c(-4, 4)) # IQR = 0 # The Old Faithful geyser data d <- density(faithful$eruptions, bw = "sj") d plot(d) plot(d, type = "n") polygon(d, col = "wheat") ## Missing values: x <- xx <- faithful$eruptions x[i.out <- sample(length(x), 10)] <- NA doR <- density(x, bw = 0.15, na.rm = TRUE) lines(doR, col = "blue") points(xx[i.out], rep(0.01, 10)) ## Weighted observations: fe <- sort(faithful$eruptions) # has quite a few non-unique values ## use 'counts / n' as weights: dw <- density(unique(fe), weights = table(fe)/length(fe), bw = d$bw) utils::str(dw) ## smaller n: only 126, but identical estimate: stopifnot(all.equal(d[1:3], dw[1:3])) ## simulation from a density() fit: # a kernel density fit is an equally-weighted mixture. fit <- density(xx) N <- 1e6 x.new <- rnorm(N, sample(xx, size = N, replace = TRUE), fit$bw) plot(fit) lines(density(x.new), col = "blue") ## The available kernels: (kernels <- eval(formals(density.default)$kernel)) ## show the kernels in the R parametrization plot (density(0, bw = 1), xlab = "", main = "R's density() kernels with bw = 1") for(i in 2:length(kernels)) lines(density(0, bw = 1, kernel = kernels[i]), col = i) legend(1.5,.4, legend = kernels, col = seq(kernels), lty = 1, cex = .8, y.intersp = 1) ## show the kernels in the S parametrization plot(density(0, from = -1.2, to = 1.2, width = 2, kernel = "gaussian"), type = "l", ylim = c(0, 1), xlab = "", main = "R's density() kernels with width = 1") for(i in 2:length(kernels)) lines(density(0, width = 2, kernel = kernels[i]), col = i) legend(0.6, 1.0, legend = kernels, col = seq(kernels), lty = 1) ##-------- Semi-advanced theoretic from here on ------------- ## Explore the old.coords TRUE --> FALSE change: set.seed(7); x <- runif(2^12) # N = 4096 den <- density(x) # -> grid of n = 512 points den0 <- density(x, old.coords = TRUE) summary(den0$y / den$y) # 1.001 ... 1.011 summary( den0$y / den$y - 1) # ~= 1/(2n-2) summary(1/ (den0$y / den$y - 1))# ~= 2n-2 = 1022 corr0 <- 1 - 1/(2*512-2) # 1 - 1/(2n-2) all.equal(den$y, den0$y * corr0)# ~ 0.0001 plot(den$x, (den0$y - den$y)/den$y, type='o', cex=1/4) title("relative error of density(runif(2^12), old.coords=TRUE)") abline(h = 1/1022, v = range(x), lty=2); axis(2, at=1/1022, "1/(2n-2)", las=1) ## The R[K] for our kernels: (RKs <- cbind(sapply(kernels, function(k) density(kernel = k, give.Rkern = TRUE)))) 100*round(RKs["epanechnikov",]/RKs, 4) ## Efficiencies bw <- bw.SJ(precip) ## sensible automatic choice plot(density(precip, bw = bw), main = "same sd bandwidths, 7 different kernels") for(i in 2:length(kernels)) lines(density(precip, bw = bw, kernel = kernels[i]), col = i) ## Bandwidth Adjustment for "Exactly Equivalent Kernels" h.f <- sapply(kernels, function(k)density(kernel = k, give.Rkern = TRUE)) (h.f <- (h.f["gaussian"] / h.f)^ .2) ## -> 1, 1.01, .995, 1.007,... close to 1 => adjustment barely visible.. plot(density(precip, bw = bw), main = "equivalent bandwidths, 7 different kernels") for(i in 2:length(kernels)) lines(density(precip, bw = bw, adjust = h.f[i], kernel = kernels[i]), col = i) legend(55, 0.035, legend = kernels, col = seq(kernels), lty = 1)
require(graphics) plot(density(c(-20, rep(0,98), 20)), xlim = c(-4, 4)) # IQR = 0 # The Old Faithful geyser data d <- density(faithful$eruptions, bw = "sj") d plot(d) plot(d, type = "n") polygon(d, col = "wheat") ## Missing values: x <- xx <- faithful$eruptions x[i.out <- sample(length(x), 10)] <- NA doR <- density(x, bw = 0.15, na.rm = TRUE) lines(doR, col = "blue") points(xx[i.out], rep(0.01, 10)) ## Weighted observations: fe <- sort(faithful$eruptions) # has quite a few non-unique values ## use 'counts / n' as weights: dw <- density(unique(fe), weights = table(fe)/length(fe), bw = d$bw) utils::str(dw) ## smaller n: only 126, but identical estimate: stopifnot(all.equal(d[1:3], dw[1:3])) ## simulation from a density() fit: # a kernel density fit is an equally-weighted mixture. fit <- density(xx) N <- 1e6 x.new <- rnorm(N, sample(xx, size = N, replace = TRUE), fit$bw) plot(fit) lines(density(x.new), col = "blue") ## The available kernels: (kernels <- eval(formals(density.default)$kernel)) ## show the kernels in the R parametrization plot (density(0, bw = 1), xlab = "", main = "R's density() kernels with bw = 1") for(i in 2:length(kernels)) lines(density(0, bw = 1, kernel = kernels[i]), col = i) legend(1.5,.4, legend = kernels, col = seq(kernels), lty = 1, cex = .8, y.intersp = 1) ## show the kernels in the S parametrization plot(density(0, from = -1.2, to = 1.2, width = 2, kernel = "gaussian"), type = "l", ylim = c(0, 1), xlab = "", main = "R's density() kernels with width = 1") for(i in 2:length(kernels)) lines(density(0, width = 2, kernel = kernels[i]), col = i) legend(0.6, 1.0, legend = kernels, col = seq(kernels), lty = 1) ##-------- Semi-advanced theoretic from here on ------------- ## Explore the old.coords TRUE --> FALSE change: set.seed(7); x <- runif(2^12) # N = 4096 den <- density(x) # -> grid of n = 512 points den0 <- density(x, old.coords = TRUE) summary(den0$y / den$y) # 1.001 ... 1.011 summary( den0$y / den$y - 1) # ~= 1/(2n-2) summary(1/ (den0$y / den$y - 1))# ~= 2n-2 = 1022 corr0 <- 1 - 1/(2*512-2) # 1 - 1/(2n-2) all.equal(den$y, den0$y * corr0)# ~ 0.0001 plot(den$x, (den0$y - den$y)/den$y, type='o', cex=1/4) title("relative error of density(runif(2^12), old.coords=TRUE)") abline(h = 1/1022, v = range(x), lty=2); axis(2, at=1/1022, "1/(2n-2)", las=1) ## The R[K] for our kernels: (RKs <- cbind(sapply(kernels, function(k) density(kernel = k, give.Rkern = TRUE)))) 100*round(RKs["epanechnikov",]/RKs, 4) ## Efficiencies bw <- bw.SJ(precip) ## sensible automatic choice plot(density(precip, bw = bw), main = "same sd bandwidths, 7 different kernels") for(i in 2:length(kernels)) lines(density(precip, bw = bw, kernel = kernels[i]), col = i) ## Bandwidth Adjustment for "Exactly Equivalent Kernels" h.f <- sapply(kernels, function(k)density(kernel = k, give.Rkern = TRUE)) (h.f <- (h.f["gaussian"] / h.f)^ .2) ## -> 1, 1.01, .995, 1.007,... close to 1 => adjustment barely visible.. plot(density(precip, bw = bw), main = "equivalent bandwidths, 7 different kernels") for(i in 2:length(kernels)) lines(density(precip, bw = bw, adjust = h.f[i], kernel = kernels[i]), col = i) legend(55, 0.035, legend = kernels, col = seq(kernels), lty = 1)
Compute derivatives of simple expressions, symbolically and algorithmically.
D (expr, name) deriv(expr, ...) deriv3(expr, ...) ## Default S3 method: deriv(expr, namevec, function.arg = NULL, tag = ".expr", hessian = FALSE, ...) ## S3 method for class 'formula' deriv(expr, namevec, function.arg = NULL, tag = ".expr", hessian = FALSE, ...) ## Default S3 method: deriv3(expr, namevec, function.arg = NULL, tag = ".expr", hessian = TRUE, ...) ## S3 method for class 'formula' deriv3(expr, namevec, function.arg = NULL, tag = ".expr", hessian = TRUE, ...)
D (expr, name) deriv(expr, ...) deriv3(expr, ...) ## Default S3 method: deriv(expr, namevec, function.arg = NULL, tag = ".expr", hessian = FALSE, ...) ## S3 method for class 'formula' deriv(expr, namevec, function.arg = NULL, tag = ".expr", hessian = FALSE, ...) ## Default S3 method: deriv3(expr, namevec, function.arg = NULL, tag = ".expr", hessian = TRUE, ...) ## S3 method for class 'formula' deriv3(expr, namevec, function.arg = NULL, tag = ".expr", hessian = TRUE, ...)
expr |
a |
name , namevec
|
character vector, giving the variable names (only
one for |
function.arg |
if specified and non- |
tag |
character; the prefix to be used for the locally created variables in result. Must be no longer than 60 bytes when translated to the native encoding. |
hessian |
a logical value indicating whether the second derivatives should be calculated and incorporated in the return value. |
... |
arguments to be passed to or from methods. |
D
is modelled after its S namesake for taking simple symbolic
derivatives.
deriv
is a generic function with a default and a
formula
method. It returns a call
for
computing the expr
and its (partial) derivatives,
simultaneously. It uses so-called algorithmic derivatives. If
function.arg
is a function, its arguments can have default
values, see the fx
example below.
Currently, deriv.formula
just calls deriv.default
after
extracting the expression to the right of ~
.
deriv3
and its methods are equivalent to deriv
and its
methods except that hessian
defaults to TRUE
for
deriv3
.
The internal code knows about the arithmetic operators +
,
-
, *
, /
and ^
, and the single-variable
functions exp
, log
, sin
, cos
, tan
,
sinh
, cosh
, sqrt
, pnorm
, dnorm
,
asin
, acos
, atan
, gamma
, lgamma
,
digamma
and trigamma
, as well as psigamma
for one
or two arguments (but derivative only with respect to the first).
(Note that only the standard normal distribution is considered.)
Since R 3.4.0, the single-variable functions log1p
,
expm1
, log2
, log10
, cospi
,
sinpi
, tanpi
, factorial
, and
lfactorial
are supported as well.
D
returns a call and therefore can easily be iterated
for higher derivatives.
deriv
and deriv3
normally return an
expression
object whose evaluation returns the function
values with a "gradient"
attribute containing the gradient
matrix. If hessian
is TRUE
the evaluation also returns
a "hessian"
attribute containing the Hessian array.
If function.arg
is not NULL
, deriv
and
deriv3
return a function with those arguments rather than an
expression.
Griewank, A. and Corliss, G. F. (1991) Automatic Differentiation of Algorithms: Theory, Implementation, and Application. SIAM proceedings, Philadelphia.
Bates, D. M. and Chambers, J. M. (1992) Nonlinear models. Chapter 10 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
nlm
and optim
for numeric minimization
which could make use of derivatives,
## formula argument : dx2x <- deriv(~ x^2, "x") ; dx2x ## Not run: expression({ .value <- x^2 .grad <- array(0, c(length(.value), 1), list(NULL, c("x"))) .grad[, "x"] <- 2 * x attr(.value, "gradient") <- .grad .value }) ## End(Not run) mode(dx2x) x <- -1:2 eval(dx2x) ## Something 'tougher': trig.exp <- expression(sin(cos(x + y^2))) ( D.sc <- D(trig.exp, "x") ) all.equal(D(trig.exp[[1]], "x"), D.sc) ( dxy <- deriv(trig.exp, c("x", "y")) ) y <- 1 eval(dxy) eval(D.sc) ## function returned: deriv((y ~ sin(cos(x) * y)), c("x","y"), function.arg = TRUE) ## function with defaulted arguments: (fx <- deriv(y ~ b0 + b1 * 2^(-x/th), c("b0", "b1", "th"), function(b0, b1, th, x = 1:7){} ) ) fx(2, 3, 4) ## First derivative D(expression(x^2), "x") stopifnot(D(as.name("x"), "x") == 1) ## Higher derivatives deriv3(y ~ b0 + b1 * 2^(-x/th), c("b0", "b1", "th"), c("b0", "b1", "th", "x") ) ## Higher derivatives: DD <- function(expr, name, order = 1) { if(order < 1) stop("'order' must be >= 1") if(order == 1) D(expr, name) else DD(D(expr, name), name, order - 1) } DD(expression(sin(x^2)), "x", 3) ## showing the limits of the internal "simplify()" : ## Not run: -sin(x^2) * (2 * x) * 2 + ((cos(x^2) * (2 * x) * (2 * x) + sin(x^2) * 2) * (2 * x) + sin(x^2) * (2 * x) * 2) ## End(Not run) ## New (R 3.4.0, 2017): D(quote(log1p(x^2)), "x") ## log1p(x) = log(1 + x) stopifnot(identical( D(quote(log1p(x^2)), "x"), D(quote(log(1+x^2)), "x"))) D(quote(expm1(x^2)), "x") ## expm1(x) = exp(x) - 1 stopifnot(identical( D(quote(expm1(x^2)), "x") -> Dex1, D(quote(exp(x^2)-1), "x")), identical(Dex1, quote(exp(x^2) * (2 * x)))) D(quote(sinpi(x^2)), "x") ## sinpi(x) = sin(pi*x) D(quote(cospi(x^2)), "x") ## cospi(x) = cos(pi*x) D(quote(tanpi(x^2)), "x") ## tanpi(x) = tan(pi*x) stopifnot(identical(D(quote(log2 (x^2)), "x"), quote(2 * x/(x^2 * log(2)))), identical(D(quote(log10(x^2)), "x"), quote(2 * x/(x^2 * log(10)))))
## formula argument : dx2x <- deriv(~ x^2, "x") ; dx2x ## Not run: expression({ .value <- x^2 .grad <- array(0, c(length(.value), 1), list(NULL, c("x"))) .grad[, "x"] <- 2 * x attr(.value, "gradient") <- .grad .value }) ## End(Not run) mode(dx2x) x <- -1:2 eval(dx2x) ## Something 'tougher': trig.exp <- expression(sin(cos(x + y^2))) ( D.sc <- D(trig.exp, "x") ) all.equal(D(trig.exp[[1]], "x"), D.sc) ( dxy <- deriv(trig.exp, c("x", "y")) ) y <- 1 eval(dxy) eval(D.sc) ## function returned: deriv((y ~ sin(cos(x) * y)), c("x","y"), function.arg = TRUE) ## function with defaulted arguments: (fx <- deriv(y ~ b0 + b1 * 2^(-x/th), c("b0", "b1", "th"), function(b0, b1, th, x = 1:7){} ) ) fx(2, 3, 4) ## First derivative D(expression(x^2), "x") stopifnot(D(as.name("x"), "x") == 1) ## Higher derivatives deriv3(y ~ b0 + b1 * 2^(-x/th), c("b0", "b1", "th"), c("b0", "b1", "th", "x") ) ## Higher derivatives: DD <- function(expr, name, order = 1) { if(order < 1) stop("'order' must be >= 1") if(order == 1) D(expr, name) else DD(D(expr, name), name, order - 1) } DD(expression(sin(x^2)), "x", 3) ## showing the limits of the internal "simplify()" : ## Not run: -sin(x^2) * (2 * x) * 2 + ((cos(x^2) * (2 * x) * (2 * x) + sin(x^2) * 2) * (2 * x) + sin(x^2) * (2 * x) * 2) ## End(Not run) ## New (R 3.4.0, 2017): D(quote(log1p(x^2)), "x") ## log1p(x) = log(1 + x) stopifnot(identical( D(quote(log1p(x^2)), "x"), D(quote(log(1+x^2)), "x"))) D(quote(expm1(x^2)), "x") ## expm1(x) = exp(x) - 1 stopifnot(identical( D(quote(expm1(x^2)), "x") -> Dex1, D(quote(exp(x^2)-1), "x")), identical(Dex1, quote(exp(x^2) * (2 * x)))) D(quote(sinpi(x^2)), "x") ## sinpi(x) = sin(pi*x) D(quote(cospi(x^2)), "x") ## cospi(x) = cos(pi*x) D(quote(tanpi(x^2)), "x") ## tanpi(x) = tan(pi*x) stopifnot(identical(D(quote(log2 (x^2)), "x"), quote(2 * x/(x^2 * log(2)))), identical(D(quote(log10(x^2)), "x"), quote(2 * x/(x^2 * log(10)))))
Returns the deviance of a fitted model object.
deviance(object, ...)
deviance(object, ...)
object |
an object for which the deviance is desired. |
... |
additional optional argument. |
This is a generic function which can be used to extract deviances for fitted models. Consult the individual modeling functions for details on how to use this function.
The value of the deviance extracted from the object object
.
Chambers, J. M. and Hastie, T. J. (1992) Statistical Models in S. Wadsworth & Brooks/Cole.
df.residual
,
extractAIC
,
glm
,
lm
.
Returns the residual degrees-of-freedom extracted from a fitted model object.
df.residual(object, ...)
df.residual(object, ...)
object |
an object for which the degrees-of-freedom are desired. |
... |
additional optional arguments. |
This is a generic function which can be used to extract residual degrees-of-freedom for fitted models. Consult the individual modeling functions for details on how to use this function.
The default method just extracts the df.residual
component.
The value of the residual degrees-of-freedom extracted from the object
x
.
Computes the inverse function of the lagged differences function
diff
.
diffinv(x, ...) ## Default S3 method: diffinv(x, lag = 1, differences = 1, xi, ...) ## S3 method for class 'ts' diffinv(x, lag = 1, differences = 1, xi, ...)
diffinv(x, ...) ## Default S3 method: diffinv(x, lag = 1, differences = 1, xi, ...) ## S3 method for class 'ts' diffinv(x, lag = 1, differences = 1, xi, ...)
x |
a numeric vector, matrix, or time series. |
lag |
a scalar lag parameter. |
differences |
an integer representing the order of the difference. |
xi |
a numeric vector, matrix, or time series containing the initial values for the integrals. If missing, zeros are used. |
... |
arguments passed to or from other methods. |
diffinv
is a generic function with methods for class "ts"
and default
for vectors and matrices.
Missing values are not handled.
A numeric vector, matrix, or time series (the latter for the
"ts"
method) representing the discrete integral of x
.
A. Trapletti
s <- 1:10 d <- diff(s) diffinv(d, xi = 1)
s <- 1:10 d <- diff(s) diffinv(d, xi = 1)
This function computes and returns the distance matrix computed by using the specified distance measure to compute the distances between the rows of a data matrix.
dist(x, method = "euclidean", diag = FALSE, upper = FALSE, p = 2) as.dist(m, diag = FALSE, upper = FALSE) ## Default S3 method: as.dist(m, diag = FALSE, upper = FALSE) ## S3 method for class 'dist' print(x, diag = NULL, upper = NULL, digits = getOption("digits"), justify = "none", right = TRUE, ...) ## S3 method for class 'dist' as.matrix(x, ...)
dist(x, method = "euclidean", diag = FALSE, upper = FALSE, p = 2) as.dist(m, diag = FALSE, upper = FALSE) ## Default S3 method: as.dist(m, diag = FALSE, upper = FALSE) ## S3 method for class 'dist' print(x, diag = NULL, upper = NULL, digits = getOption("digits"), justify = "none", right = TRUE, ...) ## S3 method for class 'dist' as.matrix(x, ...)
x |
a numeric matrix, data frame or |
method |
the distance measure to be used. This must be one of
|
diag |
logical value indicating whether the diagonal of the
distance matrix should be printed by |
upper |
logical value indicating whether the upper triangle of the
distance matrix should be printed by |
p |
The power of the Minkowski distance. |
m |
An object with distance information to be converted to a
|
digits , justify
|
passed to |
right , ...
|
further arguments, passed to other methods. |
Available distance measures are (written for two vectors and
):
euclidean
:Usual distance between the two vectors (2
norm aka ),
.
maximum
:Maximum distance between two components of
and
(supremum norm)
manhattan
:Absolute distance between the two vectors (1 norm aka ).
canberra
:.
Terms with zero numerator and denominator are omitted from the sum
and treated as if the values were missing.
This is intended for non-negative values (e.g., counts), in which
case the denominator can be written in various equivalent ways;
Originally, R used , then from 1998 to 2017,
, and then the correct
.
binary
:(aka asymmetric binary): The vectors
are regarded as binary bits, so non-zero elements are ‘on’
and zero elements are ‘off’. The distance is the
proportion of bits in which only one is on amongst those in
which at least one is on.
This also called “Jaccard” distance in some contexts.
Here, two all-zero observations have distance 0
, whereas in
traditional Jaccard definitions, the distance would be undefined for
that case and give NaN
numerically.
minkowski
:The norm, the
-th root of the
sum of the
-th powers of the differences of the components.
Missing values are allowed, and are excluded from all computations
involving the rows within which they occur.
Further, when Inf
values are involved, all pairs of values are
excluded when their contribution to the distance gave NaN
or
NA
.
If some columns are excluded in calculating a Euclidean, Manhattan,
Canberra or Minkowski distance, the sum is scaled up proportionally to
the number of columns used. If all pairs are excluded when
calculating a particular distance, the value is NA
.
The "dist"
method of as.matrix()
and as.dist()
can be used for conversion between objects of class "dist"
and conventional distance matrices.
as.dist()
is a generic function. Its default method handles
objects inheriting from class "dist"
, or coercible to matrices
using as.matrix()
. Support for classes representing
distances (also known as dissimilarities) can be added by providing an
as.matrix()
or, more directly, an as.dist
method
for such a class.
dist
returns an object of class "dist"
.
The lower triangle of the distance matrix stored by columns in a
vector, say do
. If n
is the number of
observations, i.e., n <- attr(do, "Size")
, then
for , the dissimilarity between (row) i and j is
do[n*(i-1) - i*(i-1)/2 + j-i]
.
The length of the vector is , i.e., of order
.
The object has the following attributes (besides "class"
equal
to "dist"
):
Size |
integer, the number of observations in the dataset. |
Labels |
optionally, contains the labels, if any, of the observations of the dataset. |
Diag , Upper
|
logicals corresponding to the arguments |
call |
optionally, the |
method |
optionally, the distance method used; resulting from
|
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
Mardia, K. V., Kent, J. T. and Bibby, J. M. (1979) Multivariate Analysis. Academic Press.
Borg, I. and Groenen, P. (1997) Modern Multidimensional Scaling. Theory and Applications. Springer.
daisy
in the cluster package with more
possibilities in the case of mixed (continuous / categorical)
variables.
hclust
.
require(graphics) x <- matrix(rnorm(100), nrow = 5) dist(x) dist(x, diag = TRUE) dist(x, upper = TRUE) m <- as.matrix(dist(x)) d <- as.dist(m) stopifnot(d == dist(x)) ## Use correlations between variables "as distance" dd <- as.dist((1 - cor(USJudgeRatings))/2) round(1000 * dd) # (prints more nicely) plot(hclust(dd)) # to see a dendrogram of clustered variables ## example of binary and canberra distances. x <- c(0, 0, 1, 1, 1, 1) y <- c(1, 0, 1, 1, 0, 1) dist(rbind(x, y), method = "binary") ## answer 0.4 = 2/5 dist(rbind(x, y), method = "canberra") ## answer 2 * (6/5) ## To find the names labels(eurodist) ## Examples involving "Inf" : ## 1) x[6] <- Inf (m2 <- rbind(x, y)) dist(m2, method = "binary") # warning, answer 0.5 = 2/4 ## These all give "Inf": stopifnot(Inf == dist(m2, method = "euclidean"), Inf == dist(m2, method = "maximum"), Inf == dist(m2, method = "manhattan")) ## "Inf" is same as very large number: x1 <- x; x1[6] <- 1e100 stopifnot(dist(cbind(x, y), method = "canberra") == print(dist(cbind(x1, y), method = "canberra"))) ## 2) y[6] <- Inf #-> 6-th pair is excluded dist(rbind(x, y), method = "binary" ) # warning; 0.5 dist(rbind(x, y), method = "canberra" ) # 3 dist(rbind(x, y), method = "maximum") # 1 dist(rbind(x, y), method = "manhattan") # 2.4
require(graphics) x <- matrix(rnorm(100), nrow = 5) dist(x) dist(x, diag = TRUE) dist(x, upper = TRUE) m <- as.matrix(dist(x)) d <- as.dist(m) stopifnot(d == dist(x)) ## Use correlations between variables "as distance" dd <- as.dist((1 - cor(USJudgeRatings))/2) round(1000 * dd) # (prints more nicely) plot(hclust(dd)) # to see a dendrogram of clustered variables ## example of binary and canberra distances. x <- c(0, 0, 1, 1, 1, 1) y <- c(1, 0, 1, 1, 0, 1) dist(rbind(x, y), method = "binary") ## answer 0.4 = 2/5 dist(rbind(x, y), method = "canberra") ## answer 2 * (6/5) ## To find the names labels(eurodist) ## Examples involving "Inf" : ## 1) x[6] <- Inf (m2 <- rbind(x, y)) dist(m2, method = "binary") # warning, answer 0.5 = 2/4 ## These all give "Inf": stopifnot(Inf == dist(m2, method = "euclidean"), Inf == dist(m2, method = "maximum"), Inf == dist(m2, method = "manhattan")) ## "Inf" is same as very large number: x1 <- x; x1[6] <- 1e100 stopifnot(dist(cbind(x, y), method = "canberra") == print(dist(cbind(x1, y), method = "canberra"))) ## 2) y[6] <- Inf #-> 6-th pair is excluded dist(rbind(x, y), method = "binary" ) # warning; 0.5 dist(rbind(x, y), method = "canberra" ) # 3 dist(rbind(x, y), method = "maximum") # 1 dist(rbind(x, y), method = "manhattan") # 2.4
Density, cumulative distribution function, quantile function and random variate generation for many standard probability distributions are available in the stats package.
The functions for the density/mass function, cumulative distribution
function, quantile function and random variate generation are named in the
form dxxx
, pxxx
, qxxx
and rxxx
respectively.
For the beta distribution see dbeta
.
For the binomial (including Bernoulli) distribution see
dbinom
.
For the Cauchy distribution see dcauchy
.
For the chi-squared distribution see dchisq
.
For the exponential distribution see dexp
.
For the F distribution see df
.
For the gamma distribution see dgamma
.
For the geometric distribution see dgeom
. (This is also
a special case of the negative binomial.)
For the hypergeometric distribution see dhyper
.
For the log-normal distribution see dlnorm
.
For the multinomial distribution see dmultinom
.
For the negative binomial distribution see dnbinom
.
For the normal distribution see dnorm
.
For the Poisson distribution see dpois
.
For the Student's t distribution see dt
.
For the uniform distribution see dunif
.
For the Weibull distribution see dweibull
.
For less common distributions of test statistics see
pbirthday
, dsignrank
,
ptukey
and dwilcox
(and see the
‘See Also’ section of cor.test
).
RNG
about random number generation in R.
The CRAN task view on distributions, https://CRAN.R-project.org/view=Distributions, mentioning several CRAN packages for additional distributions.
This extracts coefficients in terms of the original levels of the coefficients rather than the coded variables.
dummy.coef(object, ...) ## S3 method for class 'lm' dummy.coef(object, use.na = FALSE, ...) ## S3 method for class 'aovlist' dummy.coef(object, use.na = FALSE, ...)
dummy.coef(object, ...) ## S3 method for class 'lm' dummy.coef(object, use.na = FALSE, ...) ## S3 method for class 'aovlist' dummy.coef(object, use.na = FALSE, ...)
object |
a linear model fit. |
use.na |
logical flag for coefficients in a singular model. If
|
... |
arguments passed to or from other methods. |
A fitted linear model has coefficients for the contrasts of the factor
terms, usually one less in number than the number of levels. This
function re-expresses the coefficients in the original coding; as the
coefficients will have been fitted in the reduced basis, any implied
constraints (e.g., zero sum for contr.helmert
or contr.sum
)
will be respected. There will be little point in using
dummy.coef
for contr.treatment
contrasts, as the missing
coefficients are by definition zero.
The method used has some limitations, and will give incomplete results
for terms such as poly(x, 2)
. However, it is adequate for
its main purpose, aov
models.
A list giving for each term the values of the coefficients. For a
multistratum aov
model, such a list for each stratum.
This function is intended for human inspection of the output: it should not be used for calculations. Use coded variables for all calculations.
The results differ from S for singular values, where S can be incorrect.
options(contrasts = c("contr.helmert", "contr.poly")) ## From Venables and Ripley (2002) p.165. npk.aov <- aov(yield ~ block + N*P*K, npk) dummy.coef(npk.aov) npk.aovE <- aov(yield ~ N*P*K + Error(block), npk) dummy.coef(npk.aovE)
options(contrasts = c("contr.helmert", "contr.poly")) ## From Venables and Ripley (2002) p.165. npk.aov <- aov(yield ~ block + N*P*K, npk) dummy.coef(npk.aov) npk.aovE <- aov(yield ~ N*P*K + Error(block), npk) dummy.coef(npk.aovE)
Compute an empirical cumulative distribution function, with several methods for plotting, printing and computing with such an “ecdf” object.
ecdf(x) ## S3 method for class 'ecdf' plot(x, ..., ylab="Fn(x)", verticals = FALSE, col.01line = "gray70", pch = 19) ## S3 method for class 'ecdf' print(x, digits= getOption("digits") - 2, ...) ## S3 method for class 'ecdf' summary(object, ...) ## S3 method for class 'ecdf' quantile(x, ...)
ecdf(x) ## S3 method for class 'ecdf' plot(x, ..., ylab="Fn(x)", verticals = FALSE, col.01line = "gray70", pch = 19) ## S3 method for class 'ecdf' print(x, digits= getOption("digits") - 2, ...) ## S3 method for class 'ecdf' summary(object, ...) ## S3 method for class 'ecdf' quantile(x, ...)
x , object
|
numeric vector of the observations for |
... |
arguments to be passed to subsequent methods, e.g.,
|
ylab |
label for the y-axis. |
verticals |
see |
col.01line |
numeric or character specifying the color of the
horizontal lines at y = 0 and 1, see |
pch |
plotting character. |
digits |
number of significant digits to use, see
|
The e.c.d.f. (empirical cumulative distribution function)
is a step function with jumps
at
observation values, where
is the number of tied observations
at that value. Missing values are ignored.
For observations
x
, ...
,
is the fraction of observations less or equal to
,
i.e.,
The function plot.ecdf
which implements the plot
method for ecdf
objects, is implemented via a call to
plot.stepfun
; see its documentation.
For ecdf
, a function of class "ecdf"
, inheriting from the
"stepfun"
class, and hence inheriting a
knots()
method.
For the summary
method, a summary of the knots of object
with a "header"
attribute.
The quantile(obj, ...)
method computes the same quantiles as
quantile(x, ...)
would where x
is the original sample.
The objects of class "ecdf"
are not intended to be used for
permanent storage and may change structure between versions of R (and
did at R 3.0.0). They can usually be re-created by
eval(attr(old_obj, "call"), environment(old_obj))
since the data used is stored as part of the object's environment.
Martin Maechler; fixes and new features by other R-core members.
stepfun
, the more general class of step functions,
approxfun
and splinefun
.
##-- Simple didactical ecdf example : x <- rnorm(12) Fn <- ecdf(x) Fn # a *function* Fn(x) # returns the percentiles for x tt <- seq(-2, 2, by = 0.1) 12 * Fn(tt) # Fn is a 'simple' function {with values k/12} summary(Fn) ##--> see below for graphics knots(Fn) # the unique data values {12 of them if there were no ties} y <- round(rnorm(12), 1); y[3] <- y[1] Fn12 <- ecdf(y) Fn12 knots(Fn12) # unique values (always less than 12!) summary(Fn12) summary.stepfun(Fn12) ## Advanced: What's inside the function closure? ls(environment(Fn12)) ## "f" "method" "na.rm" "nobs" "x" "y" "yleft" "yright" utils::ls.str(environment(Fn12)) stopifnot(all.equal(quantile(Fn12), quantile(y))) ###----------------- Plotting -------------------------- require(graphics) op <- par(mfrow = c(3, 1), mgp = c(1.5, 0.8, 0), mar = .1+c(3,3,2,1)) F10 <- ecdf(rnorm(10)) summary(F10) plot(F10) plot(F10, verticals = TRUE, do.points = FALSE) plot(Fn12 , lwd = 2) ; mtext("lwd = 2", adj = 1) xx <- unique(sort(c(seq(-3, 2, length.out = 201), knots(Fn12)))) lines(xx, Fn12(xx), col = "blue") abline(v = knots(Fn12), lty = 2, col = "gray70") plot(xx, Fn12(xx), type = "o", cex = .1) #- plot.default {ugly} plot(Fn12, col.hor = "red", add = TRUE) #- plot method abline(v = knots(Fn12), lty = 2, col = "gray70") ## luxury plot plot(Fn12, verticals = TRUE, col.points = "blue", col.hor = "red", col.vert = "bisque") ##-- this works too (automatic call to ecdf(.)): plot.ecdf(rnorm(24)) title("via simple plot.ecdf(x)", adj = 1) par(op)
##-- Simple didactical ecdf example : x <- rnorm(12) Fn <- ecdf(x) Fn # a *function* Fn(x) # returns the percentiles for x tt <- seq(-2, 2, by = 0.1) 12 * Fn(tt) # Fn is a 'simple' function {with values k/12} summary(Fn) ##--> see below for graphics knots(Fn) # the unique data values {12 of them if there were no ties} y <- round(rnorm(12), 1); y[3] <- y[1] Fn12 <- ecdf(y) Fn12 knots(Fn12) # unique values (always less than 12!) summary(Fn12) summary.stepfun(Fn12) ## Advanced: What's inside the function closure? ls(environment(Fn12)) ## "f" "method" "na.rm" "nobs" "x" "y" "yleft" "yright" utils::ls.str(environment(Fn12)) stopifnot(all.equal(quantile(Fn12), quantile(y))) ###----------------- Plotting -------------------------- require(graphics) op <- par(mfrow = c(3, 1), mgp = c(1.5, 0.8, 0), mar = .1+c(3,3,2,1)) F10 <- ecdf(rnorm(10)) summary(F10) plot(F10) plot(F10, verticals = TRUE, do.points = FALSE) plot(Fn12 , lwd = 2) ; mtext("lwd = 2", adj = 1) xx <- unique(sort(c(seq(-3, 2, length.out = 201), knots(Fn12)))) lines(xx, Fn12(xx), col = "blue") abline(v = knots(Fn12), lty = 2, col = "gray70") plot(xx, Fn12(xx), type = "o", cex = .1) #- plot.default {ugly} plot(Fn12, col.hor = "red", add = TRUE) #- plot method abline(v = knots(Fn12), lty = 2, col = "gray70") ## luxury plot plot(Fn12, verticals = TRUE, col.points = "blue", col.hor = "red", col.vert = "bisque") ##-- this works too (automatic call to ecdf(.)): plot.ecdf(rnorm(24)) title("via simple plot.ecdf(x)", adj = 1) par(op)
Computes the efficiencies of fixed-effect terms in an analysis of variance model with multiple strata.
eff.aovlist(aovlist)
eff.aovlist(aovlist)
aovlist |
The result of a call to |
Fixed-effect terms in an analysis of variance model with multiple strata may be estimable in more than one stratum, in which case there is less than complete information in each. The efficiency for a term is the fraction of the maximum possible precision (inverse variance) obtainable by estimating in just that stratum. Under the assumption of balance, this is the same for all contrasts involving that term.
This function is used to pick strata in which to estimate terms in
model.tables.aovlist
and
se.contrast.aovlist
.
In many cases terms will only occur in one stratum, when all the efficiencies will be one: this is detected and no further calculations are done.
The calculation used requires orthogonal contrasts for each term, and will throw an error if non-orthogonal contrasts (e.g., treatment contrasts or an unbalanced design) are detected.
A matrix giving for each non-pure-error stratum (row) the efficiencies for each fixed-effect term in the model.
Heiberger, R. M. (1989) Computation for the Analysis of Designed Experiments. Wiley.
aov
, model.tables.aovlist
,
se.contrast.aovlist
## An example from Yates (1932), ## a 2^3 design in 2 blocks replicated 4 times Block <- gl(8, 4) A <- factor(c(0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1, 0,1,0,1,0,1,0,1,0,1,0,1)) B <- factor(c(0,0,1,1,0,0,1,1,0,1,0,1,1,0,1,0,0,0,1,1, 0,0,1,1,0,0,1,1,0,0,1,1)) C <- factor(c(0,1,1,0,1,0,0,1,0,0,1,1,0,0,1,1,0,1,0,1, 1,0,1,0,0,0,1,1,1,1,0,0)) Yield <- c(101, 373, 398, 291, 312, 106, 265, 450, 106, 306, 324, 449, 272, 89, 407, 338, 87, 324, 279, 471, 323, 128, 423, 334, 131, 103, 445, 437, 324, 361, 302, 272) aovdat <- data.frame(Block, A, B, C, Yield) old <- getOption("contrasts") options(contrasts = c("contr.helmert", "contr.poly")) (fit <- aov(Yield ~ A*B*C + Error(Block), data = aovdat)) eff.aovlist(fit) options(contrasts = old)
## An example from Yates (1932), ## a 2^3 design in 2 blocks replicated 4 times Block <- gl(8, 4) A <- factor(c(0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1, 0,1,0,1,0,1,0,1,0,1,0,1)) B <- factor(c(0,0,1,1,0,0,1,1,0,1,0,1,1,0,1,0,0,0,1,1, 0,0,1,1,0,0,1,1,0,0,1,1)) C <- factor(c(0,1,1,0,1,0,0,1,0,0,1,1,0,0,1,1,0,1,0,1, 1,0,1,0,0,0,1,1,1,1,0,0)) Yield <- c(101, 373, 398, 291, 312, 106, 265, 450, 106, 306, 324, 449, 272, 89, 407, 338, 87, 324, 279, 471, 323, 128, 423, 334, 131, 103, 445, 437, 324, 361, 302, 272) aovdat <- data.frame(Block, A, B, C, Yield) old <- getOption("contrasts") options(contrasts = c("contr.helmert", "contr.poly")) (fit <- aov(Yield ~ A*B*C + Error(Block), data = aovdat)) eff.aovlist(fit) options(contrasts = old)
Returns (orthogonal) effects from a fitted model, usually a linear
model. This is a generic function, but currently only has a methods for
objects inheriting from classes "lm"
and "glm"
.
effects(object, ...) ## S3 method for class 'lm' effects(object, set.sign = FALSE, ...)
effects(object, ...) ## S3 method for class 'lm' effects(object, set.sign = FALSE, ...)
object |
an R object; typically, the result of a model fitting function
such as |
set.sign |
logical. If |
... |
arguments passed to or from other methods. |
For a linear model fitted by lm
or aov
,
the effects are the uncorrelated single-degree-of-freedom values
obtained by projecting the data onto the successive orthogonal
subspaces generated by the QR decomposition during the fitting
process. The first (the rank of the model) are associated with
coefficients and the remainder span the space of residuals (but are
not associated with particular residuals).
Empty models do not have effects.
A (named) numeric vector of the same length as
residuals
, or a matrix if there were multiple responses
in the fitted model, in either case of class "coef"
.
The first rows are labelled by the corresponding coefficients,
and the remaining rows are unlabelled. Note that in rank-deficient
models the corresponding coefficients will be in a different
order if pivoting occurred.
Chambers, J. M. and Hastie, T. J. (1992) Statistical Models in S. Wadsworth & Brooks/Cole.
y <- c(1:3, 7, 5) x <- c(1:3, 6:7) ( ee <- effects(lm(y ~ x)) ) c( round(ee - effects(lm(y+10 ~ I(x-3.8))), 3) ) # just the first is different
y <- c(1:3, 7, 5) x <- c(1:3, 6:7) ( ee <- effects(lm(y ~ x)) ) c( round(ee - effects(lm(y+10 ~ I(x-3.8))), 3) ) # just the first is different
Embeds the time series x
into a low-dimensional
Euclidean space.
embed (x, dimension = 1)
embed (x, dimension = 1)
x |
a numeric vector, matrix, or time series. |
dimension |
a scalar representing the embedding dimension. |
Each row of the resulting matrix consists of sequences
x[t]
, x[t-1]
, ..., x[t-dimension+1]
, where
t
is the original index of x
. If x
is a matrix,
i.e., x
contains more than one variable, then x[t]
consists of the t
-th observation on each variable.
A matrix containing the embedded time series x
.
A. Trapletti, B.D. Ripley
x <- 1:10 embed (x, 3)
x <- 1:10 embed (x, 3)
Evaluates new variables as if they had been part of the formula of the
specified model. This ensures that the same na.action
and
subset
arguments are applied and allows, for example, x
to be recovered for a model using sin(x)
as a predictor.
expand.model.frame(model, extras, envir = environment(formula(model)), na.expand = FALSE)
expand.model.frame(model, extras, envir = environment(formula(model)), na.expand = FALSE)
model |
a fitted model |
extras |
one-sided formula or vector of character strings describing new variables to be added |
envir |
an environment to evaluate things in |
na.expand |
logical; see below |
If na.expand = FALSE
then NA
values in the extra variables
will be passed to the na.action
function used in
model
. This may result in a shorter data frame (with
na.omit
) or an error (with na.fail
). If
na.expand = TRUE
the returned data frame will have precisely the
same rows as model.frame(model)
, but the columns corresponding to
the extra variables may contain NA
.
A data frame.
model <- lm(log(Volume) ~ log(Girth) + log(Height), data = trees) expand.model.frame(model, ~ Girth) # prints data.frame like dd <- data.frame(x = 1:5, y = rnorm(5), z = c(1,2,NA,4,5)) model <- glm(y ~ x, data = dd, subset = 1:4, na.action = na.omit) expand.model.frame(model, "z", na.expand = FALSE) # = default expand.model.frame(model, "z", na.expand = TRUE)
model <- lm(log(Volume) ~ log(Girth) + log(Height), data = trees) expand.model.frame(model, ~ Girth) # prints data.frame like dd <- data.frame(x = 1:5, y = rnorm(5), z = c(1,2,NA,4,5)) model <- glm(y ~ x, data = dd, subset = 1:4, na.action = na.omit) expand.model.frame(model, "z", na.expand = FALSE) # = default expand.model.frame(model, "z", na.expand = TRUE)
Density, distribution function, quantile function and random
generation for the exponential distribution with rate rate
(i.e., mean 1/rate
).
dexp(x, rate = 1, log = FALSE) pexp(q, rate = 1, lower.tail = TRUE, log.p = FALSE) qexp(p, rate = 1, lower.tail = TRUE, log.p = FALSE) rexp(n, rate = 1)
dexp(x, rate = 1, log = FALSE) pexp(q, rate = 1, lower.tail = TRUE, log.p = FALSE) qexp(p, rate = 1, lower.tail = TRUE, log.p = FALSE) rexp(n, rate = 1)
x , q
|
vector of quantiles. |
p |
vector of probabilities. |
n |
number of observations. If |
rate |
vector of rates. |
log , log.p
|
logical; if TRUE, probabilities p are given as log(p). |
lower.tail |
logical; if TRUE (default), probabilities are
|
If rate
is not specified, it assumes the default value of
1
.
The exponential distribution with rate has density
for .
dexp
gives the density,
pexp
gives the distribution function,
qexp
gives the quantile function, and
rexp
generates random deviates.
The length of the result is determined by n
for
rexp
, and is the maximum of the lengths of the
numerical arguments for the other functions.
The numerical arguments other than n
are recycled to the
length of the result. Only the first elements of the logical
arguments are used.
The cumulative hazard
is
-pexp(t, r, lower = FALSE, log = TRUE)
.
dexp
, pexp
and qexp
are all calculated
from numerically stable versions of the definitions.
rexp
uses
Ahrens, J. H. and Dieter, U. (1972). Computer methods for sampling from the exponential and normal distributions. Communications of the ACM, 15, 873–882.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
Johnson, N. L., Kotz, S. and Balakrishnan, N. (1995) Continuous Univariate Distributions, volume 1, chapter 19. Wiley, New York.
exp
for the exponential function.
Distributions for other standard distributions, including
dgamma
for the gamma distribution and
dweibull
for the Weibull distribution, both of which
generalize the exponential.
dexp(1) - exp(-1) #-> 0 ## a fast way to generate *sorted* U[0,1] random numbers: rsunif <- function(n) { n1 <- n+1 cE <- cumsum(rexp(n1)); cE[seq_len(n)]/cE[n1] } plot(rsunif(1000), ylim=0:1, pch=".") abline(0,1/(1000+1), col=adjustcolor(1, 0.5))
dexp(1) - exp(-1) #-> 0 ## a fast way to generate *sorted* U[0,1] random numbers: rsunif <- function(n) { n1 <- n+1 cE <- cumsum(rexp(n1)); cE[seq_len(n)]/cE[n1] } plot(rsunif(1000), ylim=0:1, pch=".") abline(0,1/(1000+1), col=adjustcolor(1, 0.5))
Computes the (generalized) Akaike An Information Criterion for a fitted parametric model.
extractAIC(fit, scale, k = 2, ...)
extractAIC(fit, scale, k = 2, ...)
fit |
fitted model, usually the result of a fitter like
|
scale |
optional numeric specifying the scale parameter of the
model, see |
k |
numeric specifying the ‘weight’ of the
equivalent degrees of freedom ( |
... |
further arguments (currently unused in base R). |
This is a generic function, with methods in base R for classes
"aov"
, "glm"
and "lm"
as well as for
"negbin"
(package MASS) and "coxph"
and
"survreg"
(package survival).
The criterion used is
where is the likelihood and
edf
the equivalent degrees
of freedom (i.e., the number of free parameters for usual parametric
models) of fit
.
For linear models with unknown scale (i.e., for lm
and
aov
), is computed from the
deviance and uses a different additive constant to
logLik
and hence AIC
. If
denotes the (weighted) residual sum of squares then
extractAIC
uses for the formulae
(corresponding
to Mallows'
) in the case of known scale
and
for unknown scale.
AIC
only handles unknown scale and uses the formula
where
are the weights. Further
AIC
counts the scale
estimation as a parameter in the edf
and extractAIC
does not.
For glm
fits the family's aic()
function is used to
compute the AIC: see the note under logLik
about the
assumptions this makes.
k = 2
corresponds to the traditional AIC, using k =
log(n)
provides the BIC (Bayesian IC) instead.
Note that the methods for this function may differ in their
assumptions from those of methods for AIC
(usually
via a method for logLik
). We have already
mentioned the case of "lm"
models with estimated scale, and
there are similar issues in the "glm"
and "negbin"
methods where the dispersion parameter may or may not be taken as
‘free’. This is immaterial as extractAIC
is only used
to compare models of the same class (where only differences in AIC
values are considered).
A numeric vector of length 2, with first and second elements giving
edf |
the ‘equivalent degrees of freedom’
for the fitted model |
AIC |
the (generalized) Akaike Information Criterion for |
This function is used in add1
, drop1
and step
and the similar functions in package
MASS from which it was adopted.
B. D. Ripley
Venables, W. N. and Ripley, B. D. (2002) Modern Applied Statistics with S. New York: Springer (4th ed).
utils::example(glm) extractAIC(glm.D93) #>> 5 15.129
utils::example(glm) extractAIC(glm.D93) #>> 5 15.129
Perform maximum-likelihood factor analysis on a covariance matrix or data matrix.
factanal(x, factors, data = NULL, covmat = NULL, n.obs = NA, subset, na.action, start = NULL, scores = c("none", "regression", "Bartlett"), rotation = "varimax", control = NULL, ...)
factanal(x, factors, data = NULL, covmat = NULL, n.obs = NA, subset, na.action, start = NULL, scores = c("none", "regression", "Bartlett"), rotation = "varimax", control = NULL, ...)
x |
A formula or a numeric matrix or an object that can be coerced to a numeric matrix. |
factors |
The number of factors to be fitted. |
data |
An optional data frame (or similar: see
|
covmat |
A covariance matrix, or a covariance list as returned by
|
n.obs |
The number of observations, used if |
subset |
A specification of the cases to be used, if |
na.action |
The |
start |
|
scores |
Type of scores to produce, if any. The default is none,
|
rotation |
character. |
control |
A list of control values,
|
... |
Components of |
The factor analysis model is
for a –element vector
, a
matrix
of loadings, a
–element vector
of scores and a
–element vector
of
errors. None of the components other than
is observed, but
the major restriction is that the scores be uncorrelated and of unit
variance, and that the errors be independent with variances
, the uniquenesses. It is also common to
scale the observed variables to unit variance, and done in this function.
Thus factor analysis is in essence a model for the correlation matrix
of ,
There is still some indeterminacy in the model for it is unchanged
if is replaced by
for
any orthogonal matrix
. Such matrices
are known as
rotations (although the term is applied also to non-orthogonal
invertible matrices).
If covmat
is supplied it is used. Otherwise x
is used
if it is a matrix, or a formula x
is used with data
to
construct a model matrix, and that is used to construct a covariance
matrix. (It makes no sense for the formula to have a response, and
all the variables must be numeric.) Once a covariance matrix is found
or calculated from x
, it is converted to a correlation matrix
for analysis. The correlation matrix is returned as component
correlation
of the result.
The fit is done by optimizing the log likelihood assuming multivariate
normality over the uniquenesses. (The maximizing loadings for given
uniquenesses can be found analytically:
Lawley & Maxwell (1971, p. 27).)
All the starting values supplied in start
are tried
in turn and the best fit obtained is used. If start = NULL
then the first fit is started at the value suggested by
Jöreskog (1963) and given by
Lawley & Maxwell (1971, p. 31), and then control$nstart - 1
other values are
tried, randomly selected as equal values of the uniquenesses.
The uniquenesses are technically constrained to lie in ,
but near-zero values are problematical, and the optimization is
done with a lower bound of
control$lower
, default 0.005
(Lawley & Maxwell, 1971, p. 32).
Scores can only be produced if a data matrix is supplied and used.
The first method is the regression method of Thomson (1951), the
second the weighted least squares method of Bartlett (1937, 8).
Both are estimates of the unobserved scores . Thomson's method
regresses (in the population) the unknown
on
to yield
and then substitutes the sample estimates of the quantities on the
right-hand side. Bartlett's method minimizes the sum of squares of
standardized errors over the choice of , given (the fitted)
.
If x
is a formula then the standard NA
-handling is
applied to the scores (if requested): see napredict
.
The print
method (documented under loadings
)
follows the factor analysis convention of drawing attention to the
patterns of the results, so the default precision is three decimal
places, and small loadings are suppressed.
An object of class "factanal"
with components
loadings |
A matrix of loadings, one column for each factor. The
factors are ordered in decreasing order of sums of squares of
loadings, and given the sign that will make the sum of the loadings
positive. This is of class |
uniquenesses |
The uniquenesses computed. |
correlation |
The correlation matrix used. |
criteria |
The results of the optimization: the value of the criterion (a linear function of the negative log-likelihood) and information on the iterations used. |
factors |
The argument |
dof |
The number of degrees of freedom of the factor analysis model. |
method |
The method: always |
rotmat |
The rotation matrix if relevant. |
scores |
If requested, a matrix of scores. |
n.obs |
The number of observations if available, or |
call |
The matched call. |
na.action |
If relevant. |
STATISTIC , PVAL
|
The significance-test statistic and P value, if it can be computed. |
There are so many variations on factor analysis that it is hard to compare output from different programs. Further, the optimization in maximum likelihood factor analysis is hard, and many other examples we compared had less good fits than produced by this function. In particular, solutions which are ‘Heywood cases’ (with one or more uniquenesses essentially zero) are much more common than most texts and some other programs would lead one to believe.
Bartlett, M. S. (1937). The statistical conception of mental factors. British Journal of Psychology, 28, 97–104. doi:10.1111/j.2044-8295.1937.tb00863.x.
Bartlett, M. S. (1938). Methods of estimating mental factors. Nature, 141, 609–610. doi:10.1038/141246a0.
Jöreskog, K. G. (1963). Statistical Estimation in Factor Analysis. Almqvist and Wicksell.
Lawley, D. N. and Maxwell, A. E. (1971). Factor Analysis as a Statistical Method. Second edition. Butterworths.
Thomson, G. H. (1951). The Factorial Analysis of Human Ability. London University Press.
loadings
(which explains some details of the
print
method), varimax
, princomp
,
ability.cov
, Harman23.cor
,
Harman74.cor
.
Other rotation methods are available in various contributed packages, including GPArotation and psych.
# A little demonstration, v2 is just v1 with noise, # and same for v4 vs. v3 and v6 vs. v5 # Last four cases are there to add noise # and introduce a positive manifold (g factor) v1 <- c(1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,4,5,6) v2 <- c(1,2,1,1,1,1,2,1,2,1,3,4,3,3,3,4,6,5) v3 <- c(3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,5,4,6) v4 <- c(3,3,4,3,3,1,1,2,1,1,1,1,2,1,1,5,6,4) v5 <- c(1,1,1,1,1,3,3,3,3,3,1,1,1,1,1,6,4,5) v6 <- c(1,1,1,2,1,3,3,3,4,3,1,1,1,2,1,6,5,4) m1 <- cbind(v1,v2,v3,v4,v5,v6) cor(m1) factanal(m1, factors = 3) # varimax is the default factanal(m1, factors = 3, rotation = "promax") # The following shows the g factor as PC1 prcomp(m1) # signs may depend on platform ## formula interface factanal(~v1+v2+v3+v4+v5+v6, factors = 3, scores = "Bartlett")$scores ## a realistic example from Bartholomew (1987, pp. 61-65) utils::example(ability.cov)
# A little demonstration, v2 is just v1 with noise, # and same for v4 vs. v3 and v6 vs. v5 # Last four cases are there to add noise # and introduce a positive manifold (g factor) v1 <- c(1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,4,5,6) v2 <- c(1,2,1,1,1,1,2,1,2,1,3,4,3,3,3,4,6,5) v3 <- c(3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,5,4,6) v4 <- c(3,3,4,3,3,1,1,2,1,1,1,1,2,1,1,5,6,4) v5 <- c(1,1,1,1,1,3,3,3,3,3,1,1,1,1,1,6,4,5) v6 <- c(1,1,1,2,1,3,3,3,4,3,1,1,1,2,1,6,5,4) m1 <- cbind(v1,v2,v3,v4,v5,v6) cor(m1) factanal(m1, factors = 3) # varimax is the default factanal(m1, factors = 3, rotation = "promax") # The following shows the g factor as PC1 prcomp(m1) # signs may depend on platform ## formula interface factanal(~v1+v2+v3+v4+v5+v6, factors = 3, scores = "Bartlett")$scores ## a realistic example from Bartholomew (1987, pp. 61-65) utils::example(ability.cov)
add.scope
and drop.scope
compute those terms that can be
individually added to or dropped from a model while respecting the
hierarchy of terms.
add.scope(terms1, terms2) drop.scope(terms1, terms2) factor.scope(factor, scope)
add.scope(terms1, terms2) drop.scope(terms1, terms2) factor.scope(factor, scope)
terms1 |
the terms or formula for the base model. |
terms2 |
the terms or formula for the upper ( |
factor |
the |
scope |
a list with one or both components |
factor.scope
is not intended to be called directly by users.
For add.scope
and drop.scope
a character vector of
terms labels. For factor.scope
, a list with components
drop
and add
, character vectors of terms labels.
add.scope( ~ a + b + c + a:b, ~ (a + b + c)^3) # [1] "a:c" "b:c" drop.scope( ~ a + b + c + a:b) # [1] "c" "a:b"
add.scope( ~ a + b + c + a:b, ~ (a + b + c)^3) # [1] "a:c" "b:c" drop.scope( ~ a + b + c + a:b) # [1] "c" "a:b"
Family objects provide a convenient way to specify the details of the
models used by functions such as glm
. See the
documentation for glm
for the details on how such model
fitting takes place.
family(object, ...) binomial(link = "logit") gaussian(link = "identity") Gamma(link = "inverse") inverse.gaussian(link = "1/mu^2") poisson(link = "log") quasi(link = "identity", variance = "constant") quasibinomial(link = "logit") quasipoisson(link = "log")
family(object, ...) binomial(link = "logit") gaussian(link = "identity") Gamma(link = "inverse") inverse.gaussian(link = "1/mu^2") poisson(link = "log") quasi(link = "identity", variance = "constant") quasibinomial(link = "logit") quasipoisson(link = "log")
link |
a specification for the model link function. This can be
a name/expression, a literal character string, a length-one character
vector, or an object of class
The The |
variance |
for all families other than |
object |
the function |
... |
further arguments passed to methods. |
family
is a generic function with methods for classes
"glm"
and "lm"
(the latter returning gaussian()
).
For the binomial
and quasibinomial
families the response
can be specified in one of three ways:
As a factor: ‘success’ is interpreted as the factor not having the first level (and hence usually of having the second level).
As a numerical vector with values between 0
and
1
, interpreted as the proportion of successful cases (with the
total number of cases given by the weights
).
As a two-column integer matrix: the first column gives the number of successes and the second the number of failures.
The quasibinomial
and quasipoisson
families differ from
the binomial
and poisson
families only in that the
dispersion parameter is not fixed at one, so they can model
over-dispersion. For the binomial case see
McCullagh and Nelder (1989, pp. 124–8).
Although they show that there is (under some
restrictions) a model with
variance proportional to mean as in the quasi-binomial model, note
that glm
does not compute maximum-likelihood estimates in that
model. The behaviour of S is closer to the quasi- variants.
An object of class "family"
(which has a concise print method).
This is a list with elements
family |
character: the family name. |
link |
character: the link name. |
linkfun |
function: the link. |
linkinv |
function: the inverse of the link function. |
variance |
function: the variance as a function of the mean. |
dev.resids |
function giving the deviance for each observation
as a function of |
aic |
function giving the AIC value if appropriate (but |
mu.eta |
function: derivative of the inverse-link function
with respect to the linear predictor. If the inverse-link
function is |
initialize |
expression. This needs to set up whatever data
objects are needed for the family as well as |
validmu |
logical function. Returns |
valideta |
logical function. Returns |
simulate |
(optional) function |
dispersion |
(optional since R version 4.3.0) numeric: value of the
dispersion parameter, if fixed, or |
The link
and variance
arguments have rather awkward
semantics for back-compatibility. The recommended way is to supply
them as quoted character strings, but they can also be supplied
unquoted (as names or expressions). Additionally, they can be
supplied as a length-one character vector giving the name of one of
the options, or as a list (for link
, of class
"link-glm"
). The restrictions apply only to links given as
names: when given as a character string all the links known to
make.link
are accepted.
This is potentially ambiguous: supplying link = logit
could mean
the unquoted name of a link or the value of object logit
. It
is interpreted if possible as the name of an allowed link, then
as an object. (You can force the interpretation to always be the value of
an object via logit[1]
.)
The design was inspired by S functions of the same names described
in Hastie & Pregibon (1992) (except quasibinomial
and
quasipoisson
).
McCullagh P. and Nelder, J. A. (1989) Generalized Linear Models. London: Chapman and Hall.
Dobson, A. J. (1983) An Introduction to Statistical Modelling. London: Chapman and Hall.
Cox, D. R. and Snell, E. J. (1981). Applied Statistics; Principles and Examples. London: Chapman and Hall.
Hastie, T. J. and Pregibon, D. (1992) Generalized linear models. Chapter 6 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
For binomial coefficients, choose
;
the binomial and negative binomial distributions,
Binomial
, and NegBinomial
.
require(utils) # for str nf <- gaussian() # Normal family nf str(nf) gf <- Gamma() gf str(gf) gf$linkinv gf$variance(-3:4) #- == (.)^2 ## Binomial with default 'logit' link: Check some properties visually: bi <- binomial() et <- seq(-10,10, by=1/8) plot(et, bi$mu.eta(et), type="l") ## show that mu.eta() is derivative of linkinv() : lines((et[-1]+et[-length(et)])/2, col=adjustcolor("red", 1/4), diff(bi$linkinv(et))/diff(et), type="l", lwd=4) ## which here is the logistic density: lines(et, dlogis(et), lwd=3, col=adjustcolor("blue", 1/4)) stopifnot(exprs = { all.equal(bi$ mu.eta(et), dlogis(et)) all.equal(bi$linkinv(et), plogis(et) -> m) all.equal(bi$linkfun(m ), qlogis(m)) # logit(.) == qlogis(.) ! }) ## Data from example(glm) : d.AD <- data.frame(treatment = gl(3,3), outcome = gl(3,1,9), counts = c(18,17,15, 20,10,20, 25,13,12)) glm.D93 <- glm(counts ~ outcome + treatment, d.AD, family = poisson()) ## Quasipoisson: compare with above / example(glm) : glm.qD93 <- glm(counts ~ outcome + treatment, d.AD, family = quasipoisson()) glm.qD93 anova (glm.qD93, test = "F") summary(glm.qD93) ## for Poisson results (same as from 'glm.D93' !) use anova (glm.qD93, dispersion = 1, test = "Chisq") summary(glm.qD93, dispersion = 1) ## Example of user-specified link, a logit model for p^days ## See Shaffer, T. 2004. Auk 121(2): 526-540. logexp <- function(days = 1) { linkfun <- function(mu) qlogis(mu^(1/days)) linkinv <- function(eta) plogis(eta)^days mu.eta <- function(eta) days * plogis(eta)^(days-1) * binomial()$mu.eta(eta) valideta <- function(eta) TRUE link <- paste0("logexp(", days, ")") structure(list(linkfun = linkfun, linkinv = linkinv, mu.eta = mu.eta, valideta = valideta, name = link), class = "link-glm") } (bil3 <- binomial(logexp(3))) ## in practice this would be used with a vector of 'days', in ## which case use an offset of 0 in the corresponding formula ## to get the null deviance right. ## Binomial with identity link: often not a good idea, as both ## computationally and conceptually difficult: binomial(link = "identity") ## is exactly the same as binomial(link = make.link("identity")) ## tests of quasi x <- rnorm(100) y <- rpois(100, exp(1+x)) glm(y ~ x, family = quasi(variance = "mu", link = "log")) # which is the same as glm(y ~ x, family = poisson) glm(y ~ x, family = quasi(variance = "mu^2", link = "log")) ## Not run: glm(y ~ x, family = quasi(variance = "mu^3", link = "log")) # fails y <- rbinom(100, 1, plogis(x)) # need to set a starting value for the next fit glm(y ~ x, family = quasi(variance = "mu(1-mu)", link = "logit"), start = c(0,1))
require(utils) # for str nf <- gaussian() # Normal family nf str(nf) gf <- Gamma() gf str(gf) gf$linkinv gf$variance(-3:4) #- == (.)^2 ## Binomial with default 'logit' link: Check some properties visually: bi <- binomial() et <- seq(-10,10, by=1/8) plot(et, bi$mu.eta(et), type="l") ## show that mu.eta() is derivative of linkinv() : lines((et[-1]+et[-length(et)])/2, col=adjustcolor("red", 1/4), diff(bi$linkinv(et))/diff(et), type="l", lwd=4) ## which here is the logistic density: lines(et, dlogis(et), lwd=3, col=adjustcolor("blue", 1/4)) stopifnot(exprs = { all.equal(bi$ mu.eta(et), dlogis(et)) all.equal(bi$linkinv(et), plogis(et) -> m) all.equal(bi$linkfun(m ), qlogis(m)) # logit(.) == qlogis(.) ! }) ## Data from example(glm) : d.AD <- data.frame(treatment = gl(3,3), outcome = gl(3,1,9), counts = c(18,17,15, 20,10,20, 25,13,12)) glm.D93 <- glm(counts ~ outcome + treatment, d.AD, family = poisson()) ## Quasipoisson: compare with above / example(glm) : glm.qD93 <- glm(counts ~ outcome + treatment, d.AD, family = quasipoisson()) glm.qD93 anova (glm.qD93, test = "F") summary(glm.qD93) ## for Poisson results (same as from 'glm.D93' !) use anova (glm.qD93, dispersion = 1, test = "Chisq") summary(glm.qD93, dispersion = 1) ## Example of user-specified link, a logit model for p^days ## See Shaffer, T. 2004. Auk 121(2): 526-540. logexp <- function(days = 1) { linkfun <- function(mu) qlogis(mu^(1/days)) linkinv <- function(eta) plogis(eta)^days mu.eta <- function(eta) days * plogis(eta)^(days-1) * binomial()$mu.eta(eta) valideta <- function(eta) TRUE link <- paste0("logexp(", days, ")") structure(list(linkfun = linkfun, linkinv = linkinv, mu.eta = mu.eta, valideta = valideta, name = link), class = "link-glm") } (bil3 <- binomial(logexp(3))) ## in practice this would be used with a vector of 'days', in ## which case use an offset of 0 in the corresponding formula ## to get the null deviance right. ## Binomial with identity link: often not a good idea, as both ## computationally and conceptually difficult: binomial(link = "identity") ## is exactly the same as binomial(link = make.link("identity")) ## tests of quasi x <- rnorm(100) y <- rpois(100, exp(1+x)) glm(y ~ x, family = quasi(variance = "mu", link = "log")) # which is the same as glm(y ~ x, family = poisson) glm(y ~ x, family = quasi(variance = "mu^2", link = "log")) ## Not run: glm(y ~ x, family = quasi(variance = "mu^3", link = "log")) # fails y <- rbinom(100, 1, plogis(x)) # need to set a starting value for the next fit glm(y ~ x, family = quasi(variance = "mu(1-mu)", link = "logit"), start = c(0,1))
Density, distribution function, quantile function and random
generation for the F distribution with df1
and df2
degrees of freedom (and optional non-centrality parameter ncp
).
df(x, df1, df2, ncp, log = FALSE) pf(q, df1, df2, ncp, lower.tail = TRUE, log.p = FALSE) qf(p, df1, df2, ncp, lower.tail = TRUE, log.p = FALSE) rf(n, df1, df2, ncp)
df(x, df1, df2, ncp, log = FALSE) pf(q, df1, df2, ncp, lower.tail = TRUE, log.p = FALSE) qf(p, df1, df2, ncp, lower.tail = TRUE, log.p = FALSE) rf(n, df1, df2, ncp)
x , q
|
vector of quantiles. |
p |
vector of probabilities. |
n |
number of observations. If |
df1 , df2
|
degrees of freedom. |
ncp |
non-centrality parameter. If omitted the central F is assumed. |
log , log.p
|
logical; if TRUE, probabilities p are given as log(p). |
lower.tail |
logical; if TRUE (default), probabilities are
|
The F distribution with df1 =
and
df2 =
degrees of freedom has density
for .
The F distribution's cumulative distribution function (cdf),
fulfills (Abramowitz & Stegun 26.6.2, p.946)
where
, and
is the incomplete beta function; in R,
pbeta(x, a,b)
.
It is the distribution of the ratio of the mean squares of
and
independent standard normals, and hence
of the ratio of two independent chi-squared variates each divided by its
degrees of freedom. Since the ratio of a normal and the root
mean-square of
independent normals has a Student's
distribution, the square of a
variate has a F distribution on
1 and
degrees of freedom.
The non-central F distribution is again the ratio of mean squares of
independent normals of unit variance, but those in the numerator are
allowed to have non-zero means and ncp
is the sum of squares of
the means. See Chisquare for further details on
non-central distributions.
df
gives the density,
pf
gives the distribution function
qf
gives the quantile function, and
rf
generates random deviates.
Invalid arguments will result in return value NaN
, with a warning.
The length of the result is determined by n
for
rf
, and is the maximum of the lengths of the
numerical arguments for the other functions.
The numerical arguments other than n
are recycled to the
length of the result. Only the first elements of the logical
arguments are used.
Supplying ncp = 0
uses the algorithm for the non-central
distribution, which is not the same algorithm used if ncp
is
omitted. This is to give consistent behaviour in extreme cases with
values of ncp
very near zero.
The code for non-zero ncp
is principally intended to be used
for moderate values of ncp
: it will not be highly accurate,
especially in the tails, for large values.
For the central case of df
, computed via a binomial
probability, code contributed by Catherine Loader (see
dbinom
); for the non-central case computed via
dbeta
, code contributed by Peter Ruckdeschel.
For pf
, via pbeta
(or for large
df2
, via pchisq
).
For qf
, via qchisq
for large df2
,
else via qbeta
.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
Johnson, N. L., Kotz, S. and Balakrishnan, N. (1995) Continuous Univariate Distributions, volume 2, chapters 27 and 30. Wiley, New York.
Distributions for other standard distributions, including
dchisq
for chi-squared and dt
for Student's
t distributions.
## Equivalence of pt(.,nu) with pf(.^2, 1,nu): x <- seq(0.001, 5, length.out = 100) nu <- 4 stopifnot(all.equal(2*pt(x,nu) - 1, pf(x^2, 1,nu)), ## upper tails: all.equal(2*pt(x, nu, lower.tail=FALSE), pf(x^2, 1,nu, lower.tail=FALSE))) ## the density of the square of a t_m is 2*dt(x, m)/(2*x) # check this is the same as the density of F_{1,m} all.equal(df(x^2, 1, 5), dt(x, 5)/x) ## Identity (F <-> t): qf(2*p - 1, 1, df) == qt(p, df)^2 for p >= 1/2 p <- seq(1/2, .99, length.out = 50); df <- 10 rel.err <- function(x, y) ifelse(x == y, 0, abs(x-y)/mean(abs(c(x,y)))) stopifnot(all.equal(qf(2*p - 1, df1 = 1, df2 = df), qt(p, df)^2)) ## Identity (F <-> Beta <-> incompl.beta): n1 <- 7 ; n2 <- 12; qF <- c((0:4)/4, 1.5, 2:16) x <- n2/(n2 + n1*qF) stopifnot(all.equal(pf(qF, n1, n2, lower.tail=FALSE), pbeta(x, n2/2, n1/2)))
## Equivalence of pt(.,nu) with pf(.^2, 1,nu): x <- seq(0.001, 5, length.out = 100) nu <- 4 stopifnot(all.equal(2*pt(x,nu) - 1, pf(x^2, 1,nu)), ## upper tails: all.equal(2*pt(x, nu, lower.tail=FALSE), pf(x^2, 1,nu, lower.tail=FALSE))) ## the density of the square of a t_m is 2*dt(x, m)/(2*x) # check this is the same as the density of F_{1,m} all.equal(df(x^2, 1, 5), dt(x, 5)/x) ## Identity (F <-> t): qf(2*p - 1, 1, df) == qt(p, df)^2 for p >= 1/2 p <- seq(1/2, .99, length.out = 50); df <- 10 rel.err <- function(x, y) ifelse(x == y, 0, abs(x-y)/mean(abs(c(x,y)))) stopifnot(all.equal(qf(2*p - 1, df1 = 1, df2 = df), qt(p, df)^2)) ## Identity (F <-> Beta <-> incompl.beta): n1 <- 7 ; n2 <- 12; qF <- c((0:4)/4, 1.5, 2:16) x <- n2/(n2 + n1*qF) stopifnot(all.equal(pf(qF, n1, n2, lower.tail=FALSE), pbeta(x, n2/2, n1/2)))
Computes the Discrete Fourier Transform (DFT) of an array with a fast algorithm, the “Fast Fourier Transform” (FFT).
fft(z, inverse = FALSE) mvfft(z, inverse = FALSE)
fft(z, inverse = FALSE) mvfft(z, inverse = FALSE)
z |
a real or complex array containing the values to be transformed. Long vectors are not supported. |
inverse |
if |
When z
is a vector, the value computed and returned by
fft
is the unnormalized univariate discrete Fourier transform of the
sequence of values in z
. Specifically, y <- fft(z)
returns
for where n =
length(y)
. If
inverse
is TRUE
,
is replaced with
.
When z
contains an array, fft
computes and returns the
multivariate (spatial) transform. If inverse
is TRUE
,
the (unnormalized) inverse Fourier transform is returned, i.e.,
if y <- fft(z)
, then z
is
fft(y, inverse = TRUE) / length(y)
.
By contrast, mvfft
takes a real or complex matrix as argument,
and returns a similar shaped matrix, but with each column replaced by
its discrete Fourier transform. This is useful for analyzing
vector-valued series.
The FFT is fastest when the length of the series being transformed is highly composite (i.e., has many factors). If this is not the case, the transform may take a long time to compute and will use a large amount of memory.
Uses C translation of Fortran code in Singleton (1979).
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988). The New S Language. Wadsworth & Brooks/Cole.
Singleton, R. C. (1979). Mixed Radix Fast Fourier Transforms, in Programs for Digital Signal Processing, IEEE Digital Signal Processing Committee eds. IEEE Press.
Cooley, James W., and Tukey, John W. (1965). An algorithm for the machine calculation of complex Fourier series, Mathematics of Computation, 19(90), 297–301. doi:10.2307/2003354.
x <- 1:4 fft(x) fft(fft(x), inverse = TRUE)/length(x) ## Slow Discrete Fourier Transform (DFT) - e.g., for checking the formula fft0 <- function(z, inverse=FALSE) { n <- length(z) if(n == 0) return(z) k <- 0:(n-1) ff <- (if(inverse) 1 else -1) * 2*pi * 1i * k/n vapply(1:n, function(h) sum(z * exp(ff*(h-1))), complex(1)) } relD <- function(x,y) 2* abs(x - y) / abs(x + y) n <- 2^8 z <- complex(n, rnorm(n), rnorm(n)) ## relative differences in the order of 4*10^{-14} : summary(relD(fft(z), fft0(z))) summary(relD(fft(z, inverse=TRUE), fft0(z, inverse=TRUE)))
x <- 1:4 fft(x) fft(fft(x), inverse = TRUE)/length(x) ## Slow Discrete Fourier Transform (DFT) - e.g., for checking the formula fft0 <- function(z, inverse=FALSE) { n <- length(z) if(n == 0) return(z) k <- 0:(n-1) ff <- (if(inverse) 1 else -1) * 2*pi * 1i * k/n vapply(1:n, function(h) sum(z * exp(ff*(h-1))), complex(1)) } relD <- function(x,y) 2* abs(x - y) / abs(x + y) n <- 2^8 z <- complex(n, rnorm(n), rnorm(n)) ## relative differences in the order of 4*10^{-14} : summary(relD(fft(z), fft0(z))) summary(relD(fft(z, inverse=TRUE), fft0(z, inverse=TRUE)))
Applies linear filtering to a univariate time series or to each series separately of a multivariate time series.
filter(x, filter, method = c("convolution", "recursive"), sides = 2, circular = FALSE, init)
filter(x, filter, method = c("convolution", "recursive"), sides = 2, circular = FALSE, init)
x |
a univariate or multivariate time series. |
filter |
a vector of filter coefficients in reverse time order (as for AR or MA coefficients). |
method |
Either |
sides |
for convolution filters only. If |
circular |
for convolution filters only. If |
init |
for recursive filters only. Specifies the initial values of the time series just prior to the start value, in reverse time order. The default is a set of zeros. |
Missing values are allowed in x
but not in filter
(where they would lead to missing values everywhere in the output).
Note that there is an implied coefficient 1 at lag 0 in the recursive filter, which gives
No check is made to see if recursive filter is invertible: the output may diverge if it is not.
The convolution filter is
where o
is the offset: see sides
for how it is determined.
A time series object.
convolve(, type = "filter")
uses the FFT for computations
and so may be faster for long filters on univariate series,
but it does not return a time series (and so the time alignment is
unclear), nor does it handle missing values. filter
is
faster for a filter of length 100 on a series of length 1000,
for example.
x <- 1:100 filter(x, rep(1, 3)) filter(x, rep(1, 3), sides = 1) filter(x, rep(1, 3), sides = 1, circular = TRUE) filter(presidents, rep(1, 3))
x <- 1:100 filter(x, rep(1, 3)) filter(x, rep(1, 3), sides = 1) filter(x, rep(1, 3), sides = 1, circular = TRUE) filter(presidents, rep(1, 3))
Performs Fisher's exact test for testing the null of independence of rows and columns in a contingency table with fixed marginals.
fisher.test(x, y = NULL, workspace = 200000, hybrid = FALSE, hybridPars = c(expect = 5, percent = 80, Emin = 1), control = list(), or = 1, alternative = "two.sided", conf.int = TRUE, conf.level = 0.95, simulate.p.value = FALSE, B = 2000)
fisher.test(x, y = NULL, workspace = 200000, hybrid = FALSE, hybridPars = c(expect = 5, percent = 80, Emin = 1), control = list(), or = 1, alternative = "two.sided", conf.int = TRUE, conf.level = 0.95, simulate.p.value = FALSE, B = 2000)
x |
either a two-dimensional contingency table in matrix form, or a factor object. |
y |
a factor object; ignored if |
workspace |
an integer specifying the size of the workspace
used in the network algorithm. In units of 4 bytes. Only used for
non-simulated p-values larger than |
hybrid |
a logical. Only used for larger than |
hybridPars |
a numeric vector of length 3, by default describing “Cochran's conditions” for the validity of the chi-squared approximation, see ‘Details’. |
control |
a list with named components for low level algorithm
control. At present the only one used is |
or |
the hypothesized odds ratio. Only used in the
|
alternative |
indicates the alternative hypothesis and must be
one of |
conf.int |
logical indicating if a confidence interval for the
odds ratio in a |
conf.level |
confidence level for the returned confidence
interval. Only used in the |
simulate.p.value |
a logical indicating whether to compute
p-values by Monte Carlo simulation, in larger than |
B |
an integer specifying the number of replicates used in the Monte Carlo test. |
If x
is a matrix, it is taken as a two-dimensional contingency
table, and hence its entries should be nonnegative integers.
Otherwise, both x
and y
must be vectors or factors of the same
length. Incomplete cases are removed, vectors are coerced into
factor objects, and the contingency table is computed from these.
For cases, p-values are obtained directly
using the (central or non-central) hypergeometric
distribution. Otherwise, computations are based on a C version of the
FORTRAN subroutine
FEXACT
which implements the network developed by
Mehta and Patel (1983, 1986) and improved by
Clarkson, Fan and Joe (1993).
The FORTRAN code can be obtained from
https://netlib.org/toms/643. Note this fails (with an error
message) when the entries of the table are too large. (It transposes
the table if necessary so it has no more rows than columns. One
constraint is that the product of the row marginals be less than
.)
For tables, the null of conditional
independence is equivalent to the hypothesis that the odds ratio
equals one. ‘Exact’ inference can be based on observing that in
general, given all marginal totals fixed, the first element of the
contingency table has a non-central hypergeometric distribution with
non-centrality parameter given by the odds ratio (Fisher, 1935). The
alternative for a one-sided test is based on the odds ratio, so
alternative = "greater"
is a test of the odds ratio being bigger
than or
.
Two-sided tests are based on the probabilities of the tables, and take as ‘more extreme’ all tables with probabilities less than or equal to that of the observed table, the p-value being the sum of such probabilities.
For larger than tables and
hybrid = TRUE
,
asymptotic chi-squared probabilities are only used if the
‘Cochran conditions’ (or modified version thereof) specified by
hybridPars = c(expect = 5, percent = 80, Emin = 1)
are
satisfied, that is if no cell has expected counts less than
1
(= Emin
) and more than 80% (= percent
) of the
cells have expected counts at least 5 (= expect
), otherwise
the exact calculation is used. A corresponding if()
decision
is made for all sub-tables considered.
Accidentally, R has used 180
instead of 80
as
percent
, i.e., hybridPars[2]
in R versions between
3.0.0 and 3.4.1 (inclusive), i.e., the 2nd of the hybridPars
(all of which used to be hard-coded previous to R 3.5.0).
Consequently, in these versions of R, hybrid=TRUE
never made a
difference.
In the case with
or
,
internal tables can get too large for the exact test in which case an
error is signalled. Apart from increasing
workspace
sufficiently, which then may lead to very long running times, using
simulate.p.value = TRUE
may then often be sufficient and hence
advisable.
Simulation is done conditional on the row and column marginals, and
works only if the marginals are strictly positive. (A C translation
of the algorithm of Patefield (1981) is used.)
Note that the default number of replicates (B = 2000
) implies a
minimum p-value of about 0.0005 ().
A list with class "htest"
containing the following components:
p.value |
the p-value of the test. |
conf.int |
a confidence interval for the odds ratio.
Only present in the |
estimate |
an estimate of the odds ratio. Note that the
conditional Maximum Likelihood Estimate (MLE) rather than the
unconditional MLE (the sample odds ratio) is used.
Only present in the |
null.value |
the odds ratio under the null, |
alternative |
a character string describing the alternative hypothesis. |
method |
the character string
|
data.name |
a character string giving the name(s) of the data. |
Agresti, A. (1990). Categorical data analysis. New York: Wiley. Pages 59–66.
Agresti, A. (2002). Categorical data analysis. Second edition. New York: Wiley. Pages 91–101.
Fisher, R. A. (1935). The logic of inductive inference. Journal of the Royal Statistical Society Series A, 98, 39–54. doi:10.2307/2342435.
Fisher, R. A. (1962). Confidence limits for a cross-product ratio. Australian Journal of Statistics, 4, 41. doi:10.1111/j.1467-842X.1962.tb00285.x.
Fisher, R. A. (1970). Statistical Methods for Research Workers. Oliver & Boyd.
Mehta, Cyrus R. and Patel, Nitin R. (1983).
A network algorithm for performing Fisher's exact test in contingency tables.
Journal of the American Statistical Association, 78,
427–434.
doi:10.1080/01621459.1983.10477989.
Mehta, C. R. and Patel, N. R. (1986).
Algorithm 643: FEXACT, a FORTRAN subroutine for Fisher's exact test
on unordered contingency tables.
ACM Transactions on Mathematical Software, 12,
154–161.
doi:10.1145/6497.214326.
Clarkson, D. B., Fan, Y. and Joe, H. (1993)
A Remark on Algorithm 643: FEXACT: An Algorithm for Performing
Fisher's Exact Test in Contingency Tables.
ACM Transactions on Mathematical Software, 19,
484–488.
doi:10.1145/168173.168412.
Patefield, W. M. (1981). Algorithm AS 159: An efficient method of generating r x c tables with given row and column totals. Applied Statistics, 30, 91–97. doi:10.2307/2346669.
fisher.exact
in package exact2x2 for alternative
interpretations of two-sided tests and confidence intervals for
tables.
## Agresti (1990, p. 61f; 2002, p. 91) Fisher's Tea Drinker ## A British woman claimed to be able to distinguish whether milk or ## tea was added to the cup first. To test, she was given 8 cups of ## tea, in four of which milk was added first. The null hypothesis ## is that there is no association between the true order of pouring ## and the woman's guess, the alternative that there is a positive ## association (that the odds ratio is greater than 1). TeaTasting <- matrix(c(3, 1, 1, 3), nrow = 2, dimnames = list(Guess = c("Milk", "Tea"), Truth = c("Milk", "Tea"))) fisher.test(TeaTasting, alternative = "greater") ## => p = 0.2429, association could not be established ## Fisher (1962, 1970), Criminal convictions of like-sex twins Convictions <- matrix(c(2, 10, 15, 3), nrow = 2, dimnames = list(c("Dizygotic", "Monozygotic"), c("Convicted", "Not convicted"))) Convictions fisher.test(Convictions, alternative = "less") fisher.test(Convictions, conf.int = FALSE) fisher.test(Convictions, conf.level = 0.95)$conf.int fisher.test(Convictions, conf.level = 0.99)$conf.int ## A r x c table Agresti (2002, p. 57) Job Satisfaction Job <- matrix(c(1,2,1,0, 3,3,6,1, 10,10,14,9, 6,7,12,11), 4, 4, dimnames = list(income = c("< 15k", "15-25k", "25-40k", "> 40k"), satisfaction = c("VeryD", "LittleD", "ModerateS", "VeryS"))) fisher.test(Job) # 0.7827 fisher.test(Job, simulate.p.value = TRUE, B = 1e5) # also close to 0.78 ## 6th example in Mehta & Patel's JASA paper MP6 <- rbind( c(1,2,2,1,1,0,1), c(2,0,0,2,3,0,0), c(0,1,1,1,2,7,3), c(1,1,2,0,0,0,1), c(0,1,1,1,1,0,0)) fisher.test(MP6) # Exactly the same p-value, as Cochran's conditions are never met: fisher.test(MP6, hybrid=TRUE)
## Agresti (1990, p. 61f; 2002, p. 91) Fisher's Tea Drinker ## A British woman claimed to be able to distinguish whether milk or ## tea was added to the cup first. To test, she was given 8 cups of ## tea, in four of which milk was added first. The null hypothesis ## is that there is no association between the true order of pouring ## and the woman's guess, the alternative that there is a positive ## association (that the odds ratio is greater than 1). TeaTasting <- matrix(c(3, 1, 1, 3), nrow = 2, dimnames = list(Guess = c("Milk", "Tea"), Truth = c("Milk", "Tea"))) fisher.test(TeaTasting, alternative = "greater") ## => p = 0.2429, association could not be established ## Fisher (1962, 1970), Criminal convictions of like-sex twins Convictions <- matrix(c(2, 10, 15, 3), nrow = 2, dimnames = list(c("Dizygotic", "Monozygotic"), c("Convicted", "Not convicted"))) Convictions fisher.test(Convictions, alternative = "less") fisher.test(Convictions, conf.int = FALSE) fisher.test(Convictions, conf.level = 0.95)$conf.int fisher.test(Convictions, conf.level = 0.99)$conf.int ## A r x c table Agresti (2002, p. 57) Job Satisfaction Job <- matrix(c(1,2,1,0, 3,3,6,1, 10,10,14,9, 6,7,12,11), 4, 4, dimnames = list(income = c("< 15k", "15-25k", "25-40k", "> 40k"), satisfaction = c("VeryD", "LittleD", "ModerateS", "VeryS"))) fisher.test(Job) # 0.7827 fisher.test(Job, simulate.p.value = TRUE, B = 1e5) # also close to 0.78 ## 6th example in Mehta & Patel's JASA paper MP6 <- rbind( c(1,2,2,1,1,0,1), c(2,0,0,2,3,0,0), c(0,1,1,1,2,7,3), c(1,1,2,0,0,0,1), c(0,1,1,1,1,0,0)) fisher.test(MP6) # Exactly the same p-value, as Cochran's conditions are never met: fisher.test(MP6, hybrid=TRUE)
fitted
is a generic function which extracts fitted values from
objects returned by modeling functions. fitted.values
is an
alias for it.
All object classes which are returned by model fitting functions
should provide a fitted
method. (Note that the generic is
fitted
and not fitted.values
.)
Methods can make use of napredict
methods to compensate
for the omission of missing values. The default and nls
methods do.
fitted(object, ...) fitted.values(object, ...)
fitted(object, ...) fitted.values(object, ...)
object |
an object for which the extraction of model fitted values is meaningful. |
... |
other arguments. |
Fitted values extracted from the object object
.
Chambers, J. M. and Hastie, T. J. (1992) Statistical Models in S. Wadsworth & Brooks/Cole.
coefficients
, glm
, lm
,
residuals
.
Returns Tukey's five number summary (minimum, lower-hinge, median, upper-hinge, maximum) for the input data.
fivenum(x, na.rm = TRUE)
fivenum(x, na.rm = TRUE)
x |
|
na.rm |
logical; if |
A numeric vector of length 5 containing the summary information. See
boxplot.stats
for more details.
IQR
,
boxplot.stats
,
median
,
quantile
,
range
.
fivenum(c(rnorm(100), -1:1/0))
fivenum(c(rnorm(100), -1:1/0))
Performs a Fligner-Killeen (median) test of the null that the variances in each of the groups (samples) are the same.
fligner.test(x, ...) ## Default S3 method: fligner.test(x, g, ...) ## S3 method for class 'formula' fligner.test(formula, data, subset, na.action, ...)
fligner.test(x, ...) ## Default S3 method: fligner.test(x, g, ...) ## S3 method for class 'formula' fligner.test(formula, data, subset, na.action, ...)
x |
a numeric vector of data values, or a list of numeric data vectors. |
g |
a vector or factor object giving the group for the
corresponding elements of |
formula |
a formula of the form |
data |
an optional matrix or data frame (or similar: see
|
subset |
an optional vector specifying a subset of observations to be used. |
na.action |
a function which indicates what should happen when
the data contain |
... |
further arguments to be passed to or from methods. |
If x
is a list, its elements are taken as the samples to be
compared for homogeneity of variances, and hence have to be numeric
data vectors. In this case, g
is ignored, and one can simply
use fligner.test(x)
to perform the test. If the samples are
not yet contained in a list, use fligner.test(list(x, ...))
.
Otherwise, x
must be a numeric data vector, and g
must
be a vector or factor object of the same length as x
giving the
group for the corresponding elements of x
.
The Fligner-Killeen (median) test has been determined in a simulation
study as one of the many tests for homogeneity of variances which is
most robust against departures from normality, see
Conover, Johnson & Johnson (1981).
It is a -sample simple linear rank which uses
the ranks of the absolute values of the centered samples and weights
. The version implemented here uses median centering in
each of the samples (F-K:med
in the reference).
A list of class "htest"
containing the following components:
statistic |
the Fligner-Killeen:med |
parameter |
the degrees of freedom of the approximate chi-squared distribution of the test statistic. |
p.value |
the p-value of the test. |
method |
the character string
|
data.name |
a character string giving the names of the data. |
William J. Conover, Mark E. Johnson and Myrle M. Johnson (1981). A comparative study of tests for homogeneity of variances, with applications to the outer continental shelf bidding data. Technometrics, 23, 351–361. doi:10.2307/1268225.
ansari.test
and mood.test
for rank-based
two-sample test for a difference in scale parameters;
var.test
and bartlett.test
for parametric
tests for the homogeneity of variances.
require(graphics) plot(count ~ spray, data = InsectSprays) fligner.test(InsectSprays$count, InsectSprays$spray) fligner.test(count ~ spray, data = InsectSprays) ## Compare this to bartlett.test()
require(graphics) plot(count ~ spray, data = InsectSprays) fligner.test(InsectSprays$count, InsectSprays$spray) fligner.test(count ~ spray, data = InsectSprays) ## Compare this to bartlett.test()
The generic function formula
and its specific methods provide a
way of extracting formulae which have been included in other objects.
as.formula
is almost identical, additionally preserving
attributes when object
already inherits from
"formula"
.
formula(x, ...) DF2formula(x, env = parent.frame()) as.formula(object, env = parent.frame()) ## S3 method for class 'formula' print(x, showEnv = !identical(e, .GlobalEnv), ...)
formula(x, ...) DF2formula(x, env = parent.frame()) as.formula(object, env = parent.frame()) ## S3 method for class 'formula' print(x, showEnv = !identical(e, .GlobalEnv), ...)
x , object
|
R object, for |
... |
further arguments passed to or from other methods. |
env |
the environment to associate with the result, if not already a formula. |
showEnv |
logical indicating if the environment should be printed as well. |
The models fitted by, e.g., the lm
and glm
functions are specified in a compact symbolic form.
The ~
operator is basic in the formation of such models.
An expression of the form y ~ model
is interpreted
as a specification that the response y
is modelled
by a linear predictor specified symbolically by model
.
Such a model consists of a series of terms separated
by +
operators.
The terms themselves consist of variable and factor
names separated by :
operators.
Such a term is interpreted as the interaction of
all the variables and factors appearing in the term.
In addition to +
and :
, a number of other operators are
useful in model formulae.
The *
operator denotes factor crossing: a*b
is
interpreted as a + b + a:b
.
The ^
operator indicates crossing to the specified degree. For example
(a+b+c)^2
is identical to (a+b+c)*(a+b+c)
which in turn
expands to a formula containing the main effects for a
,
b
and c
together with their second-order interactions.
The %in%
operator indicates that the terms on its left are
nested within those on the right. For example a + b %in% a
expands to the formula a + a:b
.
The /
operator provides a shorthand, so that
a / b
is equivalent to a + b %in% a
.
The -
operator removes the specified terms, hence
(a+b+c)^2 - a:b
is identical to a + b + c + b:c + a:c
.
It can also used to remove the intercept term: when fitting a linear
model y ~ x - 1
specifies a line through the origin.
A model with no intercept can be also specified as y ~ x + 0
or y ~ 0 + x
.
While formulae usually involve just variable and factor
names, they can also involve arithmetic expressions.
The formula log(y) ~ a + log(x)
is quite legal.
When such arithmetic expressions involve
operators which are also used symbolically
in model formulae, there can be confusion between
arithmetic and symbolic operator use.
To avoid this confusion, the function I()
can be used to bracket those portions of a model
formula where the operators are used in their
arithmetic sense. For example, in the formula
y ~ a + I(b+c)
, the term b+c
is to be
interpreted as the sum of b
and c
.
Variable names can be quoted by backticks `like this`
in
formulae, although there is no guarantee that all code using formulae
will accept such non-syntactic names.
Most model-fitting functions accept formulae with right-hand-side
including the function offset
to indicate terms with a
fixed coefficient of one. Some functions accept other
‘specials’ such as strata
or cluster
(see the
specials
argument of terms.formula
).
There are two special interpretations of .
in a formula. The
usual one is in the context of a data
argument of model
fitting functions and means ‘all columns not otherwise in the
formula’: see terms.formula
. In the context of
update.formula
, only, it means ‘what was
previously in this part of the formula’.
When formula
is called on a fitted model object, either a
specific method is used (such as that for class "nls"
) or the
default method. The default first looks for a "formula"
component of the object (and evaluates it), then a "terms"
component, then a formula
parameter of the call (and evaluates
its value) and finally a "formula"
attribute.
There is a formula
method for data frames. When there's
"terms"
attribute with a formula, e.g., for a
model.frame()
, that formula is returned. If you'd like the
previous (R 3.5.x) behavior, use the auxiliary
DF2formula()
which does not consider a "terms"
attribute.
Otherwise, if
there is only
one column this forms the RHS with an empty LHS. For more columns,
the first column is the LHS of the formula and the remaining columns
separated by +
form the RHS.
All the functions above produce an object of class "formula"
which contains a symbolic model formula.
A formula object has an associated environment, and
this environment (rather than the parent
environment) is used by model.frame
to evaluate variables
that are not found in the supplied data
argument.
Formulas created with the ~
operator use the
environment in which they were created. Formulas created with
as.formula
will use the env
argument for their
environment.
In R versions up to 3.6.0, character
x
of length
more than one were parsed as separate lines of R code and the first
complete expression was evaluated into a formula when possible. This
silently truncates such vectors of characters inefficiently and to some
extent inconsistently as this behaviour had been undocumented. For this
reason, such use has been deprecated. If you must work via character
x
, do use a string, i.e., a character vector of length one.
E.g., eval(call("~", quote(foo + bar)))
has been an order of magnitude
more efficient
than formula(c("~", "foo + bar"))
.
Further, character “expressions” needing an eval()
to return a formula are now deprecated.
Chambers, J. M. and Hastie, T. J. (1992) Statistical models. Chapter 2 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
For formula manipulation: update.formula
,
terms.formula
, and all.vars
.
For typical use: lm
, glm
, and
coplot
.
For formula construction: reformulate
.
class(fo <- y ~ x1*x2) # "formula" fo typeof(fo) # R internal : "language" terms(fo) environment(fo) environment(as.formula("y ~ x")) environment(as.formula("y ~ x", env = new.env())) ## Create a formula for a model with a large number of variables: xnam <- paste0("x", 1:25) (fmla <- as.formula(paste("y ~ ", paste(xnam, collapse= "+")))) ## Equivalent with reformulate(): fmla2 <- reformulate(xnam, response = "y") stopifnot(identical(fmla, fmla2))
class(fo <- y ~ x1*x2) # "formula" fo typeof(fo) # R internal : "language" terms(fo) environment(fo) environment(as.formula("y ~ x")) environment(as.formula("y ~ x", env = new.env())) ## Create a formula for a model with a large number of variables: xnam <- paste0("x", 1:25) (fmla <- as.formula(paste("y ~ ", paste(xnam, collapse= "+")))) ## Equivalent with reformulate(): fmla2 <- reformulate(xnam, response = "y") stopifnot(identical(fmla, fmla2))
nls
ObjectReturns the model used to fit object
.
## S3 method for class 'nls' formula(x, ...)
## S3 method for class 'nls' formula(x, ...)
x |
an object inheriting from class |
... |
further arguments passed to or from other methods. |
a formula representing the model used to obtain object
.
José Pinheiro and Douglas Bates
fm1 <- nls(circumference ~ A/(1+exp((B-age)/C)), Orange, start = list(A = 160, B = 700, C = 350)) formula(fm1)
fm1 <- nls(circumference ~ A/(1+exp((B-age)/C)), Orange, start = list(A = 160, B = 700, C = 350)) formula(fm1)
Performs a Friedman rank sum test with unreplicated blocked data.
friedman.test(y, ...) ## Default S3 method: friedman.test(y, groups, blocks, ...) ## S3 method for class 'formula' friedman.test(formula, data, subset, na.action, ...)
friedman.test(y, ...) ## Default S3 method: friedman.test(y, groups, blocks, ...) ## S3 method for class 'formula' friedman.test(formula, data, subset, na.action, ...)
y |
either a numeric vector of data values, or a data matrix. |
groups |
a vector giving the group for the corresponding
elements of |
blocks |
a vector giving the block for the corresponding
elements of |
formula |
a formula of the form |
data |
an optional matrix or data frame (or similar: see
|
subset |
an optional vector specifying a subset of observations to be used. |
na.action |
a function which indicates what should happen when
the data contain |
... |
further arguments to be passed to or from methods. |
friedman.test
can be used for analyzing unreplicated complete
block designs (i.e., there is exactly one observation in y
for each combination of levels of groups
and blocks
)
where the normality assumption may be violated.
The null hypothesis is that apart from an effect of blocks
,
the location parameter of y
is the same in each of the
groups
.
If y
is a matrix, groups
and blocks
are
obtained from the column and row indices, respectively. NA
's
are not allowed in groups
or blocks
; if y
contains NA
's, corresponding blocks are removed.
A list with class "htest"
containing the following components:
statistic |
the value of Friedman's chi-squared statistic. |
parameter |
the degrees of freedom of the approximate chi-squared distribution of the test statistic. |
p.value |
the p-value of the test. |
method |
the character string |
data.name |
a character string giving the names of the data. |
Myles Hollander and Douglas A. Wolfe (1973), Nonparametric Statistical Methods. New York: John Wiley & Sons. Pages 139–146.
## Hollander & Wolfe (1973), p. 140ff. ## Comparison of three methods ("round out", "narrow angle", and ## "wide angle") for rounding first base. For each of 18 players ## and the three method, the average time of two runs from a point on ## the first base line 35ft from home plate to a point 15ft short of ## second base is recorded. RoundingTimes <- matrix(c(5.40, 5.50, 5.55, 5.85, 5.70, 5.75, 5.20, 5.60, 5.50, 5.55, 5.50, 5.40, 5.90, 5.85, 5.70, 5.45, 5.55, 5.60, 5.40, 5.40, 5.35, 5.45, 5.50, 5.35, 5.25, 5.15, 5.00, 5.85, 5.80, 5.70, 5.25, 5.20, 5.10, 5.65, 5.55, 5.45, 5.60, 5.35, 5.45, 5.05, 5.00, 4.95, 5.50, 5.50, 5.40, 5.45, 5.55, 5.50, 5.55, 5.55, 5.35, 5.45, 5.50, 5.55, 5.50, 5.45, 5.25, 5.65, 5.60, 5.40, 5.70, 5.65, 5.55, 6.30, 6.30, 6.25), nrow = 22, byrow = TRUE, dimnames = list(1 : 22, c("Round Out", "Narrow Angle", "Wide Angle"))) friedman.test(RoundingTimes) ## => strong evidence against the null that the methods are equivalent ## with respect to speed wb <- aggregate(warpbreaks$breaks, by = list(w = warpbreaks$wool, t = warpbreaks$tension), FUN = mean) wb friedman.test(wb$x, wb$w, wb$t) friedman.test(x ~ w | t, data = wb)
## Hollander & Wolfe (1973), p. 140ff. ## Comparison of three methods ("round out", "narrow angle", and ## "wide angle") for rounding first base. For each of 18 players ## and the three method, the average time of two runs from a point on ## the first base line 35ft from home plate to a point 15ft short of ## second base is recorded. RoundingTimes <- matrix(c(5.40, 5.50, 5.55, 5.85, 5.70, 5.75, 5.20, 5.60, 5.50, 5.55, 5.50, 5.40, 5.90, 5.85, 5.70, 5.45, 5.55, 5.60, 5.40, 5.40, 5.35, 5.45, 5.50, 5.35, 5.25, 5.15, 5.00, 5.85, 5.80, 5.70, 5.25, 5.20, 5.10, 5.65, 5.55, 5.45, 5.60, 5.35, 5.45, 5.05, 5.00, 4.95, 5.50, 5.50, 5.40, 5.45, 5.55, 5.50, 5.55, 5.55, 5.35, 5.45, 5.50, 5.55, 5.50, 5.45, 5.25, 5.65, 5.60, 5.40, 5.70, 5.65, 5.55, 6.30, 6.30, 6.25), nrow = 22, byrow = TRUE, dimnames = list(1 : 22, c("Round Out", "Narrow Angle", "Wide Angle"))) friedman.test(RoundingTimes) ## => strong evidence against the null that the methods are equivalent ## with respect to speed wb <- aggregate(warpbreaks$breaks, by = list(w = warpbreaks$wool, t = warpbreaks$tension), FUN = mean) wb friedman.test(wb$x, wb$w, wb$t) friedman.test(x ~ w | t, data = wb)
Create ‘flat’ contingency tables.
ftable(x, ...) ## Default S3 method: ftable(..., exclude = c(NA, NaN), row.vars = NULL, col.vars = NULL)
ftable(x, ...) ## Default S3 method: ftable(..., exclude = c(NA, NaN), row.vars = NULL, col.vars = NULL)
x , ...
|
R objects which can be interpreted as factors (including
character strings), or a list (or data frame) whose components can
be so interpreted, or a contingency table object of class
|
exclude |
values to use in the exclude argument of |
row.vars |
a vector of integers giving the numbers of the variables, or a character vector giving the names of the variables to be used for the rows of the flat contingency table. |
col.vars |
a vector of integers giving the numbers of the variables, or a character vector giving the names of the variables to be used for the columns of the flat contingency table. |
ftable
creates ‘flat’ contingency tables. Similar to the
usual contingency tables, these contain the counts of each combination
of the levels of the variables (factors) involved. This information
is then re-arranged as a matrix whose rows and columns correspond to
unique combinations of the levels of the row and column variables (as
specified by row.vars
and col.vars
, respectively). The
combinations are created by looping over the variables in reverse
order (so that the levels of the left-most variable vary the
slowest). Displaying a contingency table in this flat matrix form
(via print.ftable
, the print method for objects of class
"ftable"
) is often preferable to showing it as a
higher-dimensional array.
ftable
is a generic function. Its default method,
ftable.default
, first creates a contingency table in array
form from all arguments except row.vars
and col.vars
.
If the first argument is of class "table"
, it represents a
contingency table and is used as is; if it is a flat table of class
"ftable"
, the information it contains is converted to the usual
array representation using as.table
. Otherwise, the arguments
should be R objects which can be interpreted as factors (including
character strings), or a list (or data frame) whose components can be
so interpreted, which are cross-tabulated using table
.
Then, the arguments row.vars
and col.vars
are used to
collapse the contingency table into flat form. If neither of these
two is given, the last variable is used for the columns. If both are
given and their union is a proper subset of all variables involved,
the other variables are summed out.
When the arguments are R expressions interpreted as factors,
additional arguments will be passed to table
to control how
the variable names are displayed; see the last example below.
Function ftable.formula
provides a formula method for
creating flat contingency tables.
There are methods for as.table
, as.matrix
and as.data.frame
.
ftable
returns an object of class "ftable"
, which is a
matrix with counts of each combination of the levels of variables with
information on the names and levels of the (row and columns) variables
stored as attributes "row.vars"
and "col.vars"
.
ftable.formula
for the formula interface (which allows a
data = .
argument);
read.ftable
for information on reading, writing and
coercing flat contingency tables;
table
for ordinary cross-tabulation;
xtabs
for formula-based cross-tabulation.
## Start with a contingency table. ftable(Titanic, row.vars = 1:3) ftable(Titanic, row.vars = 1:2, col.vars = "Survived") ftable(Titanic, row.vars = 2:1, col.vars = "Survived") ## Start with a data frame. x <- ftable(mtcars[c("cyl", "vs", "am", "gear")]) x ftable(x, row.vars = c(2, 4)) ## Start with expressions, use table()'s "dnn" to change labels ftable(mtcars$cyl, mtcars$vs, mtcars$am, mtcars$gear, row.vars = c(2, 4), dnn = c("Cylinders", "V/S", "Transmission", "Gears"))
## Start with a contingency table. ftable(Titanic, row.vars = 1:3) ftable(Titanic, row.vars = 1:2, col.vars = "Survived") ftable(Titanic, row.vars = 2:1, col.vars = "Survived") ## Start with a data frame. x <- ftable(mtcars[c("cyl", "vs", "am", "gear")]) x ftable(x, row.vars = c(2, 4)) ## Start with expressions, use table()'s "dnn" to change labels ftable(mtcars$cyl, mtcars$vs, mtcars$am, mtcars$gear, row.vars = c(2, 4), dnn = c("Cylinders", "V/S", "Transmission", "Gears"))
Produce or manipulate a flat contingency table using formula notation.
## S3 method for class 'formula' ftable(formula, data = NULL, subset, na.action, ...)
## S3 method for class 'formula' ftable(formula, data = NULL, subset, na.action, ...)
formula |
a formula object with both left and right hand sides specifying the column and row variables of the flat table. |
data |
a data frame, list or environment (or similar: see
|
subset |
an optional vector specifying a subset of observations
to be used.
Ignored if |
na.action |
a function which indicates what should happen when
the data contain |
... |
further arguments to the default |
This is a method of the generic function ftable
.
The left and right hand side of formula
specify the column and
row variables, respectively, of the flat contingency table to be
created. Only the +
operator is allowed for combining the
variables. A .
may be used once in the formula to indicate
inclusion of all the remaining variables.
If data
is an object of class "table"
or an array with
more than 2 dimensions, it is taken as a contingency table, and hence
all entries should be nonnegative. Otherwise, if it is not a flat
contingency table (i.e., an object of class "ftable"
), it
should be a data frame or matrix, list or environment containing the
variables to be cross-tabulated. In this case, na.action
is
applied to the data to handle missing values, and, after possibly
selecting a subset of the data as specified by the subset
argument, a contingency table is computed from the variables.
The contingency table is then collapsed to a flat table, according to
the row and column variables specified by formula
.
A flat contingency table which contains the counts of each combination of the levels of the variables, collapsed into a matrix for suitably displaying the counts.
ftable
,
ftable.default
;
table
.
Titanic x <- ftable(Survived ~ ., data = Titanic) x ftable(Sex ~ Class + Age, data = x)
Titanic x <- ftable(Survived ~ ., data = Titanic) x ftable(Sex ~ Class + Age, data = x)
Density, distribution function, quantile function and random
generation for the Gamma distribution with parameters shape
and
scale
.
dgamma(x, shape, rate = 1, scale = 1/rate, log = FALSE) pgamma(q, shape, rate = 1, scale = 1/rate, lower.tail = TRUE, log.p = FALSE) qgamma(p, shape, rate = 1, scale = 1/rate, lower.tail = TRUE, log.p = FALSE) rgamma(n, shape, rate = 1, scale = 1/rate)
dgamma(x, shape, rate = 1, scale = 1/rate, log = FALSE) pgamma(q, shape, rate = 1, scale = 1/rate, lower.tail = TRUE, log.p = FALSE) qgamma(p, shape, rate = 1, scale = 1/rate, lower.tail = TRUE, log.p = FALSE) rgamma(n, shape, rate = 1, scale = 1/rate)
x , q
|
vector of quantiles. |
p |
vector of probabilities. |
n |
number of observations. If |
rate |
an alternative way to specify the scale. |
shape , scale
|
shape and scale parameters. Must be positive,
|
log , log.p
|
logical; if |
lower.tail |
logical; if TRUE (default), probabilities are
|
If scale
is omitted, it assumes the default value of 1
.
The Gamma distribution with parameters shape
and
scale
has density
for ,
and
.
(Here
is the function implemented by R's
gamma()
and defined in its help. Note that
corresponds to the trivial distribution with all mass at point 0.)
The mean and variance are
and
.
The cumulative hazard
is
-pgamma(t, ..., lower = FALSE, log = TRUE)
Note that for smallish values of shape
(and moderate
scale
) a large parts of the mass of the Gamma distribution is
on values of so near zero that they will be represented as
zero in computer arithmetic. So
rgamma
may well return values
which will be represented as zero. (This will also happen for very
large values of scale
since the actual generation is done for
scale = 1
.)
dgamma
gives the density,
pgamma
gives the distribution function,
qgamma
gives the quantile function, and
rgamma
generates random deviates.
Invalid arguments will result in return value NaN
, with a warning.
The length of the result is determined by n
for
rgamma
, and is the maximum of the lengths of the
numerical arguments for the other functions.
The numerical arguments other than n
are recycled to the
length of the result. Only the first elements of the logical
arguments are used.
The S (Becker et al., 1988) parametrization was via shape
and rate
: S had no scale
parameter. It is an error
to supply both scale
and rate
.
pgamma
is closely related to the incomplete gamma function. As
defined by Abramowitz and Stegun 6.5.1 (and by ‘Numerical
Recipes’) this is
is
pgamma(x, a)
. Other authors (for example
Karl Pearson in his 1922 tables) omit the normalizing factor,
defining the incomplete gamma function as
i.e.,
pgamma(x, a) * gamma(a)
.
Yet other use the ‘upper’ incomplete gamma function,
which can be computed by
pgamma(x, a, lower = FALSE) * gamma(a)
.
Note however that pgamma(x, a, ..)
currently requires ,
whereas the incomplete gamma function is also defined for negative
. In that case, you can use
gamma_inc(a,x)
(for
) from package gsl.
See also https://en.wikipedia.org/wiki/Incomplete_gamma_function, or https://dlmf.nist.gov/8.2#i.
dgamma
is computed via the Poisson density, using code contributed
by Catherine Loader (see dbinom
).
pgamma
uses an unpublished (and not otherwise documented)
algorithm ‘mainly by Morten Welinder’.
qgamma
is based on a C translation of
Best, D. J. and D. E. Roberts (1975). Algorithm AS91. Percentage points of the chi-squared distribution. Applied Statistics, 24, 385–388.
plus a final Newton step to improve the approximation.
rgamma
for shape >= 1
uses
Ahrens, J. H. and Dieter, U. (1982). Generating gamma variates by a modified rejection technique. Communications of the ACM, 25, 47–54,
and for 0 < shape < 1
uses
Ahrens, J. H. and Dieter, U. (1974). Computer methods for sampling from gamma, beta, Poisson and binomial distributions. Computing, 12, 223–246.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988). The New S Language. Wadsworth & Brooks/Cole.
Shea, B. L. (1988). Algorithm AS 239: Chi-squared and incomplete Gamma integral, Applied Statistics (JRSS C), 37, 466–473. doi:10.2307/2347328.
Abramowitz, M. and Stegun, I. A. (1972) Handbook of Mathematical Functions. New York: Dover. Chapter 6: Gamma and Related Functions.
NIST Digital Library of Mathematical Functions. https://dlmf.nist.gov/, section 8.2.
gamma
for the gamma function.
Distributions for other standard distributions, including
dbeta
for the Beta distribution and dchisq
for the chi-squared distribution which is a special case of the Gamma
distribution.
-log(dgamma(1:4, shape = 1)) p <- (1:9)/10 pgamma(qgamma(p, shape = 2), shape = 2) 1 - 1/exp(qgamma(p, shape = 1)) # even for shape = 0.001 about half the mass is on numbers # that cannot be represented accurately (and most of those as zero) pgamma(.Machine$double.xmin, 0.001) pgamma(5e-324, 0.001) # on most machines 5e-324 is the smallest # representable non-zero number table(rgamma(1e4, 0.001) == 0)/1e4
-log(dgamma(1:4, shape = 1)) p <- (1:9)/10 pgamma(qgamma(p, shape = 2), shape = 2) 1 - 1/exp(qgamma(p, shape = 1)) # even for shape = 0.001 about half the mass is on numbers # that cannot be represented accurately (and most of those as zero) pgamma(.Machine$double.xmin, 0.001) pgamma(5e-324, 0.001) # on most machines 5e-324 is the smallest # representable non-zero number table(rgamma(1e4, 0.001) == 0)/1e4
Density, distribution function, quantile function and random
generation for the geometric distribution with parameter prob
.
dgeom(x, prob, log = FALSE) pgeom(q, prob, lower.tail = TRUE, log.p = FALSE) qgeom(p, prob, lower.tail = TRUE, log.p = FALSE) rgeom(n, prob)
dgeom(x, prob, log = FALSE) pgeom(q, prob, lower.tail = TRUE, log.p = FALSE) qgeom(p, prob, lower.tail = TRUE, log.p = FALSE) rgeom(n, prob)
x , q
|
vector of quantiles representing the number of failures in a sequence of Bernoulli trials before success occurs. |
p |
vector of probabilities. |
n |
number of observations. If |
prob |
probability of success in each trial. |
log , log.p
|
logical; if TRUE, probabilities p are given as log(p). |
lower.tail |
logical; if TRUE (default), probabilities are
|
The geometric distribution with prob
has density
for ,
.
If an element of x
is not integer, the result of dgeom
is zero, with a warning.
The quantile is defined as the smallest value such that
, where
is the distribution function.
dgeom
gives the density,
pgeom
gives the distribution function,
qgeom
gives the quantile function, and
rgeom
generates random deviates.
Invalid prob
will result in return value NaN
, with a warning.
The length of the result is determined by n
for
rgeom
, and is the maximum of the lengths of the
numerical arguments for the other functions.
The numerical arguments other than n
are recycled to the
length of the result. Only the first elements of the logical
arguments are used.
rgeom
returns a vector of type integer unless generated
values exceed the maximum representable integer when double
values are returned.
dgeom
computes via dbinom
, using code contributed by
Catherine Loader (see dbinom
).
pgeom
and qgeom
are based on the closed-form formulae.
rgeom
uses the derivation as an exponential mixture of Poisson
distributions, see
Devroye, L. (1986) Non-Uniform Random Variate Generation. Springer-Verlag, New York. Page 480.
Distributions for other standard distributions, including
dnbinom
for the negative binomial which generalizes
the geometric distribution.
qgeom((1:9)/10, prob = .2) Ni <- rgeom(20, prob = 1/4); table(factor(Ni, 0:max(Ni)))
qgeom((1:9)/10, prob = .2) Ni <- rgeom(20, prob = 1/4); table(factor(Ni, 0:max(Ni)))
This function evaluates initial parameter estimates for a nonlinear
regression model. If data
is a parameterized data frame or
pframe
object, its parameters
attribute is returned.
Otherwise the object is examined to see if it contains a call to a
selfStart
object whose initial
attribute can be
evaluated.
getInitial(object, data, ...)
getInitial(object, data, ...)
object |
a formula or a |
data |
a data frame in which the expressions in the formula or
arguments to the |
... |
optional additional arguments |
A named numeric vector or list of starting estimates for the
parameters. The construction of many selfStart
models is such
that these "starting" estimates are, in fact, the converged parameter
estimates.
José Pinheiro and Douglas Bates
nls
, selfStart
,
selfStart.default
, selfStart.formula
.
Further, nlsList
from nlme.
PurTrt <- Puromycin[ Puromycin$state == "treated", ] print(getInitial( rate ~ SSmicmen( conc, Vm, K ), PurTrt ), digits = 3)
PurTrt <- Puromycin[ Puromycin$state == "treated", ] print(getInitial( rate ~ SSmicmen( conc, Vm, K ), PurTrt ), digits = 3)
glm
is used to fit generalized linear models, specified by
giving a symbolic description of the linear predictor and a
description of the error distribution.
glm(formula, family = gaussian, data, weights, subset, na.action, start = NULL, etastart, mustart, offset, control = list(...), model = TRUE, method = "glm.fit", x = FALSE, y = TRUE, singular.ok = TRUE, contrasts = NULL, ...) glm.fit(x, y, weights = rep.int(1, nobs), start = NULL, etastart = NULL, mustart = NULL, offset = rep.int(0, nobs), family = gaussian(), control = list(), intercept = TRUE, singular.ok = TRUE) ## S3 method for class 'glm' weights(object, type = c("prior", "working"), ...)
glm(formula, family = gaussian, data, weights, subset, na.action, start = NULL, etastart, mustart, offset, control = list(...), model = TRUE, method = "glm.fit", x = FALSE, y = TRUE, singular.ok = TRUE, contrasts = NULL, ...) glm.fit(x, y, weights = rep.int(1, nobs), start = NULL, etastart = NULL, mustart = NULL, offset = rep.int(0, nobs), family = gaussian(), control = list(), intercept = TRUE, singular.ok = TRUE) ## S3 method for class 'glm' weights(object, type = c("prior", "working"), ...)
formula |
an object of class |
family |
a description of the error distribution and link
function to be used in the model. For |
data |
an optional data frame, list or environment (or object
coercible by |
weights |
an optional vector of ‘prior weights’ to be used
in the fitting process. Should be |
subset |
an optional vector specifying a subset of observations to be used in the fitting process. |
na.action |
a function which indicates what should happen
when the data contain |
start |
starting values for the parameters in the linear predictor. |
etastart |
starting values for the linear predictor. |
mustart |
starting values for the vector of means. |
offset |
this can be used to specify an a priori known
component to be included in the linear predictor during fitting.
This should be |
control |
a list of parameters for controlling the fitting
process. For |
model |
a logical value indicating whether model frame should be included as a component of the returned value. |
method |
the method to be used in fitting the model. The default
method User-supplied fitting functions can be supplied either as a function
or a character string naming a function, with a function which takes
the same arguments as |
x , y
|
For For |
singular.ok |
logical; if |
contrasts |
an optional list. See the |
intercept |
logical. Should an intercept be included in the null model? |
object |
an object inheriting from class |
type |
character, partial matching allowed. Type of weights to extract from the fitted model object. Can be abbreviated. |
... |
For For |
A typical predictor has the form response ~ terms
where
response
is the (numeric) response vector and terms
is a
series of terms which specifies a linear predictor for
response
. For binomial
and quasibinomial
families the response can also be specified as a factor
(when the first level denotes failure and all others success) or as a
two-column matrix with the columns giving the numbers of successes and
failures. A terms specification of the form first + second
indicates all the terms in first
together with all the terms in
second
with any duplicates removed.
A specification of the form first:second
indicates the set
of terms obtained by taking the interactions of all terms in
first
with all terms in second
. The specification
first*second
indicates the cross of first
and
second
. This is the same as first + second +
first:second
.
The terms in the formula will be re-ordered so that main effects come
first, followed by the interactions, all second-order, all third-order
and so on: to avoid this pass a terms
object as the formula.
Non-NULL
weights
can be used to indicate that different
observations have different dispersions (with the values in
weights
being inversely proportional to the dispersions); or
equivalently, when the elements of weights
are positive
integers , that each response
is the mean of
unit-weight observations. For a binomial GLM prior weights
are used to give the number of trials when the response is the
proportion of successes: they would rarely be used for a Poisson GLM.
glm.fit
is the workhorse function: it is not normally called
directly but can be more efficient where the response vector, design
matrix and family have already been calculated.
If more than one of etastart
, start
and mustart
is specified, the first in the list will be used. It is often
advisable to supply starting values for a quasi
family,
and also for families with unusual links such as gaussian("log")
.
All of weights
, subset
, offset
, etastart
and mustart
are evaluated in the same way as variables in
formula
, that is first in data
and then in the
environment of formula
.
For the background to warning messages about ‘fitted probabilities numerically 0 or 1 occurred’ for binomial GLMs, see Venables & Ripley (2002, pp. 197–8).
glm
returns an object of class inheriting from "glm"
which inherits from the class "lm"
. See later in this section.
If a non-standard method
is used, the object will also inherit
from the class (if any) returned by that function.
The function summary
(i.e., summary.glm
) can
be used to obtain or print a summary of the results and the function
anova
(i.e., anova.glm
)
to produce an analysis of variance table.
The generic accessor functions coefficients
,
effects
, fitted.values
and residuals
can be used to
extract various useful features of the value returned by glm
.
weights
extracts a vector of weights, one for each case in the
fit (after subsetting and na.action
).
An object of class "glm"
is a list containing at least the
following components:
coefficients |
a named vector of coefficients |
residuals |
the working residuals, that is the residuals
in the final iteration of the IWLS fit. Since cases with zero
weights are omitted, their working residuals are |
fitted.values |
the fitted mean values, obtained by transforming the linear predictors by the inverse of the link function. |
rank |
the numeric rank of the fitted linear model. |
family |
the |
linear.predictors |
the linear fit on link scale. |
deviance |
up to a constant, minus twice the maximized log-likelihood. Where sensible, the constant is chosen so that a saturated model has deviance zero. |
aic |
A version of Akaike's An Information Criterion,
minus twice the maximized log-likelihood plus twice the number of
parameters, computed via the |
null.deviance |
The deviance for the null model, comparable with
|
iter |
the number of iterations of IWLS used. |
weights |
the working weights, that is the weights in the final iteration of the IWLS fit. |
prior.weights |
the weights initially supplied, a vector of
|
df.residual |
the residual degrees of freedom. |
df.null |
the residual degrees of freedom for the null model. |
y |
if requested (the default) the |
x |
if requested, the model matrix. |
model |
if requested (the default), the model frame. |
converged |
logical. Was the IWLS algorithm judged to have converged? |
boundary |
logical. Is the fitted value on the boundary of the attainable values? |
call |
the matched call. |
formula |
the formula supplied. |
terms |
the |
data |
the |
offset |
the offset vector used. |
control |
the value of the |
method |
the name of the fitter function used (when provided as a
|
contrasts |
(where relevant) the contrasts used. |
xlevels |
(where relevant) a record of the levels of the factors used in fitting. |
na.action |
(where relevant) information returned by
|
In addition, non-empty fits will have components qr
, R
and effects
relating to the final weighted linear fit.
Objects of class "glm"
are normally of class c("glm",
"lm")
, that is inherit from class "lm"
, and well-designed
methods for class "lm"
will be applied to the weighted linear
model at the final iteration of IWLS. However, care is needed, as
extractor functions for class "glm"
such as
residuals
and weights
do not just pick out
the component of the fit with the same name.
If a binomial
glm
model was specified by giving a
two-column response, the weights returned by prior.weights
are
the total numbers of cases (factored by the supplied case weights) and
the component y
of the result is the proportion of successes.
The argument method
serves two purposes. One is to allow the
model frame to be recreated with no fitting. The other is to allow
the default fitting function glm.fit
to be replaced by a
function which takes the same arguments and uses a different fitting
algorithm. If glm.fit
is supplied as a character string it is
used to search for a function of that name, starting in the
stats namespace.
The class of the object return by the fitter (if any) will be
prepended to the class returned by glm
.
The original R implementation of glm
was written by Simon
Davies working for Ross Ihaka at the University of Auckland, but has
since been extensively re-written by members of the R Core team.
The design was inspired by the S function of the same name described in Hastie & Pregibon (1992).
Dobson, A. J. (1990) An Introduction to Generalized Linear Models. London: Chapman and Hall.
Hastie, T. J. and Pregibon, D. (1992) Generalized linear models. Chapter 6 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
McCullagh P. and Nelder, J. A. (1989) Generalized Linear Models. London: Chapman and Hall.
Venables, W. N. and Ripley, B. D. (2002) Modern Applied Statistics with S. New York: Springer.
anova.glm
, summary.glm
, etc. for
glm
methods,
and the generic functions anova
, summary
,
effects
, fitted.values
,
and residuals
.
lm
for non-generalized linear models (which SAS
calls GLMs, for ‘general’ linear models).
loglin
and loglm
(package
MASS) for fitting log-linear models (which binomial and
Poisson GLMs are) to contingency tables.
bigglm
in package biglm for an alternative
way to fit GLMs to large datasets (especially those with many cases).
esoph
, infert
and
predict.glm
have examples of fitting binomial GLMs.
## Dobson (1990) Page 93: Randomized Controlled Trial : counts <- c(18,17,15,20,10,20,25,13,12) outcome <- gl(3,1,9) treatment <- gl(3,3) data.frame(treatment, outcome, counts) # showing data glm.D93 <- glm(counts ~ outcome + treatment, family = poisson()) anova(glm.D93) summary(glm.D93) ## Computing AIC [in many ways]: (A0 <- AIC(glm.D93)) (ll <- logLik(glm.D93)) A1 <- -2*c(ll) + 2*attr(ll, "df") A2 <- glm.D93$family$aic(counts, mu=fitted(glm.D93), wt=1) + 2 * length(coef(glm.D93)) stopifnot(exprs = { all.equal(A0, A1) all.equal(A1, A2) all.equal(A1, glm.D93$aic) }) ## an example with offsets from Venables & Ripley (2002, p.189) utils::data(anorexia, package = "MASS") anorex.1 <- glm(Postwt ~ Prewt + Treat + offset(Prewt), family = gaussian, data = anorexia) summary(anorex.1) # A Gamma example, from McCullagh & Nelder (1989, pp. 300-2) clotting <- data.frame( u = c(5,10,15,20,30,40,60,80,100), lot1 = c(118,58,42,35,27,25,21,19,18), lot2 = c(69,35,26,21,18,16,13,12,12)) summary(glm(lot1 ~ log(u), data = clotting, family = Gamma)) summary(glm(lot2 ~ log(u), data = clotting, family = Gamma)) ## Aliased ("S"ingular) -> 1 NA coefficient (fS <- glm(lot2 ~ log(u) + log(u^2), data = clotting, family = Gamma)) tools::assertError(update(fS, singular.ok=FALSE), verbose=interactive()) ## -> .. "singular fit encountered" ## Not run: ## for an example of the use of a terms object as a formula demo(glm.vr) ## End(Not run)
## Dobson (1990) Page 93: Randomized Controlled Trial : counts <- c(18,17,15,20,10,20,25,13,12) outcome <- gl(3,1,9) treatment <- gl(3,3) data.frame(treatment, outcome, counts) # showing data glm.D93 <- glm(counts ~ outcome + treatment, family = poisson()) anova(glm.D93) summary(glm.D93) ## Computing AIC [in many ways]: (A0 <- AIC(glm.D93)) (ll <- logLik(glm.D93)) A1 <- -2*c(ll) + 2*attr(ll, "df") A2 <- glm.D93$family$aic(counts, mu=fitted(glm.D93), wt=1) + 2 * length(coef(glm.D93)) stopifnot(exprs = { all.equal(A0, A1) all.equal(A1, A2) all.equal(A1, glm.D93$aic) }) ## an example with offsets from Venables & Ripley (2002, p.189) utils::data(anorexia, package = "MASS") anorex.1 <- glm(Postwt ~ Prewt + Treat + offset(Prewt), family = gaussian, data = anorexia) summary(anorex.1) # A Gamma example, from McCullagh & Nelder (1989, pp. 300-2) clotting <- data.frame( u = c(5,10,15,20,30,40,60,80,100), lot1 = c(118,58,42,35,27,25,21,19,18), lot2 = c(69,35,26,21,18,16,13,12,12)) summary(glm(lot1 ~ log(u), data = clotting, family = Gamma)) summary(glm(lot2 ~ log(u), data = clotting, family = Gamma)) ## Aliased ("S"ingular) -> 1 NA coefficient (fS <- glm(lot2 ~ log(u) + log(u^2), data = clotting, family = Gamma)) tools::assertError(update(fS, singular.ok=FALSE), verbose=interactive()) ## -> .. "singular fit encountered" ## Not run: ## for an example of the use of a terms object as a formula demo(glm.vr) ## End(Not run)
Auxiliary function for glm
fitting.
Typically only used internally by glm.fit
, but may be
used to construct a control
argument to either function.
glm.control(epsilon = 1e-8, maxit = 25, trace = FALSE)
glm.control(epsilon = 1e-8, maxit = 25, trace = FALSE)
epsilon |
positive convergence tolerance |
maxit |
integer giving the maximal number of IWLS iterations. |
trace |
logical indicating if output should be produced for each iteration. |
The control
argument of glm
is by default passed
to the control
argument of glm.fit
, which uses
its elements as arguments to glm.control
: the latter provides
defaults and sanity checking.
If epsilon
is small (less than ) it is
also used as the tolerance for the detection of collinearity in the
least squares solution.
When trace
is true, calls to cat
produce the
output for each IWLS iteration. Hence, options(digits = *)
can be used to increase the precision, see the example.
A list with components named as the arguments.
Hastie, T. J. and Pregibon, D. (1992) Generalized linear models. Chapter 6 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
glm.fit
, the fitting procedure used by glm
.
### A variation on example(glm) : ## Annette Dobson's example ... counts <- c(18,17,15,20,10,20,25,13,12) outcome <- gl(3,1,9) treatment <- gl(3,3) oo <- options(digits = 12) # to see more when tracing : glm.D93X <- glm(counts ~ outcome + treatment, family = poisson(), trace = TRUE, epsilon = 1e-14) options(oo) coef(glm.D93X) # the last two are closer to 0 than in ?glm's glm.D93
### A variation on example(glm) : ## Annette Dobson's example ... counts <- c(18,17,15,20,10,20,25,13,12) outcome <- gl(3,1,9) treatment <- gl(3,3) oo <- options(digits = 12) # to see more when tracing : glm.D93X <- glm(counts ~ outcome + treatment, family = poisson(), trace = TRUE, epsilon = 1e-14) options(oo) coef(glm.D93X) # the last two are closer to 0 than in ?glm's glm.D93
These functions are all methods
for class glm
or
summary.glm
objects.
## S3 method for class 'glm' family(object, ...) ## S3 method for class 'glm' residuals(object, type = c("deviance", "pearson", "working", "response", "partial"), ...)
## S3 method for class 'glm' family(object, ...) ## S3 method for class 'glm' residuals(object, type = c("deviance", "pearson", "working", "response", "partial"), ...)
object |
an object of class |
type |
the type of residuals which should be returned.
The alternatives are: |
... |
further arguments passed to or from other methods. |
The references define the types of residuals: Davison & Snell is a good reference for the usages of each.
The partial residuals are a matrix of working residuals, with each column formed by omitting a term from the model.
How residuals
treats cases with missing values in the original
fit is determined by the na.action
argument of that fit.
If na.action = na.omit
omitted cases will not appear in the
residuals, whereas if na.action = na.exclude
they will appear,
with residual value NA
. See also naresid
.
For fits done with y = FALSE
the response values are computed
from other components.
Davison, A. C. and Snell, E. J. (1991) Residuals and diagnostics. In: Statistical Theory and Modelling. In Honour of Sir David Cox, FRS, eds. Hinkley, D. V., Reid, N. and Snell, E. J., Chapman & Hall.
Hastie, T. J. and Pregibon, D. (1992) Generalized linear models. Chapter 6 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
McCullagh P. and Nelder, J. A. (1989) Generalized Linear Models. London: Chapman and Hall.
glm
for computing glm.obj
, anova.glm
;
the corresponding generic functions, summary.glm
,
coef
, deviance
,
df.residual
,
effects
, fitted
,
residuals
.
influence.measures for deletion diagnostics, including
standardized (rstandard
)
and studentized (rstudent
) residuals.
Hierarchical cluster analysis on a set of dissimilarities and methods for analyzing it.
hclust(d, method = "complete", members = NULL) ## S3 method for class 'hclust' plot(x, labels = NULL, hang = 0.1, check = TRUE, axes = TRUE, frame.plot = FALSE, ann = TRUE, main = "Cluster Dendrogram", sub = NULL, xlab = NULL, ylab = "Height", ...)
hclust(d, method = "complete", members = NULL) ## S3 method for class 'hclust' plot(x, labels = NULL, hang = 0.1, check = TRUE, axes = TRUE, frame.plot = FALSE, ann = TRUE, main = "Cluster Dendrogram", sub = NULL, xlab = NULL, ylab = "Height", ...)
d |
a dissimilarity structure as produced by |
method |
the agglomeration method to be used. This should
be (an unambiguous abbreviation of) one of
|
members |
|
x |
an object of the type produced by |
hang |
The fraction of the plot height by which labels should hang below the rest of the plot. A negative value will cause the labels to hang down from 0. |
check |
logical indicating if the |
labels |
A character vector of labels for the leaves of the
tree. By default the row names or row numbers of the original data are
used. If |
axes , frame.plot , ann
|
logical flags as in |
main , sub , xlab , ylab
|
character strings for
|
... |
Further graphical arguments. E.g., |
This function performs a hierarchical cluster analysis
using a set of dissimilarities for the objects being
clustered. Initially, each object is assigned to its own
cluster and then the algorithm proceeds iteratively,
at each stage joining the two most similar clusters,
continuing until there is just a single cluster.
At each stage distances between clusters are recomputed
by the Lance–Williams dissimilarity update formula
according to the particular clustering method being used.
A number of different clustering methods are provided. Ward's
minimum variance method aims at finding compact, spherical clusters.
The complete linkage method finds similar clusters. The
single linkage method (which is closely related to the minimal
spanning tree) adopts a ‘friends of friends’ clustering
strategy. The other methods can be regarded as aiming for clusters
with characteristics somewhere between the single and complete link
methods. Note however, that methods "median"
and
"centroid"
are not leading to a monotone distance
measure, or equivalently the resulting dendrograms can have so called
inversions or reversals which are hard to interpret,
but note the trichotomies in Legendre and Legendre (2012).
Two different algorithms are found in the literature for Ward clustering.
The one used by option "ward.D"
(equivalent to the only Ward
option "ward"
in R versions 3.0.3) does not implement
Ward's (1963) clustering criterion, whereas option
"ward.D2"
implements
that criterion (Murtagh and Legendre 2014). With the latter, the
dissimilarities are squared before cluster updating.
Note that agnes(*, method="ward")
corresponds
to hclust(*, "ward.D2")
.
If members != NULL
, then d
is taken to be a
dissimilarity matrix between clusters instead of dissimilarities
between singletons and members
gives the number of observations
per cluster. This way the hierarchical cluster algorithm can be
‘started in the middle of the dendrogram’, e.g., in order to
reconstruct the part of the tree above a cut (see examples).
Dissimilarities between clusters can be efficiently computed (i.e.,
without hclust
itself) only for a limited number of
distance/linkage combinations, the simplest one being squared
Euclidean distance and centroid linkage. In this case the
dissimilarities between the clusters are the squared Euclidean
distances between cluster means.
In hierarchical cluster displays, a decision is needed at each merge to
specify which subtree should go on the left and which on the right.
Since, for observations there are
merges,
there are
possible orderings for the leaves
in a cluster tree, or dendrogram.
The algorithm used in
hclust
is to order the subtree so that
the tighter cluster is on the left (the last, i.e., most recent,
merge of the left subtree is at a lower value than the last
merge of the right subtree).
Single observations are the tightest clusters possible,
and merges involving two observations place them in order by their
observation sequence number.
An object of class "hclust"
which describes the
tree produced by the clustering process.
The object is a list with components:
merge |
an |
height |
a set of |
order |
a vector giving the permutation of the original
observations suitable for plotting, in the sense that a cluster
plot using this ordering and matrix |
labels |
labels for each of the objects being clustered. |
call |
the call which produced the result. |
method |
the cluster method that has been used. |
dist.method |
the distance that has been used to create |
There are print
, plot
and identify
(see identify.hclust
) methods and the
rect.hclust()
function for hclust
objects.
Method "centroid"
is typically meant to be used with
squared Euclidean distances.
The hclust
function is based on Fortran code
contributed to STATLIB by F. Murtagh.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988). The New S Language. Wadsworth & Brooks/Cole. (S version.)
Everitt, B. (1974). Cluster Analysis. London: Heinemann Educ. Books.
Hartigan, J.A. (1975). Clustering Algorithms. New York: Wiley.
Sneath, P. H. A. and R. R. Sokal (1973). Numerical Taxonomy. San Francisco: Freeman.
Anderberg, M. R. (1973). Cluster Analysis for Applications. Academic Press: New York.
Gordon, A. D. (1999). Classification. Second Edition. London: Chapman and Hall / CRC
Murtagh, F. (1985). “Multidimensional Clustering Algorithms”, in COMPSTAT Lectures 4. Wuerzburg: Physica-Verlag (for algorithmic details of algorithms used).
McQuitty, L.L. (1966). Similarity Analysis by Reciprocal Pairs for Discrete and Continuous Data. Educational and Psychological Measurement, 26, 825–831. doi:10.1177/001316446602600402.
Legendre, P. and L. Legendre (2012). Numerical Ecology, 3rd English ed. Amsterdam: Elsevier Science BV.
Murtagh, Fionn and Legendre, Pierre (2014). Ward's hierarchical agglomerative clustering method: which algorithms implement Ward's criterion? Journal of Classification, 31, 274–295. doi:10.1007/s00357-014-9161-z.
identify.hclust
, rect.hclust
,
cutree
, dendrogram
, kmeans
.
For the Lance–Williams formula and methods that apply it generally,
see agnes
from package cluster.
require(graphics) ### Example 1: Violent crime rates by US state hc <- hclust(dist(USArrests), "ave") plot(hc) plot(hc, hang = -1) ## Do the same with centroid clustering and *squared* Euclidean distance, ## cut the tree into ten clusters and reconstruct the upper part of the ## tree from the cluster centers. hc <- hclust(dist(USArrests)^2, "cen") memb <- cutree(hc, k = 10) cent <- NULL for(k in 1:10){ cent <- rbind(cent, colMeans(USArrests[memb == k, , drop = FALSE])) } hc1 <- hclust(dist(cent)^2, method = "cen", members = table(memb)) opar <- par(mfrow = c(1, 2)) plot(hc, labels = FALSE, hang = -1, main = "Original Tree") plot(hc1, labels = FALSE, hang = -1, main = "Re-start from 10 clusters") par(opar) ### Example 2: Straight-line distances among 10 US cities ## Compare the results of algorithms "ward.D" and "ward.D2" mds2 <- -cmdscale(UScitiesD) plot(mds2, type="n", axes=FALSE, ann=FALSE) text(mds2, labels=rownames(mds2), xpd = NA) hcity.D <- hclust(UScitiesD, "ward.D") # "wrong" hcity.D2 <- hclust(UScitiesD, "ward.D2") opar <- par(mfrow = c(1, 2)) plot(hcity.D, hang=-1) plot(hcity.D2, hang=-1) par(opar)
require(graphics) ### Example 1: Violent crime rates by US state hc <- hclust(dist(USArrests), "ave") plot(hc) plot(hc, hang = -1) ## Do the same with centroid clustering and *squared* Euclidean distance, ## cut the tree into ten clusters and reconstruct the upper part of the ## tree from the cluster centers. hc <- hclust(dist(USArrests)^2, "cen") memb <- cutree(hc, k = 10) cent <- NULL for(k in 1:10){ cent <- rbind(cent, colMeans(USArrests[memb == k, , drop = FALSE])) } hc1 <- hclust(dist(cent)^2, method = "cen", members = table(memb)) opar <- par(mfrow = c(1, 2)) plot(hc, labels = FALSE, hang = -1, main = "Original Tree") plot(hc1, labels = FALSE, hang = -1, main = "Re-start from 10 clusters") par(opar) ### Example 2: Straight-line distances among 10 US cities ## Compare the results of algorithms "ward.D" and "ward.D2" mds2 <- -cmdscale(UScitiesD) plot(mds2, type="n", axes=FALSE, ann=FALSE) text(mds2, labels=rownames(mds2), xpd = NA) hcity.D <- hclust(UScitiesD, "ward.D") # "wrong" hcity.D2 <- hclust(UScitiesD, "ward.D2") opar <- par(mfrow = c(1, 2)) plot(hcity.D, hang=-1) plot(hcity.D2, hang=-1) par(opar)
A heat map is a false color image (basically
image(t(x))
) with a dendrogram added to the left side
and to the top. Typically, reordering of the rows and columns
according to some set of values (row or column means) within the
restrictions imposed by the dendrogram is carried out.
heatmap(x, Rowv = NULL, Colv = if(symm)"Rowv" else NULL, distfun = dist, hclustfun = hclust, reorderfun = function(d, w) reorder(d, w), add.expr, symm = FALSE, revC = identical(Colv, "Rowv"), scale = c("row", "column", "none"), na.rm = TRUE, margins = c(5, 5), ColSideColors, RowSideColors, cexRow = 0.2 + 1/log10(nr), cexCol = 0.2 + 1/log10(nc), labRow = NULL, labCol = NULL, main = NULL, xlab = NULL, ylab = NULL, keep.dendro = FALSE, verbose = getOption("verbose"), ...)
heatmap(x, Rowv = NULL, Colv = if(symm)"Rowv" else NULL, distfun = dist, hclustfun = hclust, reorderfun = function(d, w) reorder(d, w), add.expr, symm = FALSE, revC = identical(Colv, "Rowv"), scale = c("row", "column", "none"), na.rm = TRUE, margins = c(5, 5), ColSideColors, RowSideColors, cexRow = 0.2 + 1/log10(nr), cexCol = 0.2 + 1/log10(nc), labRow = NULL, labCol = NULL, main = NULL, xlab = NULL, ylab = NULL, keep.dendro = FALSE, verbose = getOption("verbose"), ...)
x |
numeric matrix of the values to be plotted. |
Rowv |
determines if and how the row dendrogram should be
computed and reordered. Either a |
Colv |
determines if and how the column dendrogram should be
reordered. Has the same options as the |
distfun |
function used to compute the distance (dissimilarity)
between both rows and columns. Defaults to |
hclustfun |
function used to compute the hierarchical clustering
when |
reorderfun |
|
add.expr |
expression that will be evaluated after the call to
|
symm |
logical indicating if |
revC |
logical indicating if the column order should be
|
scale |
character indicating if the values should be centered and
scaled in either the row direction or the column direction, or
none. The default is |
na.rm |
logical indicating whether |
margins |
numeric vector of length 2 containing the margins
(see |
ColSideColors |
(optional) character vector of length |
RowSideColors |
(optional) character vector of length |
cexRow , cexCol
|
positive numbers, used as |
labRow , labCol
|
character vectors with row and column labels to
use; these default to |
main , xlab , ylab
|
main, x- and y-axis titles; defaults to none. |
keep.dendro |
logical indicating if the dendrogram(s) should be
kept as part of the result (when |
verbose |
logical indicating if information should be printed. |
... |
additional arguments passed on to |
If either Rowv
or Colv
are dendrograms they are honored
(and not reordered). Otherwise, dendrograms are computed as
dd <- as.dendrogram(hclustfun(distfun(X)))
where X
is
either x
or t(x)
.
If either is a vector (of ‘weights’) then the appropriate
dendrogram is reordered according to the supplied values subject to
the constraints imposed by the dendrogram, by reorder(dd,
Rowv)
, in the row case.
If either is missing, as by default, then the ordering of the
corresponding dendrogram is by the mean value of the rows/columns,
i.e., in the case of rows, Rowv <- rowMeans(x, na.rm = na.rm)
.
If either is NA
, no reordering will be done for
the corresponding side.
By default (scale = "row"
) the rows are scaled to have mean
zero and standard deviation one. There is some empirical evidence
from genomic plotting that this is useful.
Invisibly, a list with components
rowInd |
row index permutation vector as returned by
|
colInd |
column index permutation vector. |
Rowv |
the row dendrogram; only if input |
Colv |
the column dendrogram; only if input |
Unless Rowv = NA
(or Colw = NA
), the original rows and
columns are reordered in any case to match the dendrogram,
e.g., the rows by order.dendrogram(Rowv)
where
Rowv
is the (possibly reorder()
ed) row
dendrogram.
heatmap()
uses layout
and draws the
image
in the lower right corner of a 2x2 layout.
Consequentially, it can not be used in a multi column/row
layout, i.e., when par(mfrow = *)
or (mfcol = *)
has been called.
Andy Liaw, original; R. Gentleman, M. Maechler, W. Huber, revisions.
require(graphics); require(grDevices) x <- as.matrix(mtcars) rc <- rainbow(nrow(x), start = 0, end = .3) cc <- rainbow(ncol(x), start = 0, end = .3) hv <- heatmap(x, col = cm.colors(256), scale = "column", RowSideColors = rc, ColSideColors = cc, margins = c(5,10), xlab = "specification variables", ylab = "Car Models", main = "heatmap(<Mtcars data>, ..., scale = \"column\")") utils::str(hv) # the two re-ordering index vectors ## no column dendrogram (nor reordering) at all: heatmap(x, Colv = NA, col = cm.colors(256), scale = "column", RowSideColors = rc, margins = c(5,10), xlab = "specification variables", ylab = "Car Models", main = "heatmap(<Mtcars data>, ..., scale = \"column\")") ## "no nothing" heatmap(x, Rowv = NA, Colv = NA, scale = "column", main = "heatmap(*, NA, NA) ~= image(t(x))") round(Ca <- cor(attitude), 2) symnum(Ca) # simple graphic heatmap(Ca, symm = TRUE, margins = c(6,6)) # with reorder() heatmap(Ca, Rowv = FALSE, symm = TRUE, margins = c(6,6)) # _NO_ reorder() ## slightly artificial with color bar, without and with ordering: cc <- rainbow(nrow(Ca)) heatmap(Ca, Rowv = FALSE, symm = TRUE, RowSideColors = cc, ColSideColors = cc, margins = c(6,6)) heatmap(Ca, symm = TRUE, RowSideColors = cc, ColSideColors = cc, margins = c(6,6)) ## For variable clustering, rather use distance based on cor(): symnum( cU <- cor(USJudgeRatings) ) hU <- heatmap(cU, Rowv = FALSE, symm = TRUE, col = topo.colors(16), distfun = function(c) as.dist(1 - c), keep.dendro = TRUE) ## The Correlation matrix with same reordering: round(100 * cU[hU[[1]], hU[[2]]]) ## The column dendrogram: utils::str(hU$Colv)
require(graphics); require(grDevices) x <- as.matrix(mtcars) rc <- rainbow(nrow(x), start = 0, end = .3) cc <- rainbow(ncol(x), start = 0, end = .3) hv <- heatmap(x, col = cm.colors(256), scale = "column", RowSideColors = rc, ColSideColors = cc, margins = c(5,10), xlab = "specification variables", ylab = "Car Models", main = "heatmap(<Mtcars data>, ..., scale = \"column\")") utils::str(hv) # the two re-ordering index vectors ## no column dendrogram (nor reordering) at all: heatmap(x, Colv = NA, col = cm.colors(256), scale = "column", RowSideColors = rc, margins = c(5,10), xlab = "specification variables", ylab = "Car Models", main = "heatmap(<Mtcars data>, ..., scale = \"column\")") ## "no nothing" heatmap(x, Rowv = NA, Colv = NA, scale = "column", main = "heatmap(*, NA, NA) ~= image(t(x))") round(Ca <- cor(attitude), 2) symnum(Ca) # simple graphic heatmap(Ca, symm = TRUE, margins = c(6,6)) # with reorder() heatmap(Ca, Rowv = FALSE, symm = TRUE, margins = c(6,6)) # _NO_ reorder() ## slightly artificial with color bar, without and with ordering: cc <- rainbow(nrow(Ca)) heatmap(Ca, Rowv = FALSE, symm = TRUE, RowSideColors = cc, ColSideColors = cc, margins = c(6,6)) heatmap(Ca, symm = TRUE, RowSideColors = cc, ColSideColors = cc, margins = c(6,6)) ## For variable clustering, rather use distance based on cor(): symnum( cU <- cor(USJudgeRatings) ) hU <- heatmap(cU, Rowv = FALSE, symm = TRUE, col = topo.colors(16), distfun = function(c) as.dist(1 - c), keep.dendro = TRUE) ## The Correlation matrix with same reordering: round(100 * cU[hU[[1]], hU[[2]]]) ## The column dendrogram: utils::str(hU$Colv)
Computes Holt-Winters Filtering of a given time series. Unknown parameters are determined by minimizing the squared prediction error.
HoltWinters(x, alpha = NULL, beta = NULL, gamma = NULL, seasonal = c("additive", "multiplicative"), start.periods = 2, l.start = NULL, b.start = NULL, s.start = NULL, optim.start = c(alpha = 0.3, beta = 0.1, gamma = 0.1), optim.control = list())
HoltWinters(x, alpha = NULL, beta = NULL, gamma = NULL, seasonal = c("additive", "multiplicative"), start.periods = 2, l.start = NULL, b.start = NULL, s.start = NULL, optim.start = c(alpha = 0.3, beta = 0.1, gamma = 0.1), optim.control = list())
x |
An object of class |
alpha |
|
beta |
|
gamma |
|
seasonal |
Character string to select an |
start.periods |
Start periods used in the autodetection of start values. Must be at least 2. |
l.start |
Start value for level (a[0]). |
b.start |
Start value for trend (b[0]). |
s.start |
Vector of start values for the seasonal component
( |
optim.start |
Vector with named components |
optim.control |
Optional list with additional control parameters
passed to |
The additive Holt-Winters prediction function (for time series with period length p) is
where ,
and
are given by
The multiplicative Holt-Winters prediction function (for time series with period length p) is
where ,
and
are given by
The data in x
are required to be non-zero for a multiplicative
model, but it makes most sense if they are all positive.
The function tries to find the optimal values of and/or
and/or
by minimizing the squared one-step
prediction error if they are
NULL
(the default). optimize
will be used for the single-parameter case, and optim
otherwise.
For seasonal models, start values for a
, b
and s
are inferred by performing a simple decomposition in trend and
seasonal component using moving averages (see function
decompose
) on the start.periods
first periods (a simple
linear regression on the trend component is used for starting level
and trend). For level/trend-models (no seasonal component), start
values for a
and b
are x[2]
and x[2] -
x[1]
, respectively. For level-only models (ordinary exponential
smoothing), the start value for a
is x[1]
.
An object of class "HoltWinters"
, a list with components:
fitted |
A multiple time series with one column for the filtered series as well as for the level, trend and seasonal components, estimated contemporaneously (that is at time t and not at the end of the series). |
x |
The original series |
alpha |
alpha used for filtering |
beta |
beta used for filtering |
gamma |
gamma used for filtering |
coefficients |
A vector with named components |
seasonal |
The specified |
SSE |
The final sum of squared errors achieved in optimizing |
call |
The call used |
David Meyer [email protected]
C. C. Holt (1957) Forecasting seasonals and trends by exponentially weighted moving averages, ONR Research Memorandum, Carnegie Institute of Technology 52. (reprint at doi:10.1016/j.ijforecast.2003.09.015).
P. R. Winters (1960). Forecasting sales by exponentially weighted moving averages. Management Science, 6, 324–342. doi:10.1287/mnsc.6.3.324.
require(graphics) ## Seasonal Holt-Winters (m <- HoltWinters(co2)) plot(m) plot(fitted(m)) (m <- HoltWinters(AirPassengers, seasonal = "mult")) plot(m) ## Non-Seasonal Holt-Winters x <- uspop + rnorm(uspop, sd = 5) m <- HoltWinters(x, gamma = FALSE) plot(m) ## Exponential Smoothing m2 <- HoltWinters(x, gamma = FALSE, beta = FALSE) lines(fitted(m2)[,1], col = 3)
require(graphics) ## Seasonal Holt-Winters (m <- HoltWinters(co2)) plot(m) plot(fitted(m)) (m <- HoltWinters(AirPassengers, seasonal = "mult")) plot(m) ## Non-Seasonal Holt-Winters x <- uspop + rnorm(uspop, sd = 5) m <- HoltWinters(x, gamma = FALSE) plot(m) ## Exponential Smoothing m2 <- HoltWinters(x, gamma = FALSE, beta = FALSE) lines(fitted(m2)[,1], col = 3)
Density, distribution function, quantile function and random generation for the hypergeometric distribution.
dhyper(x, m, n, k, log = FALSE) phyper(q, m, n, k, lower.tail = TRUE, log.p = FALSE) qhyper(p, m, n, k, lower.tail = TRUE, log.p = FALSE) rhyper(nn, m, n, k)
dhyper(x, m, n, k, log = FALSE) phyper(q, m, n, k, lower.tail = TRUE, log.p = FALSE) qhyper(p, m, n, k, lower.tail = TRUE, log.p = FALSE) rhyper(nn, m, n, k)
x , q
|
vector of quantiles representing the number of white balls drawn without replacement from an urn which contains both black and white balls. |
m |
the number of white balls in the urn. |
n |
the number of black balls in the urn. |
k |
the number of balls drawn from the urn, hence must be in
|
p |
probability, it must be between 0 and 1. |
nn |
number of observations. If |
log , log.p
|
logical; if TRUE, probabilities p are given as log(p). |
lower.tail |
logical; if TRUE (default), probabilities are
|
The hypergeometric distribution is used for sampling without
replacement. The density of this distribution with parameters
m
, n
and k
(named ,
, and
, respectively in the reference below, where
is also used
in other references) is given by
for .
Note that is non-zero only for
.
With (hence
in the
reference's notation), the first two moments are mean
and variance
which shows the closeness to the Binomial (where the
hypergeometric has smaller variance unless
).
The quantile is defined as the smallest value such that
, where
is the distribution function.
In rhyper()
, if one of exceeds
.Machine$integer.max
,
currently the equivalent of qhyper(runif(nn), m,n,k)
is used
which is comparably slow while instead a binomial approximation may be
considerably more efficient.
dhyper
gives the density,
phyper
gives the distribution function,
qhyper
gives the quantile function, and
rhyper
generates random deviates.
Invalid arguments will result in return value NaN
, with a warning.
The length of the result is determined by n
for
rhyper
, and is the maximum of the lengths of the
numerical arguments for the other functions.
The numerical arguments other than n
are recycled to the
length of the result. Only the first elements of the logical
arguments are used.
dhyper
computes via binomial probabilities, using code
contributed by Catherine Loader (see dbinom
).
phyper
is based on calculating dhyper
and
phyper(...)/dhyper(...)
(as a summation), based on ideas of Ian
Smith and Morten Welinder.
qhyper
is based on inversion (of an earlier phyper()
algorithm).
rhyper
is based on a corrected version of
Kachitvichyanukul, V. and Schmeiser, B. (1985). Computer generation of hypergeometric random variates. Journal of Statistical Computation and Simulation, 22, 127–145.
Johnson, N. L., Kotz, S., and Kemp, A. W. (1992) Univariate Discrete Distributions, Second Edition. New York: Wiley.
Distributions for other standard distributions.
m <- 10; n <- 7; k <- 8 x <- 0:(k+1) rbind(phyper(x, m, n, k), dhyper(x, m, n, k)) all(phyper(x, m, n, k) == cumsum(dhyper(x, m, n, k))) # FALSE ## but errors are very small: signif(phyper(x, m, n, k) - cumsum(dhyper(x, m, n, k)), digits = 3) stopifnot(abs(phyper(x, m, n, k) - cumsum(dhyper(x, m, n, k))) < 5e-16)
m <- 10; n <- 7; k <- 8 x <- 0:(k+1) rbind(phyper(x, m, n, k), dhyper(x, m, n, k)) all(phyper(x, m, n, k) == cumsum(dhyper(x, m, n, k))) # FALSE ## but errors are very small: signif(phyper(x, m, n, k) - cumsum(dhyper(x, m, n, k)), digits = 3) stopifnot(abs(phyper(x, m, n, k) - cumsum(dhyper(x, m, n, k))) < 5e-16)
identify.hclust
reads the position of the graphics pointer when the
(first) mouse button is pressed. It then cuts the tree at the
vertical position of the pointer and highlights the cluster containing
the horizontal position of the pointer. Optionally a function is
applied to the index of data points contained in the cluster.
## S3 method for class 'hclust' identify(x, FUN = NULL, N = 20, MAXCLUSTER = 20, DEV.FUN = NULL, ...)
## S3 method for class 'hclust' identify(x, FUN = NULL, N = 20, MAXCLUSTER = 20, DEV.FUN = NULL, ...)
x |
an object of the type produced by |
FUN |
(optional) function to be applied to the index numbers of the data points in a cluster (see ‘Details’ below). |
N |
the maximum number of clusters to be identified. |
MAXCLUSTER |
the maximum number of clusters that can be produced by a cut (limits the effective vertical range of the pointer). |
DEV.FUN |
(optional) integer scalar. If specified, the
corresponding graphics device is made active before |
... |
further arguments to |
By default clusters can be identified using the mouse and an
invisible
list of indices of the respective data points
is returned.
If FUN
is not NULL
, then the index vector of data points
is passed to this function as first argument, see the examples
below. The active graphics device for FUN
can be specified using
DEV.FUN
.
The identification process is terminated by pressing any mouse
button other than the first, see also identify
.
Either a list of data point index vectors or a list of return values
of FUN
.
## Not run: require(graphics) hca <- hclust(dist(USArrests)) plot(hca) (x <- identify(hca)) ## Terminate with 2nd mouse button !! hci <- hclust(dist(iris[,1:4])) plot(hci) identify(hci, function(k) print(table(iris[k,5]))) # open a new device (one for dendrogram, one for bars): dev.new() # << make that narrow (& small) # and *beside* 1st one nD <- dev.cur() # to be for the barplot dev.set(dev.prev()) # old one for dendrogram plot(hci) ## select subtrees in dendrogram and "see" the species distribution: identify(hci, function(k) barplot(table(iris[k,5]), col = 2:4), DEV.FUN = nD) ## End(Not run)
## Not run: require(graphics) hca <- hclust(dist(USArrests)) plot(hca) (x <- identify(hca)) ## Terminate with 2nd mouse button !! hci <- hclust(dist(iris[,1:4])) plot(hci) identify(hci, function(k) print(table(iris[k,5]))) # open a new device (one for dendrogram, one for bars): dev.new() # << make that narrow (& small) # and *beside* 1st one nD <- dev.cur() # to be for the barplot dev.set(dev.prev()) # old one for dendrogram plot(hci) ## select subtrees in dendrogram and "see" the species distribution: identify(hci, function(k) barplot(table(iris[k,5]), col = 2:4), DEV.FUN = nD) ## End(Not run)
This suite of functions can be used to compute some of the regression (leave-one-out deletion) diagnostics for linear and generalized linear models discussed in Belsley, Kuh and Welsch (1980), Cook and Weisberg (1982), etc.
influence.measures(model, infl = influence(model)) rstandard(model, ...) ## S3 method for class 'lm' rstandard(model, infl = lm.influence(model, do.coef = FALSE), sd = sqrt(deviance(model)/df.residual(model)), type = c("sd.1", "predictive"), ...) ## S3 method for class 'glm' rstandard(model, infl = influence(model, do.coef = FALSE), type = c("deviance", "pearson"), ...) rstudent(model, ...) ## S3 method for class 'lm' rstudent(model, infl = lm.influence(model, do.coef = FALSE), res = infl$wt.res, ...) ## S3 method for class 'glm' rstudent(model, infl = influence(model, do.coef = FALSE), ...) dffits(model, infl = , res = ) dfbeta(model, ...) ## S3 method for class 'lm' dfbeta(model, infl = lm.influence(model, do.coef = TRUE), ...) dfbetas(model, ...) ## S3 method for class 'lm' dfbetas(model, infl = lm.influence(model, do.coef = TRUE), ...) covratio(model, infl = lm.influence(model, do.coef = FALSE), res = weighted.residuals(model)) cooks.distance(model, ...) ## S3 method for class 'lm' cooks.distance(model, infl = lm.influence(model, do.coef = FALSE), res = weighted.residuals(model), sd = sqrt(deviance(model)/df.residual(model)), hat = infl$hat, ...) ## S3 method for class 'glm' cooks.distance(model, infl = influence(model, do.coef = FALSE), res = infl$pear.res, dispersion = summary(model)$dispersion, hat = infl$hat, ...) hatvalues(model, ...) ## S3 method for class 'lm' hatvalues(model, infl = lm.influence(model, do.coef = FALSE), ...) hat(x, intercept = TRUE)
influence.measures(model, infl = influence(model)) rstandard(model, ...) ## S3 method for class 'lm' rstandard(model, infl = lm.influence(model, do.coef = FALSE), sd = sqrt(deviance(model)/df.residual(model)), type = c("sd.1", "predictive"), ...) ## S3 method for class 'glm' rstandard(model, infl = influence(model, do.coef = FALSE), type = c("deviance", "pearson"), ...) rstudent(model, ...) ## S3 method for class 'lm' rstudent(model, infl = lm.influence(model, do.coef = FALSE), res = infl$wt.res, ...) ## S3 method for class 'glm' rstudent(model, infl = influence(model, do.coef = FALSE), ...) dffits(model, infl = , res = ) dfbeta(model, ...) ## S3 method for class 'lm' dfbeta(model, infl = lm.influence(model, do.coef = TRUE), ...) dfbetas(model, ...) ## S3 method for class 'lm' dfbetas(model, infl = lm.influence(model, do.coef = TRUE), ...) covratio(model, infl = lm.influence(model, do.coef = FALSE), res = weighted.residuals(model)) cooks.distance(model, ...) ## S3 method for class 'lm' cooks.distance(model, infl = lm.influence(model, do.coef = FALSE), res = weighted.residuals(model), sd = sqrt(deviance(model)/df.residual(model)), hat = infl$hat, ...) ## S3 method for class 'glm' cooks.distance(model, infl = influence(model, do.coef = FALSE), res = infl$pear.res, dispersion = summary(model)$dispersion, hat = infl$hat, ...) hatvalues(model, ...) ## S3 method for class 'lm' hatvalues(model, infl = lm.influence(model, do.coef = FALSE), ...) hat(x, intercept = TRUE)
model |
|
infl |
influence structure as returned by
|
res |
(possibly weighted) residuals, with proper default. |
sd |
standard deviation to use, see default. |
dispersion |
dispersion (for |
hat |
hat values |
type |
type of residuals for |
x |
the |
intercept |
should an intercept column be prepended to |
... |
further arguments passed to or from other methods. |
The primary high-level function is influence.measures
which produces a
class "infl"
object tabular display showing the DFBETAs for
each model variable, DFFITs, covariance ratios, Cook's distances and
the diagonal elements of the hat matrix. Cases which are influential
with respect to any of these measures are marked with an asterisk.
The functions dfbetas
, dffits
,
covratio
and cooks.distance
provide direct access to the
corresponding diagnostic quantities. Functions rstandard
and
rstudent
give the standardized and Studentized residuals
respectively. (These re-normalize the residuals to have unit variance,
using an overall and leave-one-out measure of the error variance
respectively.)
Note that for multivariate lm()
models (of class
"mlm"
), these functions return 3d arrays instead of matrices,
or matrices instead of vectors.
Values for generalized linear models are approximations, as described
in Williams (1987) (except that Cook's distances are scaled as
rather than as chi-square values). The approximations can be
poor when some cases have large influence.
The optional infl
, res
and sd
arguments are there
to encourage the use of these direct access functions, in situations
where, e.g., the underlying basic influence measures (from
lm.influence
or the generic influence
) are
already available.
Note that cases with weights == 0
are dropped from all
these functions, but that if a linear model has been fitted with
na.action = na.exclude
, suitable values are filled in for the
cases excluded during fitting.
For linear models, rstandard(*, type = "predictive")
provides
leave-one-out cross validation residuals, and the “PRESS”
statistic (PREdictive Sum of Squares, the same as
the CV score) of model model
is
PRESS <- sum(rstandard(model, type="pred")^2)
The function hat()
exists mainly for S (version 2)
compatibility; we recommend using hatvalues()
instead.
For hatvalues
, dfbeta
, and dfbetas
, the method
for linear models also works for generalized linear models.
Several R core team members and John Fox, originally in his ‘car’ package.
Belsley, D. A., Kuh, E. and Welsch, R. E. (1980). Regression Diagnostics. New York: Wiley.
Cook, R. D. and Weisberg, S. (1982). Residuals and Influence in Regression. London: Chapman and Hall.
Williams, D. A. (1987). Generalized linear model diagnostics using the deviance and single case deletions. Applied Statistics, 36, 181–191. doi:10.2307/2347550.
Fox, J. (1997). Applied Regression, Linear Models, and Related Methods. Sage.
Fox, J. (2002) An R and S-Plus Companion to Applied Regression. Sage Publ.
Fox, J. and Weisberg, S. (2011). An R Companion to Applied Regression, second edition. Sage Publ; https://socialsciences.mcmaster.ca/jfox/Books/Companion/.
influence
(containing lm.influence
).
‘plotmath’ for the use of hat
in plot annotation.
require(graphics) ## Analysis of the life-cycle savings data ## given in Belsley, Kuh and Welsch. lm.SR <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings) inflm.SR <- influence.measures(lm.SR) which(apply(inflm.SR$is.inf, 1, any)) # which observations 'are' influential summary(inflm.SR) # only these inflm.SR # all plot(rstudent(lm.SR) ~ hatvalues(lm.SR)) # recommended by some plot(lm.SR, which = 5) # an enhanced version of that via plot(<lm>) ## The 'infl' argument is not needed, but avoids recomputation: rs <- rstandard(lm.SR) iflSR <- influence(lm.SR) all.equal(rs, rstandard(lm.SR, infl = iflSR), tolerance = 1e-10) ## to "see" the larger values: 1000 * round(dfbetas(lm.SR, infl = iflSR), 3) cat("PRESS :"); (PRESS <- sum( rstandard(lm.SR, type = "predictive")^2 )) stopifnot(all.equal(PRESS, sum( (residuals(lm.SR) / (1 - iflSR$hat))^2))) ## Show that "PRE-residuals" == L.O.O. Crossvalidation (CV) errors: X <- model.matrix(lm.SR) y <- model.response(model.frame(lm.SR)) ## Leave-one-out CV least-squares prediction errors (relatively fast) rCV <- vapply(seq_len(nrow(X)), function(i) y[i] - X[i,] %*% .lm.fit(X[-i,], y[-i])$coefficients, numeric(1)) ## are the same as the *faster* rstandard(*, "pred") : stopifnot(all.equal(rCV, unname(rstandard(lm.SR, type = "predictive")))) ## Huber's data [Atkinson 1985] xh <- c(-4:0, 10) yh <- c(2.48, .73, -.04, -1.44, -1.32, 0) lmH <- lm(yh ~ xh) summary(lmH) im <- influence.measures(lmH) im is.inf <- apply(im$is.inf, 1, any) plot(xh,yh, main = "Huber's data: L.S. line and influential obs.") abline(lmH); points(xh[is.inf], yh[is.inf], pch = 20, col = 2) ## Irwin's data [Williams 1987] xi <- 1:5 yi <- c(0,2,14,19,30) # number of mice responding to dose xi mi <- rep(40, 5) # number of mice exposed glmI <- glm(cbind(yi, mi -yi) ~ xi, family = binomial) summary(glmI) signif(cooks.distance(glmI), 3) # ~= Ci in Table 3, p.184 imI <- influence.measures(glmI) imI stopifnot(all.equal(imI$infmat[,"cook.d"], cooks.distance(glmI)))
require(graphics) ## Analysis of the life-cycle savings data ## given in Belsley, Kuh and Welsch. lm.SR <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings) inflm.SR <- influence.measures(lm.SR) which(apply(inflm.SR$is.inf, 1, any)) # which observations 'are' influential summary(inflm.SR) # only these inflm.SR # all plot(rstudent(lm.SR) ~ hatvalues(lm.SR)) # recommended by some plot(lm.SR, which = 5) # an enhanced version of that via plot(<lm>) ## The 'infl' argument is not needed, but avoids recomputation: rs <- rstandard(lm.SR) iflSR <- influence(lm.SR) all.equal(rs, rstandard(lm.SR, infl = iflSR), tolerance = 1e-10) ## to "see" the larger values: 1000 * round(dfbetas(lm.SR, infl = iflSR), 3) cat("PRESS :"); (PRESS <- sum( rstandard(lm.SR, type = "predictive")^2 )) stopifnot(all.equal(PRESS, sum( (residuals(lm.SR) / (1 - iflSR$hat))^2))) ## Show that "PRE-residuals" == L.O.O. Crossvalidation (CV) errors: X <- model.matrix(lm.SR) y <- model.response(model.frame(lm.SR)) ## Leave-one-out CV least-squares prediction errors (relatively fast) rCV <- vapply(seq_len(nrow(X)), function(i) y[i] - X[i,] %*% .lm.fit(X[-i,], y[-i])$coefficients, numeric(1)) ## are the same as the *faster* rstandard(*, "pred") : stopifnot(all.equal(rCV, unname(rstandard(lm.SR, type = "predictive")))) ## Huber's data [Atkinson 1985] xh <- c(-4:0, 10) yh <- c(2.48, .73, -.04, -1.44, -1.32, 0) lmH <- lm(yh ~ xh) summary(lmH) im <- influence.measures(lmH) im is.inf <- apply(im$is.inf, 1, any) plot(xh,yh, main = "Huber's data: L.S. line and influential obs.") abline(lmH); points(xh[is.inf], yh[is.inf], pch = 20, col = 2) ## Irwin's data [Williams 1987] xi <- 1:5 yi <- c(0,2,14,19,30) # number of mice responding to dose xi mi <- rep(40, 5) # number of mice exposed glmI <- glm(cbind(yi, mi -yi) ~ xi, family = binomial) summary(glmI) signif(cooks.distance(glmI), 3) # ~= Ci in Table 3, p.184 imI <- influence.measures(glmI) imI stopifnot(all.equal(imI$infmat[,"cook.d"], cooks.distance(glmI)))
Adaptive quadrature of functions of one variable over a finite or infinite interval.
integrate(f, lower, upper, ..., subdivisions = 100L, rel.tol = .Machine$double.eps^0.25, abs.tol = rel.tol, stop.on.error = TRUE, keep.xy = FALSE, aux = NULL)
integrate(f, lower, upper, ..., subdivisions = 100L, rel.tol = .Machine$double.eps^0.25, abs.tol = rel.tol, stop.on.error = TRUE, keep.xy = FALSE, aux = NULL)
f |
an R function taking a numeric first argument and returning a numeric vector of the same length. Returning a non-finite element will generate an error. |
lower , upper
|
the limits of integration. Can be infinite. |
... |
additional arguments to be passed to |
subdivisions |
the maximum number of subintervals. |
rel.tol |
relative accuracy requested. |
abs.tol |
absolute accuracy requested. |
stop.on.error |
logical. If true (the default) an error stops the
function. If false some errors will give a result with a warning in
the |
keep.xy |
unused. For compatibility with S. |
aux |
unused. For compatibility with S. |
Note that arguments after ...
must be matched exactly.
If one or both limits are infinite, the infinite range is mapped onto a finite interval.
For a finite interval, globally adaptive interval subdivision is used in connection with extrapolation by Wynn's Epsilon algorithm, with the basic step being Gauss–Kronrod quadrature.
rel.tol
cannot be less than max(50*.Machine$double.eps,
0.5e-28)
if abs.tol <= 0
.
Note that the comments in the C source code in
‘R/src/appl/integrate.c’ give more details, particularly about
reasons for failure (internal error code ier >= 1
).
In R versions 3.2.x, the first entries of
lower
and upper
were used whereas an error is signalled
now if they are not of length one.
A list of class "integrate"
with components
value |
the final estimate of the integral. |
abs.error |
estimate of the modulus of the absolute error. |
subdivisions |
the number of subintervals produced in the subdivision process. |
message |
|
call |
the matched call. |
Like all numerical integration routines, these evaluate the function on a finite set of points. If the function is approximately constant (in particular, zero) over nearly all its range it is possible that the result and error estimate may be seriously wrong.
When integrating over infinite intervals do so explicitly, rather than just using a large number as the endpoint. This increases the chance of a correct answer – any function whose integral over an infinite interval is finite must be near zero for most of that interval.
For values at a finite set of points to be a fair reflection of the behaviour of the function elsewhere, the function needs to be well-behaved, for example differentiable except perhaps for a small number of jumps or integrable singularities.
f
must accept a vector of inputs and produce a vector of function
evaluations at those points. The Vectorize
function
may be helpful to convert f
to this form.
Based on QUADPACK routines dqags
and dqagi
by
R. Piessens and E. deDoncker–Kapenga, available from Netlib.
R. Piessens, E. deDoncker–Kapenga, C. Uberhuber, D. Kahaner (1983) Quadpack: a Subroutine Package for Automatic Integration; Springer Verlag.
integrate(dnorm, -1.96, 1.96) integrate(dnorm, -Inf, Inf) ## a slowly-convergent integral integrand <- function(x) {1/((x+1)*sqrt(x))} integrate(integrand, lower = 0, upper = Inf) ## don't do this if you really want the integral from 0 to Inf integrate(integrand, lower = 0, upper = 10) integrate(integrand, lower = 0, upper = 100000) integrate(integrand, lower = 0, upper = 1000000, stop.on.error = FALSE) ## some functions do not handle vector input properly f <- function(x) 2.0 try(integrate(f, 0, 1)) integrate(Vectorize(f), 0, 1) ## correct integrate(function(x) rep(2.0, length(x)), 0, 1) ## correct ## integrate can fail if misused integrate(dnorm, 0, 2) integrate(dnorm, 0, 20) integrate(dnorm, 0, 200) integrate(dnorm, 0, 2000) integrate(dnorm, 0, 20000) ## fails on many systems integrate(dnorm, 0, Inf) ## works integrate(dnorm, 0:1, 20) #-> error! ## "silently" gave integrate(dnorm, 0, 20) in earlier versions of R
integrate(dnorm, -1.96, 1.96) integrate(dnorm, -Inf, Inf) ## a slowly-convergent integral integrand <- function(x) {1/((x+1)*sqrt(x))} integrate(integrand, lower = 0, upper = Inf) ## don't do this if you really want the integral from 0 to Inf integrate(integrand, lower = 0, upper = 10) integrate(integrand, lower = 0, upper = 100000) integrate(integrand, lower = 0, upper = 1000000, stop.on.error = FALSE) ## some functions do not handle vector input properly f <- function(x) 2.0 try(integrate(f, 0, 1)) integrate(Vectorize(f), 0, 1) ## correct integrate(function(x) rep(2.0, length(x)), 0, 1) ## correct ## integrate can fail if misused integrate(dnorm, 0, 2) integrate(dnorm, 0, 20) integrate(dnorm, 0, 200) integrate(dnorm, 0, 2000) integrate(dnorm, 0, 20000) ## fails on many systems integrate(dnorm, 0, Inf) ## works integrate(dnorm, 0:1, 20) #-> error! ## "silently" gave integrate(dnorm, 0, 20) in earlier versions of R
Plots the mean (or other summary) of the response for two-way combinations of factors, thereby illustrating possible interactions.
interaction.plot(x.factor, trace.factor, response, fun = mean, type = c("l", "p", "b", "o", "c"), legend = TRUE, trace.label = deparse1(substitute(trace.factor)), fixed = FALSE, xlab = deparse1(substitute(x.factor)), ylab = ylabel, ylim = range(cells, na.rm = TRUE), lty = nc:1, col = 1, pch = c(1:9, 0, letters), xpd = NULL, leg.bg = par("bg"), leg.bty = "n", xtick = FALSE, xaxt = par("xaxt"), axes = TRUE, ...)
interaction.plot(x.factor, trace.factor, response, fun = mean, type = c("l", "p", "b", "o", "c"), legend = TRUE, trace.label = deparse1(substitute(trace.factor)), fixed = FALSE, xlab = deparse1(substitute(x.factor)), ylab = ylabel, ylim = range(cells, na.rm = TRUE), lty = nc:1, col = 1, pch = c(1:9, 0, letters), xpd = NULL, leg.bg = par("bg"), leg.bty = "n", xtick = FALSE, xaxt = par("xaxt"), axes = TRUE, ...)
x.factor |
a factor whose levels will form the x axis. |
trace.factor |
another factor whose levels will form the traces. |
response |
a numeric variable giving the response. |
fun |
the function to compute the summary. Should return a single real value. |
type |
the type of plot (see |
legend |
logical. Should a legend be included? |
trace.label |
overall label for the legend. |
fixed |
logical. Should the legend be in the order of the levels
of |
xlab , ylab
|
the x and y label of the plot each with a sensible default. |
ylim |
numeric of length 2 giving the y limits for the plot. |
lty |
line type for the lines drawn, with sensible default. |
col |
the color to be used for plotting. |
pch |
a vector of plotting symbols or characters, with sensible default. |
xpd |
determines clipping behaviour for the |
leg.bg , leg.bty
|
arguments passed to |
xtick |
logical. Should tick marks be used on the x axis? |
xaxt , axes , ...
|
graphics parameters to be passed to the plotting routines. |
By default the levels of x.factor
are plotted on the x axis in
their given order, with extra space on the right for the legend
(if specified). If x.factor
is an ordered factor and the levels
are numeric, these numeric values are used for the x axis.
The response and hence its summary can contain missing values. If so, the missing values and the line segments joining them are omitted from the plot (and this can be somewhat disconcerting).
The graphics parameters xlab
, ylab
, ylim
,
lty
, col
and pch
are given suitable defaults
(and xlim
and xaxs
are set and cannot be overridden).
The defaults are to cycle through the line types, use the foreground
colour, and to use the symbols 1:9, 0, and the small letters to plot
the traces.
Some of the argument names and the precise behaviour are chosen for S-compatibility.
Chambers, J. M., Freeny, A and Heiberger, R. M. (1992) Analysis of variance; designed experiments. Chapter 5 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
require(graphics) with(ToothGrowth, { interaction.plot(dose, supp, len, fixed = TRUE) dose <- ordered(dose) interaction.plot(dose, supp, len, fixed = TRUE, col = 2:3, leg.bty = "o", xtick = TRUE) interaction.plot(dose, supp, len, fixed = TRUE, col = 2:3, type = "p") }) with(OrchardSprays, { interaction.plot(treatment, rowpos, decrease) interaction.plot(rowpos, treatment, decrease, cex.axis = 0.8) ## order the rows by their mean effect rowpos <- factor(rowpos, levels = sort.list(tapply(decrease, rowpos, mean))) interaction.plot(rowpos, treatment, decrease, col = 2:9, lty = 1) })
require(graphics) with(ToothGrowth, { interaction.plot(dose, supp, len, fixed = TRUE) dose <- ordered(dose) interaction.plot(dose, supp, len, fixed = TRUE, col = 2:3, leg.bty = "o", xtick = TRUE) interaction.plot(dose, supp, len, fixed = TRUE, col = 2:3, type = "p") }) with(OrchardSprays, { interaction.plot(treatment, rowpos, decrease) interaction.plot(rowpos, treatment, decrease, cex.axis = 0.8) ## order the rows by their mean effect rowpos <- factor(rowpos, levels = sort.list(tapply(decrease, rowpos, mean))) interaction.plot(rowpos, treatment, decrease, col = 2:9, lty = 1) })
computes interquartile range of the x
values.
IQR(x, na.rm = FALSE, type = 7)
IQR(x, na.rm = FALSE, type = 7)
x |
a numeric vector. |
na.rm |
logical. Should missing values be removed? |
type |
an integer selecting one of the many quantile algorithms,
see |
Note that this function computes the quartiles using the
quantile
function rather than following
Tukey's recommendations,
i.e., IQR(x) = quantile(x, 3/4) - quantile(x, 1/4)
.
For normally distributed
, the expected value of
IQR(X)
is 2*qnorm(3/4) = 1.3490
, i.e., for a normal-consistent
estimate of the standard deviation, use IQR(x) / 1.349
.
Tukey, J. W. (1977). Exploratory Data Analysis. Reading: Addison-Wesley.
fivenum
, mad
which is more robust,
range
, quantile
.
IQR(rivers)
IQR(rivers)
R's formula notation allows models with no intercept and no
predictors. These require special handling internally.
is.empty.model()
checks whether an object describes an empty
model.
is.empty.model(x)
is.empty.model(x)
x |
A |
TRUE
if the model is empty
y <- rnorm(20) is.empty.model(y ~ 0) is.empty.model(y ~ -1) is.empty.model(lm(y ~ 0))
y <- rnorm(20) is.empty.model(y ~ 0) is.empty.model(y ~ -1) is.empty.model(lm(y ~ 0))
Compute the isotonic (monotonically increasing nonparametric) least squares regression which is piecewise constant.
isoreg(x, y = NULL)
isoreg(x, y = NULL)
x , y
|
coordinate vectors of the regression points. Alternatively a single
plotting structure can be specified: see |
The algorithm determines the convex minorant of the
cumulative data (i.e.,
cumsum(y)
) which is piecewise
linear and the result is , a step function with level
changes at locations where the convex
touches the
cumulative data polygon and changes slope.
as.stepfun()
returns a stepfun
object which can be more parsimonious.
isoreg()
returns an object of class isoreg
which is
basically a list with components
x |
original (constructed) abscissa values |
y |
corresponding y values. |
yf |
fitted values corresponding to ordered x values. |
yc |
cumulative y values corresponding to ordered x values. |
iKnots |
integer vector giving indices where the fitted curve jumps, i.e., where the convex minorant has kinks. |
isOrd |
logical indicating if original x values were ordered increasingly already. |
ord |
|
call |
the |
The inputs can be long vectors, but iKnots
will wrap around at
.
The code should be improved to accept weights additionally and
solve the corresponding weighted least squares problem.
‘Patches are welcome!’
Barlow, R. E., Bartholomew, D. J., Bremner, J. M., and Brunk, H. D. (1972) Statistical Inference under Order Restrictions; Wiley, London.
Robertson, T., Wright, F. T. and Dykstra, R. L. (1988) Order Restricted Statistical Inference; Wiley, New York.
the plotting method plot.isoreg
with more examples;
isoMDS()
from the MASS package internally
uses isotonic regression.
require(graphics) (ir <- isoreg(c(1,0,4,3,3,5,4,2,0))) plot(ir, plot.type = "row") (ir3 <- isoreg(y3 <- c(1,0,4,3,3,5,4,2, 3))) # last "3", not "0" (fi3 <- as.stepfun(ir3)) (ir4 <- isoreg(1:10, y4 <- c(5, 9, 1:2, 5:8, 3, 8))) cat(sprintf("R^2 = %.2f\n", 1 - sum(residuals(ir4)^2) / ((10-1)*var(y4)))) ## If you are interested in the knots alone : with(ir4, cbind(iKnots, yf[iKnots])) ## Example of unordered x[] with ties: x <- sample((0:30)/8) y <- exp(x) x. <- round(x) # ties! plot(m <- isoreg(x., y)) stopifnot(all.equal(with(m, yf[iKnots]), as.vector(tapply(y, x., mean))))
require(graphics) (ir <- isoreg(c(1,0,4,3,3,5,4,2,0))) plot(ir, plot.type = "row") (ir3 <- isoreg(y3 <- c(1,0,4,3,3,5,4,2, 3))) # last "3", not "0" (fi3 <- as.stepfun(ir3)) (ir4 <- isoreg(1:10, y4 <- c(5, 9, 1:2, 5:8, 3, 8))) cat(sprintf("R^2 = %.2f\n", 1 - sum(residuals(ir4)^2) / ((10-1)*var(y4)))) ## If you are interested in the knots alone : with(ir4, cbind(iKnots, yf[iKnots])) ## Example of unordered x[] with ties: x <- sample((0:30)/8) y <- exp(x) x. <- round(x) # ties! plot(m <- isoreg(x., y)) stopifnot(all.equal(with(m, yf[iKnots]), as.vector(tapply(y, x., mean))))
Use Kalman Filtering to find the (Gaussian) log-likelihood, or for forecasting or smoothing.
KalmanLike(y, mod, nit = 0L, update = FALSE) KalmanRun(y, mod, nit = 0L, update = FALSE) KalmanSmooth(y, mod, nit = 0L) KalmanForecast(n.ahead = 10L, mod, update = FALSE) makeARIMA(phi, theta, Delta, kappa = 1e6, SSinit = c("Gardner1980", "Rossignol2011"), tol = .Machine$double.eps)
KalmanLike(y, mod, nit = 0L, update = FALSE) KalmanRun(y, mod, nit = 0L, update = FALSE) KalmanSmooth(y, mod, nit = 0L) KalmanForecast(n.ahead = 10L, mod, update = FALSE) makeARIMA(phi, theta, Delta, kappa = 1e6, SSinit = c("Gardner1980", "Rossignol2011"), tol = .Machine$double.eps)
y |
a univariate time series. |
mod |
a list describing the state-space model: see ‘Details’. |
nit |
the time at which the initialization is computed.
|
update |
if |
n.ahead |
the number of steps ahead for which prediction is required. |
phi , theta
|
numeric vectors of length |
Delta |
vector of differencing coefficients, so an ARMA model is
fitted to |
kappa |
the prior variance (as a multiple of the innovations variance) for the past observations in a differenced model. |
SSinit |
a string specifying the algorithm to compute the
|
tol |
tolerance eventually passed to |
These functions work with a general univariate state-space model
with state vector ‘a’, transitions ‘a <- T a + R e’,
and observation
equation ‘y = Z'a + eta’,
.
The likelihood is a profile likelihood after estimation of
.
The model is specified as a list with at least components
T
the transition matrix
Z
the observation coefficients
h
the observation variance
V
‘RQR'’
a
the current state estimate
P
the current estimate of the state uncertainty matrix
Pn
the estimate at time of the state
uncertainty matrix
(not updated by
KalmanForecast
).
KalmanSmooth
is the workhorse function for tsSmooth
.
makeARIMA
constructs the state-space model for an ARIMA model,
see also arima
.
The state-space initialization has used Gardner et al.'s method
(SSinit = "Gardner1980"
), as only method for years. However,
that suffers sometimes from deficiencies when close to non-stationarity.
For this reason, it may be replaced as default in the future and only
kept for reproducibility reasons. Explicit specification of
SSinit
is therefore recommended, notably also in
arima()
.
The "Rossignol2011"
method has been proposed and partly
documented by Raphael Rossignol, Univ. Grenoble, on 2011-09-20 (see
PR#14682, below), and later been ported to C by Matwey V. Kornilov.
It computes the covariance matrix of
by the method of difference equations
(page 93 of Brockwell and Davis (1991)),
apparently suggested by a referee of Gardner et al. (see p.314 of
their paper).
For KalmanLike
, a list with components Lik
(the
log-likelihood less some constants) and s2
, the estimate of
.
For KalmanRun
, a list with components values
, a vector
of length 2 giving the output of KalmanLike
, resid
(the
residuals) and states
, the contemporaneous state estimates,
a matrix with one row for each observation time.
For KalmanSmooth
, a list with two components.
Component smooth
is a n
by p
matrix of state
estimates based on all the observations, with one row for each time.
Component var
is a n
by p
by p
array of
variance matrices.
For KalmanForecast
, a list with components pred
, the
predictions, and var
, the unscaled variances of the prediction
errors (to be multiplied by s2
).
For makeARIMA
, a model list including components for
its arguments.
These functions are designed to be called from other functions which check the validity of the arguments passed, so very little checking is done.
Brockwell, P. J. and Davis, R. A. (1991). Time Series: Theory and Methods, second edition. Springer.
Durbin, J. and Koopman, S. J. (2001). Time Series Analysis by State Space Methods. Oxford University Press.
Gardner, G, Harvey, A. C. and Phillips, G. D. A. (1980). Algorithm AS 154: An algorithm for exact maximum likelihood estimation of autoregressive-moving average models by means of Kalman filtering. Applied Statistics, 29, 311–322. doi:10.2307/2346910.
R bug report PR#14682 (2011-2013) https://bugs.r-project.org/show_bug.cgi?id=14682.
## an ARIMA fit fit3 <- arima(presidents, c(3, 0, 0)) predict(fit3, 12) ## reconstruct this pr <- KalmanForecast(12, fit3$model) pr$pred + fit3$coef[4] sqrt(pr$var * fit3$sigma2) ## and now do it year by year mod <- fit3$model for(y in 1:3) { pr <- KalmanForecast(4, mod, TRUE) print(list(pred = pr$pred + fit3$coef["intercept"], se = sqrt(pr$var * fit3$sigma2))) mod <- attr(pr, "mod") }
## an ARIMA fit fit3 <- arima(presidents, c(3, 0, 0)) predict(fit3, 12) ## reconstruct this pr <- KalmanForecast(12, fit3$model) pr$pred + fit3$coef[4] sqrt(pr$var * fit3$sigma2) ## and now do it year by year mod <- fit3$model for(y in 1:3) { pr <- KalmanForecast(4, mod, TRUE) print(list(pred = pr$pred + fit3$coef["intercept"], se = sqrt(pr$var * fit3$sigma2))) mod <- attr(pr, "mod") }
kernapply
computes the convolution between an input sequence
and a specific kernel.
kernapply(x, ...) ## Default S3 method: kernapply(x, k, circular = FALSE, ...) ## S3 method for class 'ts' kernapply(x, k, circular = FALSE, ...) ## S3 method for class 'vector' kernapply(x, k, circular = FALSE, ...) ## S3 method for class 'tskernel' kernapply(x, k, ...)
kernapply(x, ...) ## Default S3 method: kernapply(x, k, circular = FALSE, ...) ## S3 method for class 'ts' kernapply(x, k, circular = FALSE, ...) ## S3 method for class 'vector' kernapply(x, k, circular = FALSE, ...) ## S3 method for class 'tskernel' kernapply(x, k, ...)
x |
an input vector, matrix, time series or kernel to be smoothed. |
k |
smoothing |
circular |
a logical indicating whether the input sequence to be smoothed is treated as circular, i.e., periodic. |
... |
arguments passed to or from other methods. |
A smoothed version of the input sequence.
This uses fft
to perform the convolution, so is fastest
when NROW(x)
is a power of 2 or some other highly composite
integer.
A. Trapletti
kernel
, convolve
, filter
,
spectrum
## see 'kernel' for examples
## see 'kernel' for examples
The "tskernel"
class is designed to represent discrete
symmetric normalized smoothing kernels. These kernels can be used to
smooth vectors, matrices, or time series objects.
There are print
, plot
and [
methods for these kernel objects.
kernel(coef, m = 2, r, name) df.kernel(k) bandwidth.kernel(k) is.tskernel(k) ## S3 method for class 'tskernel' plot(x, type = "h", xlab = "k", ylab = "W[k]", main = attr(x,"name"), ...)
kernel(coef, m = 2, r, name) df.kernel(k) bandwidth.kernel(k) is.tskernel(k) ## S3 method for class 'tskernel' plot(x, type = "h", xlab = "k", ylab = "W[k]", main = attr(x,"name"), ...)
coef |
the upper half of the smoothing kernel coefficients
(including coefficient zero) or the name of a kernel
(currently |
m |
the kernel dimension(s) if |
name |
the name the kernel will be called. |
r |
the kernel order for a Fejer kernel. |
k , x
|
a |
type , xlab , ylab , main , ...
|
arguments passed to
|
kernel
is used to construct a general kernel or named specific
kernels. The modified Daniell kernel halves the end coefficients.
The [
method allows natural indexing of kernel objects
with indices in (-m) : m
. The normalization is such that for
k <- kernel(*)
, sum(k[ -k$m : k$m ])
is one.
df.kernel
returns the ‘equivalent degrees of freedom’ of
a smoothing kernel as defined in
Brockwell and Davis (1991), page 362,
and bandwidth.kernel
returns the equivalent bandwidth as
defined in Bloomfield (1976), p. 201,
with a continuity correction.
kernel()
returns an object of class "tskernel"
which is
basically a list with the two components coef
and the kernel
dimension m
. An additional attribute is "name"
.
A. Trapletti; modifications by B.D. Ripley
Bloomfield, P. (1976) Fourier Analysis of Time Series: An Introduction. Wiley.
Brockwell, P.J. and Davis, R.A. (1991) Time Series: Theory and Methods. Second edition. Springer, pp. 350–365.
require(graphics) ## Demonstrate a simple trading strategy for the ## financial time series German stock index DAX. x <- EuStockMarkets[,1] k1 <- kernel("daniell", 50) # a long moving average k2 <- kernel("daniell", 10) # and a short one plot(k1) plot(k2) x1 <- kernapply(x, k1) x2 <- kernapply(x, k2) plot(x) lines(x1, col = "red") # go long if the short crosses the long upwards lines(x2, col = "green") # and go short otherwise ## More interesting kernels kd <- kernel("daniell", c(3, 3)) kd # note the unusual indexing kd[-2:2] plot(kernel("fejer", 100, r = 6)) plot(kernel("modified.daniell", c(7,5,3))) # Reproduce example 10.4.3 from Brockwell and Davis (1991) spectrum(sunspot.year, kernel = kernel("daniell", c(11,7,3)), log = "no")
require(graphics) ## Demonstrate a simple trading strategy for the ## financial time series German stock index DAX. x <- EuStockMarkets[,1] k1 <- kernel("daniell", 50) # a long moving average k2 <- kernel("daniell", 10) # and a short one plot(k1) plot(k2) x1 <- kernapply(x, k1) x2 <- kernapply(x, k2) plot(x) lines(x1, col = "red") # go long if the short crosses the long upwards lines(x2, col = "green") # and go short otherwise ## More interesting kernels kd <- kernel("daniell", c(3, 3)) kd # note the unusual indexing kd[-2:2] plot(kernel("fejer", 100, r = 6)) plot(kernel("modified.daniell", c(7,5,3))) # Reproduce example 10.4.3 from Brockwell and Davis (1991) spectrum(sunspot.year, kernel = kernel("daniell", c(11,7,3)), log = "no")
Perform k-means clustering on a data matrix.
kmeans(x, centers, iter.max = 10, nstart = 1, algorithm = c("Hartigan-Wong", "Lloyd", "Forgy", "MacQueen"), trace = FALSE) ## S3 method for class 'kmeans' fitted(object, method = c("centers", "classes"), ...)
kmeans(x, centers, iter.max = 10, nstart = 1, algorithm = c("Hartigan-Wong", "Lloyd", "Forgy", "MacQueen"), trace = FALSE) ## S3 method for class 'kmeans' fitted(object, method = c("centers", "classes"), ...)
x |
numeric matrix of data, or an object that can be coerced to such a matrix (such as a numeric vector or a data frame with all numeric columns). |
centers |
either the number of clusters, say |
iter.max |
the maximum number of iterations allowed. |
nstart |
if |
algorithm |
character: may be abbreviated. Note that
|
object |
an R object of class |
method |
character: may be abbreviated. |
trace |
logical or integer number, currently only used in the
default method ( |
... |
not used. |
The data given by x
are clustered by the -means method,
which aims to partition the points into
groups such that the
sum of squares from points to the assigned cluster centres is minimized.
At the minimum, all cluster centres are at the mean of their Voronoi
sets (the set of data points which are nearest to the cluster centre).
The algorithm of Hartigan and Wong (1979) is used by
default. Note that some authors use -means to refer to a
specific algorithm rather than the general method: most commonly the
algorithm given by MacQueen (1967) but sometimes that given
by Lloyd (1957) and Forgy (1965).
The Hartigan–Wong algorithm generally does a better job than
either of those, but trying several random starts (
nstart
) is often recommended. In rare cases, when some of the points
(rows of
x
) are extremely close, the algorithm may not converge
in the “Quick-Transfer” stage, signalling a warning (and
returning ifault = 4
). Slight
rounding of the data may be advisable in that case.
For ease of programmatic exploration, is allowed, notably
returning the center and
withinss
.
Except for the Lloyd–Forgy method, clusters will always be
returned if a number is specified.
If an initial matrix of centres is supplied, it is possible that
no point will be closest to one or more centres, which is currently
an error for the Hartigan–Wong method.
kmeans
returns an object of class "kmeans"
which has a
print
and a fitted
method. It is a list with at least
the following components:
cluster |
A vector of integers (from |
centers |
A matrix of cluster centres. |
totss |
The total sum of squares. |
withinss |
Vector of within-cluster sum of squares, one component per cluster. |
tot.withinss |
Total within-cluster sum of squares,
i.e. |
betweenss |
The between-cluster sum of squares,
i.e. |
size |
The number of points in each cluster. |
iter |
The number of (outer) iterations. |
ifault |
integer: indicator of a possible algorithm problem – for experts. |
The clusters are numbered in the returned object, but they are a set and no ordering is implied. (Their apparent ordering may differ by platform.)
Forgy, E. W. (1965). Cluster analysis of multivariate data: efficiency vs interpretability of classifications. Biometrics, 21, 768–769.
Hartigan, J. A. and Wong, M. A. (1979). Algorithm AS 136: A K-means clustering algorithm. Applied Statistics, 28, 100–108. doi:10.2307/2346830.
Lloyd, S. P. (1957, 1982). Least squares quantization in PCM. Technical Note, Bell Laboratories. Published in 1982 in IEEE Transactions on Information Theory, 28, 128–137.
MacQueen, J. (1967). Some methods for classification and analysis of multivariate observations. In Proceedings of the Fifth Berkeley Symposium on Mathematical Statistics and Probability, eds L. M. Le Cam & J. Neyman, 1, pp. 281–297. Berkeley, CA: University of California Press.
require(graphics) # a 2-dimensional example x <- rbind(matrix(rnorm(100, sd = 0.3), ncol = 2), matrix(rnorm(100, mean = 1, sd = 0.3), ncol = 2)) colnames(x) <- c("x", "y") (cl <- kmeans(x, 2)) plot(x, col = cl$cluster) points(cl$centers, col = 1:2, pch = 8, cex = 2) # sum of squares ss <- function(x) sum(scale(x, scale = FALSE)^2) ## cluster centers "fitted" to each obs.: fitted.x <- fitted(cl); head(fitted.x) resid.x <- x - fitted(cl) ## Equalities : ---------------------------------- cbind(cl[c("betweenss", "tot.withinss", "totss")], # the same two columns c(ss(fitted.x), ss(resid.x), ss(x))) stopifnot(all.equal(cl$ totss, ss(x)), all.equal(cl$ tot.withinss, ss(resid.x)), ## these three are the same: all.equal(cl$ betweenss, ss(fitted.x)), all.equal(cl$ betweenss, cl$totss - cl$tot.withinss), ## and hence also all.equal(ss(x), ss(fitted.x) + ss(resid.x)) ) kmeans(x,1)$withinss # trivial one-cluster, (its W.SS == ss(x)) ## random starts do help here with too many clusters ## (and are often recommended anyway!): ## The ordering of the clusters may be platform-dependent. (cl <- kmeans(x, 5, nstart = 25)) plot(x, col = cl$cluster) points(cl$centers, col = 1:5, pch = 8)
require(graphics) # a 2-dimensional example x <- rbind(matrix(rnorm(100, sd = 0.3), ncol = 2), matrix(rnorm(100, mean = 1, sd = 0.3), ncol = 2)) colnames(x) <- c("x", "y") (cl <- kmeans(x, 2)) plot(x, col = cl$cluster) points(cl$centers, col = 1:2, pch = 8, cex = 2) # sum of squares ss <- function(x) sum(scale(x, scale = FALSE)^2) ## cluster centers "fitted" to each obs.: fitted.x <- fitted(cl); head(fitted.x) resid.x <- x - fitted(cl) ## Equalities : ---------------------------------- cbind(cl[c("betweenss", "tot.withinss", "totss")], # the same two columns c(ss(fitted.x), ss(resid.x), ss(x))) stopifnot(all.equal(cl$ totss, ss(x)), all.equal(cl$ tot.withinss, ss(resid.x)), ## these three are the same: all.equal(cl$ betweenss, ss(fitted.x)), all.equal(cl$ betweenss, cl$totss - cl$tot.withinss), ## and hence also all.equal(ss(x), ss(fitted.x) + ss(resid.x)) ) kmeans(x,1)$withinss # trivial one-cluster, (its W.SS == ss(x)) ## random starts do help here with too many clusters ## (and are often recommended anyway!): ## The ordering of the clusters may be platform-dependent. (cl <- kmeans(x, 5, nstart = 25)) plot(x, col = cl$cluster) points(cl$centers, col = 1:5, pch = 8)
Performs a Kruskal-Wallis rank sum test.
kruskal.test(x, ...) ## Default S3 method: kruskal.test(x, g, ...) ## S3 method for class 'formula' kruskal.test(formula, data, subset, na.action, ...)
kruskal.test(x, ...) ## Default S3 method: kruskal.test(x, g, ...) ## S3 method for class 'formula' kruskal.test(formula, data, subset, na.action, ...)
x |
a numeric vector of data values, or a list of numeric data vectors. Non-numeric elements of a list will be coerced, with a warning. |
g |
a vector or factor object giving the group for the
corresponding elements of |
formula |
a formula of the form |
data |
an optional matrix or data frame (or similar: see
|
subset |
an optional vector specifying a subset of observations to be used. |
na.action |
a function which indicates what should happen when
the data contain |
... |
further arguments to be passed to or from methods. |
kruskal.test
performs a Kruskal-Wallis rank sum test of the
null that the location parameters of the distribution of x
are the same in each group (sample). The alternative is that they
differ in at least one.
If x
is a list, its elements are taken as the samples to be
compared, and hence have to be numeric data vectors. In this case,
g
is ignored, and one can simply use kruskal.test(x)
to perform the test. If the samples are not yet contained in a
list, use kruskal.test(list(x, ...))
.
Otherwise, x
must be a numeric data vector, and g
must
be a vector or factor object of the same length as x
giving
the group for the corresponding elements of x
.
A list with class "htest"
containing the following components:
statistic |
the Kruskal-Wallis rank sum statistic. |
parameter |
the degrees of freedom of the approximate chi-squared distribution of the test statistic. |
p.value |
the p-value of the test. |
method |
the character string |
data.name |
a character string giving the names of the data. |
Myles Hollander and Douglas A. Wolfe (1973), Nonparametric Statistical Methods. New York: John Wiley & Sons. Pages 115–120.
The Wilcoxon rank sum test (wilcox.test
) as the special
case for two samples;
lm
together with anova
for performing
one-way location analysis under normality assumptions; with Student's
t test (t.test
) as the special case for two samples.
wilcox_test
in package
coin for exact, asymptotic and Monte Carlo
conditional p-values, including in the presence of ties.
## Hollander & Wolfe (1973), 116. ## Mucociliary efficiency from the rate of removal of dust in normal ## subjects, subjects with obstructive airway disease, and subjects ## with asbestosis. x <- c(2.9, 3.0, 2.5, 2.6, 3.2) # normal subjects y <- c(3.8, 2.7, 4.0, 2.4) # with obstructive airway disease z <- c(2.8, 3.4, 3.7, 2.2, 2.0) # with asbestosis kruskal.test(list(x, y, z)) ## Equivalently, x <- c(x, y, z) g <- factor(rep(1:3, c(5, 4, 5)), labels = c("Normal subjects", "Subjects with obstructive airway disease", "Subjects with asbestosis")) kruskal.test(x, g) ## Formula interface. require(graphics) boxplot(Ozone ~ Month, data = airquality) kruskal.test(Ozone ~ Month, data = airquality)
## Hollander & Wolfe (1973), 116. ## Mucociliary efficiency from the rate of removal of dust in normal ## subjects, subjects with obstructive airway disease, and subjects ## with asbestosis. x <- c(2.9, 3.0, 2.5, 2.6, 3.2) # normal subjects y <- c(3.8, 2.7, 4.0, 2.4) # with obstructive airway disease z <- c(2.8, 3.4, 3.7, 2.2, 2.0) # with asbestosis kruskal.test(list(x, y, z)) ## Equivalently, x <- c(x, y, z) g <- factor(rep(1:3, c(5, 4, 5)), labels = c("Normal subjects", "Subjects with obstructive airway disease", "Subjects with asbestosis")) kruskal.test(x, g) ## Formula interface. require(graphics) boxplot(Ozone ~ Month, data = airquality) kruskal.test(Ozone ~ Month, data = airquality)
Perform a one- or two-sample Kolmogorov-Smirnov test.
ks.test(x, ...) ## Default S3 method: ks.test(x, y, ..., alternative = c("two.sided", "less", "greater"), exact = NULL, simulate.p.value = FALSE, B = 2000) ## S3 method for class 'formula' ks.test(formula, data, subset, na.action, ...)
ks.test(x, ...) ## Default S3 method: ks.test(x, y, ..., alternative = c("two.sided", "less", "greater"), exact = NULL, simulate.p.value = FALSE, B = 2000) ## S3 method for class 'formula' ks.test(formula, data, subset, na.action, ...)
x |
a numeric vector of data values. |
y |
either a numeric vector of data values, or a character string
naming a cumulative distribution function or an actual cumulative
distribution function such as |
... |
for the default method, parameters of the distribution
specified (as a character string) by |
alternative |
indicates the alternative hypothesis and must be
one of |
exact |
|
simulate.p.value |
a logical indicating whether to compute p-values by Monte Carlo simulation. (Ignored for the one-sample test.) |
B |
an integer specifying the number of replicates used in the Monte Carlo test. |
formula |
a formula of the form |
data |
an optional matrix or data frame (or similar: see
|
subset |
an optional vector specifying a subset of observations to be used. |
na.action |
a function which indicates what should happen when
the data contain |
If y
is numeric, a two-sample (Smirnov) test of the null
hypothesis that x
and y
were drawn from the same
distribution is performed.
Alternatively, y
can be a character string naming a continuous
(cumulative) distribution function, or such a function. In this case,
a one-sample (Kolmogorov) test is carried out of the null that the
distribution function which generated x
is distribution
y
with parameters specified by ...
.
The presence of ties always generates a warning in the one-sample case, as continuous
distributions do not generate them. If the ties arose from rounding
the tests may be approximately valid, but even modest amounts of
rounding can have a significant effect on the calculated statistic.
Missing values are silently omitted from x
and (in the
two-sample case) y
.
The possible values "two.sided"
, "less"
and
"greater"
of alternative
specify the null hypothesis
that the true cumulative distribution function (CDF) of x
is equal
to, not less than or not greater than the hypothesized CDF (one-sample
case) or the CDF of y
(two-sample case), respectively. The test
compares the CDFs taking their maximal difference as test statistic,
with the statistic in the "greater"
alternative being
.
Thus in the two-sample case
alternative = "greater"
includes
distributions for which x
is stochastically smaller than
y
(the CDF of x
lies above and hence to the left of that
for y
), in contrast to t.test
or
wilcox.test
.
Exact p-values are not available for the one-sample case in the
presence of ties.
If exact = NULL
(the default), an
exact p-value is computed if the sample size is less than 100 in the
one-sample case and there are no ties, and if the product of
the sample sizes is less than 10000 in the two-sample case, with or
without ties (using the algorithm described in
Schröer and Trenkler (1995)).
Otherwise, the p-value is computed via Monte Carlo simulation in the
two-sample case if simulate.p.value
is TRUE
, or else
asymptotic distributions are used whose approximations may
be inaccurate in small samples. In the one-sample two-sided case,
exact p-values are obtained as described in
Marsaglia, Tsang & Wang (2003)
(but not using the optional approximation in the right tail, so
this can be slow for small p-values). The formula of
Birnbaum & Tingey (1951) is used for the one-sample
one-sided case.
If a one-sample test is used, the parameters specified in
...
must be pre-specified and not estimated from the data.
There is some more refined distribution theory for the KS test with
estimated parameters (see Durbin, 1973), but that is not implemented
in ks.test
.
A list inheriting from classes "ks.test"
and "htest"
containing the following components:
statistic |
the value of the test statistic. |
p.value |
the p-value of the test. |
alternative |
a character string describing the alternative hypothesis. |
method |
a character string indicating what type of test was performed. |
data.name |
a character string giving the name(s) of the data. |
The two-sided one-sample distribution comes via Marsaglia, Tsang and Wang (2003).
Exact distributions for the two-sample (Smirnov) test are computed by the algorithm proposed by Schröer (1991) and Schröer & Trenkler (1995) using numerical improvements along the lines of Viehmann (2021).
Z. W. Birnbaum and Fred H. Tingey (1951). One-sided confidence contours for probability distribution functions. The Annals of Mathematical Statistics, 22/4, 592–596. doi:10.1214/aoms/1177729550.
William J. Conover (1971). Practical Nonparametric Statistics. New York: John Wiley & Sons. Pages 295–301 (one-sample Kolmogorov test), 309–314 (two-sample Smirnov test).
Durbin, J. (1973). Distribution theory for tests based on the sample distribution function. SIAM.
W. Feller (1948). On the Kolmogorov-Smirnov limit theorems for empirical distributions. The Annals of Mathematical Statistics, 19(2), 177–189. doi:10.1214/aoms/1177730243.
George Marsaglia, Wai Wan Tsang and Jingbo Wang (2003). Evaluating Kolmogorov's distribution. Journal of Statistical Software, 8/18. doi:10.18637/jss.v008.i18.
Gunar Schröer (1991). Computergestützte statistische Inferenz am Beispiel der Kolmogorov-Smirnov Tests. Diplomarbeit Universität Osnabrück.
Gunar Schröer and Dietrich Trenkler (1995). Exact and Randomization Distributions of Kolmogorov-Smirnov Tests for Two or Three Samples. Computational Statistics & Data Analysis, 20(2), 185–202. doi:10.1016/0167-9473(94)00040-P.
Thomas Viehmann (2021). Numerically more stable computation of the p-values for the two-sample Kolmogorov-Smirnov test. https://arxiv.org/abs/2102.08037.
shapiro.test
which performs the Shapiro-Wilk test for
normality.
require("graphics") x <- rnorm(50) y <- runif(30) # Do x and y come from the same distribution? ks.test(x, y) # Does x come from a shifted gamma distribution with shape 3 and rate 2? ks.test(x+2, "pgamma", 3, 2) # two-sided, exact ks.test(x+2, "pgamma", 3, 2, exact = FALSE) ks.test(x+2, "pgamma", 3, 2, alternative = "gr") # test if x is stochastically larger than x2 x2 <- rnorm(50, -1) plot(ecdf(x), xlim = range(c(x, x2))) plot(ecdf(x2), add = TRUE, lty = "dashed") t.test(x, x2, alternative = "g") wilcox.test(x, x2, alternative = "g") ks.test(x, x2, alternative = "l") # with ties, example from Schröer and Trenkler (1995) # D = 3/7, p = 8/33 = 0.242424.. ks.test(c(1, 2, 2, 3, 3), c(1, 2, 3, 3, 4, 5, 6))# -> exact # formula interface, see ?wilcox.test ks.test(Ozone ~ Month, data = airquality, subset = Month %in% c(5, 8))
require("graphics") x <- rnorm(50) y <- runif(30) # Do x and y come from the same distribution? ks.test(x, y) # Does x come from a shifted gamma distribution with shape 3 and rate 2? ks.test(x+2, "pgamma", 3, 2) # two-sided, exact ks.test(x+2, "pgamma", 3, 2, exact = FALSE) ks.test(x+2, "pgamma", 3, 2, alternative = "gr") # test if x is stochastically larger than x2 x2 <- rnorm(50, -1) plot(ecdf(x), xlim = range(c(x, x2))) plot(ecdf(x2), add = TRUE, lty = "dashed") t.test(x, x2, alternative = "g") wilcox.test(x, x2, alternative = "g") ks.test(x, x2, alternative = "l") # with ties, example from Schröer and Trenkler (1995) # D = 3/7, p = 8/33 = 0.242424.. ks.test(c(1, 2, 2, 3, 3), c(1, 2, 3, 3, 4, 5, 6))# -> exact # formula interface, see ?wilcox.test ks.test(Ozone ~ Month, data = airquality, subset = Month %in% c(5, 8))
The Nadaraya–Watson kernel regression estimate.
ksmooth(x, y, kernel = c("box", "normal"), bandwidth = 0.5, range.x = range(x), n.points = max(100L, length(x)), x.points)
ksmooth(x, y, kernel = c("box", "normal"), bandwidth = 0.5, range.x = range(x), n.points = max(100L, length(x)), x.points)
x |
input x values. Long vectors are supported. |
y |
input y values. Long vectors are supported. |
kernel |
the kernel to be used. Can be abbreviated. |
bandwidth |
the bandwidth. The kernels are scaled so that their
quartiles (viewed as probability densities) are at
|
range.x |
the range of points to be covered in the output. |
n.points |
the number of points at which to evaluate the fit. |
x.points |
points at which to evaluate the smoothed fit. If
missing, |
A list with components
x |
values at which the smoothed fit is evaluated. Guaranteed to be in increasing order. |
y |
fitted values corresponding to |
This function was implemented for compatibility with S, although it is nowhere near as slow as the S function. Better kernel smoothers are available in other packages such as KernSmooth.
require(graphics) with(cars, { plot(speed, dist) lines(ksmooth(speed, dist, "normal", bandwidth = 2), col = 2) lines(ksmooth(speed, dist, "normal", bandwidth = 5), col = 3) })
require(graphics) with(cars, { plot(speed, dist) lines(ksmooth(speed, dist, "normal", bandwidth = 2), col = 2) lines(ksmooth(speed, dist, "normal", bandwidth = 5), col = 3) })
Compute a lagged version of a time series, shifting the time base back by a given number of observations.
lag
is a generic function; this page documents its default
method.
lag(x, ...) ## Default S3 method: lag(x, k = 1, ...)
lag(x, ...) ## Default S3 method: lag(x, k = 1, ...)
x |
A vector or matrix or univariate or multivariate time series |
k |
The number of lags (in units of observations). |
... |
further arguments to be passed to or from methods. |
Vector or matrix arguments x
are given a tsp
attribute
via hasTsp
.
A time series object with the same class as x
.
Note the sign of k
: a series lagged by a positive k
starts earlier.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
lag(ldeaths, 12) # starts one year earlier
lag(ldeaths, 12) # starts one year earlier
Plot time series against lagged versions of themselves. Helps visualizing ‘auto-dependence’ even when auto-correlations vanish.
lag.plot(x, lags = 1, layout = NULL, set.lags = 1:lags, main = NULL, asp = 1, diag = TRUE, diag.col = "gray", type = "p", oma = NULL, ask = NULL, do.lines = (n <= 150), labels = do.lines, ...)
lag.plot(x, lags = 1, layout = NULL, set.lags = 1:lags, main = NULL, asp = 1, diag = TRUE, diag.col = "gray", type = "p", oma = NULL, ask = NULL, do.lines = (n <= 150), labels = do.lines, ...)
x |
time-series (univariate or multivariate) |
lags |
number of lag plots desired, see argument |
layout |
the layout of multiple plots, basically the |
set.lags |
vector of positive integers allowing specification of
the set of lags used; defaults to |
main |
character with a main header title to be done on the top of each page. |
asp |
Aspect ratio to be fixed, see |
diag |
logical indicating if the x=y diagonal should be drawn. |
diag.col |
color to be used for the diagonal |
type |
plot type to be used, but see |
oma |
outer margins, see |
ask |
logical or |
do.lines |
logical indicating if lines should be drawn. |
labels |
logical indicating if labels should be used. |
... |
Further arguments to |
If just one plot is produced, this is a conventional plot. If more
than one plot is to be produced, par(mfrow)
and several other
graphics parameters will be set, so it is not (easily) possible to mix
such lag plots with other plots on the same page.
If ask = NULL
, par(ask = TRUE)
will be called if more than
one page of plots is to be produced and the device is interactive.
It is more flexible and has different default behaviour than
the S version. We use main =
instead of head =
for
internal consistency.
Martin Maechler
plot.ts
which is the basic work horse.
require(graphics) lag.plot(nhtemp, 8, diag.col = "forest green") lag.plot(nhtemp, 5, main = "Average Temperatures in New Haven") ## ask defaults to TRUE when we have more than one page: lag.plot(nhtemp, 6, layout = c(2,1), asp = NA, main = "New Haven Temperatures", col.main = "blue") ## Multivariate (but non-stationary! ...) lag.plot(freeny.x, lags = 3) ## no lines for long series : lag.plot(sqrt(sunspots), set.lags = c(1:4, 9:12), pch = ".", col = "gold")
require(graphics) lag.plot(nhtemp, 8, diag.col = "forest green") lag.plot(nhtemp, 5, main = "Average Temperatures in New Haven") ## ask defaults to TRUE when we have more than one page: lag.plot(nhtemp, 6, layout = c(2,1), asp = NA, main = "New Haven Temperatures", col.main = "blue") ## Multivariate (but non-stationary! ...) lag.plot(freeny.x, lags = 3) ## no lines for long series : lag.plot(sqrt(sunspots), set.lags = c(1:4, 9:12), pch = ".", col = "gold")
Fit a line robustly as recommended in Exploratory Data Analysis.
Currently by default (iter = 1
) the initial median-median line is not iterated (as
opposed to Tukey's “resistant line” in the references).
line(x, y, iter = 1)
line(x, y, iter = 1)
x , y
|
the arguments can be any way of specifying x-y pairs. See
|
iter |
positive integer specifying the number of
“polishing” iterations. Note that this was hard coded to
|
Cases with missing values are omitted.
Contrary to the references where the data is split in three (almost)
equally sized groups with symmetric sizes depending on and
n %% 3
and computes medians inside each group, the
line()
code splits into three groups using all observations
with x[.] <= q1
and x[.] >= q2
, where q1, q2
are
(a kind of) quantiles for probabilities and
of the form
(x[j1]+x[j2])/2
where j1 = floor(p*(n-1))
and j2 = ceiling(p(n-1))
, n = length(x)
.
Long vectors are not supported yet.
An object of class "tukeyline"
.
Methods are available for the generic functions coef
,
residuals
, fitted
, and print
.
Tukey, J. W. (1977). Exploratory Data Analysis, Reading Massachusetts: Addison-Wesley.
Velleman, P. F. and Hoaglin, D. C. (1981). Applications, Basics and Computing of Exploratory Data Analysis, Duxbury Press. Chapter 5.
Emerson, J. D. and Hoaglin, D. C. (1983).
Resistant Lines for versus
.
Chapter 5 of Understanding Robust and Exploratory Data Analysis,
eds. David C. Hoaglin, Frederick Mosteller and John W. Tukey. Wiley.
Iain M. Johnstone and Paul F. Velleman (1985). The Resistant Line and Related Regression Methods. Journal of the American Statistical Association, 80, 1041–1054. doi:10.2307/2288572.
lm
.
There are alternatives for robust linear regression more robust and
more (statistically) efficient,
see rlm()
from MASS, or
lmrob()
from robustbase.
require(graphics) plot(cars) (z <- line(cars)) abline(coef(z)) ## Tukey-Anscombe Plot : plot(residuals(z) ~ fitted(z), main = deparse(z$call)) ## Andrew Siegel's pathological 9-point data, y-values multiplied by 3: d.AS <- data.frame(x = c(-4:3, 12), y = 3*c(rep(0,6), -5, 5, 1)) cAS <- with(d.AS, t(sapply(1:10, function(it) line(x,y, iter=it)$coefficients))) dimnames(cAS) <- list(paste("it =", format(1:10)), c("intercept", "slope")) cAS ## iterations started to oscillate, repeating iteration 7,8 indefinitely
require(graphics) plot(cars) (z <- line(cars)) abline(coef(z)) ## Tukey-Anscombe Plot : plot(residuals(z) ~ fitted(z), main = deparse(z$call)) ## Andrew Siegel's pathological 9-point data, y-values multiplied by 3: d.AS <- data.frame(x = c(-4:3, 12), y = 3*c(rep(0,6), -5, 5, 1)) cAS <- with(d.AS, t(sapply(1:10, function(it) line(x,y, iter=it)$coefficients))) dimnames(cAS) <- list(paste("it =", format(1:10)), c("intercept", "slope")) cAS ## iterations started to oscillate, repeating iteration 7,8 indefinitely
Class "listof"
is used by aov
and the "lm"
method of alias
for lists of model fits or parts
thereof. It is simply a list with an assigned class to control the way
methods, especially printing, act on it.
It has a coef
method in this package (which returns an
object of this class), and [
and print
methods in
package base.
lm
is used to fit linear models, including multivariate ones.
It can be used to carry out regression,
single stratum analysis of variance and
analysis of covariance (although aov
may provide a more
convenient interface for these).
lm(formula, data, subset, weights, na.action, method = "qr", model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE, contrasts = NULL, offset, ...) ## S3 method for class 'lm' print(x, digits = max(3L, getOption("digits") - 3L), ...)
lm(formula, data, subset, weights, na.action, method = "qr", model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE, contrasts = NULL, offset, ...) ## S3 method for class 'lm' print(x, digits = max(3L, getOption("digits") - 3L), ...)
formula |
an object of class |
data |
an optional data frame, list or environment (or object
coercible by |
subset |
an optional vector specifying a subset of observations
to be used in the fitting process. (See additional details about how
this argument interacts with data-dependent bases in the
‘Details’ section of the |
weights |
an optional vector of weights to be used in the fitting
process. Should be |
na.action |
a function which indicates what should happen
when the data contain |
method |
the method to be used; for fitting, currently only
|
model , x , y , qr
|
logicals. If |
singular.ok |
logical. If |
contrasts |
an optional list. See the |
offset |
this can be used to specify an a priori known
component to be included in the linear predictor during fitting.
This should be |
... |
For |
digits |
the number of significant digits to be
passed to |
Models for lm
are specified symbolically. A typical model has
the form response ~ terms
where response
is the (numeric)
response vector and terms
is a series of terms which specifies a
linear predictor for response
. A terms specification of the form
first + second
indicates all the terms in first
together
with all the terms in second
with duplicates removed. A
specification of the form first:second
indicates the set of
terms obtained by taking the interactions of all terms in first
with all terms in second
. The specification first*second
indicates the cross of first
and second
. This is
the same as first + second + first:second
.
If the formula includes an offset
, this is evaluated and
subtracted from the response.
If response
is a matrix a linear model is fitted separately by
least-squares to each column of the matrix and the result inherits from
"mlm"
(“multivariate linear model”).
See model.matrix
for some further details. The terms in
the formula will be re-ordered so that main effects come first,
followed by the interactions, all second-order, all third-order and so
on: to avoid this pass a terms
object as the formula (see
aov
and demo(glm.vr)
for an example).
A formula has an implied intercept term. To remove this use either
y ~ x - 1
or y ~ 0 + x
. See formula
for
more details of allowed formulae.
Non-NULL
weights
can be used to indicate that
different observations have different variances (with the values in
weights
being inversely proportional to the variances); or
equivalently, when the elements of weights
are positive
integers , that each response
is the mean of
unit-weight observations (including the case that there
are
observations equal to
and the data have been
summarized). However, in the latter case, notice that within-group
variation is not used. Therefore, the sigma estimate and residual
degrees of freedom may be suboptimal; in the case of replication
weights, even wrong. Hence, standard errors and analysis of variance
tables should be treated with care.
lm
calls the lower level functions lm.fit
, etc,
see below, for the actual numerical computations. For programming
only, you may consider doing likewise.
All of weights
, subset
and offset
are evaluated
in the same way as variables in formula
, that is first in
data
and then in the environment of formula
.
lm
returns an object of class
"lm"
or for
multivariate (‘multiple’) responses of class c("mlm", "lm")
.
The functions summary
and anova
are used to
obtain and print a summary and analysis of variance table of the
results. The generic accessor functions coefficients
,
effects
, fitted.values
and residuals
extract
various useful features of the value returned by lm
.
An object of class "lm"
is a list containing at least the
following components:
coefficients |
a named vector of coefficients |
residuals |
the residuals, that is response minus fitted values. |
fitted.values |
the fitted mean values. |
rank |
the numeric rank of the fitted linear model. |
weights |
(only for weighted fits) the specified weights. |
df.residual |
the residual degrees of freedom. |
call |
the matched call. |
terms |
the |
contrasts |
(only where relevant) the contrasts used. |
xlevels |
(only where relevant) a record of the levels of the factors used in fitting. |
offset |
the offset used (missing if none were used). |
y |
if requested, the response used. |
x |
if requested, the model matrix used. |
model |
if requested (the default), the model frame used. |
na.action |
(where relevant) information returned by
|
In addition, non-null fits will have components assign
,
effects
and (unless not requested) qr
relating to the linear
fit, for use by extractor functions such as summary
and
effects
.
Considerable care is needed when using lm
with time series.
Unless na.action = NULL
, the time series attributes are
stripped from the variables before the regression is done. (This is
necessary as omitting NA
s would invalidate the time series
attributes, and if NA
s are omitted in the middle of the series
the result would no longer be a regular time series.)
Even if the time series attributes are retained, they are not used to
line up series, so that the time shift of a lagged or differenced
regressor would be ignored. It is good practice to prepare a
data
argument by ts.intersect(..., dframe = TRUE)
,
then apply a suitable na.action
to that data frame and call
lm
with na.action = NULL
so that residuals and fitted
values are time series.
The design was inspired by the S function of the same name described in Chambers (1992). The implementation of model formula by Ross Ihaka was based on Wilkinson & Rogers (1973).
Chambers, J. M. (1992) Linear models. Chapter 4 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
Wilkinson, G. N. and Rogers, C. E. (1973). Symbolic descriptions of factorial models for analysis of variance. Applied Statistics, 22, 392–399. doi:10.2307/2346786.
summary.lm
for more detailed summaries and anova.lm
for
the ANOVA table; aov
for a different interface.
The generic functions coef
, effects
,
residuals
, fitted
, vcov
.
predict.lm
(via predict
) for prediction,
including confidence and prediction intervals;
confint
for confidence intervals of parameters.
lm.influence
for regression diagnostics, and
glm
for generalized linear models.
The underlying low level functions,
lm.fit
for plain, and lm.wfit
for weighted
regression fitting.
More lm()
examples are available e.g., in
anscombe
, attitude
, freeny
,
LifeCycleSavings
, longley
,
stackloss
, swiss
.
biglm
in package biglm for an alternative
way to fit linear models to large datasets (especially those with many
cases).
require(graphics) ## Annette Dobson (1990) "An Introduction to Generalized Linear Models". ## Page 9: Plant Weight Data. ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69) group <- gl(2, 10, 20, labels = c("Ctl","Trt")) weight <- c(ctl, trt) lm.D9 <- lm(weight ~ group) lm.D90 <- lm(weight ~ group - 1) # omitting intercept anova(lm.D9) summary(lm.D90) opar <- par(mfrow = c(2,2), oma = c(0, 0, 1.1, 0)) plot(lm.D9, las = 1) # Residuals, Fitted, ... par(opar) ### less simple examples in "See Also" above
require(graphics) ## Annette Dobson (1990) "An Introduction to Generalized Linear Models". ## Page 9: Plant Weight Data. ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69) group <- gl(2, 10, 20, labels = c("Ctl","Trt")) weight <- c(ctl, trt) lm.D9 <- lm(weight ~ group) lm.D90 <- lm(weight ~ group - 1) # omitting intercept anova(lm.D9) summary(lm.D90) opar <- par(mfrow = c(2,2), oma = c(0, 0, 1.1, 0)) plot(lm.D9, las = 1) # Residuals, Fitted, ... par(opar) ### less simple examples in "See Also" above
These are the basic computing engines called by lm
used
to fit linear models. These should usually not be used
directly unless by experienced users. .lm.fit()
is a bare-bones
wrapper to the innermost QR-based C code, on which
glm.fit
and lsfit
are also based, for
even more experienced users.
lm.fit (x, y, offset = NULL, method = "qr", tol = 1e-7, singular.ok = TRUE, ...) lm.wfit(x, y, w, offset = NULL, method = "qr", tol = 1e-7, singular.ok = TRUE, ...) .lm.fit(x, y, tol = 1e-7)
lm.fit (x, y, offset = NULL, method = "qr", tol = 1e-7, singular.ok = TRUE, ...) lm.wfit(x, y, w, offset = NULL, method = "qr", tol = 1e-7, singular.ok = TRUE, ...) .lm.fit(x, y, tol = 1e-7)
x |
design matrix of dimension |
y |
vector of observations of length |
w |
vector of weights (length |
offset |
(numeric of length |
method |
currently, only |
tol |
tolerance for the |
singular.ok |
logical. If |
... |
currently disregarded. |
If y
is a matrix, offset
can be a numeric matrix of the
same dimensions, in which case each column is applied to the
corresponding column of y
.
a list
with components (for lm.fit
and lm.wfit
)
coefficients |
|
residuals |
|
fitted.values |
|
effects |
|
weights |
|
rank |
integer, giving the rank |
df.residual |
degrees of freedom of residuals |
qr |
the QR decomposition, see |
Fits without any columns or non-zero weights do not have the
effects
and qr
components.
.lm.fit()
returns a subset of the above, the qr
part
unwrapped, plus a logical component pivoted
indicating if the
underlying QR algorithm did pivot.
lm
which you should use for linear least squares regression,
unless you know better.
require(utils) set.seed(129) n <- 7 ; p <- 2 X <- matrix(rnorm(n * p), n, p) # no intercept! y <- rnorm(n) w <- rnorm(n)^2 str(lmw <- lm.wfit(x = X, y = y, w = w)) str(lm. <- lm.fit (x = X, y = y)) ## fits w/o intercept: all.equal(unname(coef(lm(y ~ X-1))), unname(coef( lm.fit(X,y)))) all.equal(unname(coef( lm.fit(X,y))), coef(.lm.fit(X,y))) if(require("microbenchmark")) { mb <- microbenchmark(lm(y~X-1), lm.fit(X,y), .lm.fit(X,y)) print(mb) boxplot(mb, notch=TRUE) }
require(utils) set.seed(129) n <- 7 ; p <- 2 X <- matrix(rnorm(n * p), n, p) # no intercept! y <- rnorm(n) w <- rnorm(n)^2 str(lmw <- lm.wfit(x = X, y = y, w = w)) str(lm. <- lm.fit (x = X, y = y)) ## fits w/o intercept: all.equal(unname(coef(lm(y ~ X-1))), unname(coef( lm.fit(X,y)))) all.equal(unname(coef( lm.fit(X,y))), coef(.lm.fit(X,y))) if(require("microbenchmark")) { mb <- microbenchmark(lm(y~X-1), lm.fit(X,y), .lm.fit(X,y)) print(mb) boxplot(mb, notch=TRUE) }
This function provides the basic quantities which are used in forming a wide variety of diagnostics for checking the quality of regression fits.
influence(model, ...) ## S3 method for class 'lm' influence(model, do.coef = TRUE, ...) ## S3 method for class 'glm' influence(model, do.coef = TRUE, ...) lm.influence(model, do.coef = TRUE)
influence(model, ...) ## S3 method for class 'lm' influence(model, do.coef = TRUE, ...) ## S3 method for class 'glm' influence(model, do.coef = TRUE, ...) lm.influence(model, do.coef = TRUE)
model |
|
do.coef |
logical indicating if the changed |
... |
further arguments passed to or from other methods. |
The influence.measures()
and other functions listed in
See Also provide a more user oriented way of computing a
variety of regression diagnostics. These all build on
lm.influence
. Note that for GLMs (other than the Gaussian
family with identity link) these are based on one-step approximations
which may be inadequate if a case has high influence.
An attempt is made to ensure that computed hat values that are
probably one are treated as one, and the corresponding rows in
sigma
and coefficients
are NaN
. (Dropping such a
case would normally result in a variable being dropped, so it is not
possible to give simple drop-one diagnostics.)
naresid
is applied to the results and so will fill in
with NA
s it the fit had na.action = na.exclude
.
A list containing the following components of the same length or
number of rows , which is the number of non-zero weights.
Cases omitted in the fit are omitted unless a
na.action
method was used (such as na.exclude
) which restores them.
hat |
a vector containing the diagonal of the ‘hat’ matrix. |
coefficients |
(unless |
sigma |
a vector whose i-th element contains the estimate
of the residual standard deviation obtained when the i-th
case is dropped from the regression. (The approximations needed for
GLMs can result in this being |
wt.res |
a vector of weighted (or for class |
The coefficients
returned by the R version
of lm.influence
differ from those computed by S.
Rather than returning the coefficients which result
from dropping each case, we return the changes in the coefficients.
This is more directly useful in many diagnostic measures.
Since these need computing time, they can be omitted by
do.coef = FALSE
.
Note that cases with weights == 0
are dropped (contrary
to the situation in S).
If a model has been fitted with na.action = na.exclude
(see
na.exclude
), cases excluded in the fit are
considered here.
See the list in the documentation for influence.measures
.
Chambers, J. M. (1992) Linear models. Chapter 4 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
summary.lm
for summary
and related methods;influence.measures
,hat
for the hat matrix diagonals,dfbetas
,
dffits
,
covratio
,
cooks.distance
,
lm
.
## Analysis of the life-cycle savings data ## given in Belsley, Kuh and Welsch. summary(lm.SR <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings), correlation = TRUE) utils::str(lmI <- lm.influence(lm.SR)) ## For more "user level" examples, use example(influence.measures)
## Analysis of the life-cycle savings data ## given in Belsley, Kuh and Welsch. summary(lm.SR <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings), correlation = TRUE) utils::str(lmI <- lm.influence(lm.SR)) ## For more "user level" examples, use example(influence.measures)
All these functions are methods
for class "lm"
objects.
## S3 method for class 'lm' family(object, ...) ## S3 method for class 'lm' formula(x, ...) ## S3 method for class 'lm' residuals(object, type = c("working", "response", "deviance", "pearson", "partial"), ...) ## S3 method for class 'lm' labels(object, ...)
## S3 method for class 'lm' family(object, ...) ## S3 method for class 'lm' formula(x, ...) ## S3 method for class 'lm' residuals(object, type = c("working", "response", "deviance", "pearson", "partial"), ...) ## S3 method for class 'lm' labels(object, ...)
object , x
|
an object inheriting from class |
... |
further arguments passed to or from other methods. |
type |
the type of residuals which should be returned. Can be abbreviated. |
The generic accessor functions coef
, effects
,
fitted
and residuals
can be used to extract
various useful features of the value returned by lm
.
The working and response residuals are ‘observed - fitted’. The
deviance and Pearson residuals are weighted residuals, scaled by the
square root of the weights used in fitting. The partial residuals
are a matrix with each column formed by omitting a term from the
model. In all these, zero weight cases are never omitted (as opposed
to the standardized rstudent
residuals, and the
weighted.residuals
).
How residuals
treats cases with missing values in the original
fit is determined by the na.action
argument of that fit.
If na.action = na.omit
omitted cases will not appear in the
residuals, whereas if na.action = na.exclude
they will appear,
with residual value NA
. See also naresid
.
The "lm"
method for generic labels
returns the
term labels for estimable terms, that is the names of the terms with
an least one estimable coefficient.
Chambers, J. M. (1992) Linear models. Chapter 4 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
The model fitting function lm
, anova.lm
.
coef
, deviance
,
df.residual
,
effects
, fitted
,
glm
for generalized linear models,
influence
(etc on that page) for regression diagnostics,
weighted.residuals
,
residuals
, residuals.glm
,
summary.lm
, weights
.
influence.measures for deletion diagnostics, including
standardized (rstandard
)
and studentized (rstudent
) residuals.
##-- Continuing the lm(.) example: coef(lm.D90) # the bare coefficients ## The 2 basic regression diagnostic plots [plot.lm(.) is preferred] plot(resid(lm.D90), fitted(lm.D90)) # Tukey-Anscombe's abline(h = 0, lty = 2, col = "gray") qqnorm(residuals(lm.D90))
##-- Continuing the lm(.) example: coef(lm.D90) # the bare coefficients ## The 2 basic regression diagnostic plots [plot.lm(.) is preferred] plot(resid(lm.D90), fitted(lm.D90)) # Tukey-Anscombe's abline(h = 0, lty = 2, col = "gray") qqnorm(residuals(lm.D90))
Extract or print loadings in factor analysis (or principal components analysis).
loadings(x, ...) ## S3 method for class 'loadings' print(x, digits = 3, cutoff = 0.1, sort = FALSE, ...) ## S3 method for class 'factanal' print(x, digits = 3, ...)
loadings(x, ...) ## S3 method for class 'loadings' print(x, digits = 3, cutoff = 0.1, sort = FALSE, ...) ## S3 method for class 'factanal' print(x, digits = 3, ...)
x |
an object of class |
digits |
number of decimal places to use in printing uniquenesses and loadings. |
cutoff |
loadings smaller than this (in absolute value) are suppressed. |
sort |
logical. If true, the variables are sorted by their importance on each factor. Each variable with any loading larger than 0.5 (in modulus) is assigned to the factor with the largest loading, and the variables are printed in the order of the factor they are assigned to, then those unassigned. |
... |
further arguments for other methods,
ignored for |
‘Loadings’ is a term from factor analysis, but because
factor analysis and principal component analysis (PCA) are often
conflated in the social science literature, it was used for PCA by
SPSS and hence by princomp
in S-PLUS to help SPSS users.
Small loadings are conventionally not printed (replaced by spaces), to draw the eye to the pattern of the larger loadings.
The print
method for class "factanal"
calls the
"loadings"
method to print the loadings, and so passes down
arguments such as cutoff
and sort
.
The signs of the loadings vectors are arbitrary for both factor analysis and PCA.
There are other functions called loadings
in contributed
packages which are S3 or S4 generic: the ...
argument is to
make it easier for this one to become a default method.
Fit a locally polynomial surface determined by one or more numerical predictors, using local fitting.
loess(formula, data, weights, subset, na.action, model = FALSE, span = 0.75, enp.target, degree = 2, parametric = FALSE, drop.square = FALSE, normalize = TRUE, family = c("gaussian", "symmetric"), method = c("loess", "model.frame"), control = loess.control(...), ...)
loess(formula, data, weights, subset, na.action, model = FALSE, span = 0.75, enp.target, degree = 2, parametric = FALSE, drop.square = FALSE, normalize = TRUE, family = c("gaussian", "symmetric"), method = c("loess", "model.frame"), control = loess.control(...), ...)
formula |
a formula specifying the numeric response and one to four numeric predictors (best specified via an interaction, but can also be specified additively). Will be coerced to a formula if necessary. |
data |
an optional data frame, list or environment (or object
coercible by |
weights |
optional weights for each case. |
subset |
an optional specification of a subset of the data to be used. |
na.action |
the action to be taken with missing values in the
response or predictors. The default is given by
|
model |
should the model frame be returned? |
span |
the parameter |
enp.target |
an alternative way to specify |
degree |
the degree of the polynomials to be used, normally 1 or 2. (Degree 0 is also allowed, but see the ‘Note’.) |
parametric |
should any terms be fitted globally rather than locally? Terms can be specified by name, number or as a logical vector of the same length as the number of predictors. |
drop.square |
for fits with more than one predictor and
|
normalize |
should the predictors be normalized to a common scale if there is more than one? The normalization used is to set the 10% trimmed standard deviation to one. Set to false for spatial coordinate predictors and others known to be on a common scale. |
family |
if |
method |
fit the model or just extract the model frame. Can be abbreviated. |
control |
control parameters: see |
... |
control parameters can also be supplied directly
(if |
Fitting is done locally. That is, for the fit at point , the
fit is made using points in a neighbourhood of
, weighted by
their distance from
(with differences in ‘parametric’
variables being ignored when computing the distance). The size of the
neighbourhood is controlled by
(set by
span
or
enp.target
). For , the
neighbourhood includes proportion
of the points,
and these have tricubic weighting (proportional to
). For
, all points are used, with the
‘maximum distance’ assumed to be
times the actual maximum distance for
explanatory variables.
For the default family, fitting is by (weighted) least squares. For
family="symmetric"
a few iterations of an M-estimation
procedure with Tukey's biweight are used. Be aware that as the initial
value is the least-squares fit, this need not be a very resistant fit.
It can be important to tune the control list to achieve acceptable
speed. See loess.control
for details.
An object of class "loess"
,
with print()
, summary()
, predict
and
anova
methods.
As this is based on cloess
, it is similar to but not identical to
the loess
function of S. In particular, conditioning is not
implemented.
The memory usage of this implementation of loess
is roughly
quadratic in the number of points, with 1000 points taking about 10Mb.
degree = 0
, local constant fitting, is allowed in this
implementation but not documented in the reference. It seems very little
tested, so use with caution.
B. D. Ripley, based on the cloess
package of Cleveland,
Grosse and Shyu.
The 1998 version of cloess
package of Cleveland,
Grosse and Shyu. A later version is available as dloess
at
https://netlib.org/a/.
W. S. Cleveland, E. Grosse and W. M. Shyu (1992) Local regression models. Chapter 8 of Statistical Models in S eds J.M. Chambers and T.J. Hastie, Wadsworth & Brooks/Cole.
lowess
, the ancestor of loess
(with
different defaults!).
cars.lo <- loess(dist ~ speed, cars) predict(cars.lo, data.frame(speed = seq(5, 30, 1)), se = TRUE) # to allow extrapolation cars.lo2 <- loess(dist ~ speed, cars, control = loess.control(surface = "direct")) predict(cars.lo2, data.frame(speed = seq(5, 30, 1)), se = TRUE)
cars.lo <- loess(dist ~ speed, cars) predict(cars.lo, data.frame(speed = seq(5, 30, 1)), se = TRUE) # to allow extrapolation cars.lo2 <- loess(dist ~ speed, cars, control = loess.control(surface = "direct")) predict(cars.lo2, data.frame(speed = seq(5, 30, 1)), se = TRUE)
loess
Set control parameters for loess
fits.
loess.control(surface = c("interpolate", "direct"), statistics = c("approximate", "exact", "none"), trace.hat = c("exact", "approximate"), cell = 0.2, iterations = 4, iterTrace = FALSE, ...)
loess.control(surface = c("interpolate", "direct"), statistics = c("approximate", "exact", "none"), trace.hat = c("exact", "approximate"), cell = 0.2, iterations = 4, iterTrace = FALSE, ...)
surface |
should the fitted surface be computed exactly
( |
statistics |
should the statistics be computed exactly, approximately or not at all? Exact computation can be very slow. Can be abbreviated. |
trace.hat |
Only for the (default) case |
cell |
if interpolation is used this controls the accuracy of the
approximation via the maximum number of points in a cell in the k-d
tree. Cells with more than |
iterations |
the number of iterations used in robust fitting,
i.e. only if |
iterTrace |
logical (or integer) determining if tracing
information during the robust iterations ( |
... |
further arguments which are ignored. |
A list with components
surface |
|
statistics |
|
trace.hat |
|
cell |
|
iterations |
|
iterTrace |
with meanings as explained under ‘Arguments’.
Density, distribution function, quantile function and random
generation for the logistic distribution with parameters
location
and scale
.
dlogis(x, location = 0, scale = 1, log = FALSE) plogis(q, location = 0, scale = 1, lower.tail = TRUE, log.p = FALSE) qlogis(p, location = 0, scale = 1, lower.tail = TRUE, log.p = FALSE) rlogis(n, location = 0, scale = 1)
dlogis(x, location = 0, scale = 1, log = FALSE) plogis(q, location = 0, scale = 1, lower.tail = TRUE, log.p = FALSE) qlogis(p, location = 0, scale = 1, lower.tail = TRUE, log.p = FALSE) rlogis(n, location = 0, scale = 1)
x , q
|
vector of quantiles. |
p |
vector of probabilities. |
n |
number of observations. If |
location , scale
|
location and scale parameters. |
log , log.p
|
logical; if TRUE, probabilities p are given as log(p). |
lower.tail |
logical; if TRUE (default), probabilities are
|
If location
or scale
are omitted, they assume the
default values of 0
and 1
respectively.
The Logistic distribution with location
and
scale
has distribution function
and density
It is a long-tailed distribution with mean and variance
.
dlogis
gives the density,
plogis
gives the distribution function,
qlogis
gives the quantile function, and
rlogis
generates random deviates.
The length of the result is determined by n
for
rlogis
, and is the maximum of the lengths of the
numerical arguments for the other functions.
The numerical arguments other than n
are recycled to the
length of the result. Only the first elements of the logical
arguments are used.
qlogis(p)
is the same as the well known ‘logit’
function, ,
and
plogis(x)
has consequently been called the ‘inverse logit’.
The distribution function is a rescaled hyperbolic tangent,
plogis(x) == (1+ tanh(x/2))/2
, and it is called a
sigmoid function in contexts such as neural networks.
[dpq]logis
are calculated directly from the definitions.
rlogis
uses inversion.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
Johnson, N. L., Kotz, S. and Balakrishnan, N. (1995) Continuous Univariate Distributions, volume 2, chapter 23. Wiley, New York.
Distributions for other standard distributions.
var(rlogis(4000, 0, scale = 5)) # approximately (+/- 3) pi^2/3 * 5^2
var(rlogis(4000, 0, scale = 5)) # approximately (+/- 3) pi^2/3 * 5^2
This function is generic; method functions can be written to handle
specific classes of objects. Classes which have methods for this
function include: "glm"
, "lm"
, "nls"
and
"Arima"
. Packages contain methods for other classes, such as
"fitdistr"
, "negbin"
and "polr"
in package
MASS, "multinom"
in package nnet and
"gls"
, "gnls"
"lme"
and others in package
nlme.
logLik(object, ...) ## S3 method for class 'lm' logLik(object, REML = FALSE, ...)
logLik(object, ...) ## S3 method for class 'lm' logLik(object, REML = FALSE, ...)
object |
any object from which a log-likelihood value, or a contribution to a log-likelihood value, can be extracted. |
... |
some methods for this generic function require additional arguments. |
REML |
an optional logical value. If |
logLik
is most commonly used for a model fitted by maximum
likelihood, and some uses, e.g. by AIC
, assume
this. So care is needed where other fit criteria have been used, for
example REML (the default for "lme"
).
For a "glm"
fit the family
does not have to
specify how to calculate the log-likelihood, so this is based on using
the family's aic()
function to compute the AIC. For the
gaussian
, Gamma
and
inverse.gaussian
families it assumed that the dispersion
of the GLM is estimated and has been counted as a parameter in the AIC
value, and for all other families it is assumed that the dispersion is
known. Note that this procedure does not give the maximized
likelihood for "glm"
fits from the Gamma and inverse gaussian
families, as the estimate of dispersion used is not the MLE.
For "lm"
fits it is assumed that the scale has been estimated
(by maximum likelihood or REML), and all the constants in the
log-likelihood are included. That method is only applicable to
single-response fits.
Returns an object of class logLik
. This is a number with at
least one attribute, "df"
(degrees of freedom),
giving the number of (estimated) parameters in the model.
There is a simple print
method for "logLik"
objects.
There may be other attributes depending on the method used: see the
appropriate documentation. One that is used by several methods is
"nobs"
, the number of observations used in estimation (after
the restrictions if REML = TRUE
).
José Pinheiro and Douglas Bates
For logLik.lm
:
Harville, D.A. (1974). Bayesian inference for variance components using only error contrasts. Biometrika, 61, 383–385. doi:10.2307/2334370.
logLik.gls
, logLik.lme
, in
package nlme, etc.
x <- 1:5 lmx <- lm(x ~ 1) logLik(lmx) # using print.logLik() method utils::str(logLik(lmx)) ## lm method (fm1 <- lm(rating ~ ., data = attitude)) logLik(fm1) logLik(fm1, REML = TRUE) utils::data(Orthodont, package = "nlme") fm1 <- lm(distance ~ Sex * age, Orthodont) logLik(fm1) logLik(fm1, REML = TRUE)
x <- 1:5 lmx <- lm(x ~ 1) logLik(lmx) # using print.logLik() method utils::str(logLik(lmx)) ## lm method (fm1 <- lm(rating ~ ., data = attitude)) logLik(fm1) logLik(fm1, REML = TRUE) utils::data(Orthodont, package = "nlme") fm1 <- lm(distance ~ Sex * age, Orthodont) logLik(fm1) logLik(fm1, REML = TRUE)
loglin
is used to fit log-linear models to multidimensional
contingency tables by Iterative Proportional Fitting.
loglin(table, margin, start = rep(1, length(table)), fit = FALSE, eps = 0.1, iter = 20, param = FALSE, print = TRUE)
loglin(table, margin, start = rep(1, length(table)), fit = FALSE, eps = 0.1, iter = 20, param = FALSE, print = TRUE)
table |
a contingency table to be fit, typically the output from
|
margin |
a list of vectors with the marginal totals to be fit. (Hierarchical) log-linear models can be specified in terms of these
marginal totals which give the ‘maximal’ factor subsets contained
in the model. For example, in a three-factor model,
The names of factors (i.e., |
start |
a starting estimate for the fitted table. This optional
argument is important for incomplete tables with structural zeros
in |
fit |
a logical indicating whether the fitted values should be returned. |
eps |
maximum deviation allowed between observed and fitted margins. |
iter |
maximum number of iterations. |
param |
a logical indicating whether the parameter values should be returned. |
print |
a logical. If |
The Iterative Proportional Fitting algorithm as presented in
Haberman (1972) is used for fitting the model. At most iter
iterations are performed, convergence is taken to occur when the
maximum deviation between observed and fitted margins is less than
eps
. All internal computations are done in double precision;
there is no limit on the number of factors (the dimension of the
table) in the model.
Assuming that there are no structural zeros, both the Likelihood
Ratio Test and Pearson test statistics have an asymptotic chi-squared
distribution with df
degrees of freedom.
Note that the IPF steps are applied to the factors in the order given
in margin
. Hence if the model is decomposable and the order
given in margin
is a running intersection property ordering
then IPF will converge in one iteration.
Package MASS contains loglm
, a front-end to
loglin
which allows the log-linear model to be specified and
fitted in a formula-based manner similar to that of other fitting
functions such as lm
or glm
.
A list with the following components.
lrt |
the Likelihood Ratio Test statistic. |
pearson |
the Pearson test statistic (X-squared). |
df |
the degrees of freedom for the fitted model. There is no adjustment for structural zeros. |
margin |
list of the margins that were fit. Basically the same
as the input |
fit |
An array like |
param |
A list containing the estimated parameters of the
model. The ‘standard’ constraints of zero marginal sums
(e.g., zero row and column sums for a two factor parameter) are
employed. Only returned if |
Kurt Hornik
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988). The New S Language. Wadsworth & Brooks/Cole.
Haberman, S. J. (1972). Algorithm AS 51: Log-linear fit for contingency tables. Applied Statistics, 21, 218–225. doi:10.2307/2346506.
Agresti, A. (1990). Categorical data analysis. New York: Wiley.
loglm
in package MASS for a
user-friendly wrapper.
glm
for another way to fit log-linear models.
## Model of joint independence of sex from hair and eye color. fm <- loglin(HairEyeColor, list(c(1, 2), c(1, 3), c(2, 3))) fm 1 - pchisq(fm$lrt, fm$df) ## Model with no three-factor interactions fits well.
## Model of joint independence of sex from hair and eye color. fm <- loglin(HairEyeColor, list(c(1, 2), c(1, 3), c(2, 3))) fm 1 - pchisq(fm$lrt, fm$df) ## Model with no three-factor interactions fits well.
Density, distribution function, quantile function and random
generation for the log normal distribution whose logarithm has mean
equal to meanlog
and standard deviation equal to sdlog
.
dlnorm(x, meanlog = 0, sdlog = 1, log = FALSE) plnorm(q, meanlog = 0, sdlog = 1, lower.tail = TRUE, log.p = FALSE) qlnorm(p, meanlog = 0, sdlog = 1, lower.tail = TRUE, log.p = FALSE) rlnorm(n, meanlog = 0, sdlog = 1)
dlnorm(x, meanlog = 0, sdlog = 1, log = FALSE) plnorm(q, meanlog = 0, sdlog = 1, lower.tail = TRUE, log.p = FALSE) qlnorm(p, meanlog = 0, sdlog = 1, lower.tail = TRUE, log.p = FALSE) rlnorm(n, meanlog = 0, sdlog = 1)
x , q
|
vector of quantiles. |
p |
vector of probabilities. |
n |
number of observations. If |
meanlog , sdlog
|
mean and standard deviation of the distribution
on the log scale with default values of |
log , log.p
|
logical; if TRUE, probabilities p are given as log(p). |
lower.tail |
logical; if TRUE (default), probabilities are
|
The log normal distribution has density
where and
are the mean and standard
deviation of the logarithm.
The mean is
,
the median is
, and the variance
and hence the coefficient of variation is
which is
approximately
when that is small (e.g.,
).
dlnorm
gives the density,
plnorm
gives the distribution function,
qlnorm
gives the quantile function, and
rlnorm
generates random deviates.
The length of the result is determined by n
for
rlnorm
, and is the maximum of the lengths of the
numerical arguments for the other functions.
The numerical arguments other than n
are recycled to the
length of the result. Only the first elements of the logical
arguments are used.
The cumulative hazard
is
-plnorm(t, r, lower = FALSE, log = TRUE)
.
dlnorm
is calculated from the definition (in ‘Details’).
[pqr]lnorm
are based on the relationship to the normal.
Consequently, they model a single point mass at exp(meanlog)
for the boundary case sdlog = 0
.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
Johnson, N. L., Kotz, S. and Balakrishnan, N. (1995) Continuous Univariate Distributions, volume 1, chapter 14. Wiley, New York.
Distributions for other standard distributions, including
dnorm
for the normal distribution.
dlnorm(1) == dnorm(0)
dlnorm(1) == dnorm(0)
This function performs the computations for the LOWESS smoother which uses locally-weighted polynomial regression (see the references).
lowess(x, y = NULL, f = 2/3, iter = 3, delta = 0.01 * diff(range(x)))
lowess(x, y = NULL, f = 2/3, iter = 3, delta = 0.01 * diff(range(x)))
x , y
|
vectors giving the coordinates of the points in the scatter plot.
Alternatively a single plotting structure can be specified – see
|
f |
the smoother span. This gives the proportion of points in the plot which influence the smooth at each value. Larger values give more smoothness. |
iter |
the number of ‘robustifying’ iterations which should be
performed.
Using smaller values of |
delta |
See ‘Details’. Defaults to 1/100th of the range
of |
lowess
is defined by a complex algorithm, the Ratfor original
of which (by W. S. Cleveland) can be found in the R sources as file
‘src/library/stats/src/lowess.doc’. Normally a local linear polynomial fit is
used, but under some circumstances (see the file) a local constant fit
can be used. ‘Local’ is defined by the distance to the
floor(f*n)
-th nearest neighbour, and tricubic weighting is used
for x
which fall within the neighbourhood.
The initial fit is done using weighted least squares. If
iter > 0
, further weighted fits are done using the product of
the weights from the proximity of the x
values and case weights
derived from the residuals at the previous iteration. Specifically,
the case weight is Tukey's biweight, with cutoff 6 times the MAD of the
residuals. (The current R implementation differs from the original
in stopping iteration if the MAD is effectively zero since the
algorithm is highly unstable in that case.)
delta
is used to speed up computation: instead of computing the
local polynomial fit at each data point it is not computed for points
within delta
of the last computed point, and linear
interpolation is used to fill in the fitted values for the skipped
points.
lowess
returns a list containing components
x
and y
which give the coordinates of the smooth.
The smooth can be added to a plot of the original
points with the function lines
: see the examples.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988). The New S Language. Wadsworth & Brooks/Cole.
Cleveland, W. S. (1979). Robust locally weighted regression and smoothing scatterplots. Journal of the American Statistical Association, 74, 829–836. doi:10.1080/01621459.1979.10481038.
Cleveland, W. S. (1981) LOWESS: A program for smoothing scatterplots by robust locally weighted regression. The American Statistician, 35, 54. doi:10.2307/2683591.
loess
, a newer
formula based version of lowess
(with different defaults!).
require(graphics) plot(cars, main = "lowess(cars)") lines(lowess(cars), col = 2) lines(lowess(cars, f = .2), col = 3) legend(5, 120, c(paste("f = ", c("2/3", ".2"))), lty = 1, col = 2:3)
require(graphics) plot(cars, main = "lowess(cars)") lines(lowess(cars), col = 2) lines(lowess(cars, f = .2), col = 3) legend(5, 120, c(paste("f = ", c("2/3", ".2"))), lty = 1, col = 2:3)
lsfit
Regression ResultsComputes basic statistics, including standard errors, t- and p-values for the regression coefficients.
ls.diag(ls.out)
ls.diag(ls.out)
ls.out |
Typically the result of |
A list
with the following numeric components.
std.dev |
The standard deviation of the errors, an estimate of
|
hat |
diagonal entries |
std.res |
standardized residuals |
stud.res |
studentized residuals |
cooks |
Cook's distances |
dfits |
DFITS statistics |
correlation |
correlation matrix |
std.err |
standard errors of the regression coefficients |
cov.scaled |
Scaled covariance matrix of the coefficients |
cov.unscaled |
Unscaled covariance matrix of the coefficients |
Belsley, D. A., Kuh, E. and Welsch, R. E. (1980) Regression Diagnostics. New York: Wiley.
hat
for the hat matrix diagonals,
ls.print
,
lm.influence
, summary.lm
,
anova
.
##-- Using the same data as the lm(.) example: lsD9 <- lsfit(x = as.numeric(gl(2, 10, 20)), y = weight) dlsD9 <- ls.diag(lsD9) utils::str(dlsD9, give.attr = FALSE) abs(1 - sum(dlsD9$hat) / 2) < 10*.Machine$double.eps # sum(h.ii) = p plot(dlsD9$hat, dlsD9$stud.res, xlim = c(0, 0.11)) abline(h = 0, lty = 2, col = "lightgray")
##-- Using the same data as the lm(.) example: lsD9 <- lsfit(x = as.numeric(gl(2, 10, 20)), y = weight) dlsD9 <- ls.diag(lsD9) utils::str(dlsD9, give.attr = FALSE) abs(1 - sum(dlsD9$hat) / 2) < 10*.Machine$double.eps # sum(h.ii) = p plot(dlsD9$hat, dlsD9$stud.res, xlim = c(0, 0.11)) abline(h = 0, lty = 2, col = "lightgray")
lsfit
Regression ResultsComputes basic statistics, including standard errors, t- and p-values
for the regression coefficients and prints them if print.it
is
TRUE
.
ls.print(ls.out, digits = 4, print.it = TRUE)
ls.print(ls.out, digits = 4, print.it = TRUE)
ls.out |
Typically the result of |
digits |
The number of significant digits used for printing |
print.it |
a logical indicating whether the result should also be printed |
A list with the components
summary |
The ANOVA table of the regression |
coef.table |
matrix with regression coefficients, standard errors, t- and p-values |
Usually you would use summary(lm(...))
and
anova(lm(...))
to obtain similar output.
ls.diag
, lsfit
, also for examples;
lm
, lm.influence
which usually are
preferable.
The least squares estimate of in the model
is found.
lsfit(x, y, wt = NULL, intercept = TRUE, tolerance = 1e-07, yname = NULL)
lsfit(x, y, wt = NULL, intercept = TRUE, tolerance = 1e-07, yname = NULL)
x |
a matrix whose rows correspond to cases and whose columns correspond to variables. |
y |
the responses, possibly a matrix if you want to fit multiple left hand sides. |
wt |
an optional vector of weights for performing weighted least squares. |
intercept |
whether or not an intercept term should be used. |
tolerance |
the tolerance to be used in the matrix decomposition. |
yname |
names to be used for the response variables. |
If weights are specified then a weighted least squares is performed
with the weight given to the j-th case specified by the j-th
entry in wt
.
If any observation has a missing value in any field, that observation is removed before the analysis is carried out. This can be quite inefficient if there is a lot of missing data.
The implementation is via a modification of the LINPACK subroutines which allow for multiple left-hand sides.
A list with the following named components:
coef |
the least squares estimates of the coefficients in
the model ( |
residuals |
residuals from the fit. |
intercept |
indicates whether an intercept was fitted. |
qr |
the QR decomposition of the design matrix. |
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
lm
which usually is preferable;
ls.print
, ls.diag
.
##-- Using the same data as the lm(.) example: lsD9 <- lsfit(x = unclass(gl(2, 10)), y = weight) ls.print(lsD9)
##-- Using the same data as the lm(.) example: lsD9 <- lsfit(x = unclass(gl(2, 10)), y = weight) ls.print(lsD9)
Compute the median absolute deviation, i.e., the (lo-/hi-) median of the absolute deviations from the median, and (by default) adjust by a factor for asymptotically normal consistency.
mad(x, center = median(x), constant = 1.4826, na.rm = FALSE, low = FALSE, high = FALSE)
mad(x, center = median(x), constant = 1.4826, na.rm = FALSE, low = FALSE, high = FALSE)
x |
a numeric vector. |
center |
Optionally, the centre: defaults to the median. |
constant |
scale factor. |
na.rm |
if |
low |
if |
high |
if |
The actual value calculated is constant * cMedian(abs(x - center))
with the default value of center
being median(x)
, and
cMedian
being the usual, the ‘low’ or ‘high’ median, see
the arguments description for low
and high
above.
In the case of non-missing values and default
center
, the
result is 0
, consistent with “no deviation from the center”.
The default constant = 1.4826
(approximately
=
1/qnorm(3/4)
)
ensures consistency, i.e.,
for distributed as
and large
.
If na.rm
is TRUE
then NA
values are stripped from x
before computation takes place.
If this is not done then an NA
value in
x
will cause mad
to return NA
.
IQR
which is simpler but less robust,
median
, var
.
mad(c(1:9)) print(mad(c(1:9), constant = 1)) == mad(c(1:8, 100), constant = 1) # = 2 ; TRUE x <- c(1,2,3,5,7,8) sort(abs(x - median(x))) c(mad(x, constant = 1), mad(x, constant = 1, low = TRUE), mad(x, constant = 1, high = TRUE))
mad(c(1:9)) print(mad(c(1:9), constant = 1)) == mad(c(1:8, 100), constant = 1) # = 2 ; TRUE x <- c(1,2,3,5,7,8) sort(abs(x - median(x))) c(mad(x, constant = 1), mad(x, constant = 1, low = TRUE), mad(x, constant = 1, high = TRUE))
Returns the squared Mahalanobis distance of all rows in x
and the
vector =
center
with respect to
=
cov
.
This is (for vector x
) defined as
mahalanobis(x, center, cov, inverted = FALSE, ...)
mahalanobis(x, center, cov, inverted = FALSE, ...)
x |
vector or matrix of data with, say, |
center |
mean vector of the distribution or second data vector of
length |
cov |
covariance matrix ( |
inverted |
logical. If |
... |
passed to |
require(graphics) ma <- cbind(1:6, 1:3) (S <- var(ma)) mahalanobis(c(0, 0), 1:2, S) x <- matrix(rnorm(100*3), ncol = 3) stopifnot(mahalanobis(x, 0, diag(ncol(x))) == rowSums(x*x)) ##- Here, D^2 = usual squared Euclidean distances Sx <- cov(x) D2 <- mahalanobis(x, colMeans(x), Sx) plot(density(D2, bw = 0.5), main="Squared Mahalanobis distances, n=100, p=3") ; rug(D2) qqplot(qchisq(ppoints(100), df = 3), D2, main = expression("Q-Q plot of Mahalanobis" * ~D^2 * " vs. quantiles of" * ~ chi[3]^2)) abline(0, 1, col = 'gray')
require(graphics) ma <- cbind(1:6, 1:3) (S <- var(ma)) mahalanobis(c(0, 0), 1:2, S) x <- matrix(rnorm(100*3), ncol = 3) stopifnot(mahalanobis(x, 0, diag(ncol(x))) == rowSums(x*x)) ##- Here, D^2 = usual squared Euclidean distances Sx <- cov(x) D2 <- mahalanobis(x, colMeans(x), Sx) plot(density(D2, bw = 0.5), main="Squared Mahalanobis distances, n=100, p=3") ; rug(D2) qqplot(qchisq(ppoints(100), df = 3), D2, main = expression("Q-Q plot of Mahalanobis" * ~D^2 * " vs. quantiles of" * ~ chi[3]^2)) abline(0, 1, col = 'gray')
This function is used with the family
functions in
glm()
.
Given the name of a link, it returns a link function, an inverse link
function, the derivative and a function
for domain checking.
make.link(link)
make.link(link)
link |
character; one of |
A object of class "link-glm"
, a list with components
linkfun |
Link function |
linkinv |
Inverse link function |
mu.eta |
Derivative |
valideta |
|
name |
a name to be used for the link |
.
utils::str(make.link("logit"))
utils::str(make.link("logit"))
A utility to help model.frame.default
create the right
matrices when predicting from models with terms like (univariate)
poly
or ns
.
makepredictcall(var, call)
makepredictcall(var, call)
var |
A variable. |
call |
The term in the formula, as a call. |
This is a generic function with methods for poly
, bs
and
ns
: the default method handles scale
. If
model.frame.default
encounters such a term when
creating a model frame, it modifies the predvars
attribute of
the terms supplied by replacing the term with one which will work for
predicting new data. For example makepredictcall.ns
adds
arguments for the knots and intercept.
To make use of this, have your model-fitting function return the
terms
attribute of the model frame, or copy the predvars
attribute of the terms
attribute of the model frame to your
terms
object.
To extend this, make sure the term creates variables with a class, and write a suitable method for that class.
A replacement for call
for the predvars
attribute of
the terms.
model.frame
, poly
, scale
;
bs
and ns
in package splines.
cars
for an example of prediction from a polynomial fit.
require(graphics) ## using poly: this did not work in R < 1.5.0 fm <- lm(weight ~ poly(height, 2), data = women) plot(women, xlab = "Height (in)", ylab = "Weight (lb)") ht <- seq(57, 73, length.out = 200) nD <- data.frame(height = ht) pfm <- predict(fm, nD) lines(ht, pfm) pf2 <- predict(update(fm, ~ stats::poly(height, 2)), nD) stopifnot(all.equal(pfm, pf2)) ## was off (rel.diff. 0.0766) in R <= 3.5.0 ## see also example(cars) ## see bs and ns for spline examples.
require(graphics) ## using poly: this did not work in R < 1.5.0 fm <- lm(weight ~ poly(height, 2), data = women) plot(women, xlab = "Height (in)", ylab = "Weight (lb)") ht <- seq(57, 73, length.out = 200) nD <- data.frame(height = ht) pfm <- predict(fm, nD) lines(ht, pfm) pf2 <- predict(update(fm, ~ stats::poly(height, 2)), nD) stopifnot(all.equal(pfm, pf2)) ## was off (rel.diff. 0.0766) in R <= 3.5.0 ## see also example(cars) ## see bs and ns for spline examples.
A class for the multivariate analysis of variance.
manova(...)
manova(...)
... |
Arguments to be passed to |
Class "manova"
differs from class "aov"
in selecting a
different summary
method. Function manova
calls
aov
and then add class "manova"
to the result
object for each stratum.
See aov
and the comments in ‘Details’ here.
Krzanowski, W. J. (1988) Principles of Multivariate Analysis. A User's Perspective. Oxford.
Hand, D. J. and Taylor, C. C. (1987) Multivariate Analysis of Variance and Repeated Measures. Chapman and Hall.
aov
, summary.manova
, the latter containing
more examples.
## Set orthogonal contrasts. op <- options(contrasts = c("contr.helmert", "contr.poly")) ## Fake a 2nd response variable npk2 <- within(npk, foo <- rnorm(24)) ( npk2.aov <- manova(cbind(yield, foo) ~ block + N*P*K, npk2) ) summary(npk2.aov) ( npk2.aovE <- manova(cbind(yield, foo) ~ N*P*K + Error(block), npk2) ) summary(npk2.aovE)
## Set orthogonal contrasts. op <- options(contrasts = c("contr.helmert", "contr.poly")) ## Fake a 2nd response variable npk2 <- within(npk, foo <- rnorm(24)) ( npk2.aov <- manova(cbind(yield, foo) ~ block + N*P*K, npk2) ) summary(npk2.aov) ( npk2.aovE <- manova(cbind(yield, foo) ~ N*P*K + Error(block), npk2) ) summary(npk2.aovE)
Performs a Cochran-Mantel-Haenszel chi-squared test of the null that two nominal variables are conditionally independent in each stratum, assuming that there is no three-way interaction.
mantelhaen.test(x, y = NULL, z = NULL, alternative = c("two.sided", "less", "greater"), correct = TRUE, exact = FALSE, conf.level = 0.95)
mantelhaen.test(x, y = NULL, z = NULL, alternative = c("two.sided", "less", "greater"), correct = TRUE, exact = FALSE, conf.level = 0.95)
x |
either a 3-dimensional contingency table in array form where each dimension is at least 2 and the last dimension corresponds to the strata, or a factor object with at least 2 levels. |
y |
a factor object with at least 2 levels; ignored if |
z |
a factor object with at least 2 levels identifying to which
stratum the corresponding elements in |
alternative |
indicates the alternative hypothesis and must be
one of |
correct |
a logical indicating whether to apply continuity
correction when computing the test statistic.
Only used in the 2 by 2 by |
exact |
a logical indicating whether the Mantel-Haenszel test or
the exact conditional test (given the strata margins) should be
computed.
Only used in the 2 by 2 by |
conf.level |
confidence level for the returned confidence
interval.
Only used in the 2 by 2 by |
If x
is an array, each dimension must be at least 2, and
the entries should be nonnegative integers. NA
's are not
allowed. Otherwise, x
, y
and z
must have the
same length. Triples containing NA
's are removed. All
variables must take at least two different values.
A list with class "htest"
containing the following components:
statistic |
Only present if no exact test is performed. In the
classical case of a 2 by 2 by |
parameter |
the degrees of freedom of the approximate chi-squared
distribution of the test statistic ( |
p.value |
the p-value of the test. |
conf.int |
a confidence interval for the common odds ratio.
Only present in the 2 by 2 by |
estimate |
an estimate of the common odds ratio. If an exact
test is performed, the conditional Maximum Likelihood Estimate is
given; otherwise, the Mantel-Haenszel estimate.
Only present in the 2 by 2 by |
null.value |
the common odds ratio under the null of
independence, |
alternative |
a character string describing the alternative
hypothesis.
Only present in the 2 by 2 by |
method |
a character string indicating the method employed, and whether or not continuity correction was used. |
data.name |
a character string giving the names of the data. |
The asymptotic distribution is only valid if there is no three-way
interaction. In the classical 2 by 2 by case, this is
equivalent to the conditional odds ratios in each stratum being
identical. Currently, no inference on homogeneity of the odds ratios
is performed.
See also the example below.
Alan Agresti (1990). Categorical data analysis. New York: Wiley. Pages 230–235.
Alan Agresti (2002). Categorical data analysis (second edition). New York: Wiley.
## Agresti (1990), pages 231--237, Penicillin and Rabbits ## Investigation of the effectiveness of immediately injected or 1.5 ## hours delayed penicillin in protecting rabbits against a lethal ## injection with beta-hemolytic streptococci. Rabbits <- array(c(0, 0, 6, 5, 3, 0, 3, 6, 6, 2, 0, 4, 5, 6, 1, 0, 2, 5, 0, 0), dim = c(2, 2, 5), dimnames = list( Delay = c("None", "1.5h"), Response = c("Cured", "Died"), Penicillin.Level = c("1/8", "1/4", "1/2", "1", "4"))) Rabbits ## Classical Mantel-Haenszel test mantelhaen.test(Rabbits) ## => p = 0.047, some evidence for higher cure rate of immediate ## injection ## Exact conditional test mantelhaen.test(Rabbits, exact = TRUE) ## => p - 0.040 ## Exact conditional test for one-sided alternative of a higher ## cure rate for immediate injection mantelhaen.test(Rabbits, exact = TRUE, alternative = "greater") ## => p = 0.020 ## UC Berkeley Student Admissions mantelhaen.test(UCBAdmissions) ## No evidence for association between admission and gender ## when adjusted for department. However, apply(UCBAdmissions, 3, function(x) (x[1,1]*x[2,2])/(x[1,2]*x[2,1])) ## This suggests that the assumption of homogeneous (conditional) ## odds ratios may be violated. The traditional approach would be ## using the Woolf test for interaction: woolf <- function(x) { x <- x + 1 / 2 k <- dim(x)[3] or <- apply(x, 3, function(x) (x[1,1]*x[2,2])/(x[1,2]*x[2,1])) w <- apply(x, 3, function(x) 1 / sum(1 / x)) 1 - pchisq(sum(w * (log(or) - weighted.mean(log(or), w)) ^ 2), k - 1) } woolf(UCBAdmissions) ## => p = 0.003, indicating that there is significant heterogeneity. ## (And hence the Mantel-Haenszel test cannot be used.) ## Agresti (2002), p. 287f and p. 297. ## Job Satisfaction example. Satisfaction <- as.table(array(c(1, 2, 0, 0, 3, 3, 1, 2, 11, 17, 8, 4, 2, 3, 5, 2, 1, 0, 0, 0, 1, 3, 0, 1, 2, 5, 7, 9, 1, 1, 3, 6), dim = c(4, 4, 2), dimnames = list(Income = c("<5000", "5000-15000", "15000-25000", ">25000"), "Job Satisfaction" = c("V_D", "L_S", "M_S", "V_S"), Gender = c("Female", "Male")))) ## (Satisfaction categories abbreviated for convenience.) ftable(. ~ Gender + Income, Satisfaction) ## Table 7.8 in Agresti (2002), p. 288. mantelhaen.test(Satisfaction) ## See Table 7.12 in Agresti (2002), p. 297.
## Agresti (1990), pages 231--237, Penicillin and Rabbits ## Investigation of the effectiveness of immediately injected or 1.5 ## hours delayed penicillin in protecting rabbits against a lethal ## injection with beta-hemolytic streptococci. Rabbits <- array(c(0, 0, 6, 5, 3, 0, 3, 6, 6, 2, 0, 4, 5, 6, 1, 0, 2, 5, 0, 0), dim = c(2, 2, 5), dimnames = list( Delay = c("None", "1.5h"), Response = c("Cured", "Died"), Penicillin.Level = c("1/8", "1/4", "1/2", "1", "4"))) Rabbits ## Classical Mantel-Haenszel test mantelhaen.test(Rabbits) ## => p = 0.047, some evidence for higher cure rate of immediate ## injection ## Exact conditional test mantelhaen.test(Rabbits, exact = TRUE) ## => p - 0.040 ## Exact conditional test for one-sided alternative of a higher ## cure rate for immediate injection mantelhaen.test(Rabbits, exact = TRUE, alternative = "greater") ## => p = 0.020 ## UC Berkeley Student Admissions mantelhaen.test(UCBAdmissions) ## No evidence for association between admission and gender ## when adjusted for department. However, apply(UCBAdmissions, 3, function(x) (x[1,1]*x[2,2])/(x[1,2]*x[2,1])) ## This suggests that the assumption of homogeneous (conditional) ## odds ratios may be violated. The traditional approach would be ## using the Woolf test for interaction: woolf <- function(x) { x <- x + 1 / 2 k <- dim(x)[3] or <- apply(x, 3, function(x) (x[1,1]*x[2,2])/(x[1,2]*x[2,1])) w <- apply(x, 3, function(x) 1 / sum(1 / x)) 1 - pchisq(sum(w * (log(or) - weighted.mean(log(or), w)) ^ 2), k - 1) } woolf(UCBAdmissions) ## => p = 0.003, indicating that there is significant heterogeneity. ## (And hence the Mantel-Haenszel test cannot be used.) ## Agresti (2002), p. 287f and p. 297. ## Job Satisfaction example. Satisfaction <- as.table(array(c(1, 2, 0, 0, 3, 3, 1, 2, 11, 17, 8, 4, 2, 3, 5, 2, 1, 0, 0, 0, 1, 3, 0, 1, 2, 5, 7, 9, 1, 1, 3, 6), dim = c(4, 4, 2), dimnames = list(Income = c("<5000", "5000-15000", "15000-25000", ">25000"), "Job Satisfaction" = c("V_D", "L_S", "M_S", "V_S"), Gender = c("Female", "Male")))) ## (Satisfaction categories abbreviated for convenience.) ftable(. ~ Gender + Income, Satisfaction) ## Table 7.8 in Agresti (2002), p. 288. mantelhaen.test(Satisfaction) ## See Table 7.12 in Agresti (2002), p. 297.
Tests whether a Wishart-distributed covariance matrix (or transformation thereof) is proportional to a given matrix.
mauchly.test(object, ...) ## S3 method for class 'mlm' mauchly.test(object, ...) ## S3 method for class 'SSD' mauchly.test(object, Sigma = diag(nrow = p), T = Thin.row(Proj(M) - Proj(X)), M = diag(nrow = p), X = ~0, idata = data.frame(index = seq_len(p)), ...)
mauchly.test(object, ...) ## S3 method for class 'mlm' mauchly.test(object, ...) ## S3 method for class 'SSD' mauchly.test(object, Sigma = diag(nrow = p), T = Thin.row(Proj(M) - Proj(X)), M = diag(nrow = p), X = ~0, idata = data.frame(index = seq_len(p)), ...)
object |
object of class |
Sigma |
matrix to be proportional to. |
T |
transformation matrix. By default computed from |
M |
formula or matrix describing the outer projection (see below). |
X |
formula or matrix describing the inner projection (see below). |
idata |
data frame describing intra-block design. |
... |
arguments to be passed to or from other methods. |
This is a generic function with methods for classes "mlm"
and
"SSD"
.
The basic method is for objects of
class SSD
the method for mlm
objects just extracts the
SSD matrix and invokes the corresponding method with the same options
and arguments.
The T
argument is used to transform the observations prior to
testing. This typically involves transformation to intra-block
differences, but more complicated within-block designs can be
encountered, making more elaborate transformations necessary. A
matrix T
can be given directly or specified as
the difference between two projections onto the spaces spanned by
M
and X
, which in turn can be given as matrices or as
model formulas with respect to idata
(the tests will be
invariant to parametrization of the quotient space M/X
).
The common use of this test is in repeated measurements designs, with
X = ~1
. This is almost, but not quite the same as testing for
compound symmetry in the untransformed covariance matrix.
Notice that the defaults involve p
, which is calculated
internally as the dimension of the SSD matrix, and a couple of hidden
functions in the stats namespace, namely proj
which
calculates projection matrices from design matrices or model formulas
and Thin.row
which removes linearly dependent rows from a
matrix until it has full row rank.
An object of class "htest"
The p-value differs slightly from that of SAS because a second order term is included in the asymptotic approximation in R.
T. W. Anderson (1958). An Introduction to Multivariate Statistical Analysis. Wiley.
utils::example(SSD) # Brings in the mlmfit and reacttime objects ### traditional test of intrasubj. contrasts mauchly.test(mlmfit, X = ~1) ### tests using intra-subject 3x2 design idata <- data.frame(deg = gl(3, 1, 6, labels = c(0,4,8)), noise = gl(2, 3, 6, labels = c("A","P"))) mauchly.test(mlmfit, X = ~ deg + noise, idata = idata) mauchly.test(mlmfit, M = ~ deg + noise, X = ~ noise, idata = idata)
utils::example(SSD) # Brings in the mlmfit and reacttime objects ### traditional test of intrasubj. contrasts mauchly.test(mlmfit, X = ~1) ### tests using intra-subject 3x2 design idata <- data.frame(deg = gl(3, 1, 6, labels = c(0,4,8)), noise = gl(2, 3, 6, labels = c("A","P"))) mauchly.test(mlmfit, X = ~ deg + noise, idata = idata) mauchly.test(mlmfit, M = ~ deg + noise, X = ~ noise, idata = idata)
Performs McNemar's chi-squared test for symmetry of rows and columns in a two-dimensional contingency table.
mcnemar.test(x, y = NULL, correct = TRUE)
mcnemar.test(x, y = NULL, correct = TRUE)
x |
either a two-dimensional contingency table in matrix form, or a factor object. |
y |
a factor object; ignored if |
correct |
a logical indicating whether to apply continuity correction when computing the test statistic. |
The null is that the probabilities of being classified into cells
[i,j]
and [j,i]
are the same.
If x
is a matrix, it is taken as a two-dimensional contingency
table, and hence its entries should be nonnegative integers.
Otherwise, both x
and y
must be vectors or factors of the
same length. Incomplete cases are removed, vectors are coerced into
factors, and the contingency table is computed from these.
Continuity correction is only used in the 2-by-2 case if
correct
is TRUE
.
A list with class "htest"
containing the following components:
statistic |
the value of McNemar's statistic. |
parameter |
the degrees of freedom of the approximate chi-squared distribution of the test statistic. |
p.value |
the p-value of the test. |
method |
a character string indicating the type of test performed, and whether continuity correction was used. |
data.name |
a character string giving the name(s) of the data. |
Alan Agresti (1990). Categorical data analysis. New York: Wiley. Pages 350–354.
## Agresti (1990), p. 350. ## Presidential Approval Ratings. ## Approval of the President's performance in office in two surveys, ## one month apart, for a random sample of 1600 voting-age Americans. Performance <- matrix(c(794, 86, 150, 570), nrow = 2, dimnames = list("1st Survey" = c("Approve", "Disapprove"), "2nd Survey" = c("Approve", "Disapprove"))) Performance mcnemar.test(Performance) ## => significant change (in fact, drop) in approval ratings
## Agresti (1990), p. 350. ## Presidential Approval Ratings. ## Approval of the President's performance in office in two surveys, ## one month apart, for a random sample of 1600 voting-age Americans. Performance <- matrix(c(794, 86, 150, 570), nrow = 2, dimnames = list("1st Survey" = c("Approve", "Disapprove"), "2nd Survey" = c("Approve", "Disapprove"))) Performance mcnemar.test(Performance) ## => significant change (in fact, drop) in approval ratings
Compute the sample median.
median(x, na.rm = FALSE, ...) ## Default S3 method: median(x, na.rm = FALSE, ...)
median(x, na.rm = FALSE, ...) ## Default S3 method: median(x, na.rm = FALSE, ...)
x |
an object for which a method has been defined, or a numeric vector containing the values whose median is to be computed. |
na.rm |
a logical value indicating whether |
... |
potentially further arguments for methods; not used in the default method. |
This is a generic function for which methods can be written. However,
the default method makes use of is.na
, sort
and
mean
from package base all of which are generic, and so
the default method will work for most classes
(e.g., "Date"
) for which a median is a reasonable
concept.
The default method returns a length-one object of the same type as
x
, except when x
is logical or integer of even length,
when the result will be double.
If there are no values or if na.rm = FALSE
and there are NA
values the result is NA
of the same type as x
(or more
generally the result of x[NA_integer_]
).
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
quantile
for general quantiles.
median(1:4) # = 2.5 [even number] median(c(1:3, 100, 1000)) # = 3 [odd, robust]
median(1:4) # = 2.5 [even number] median(c(1:3, 100, 1000)) # = 3 [odd, robust]
Fits an additive model (two-way decomposition) using Tukey's median polish procedure.
medpolish(x, eps = 0.01, maxiter = 10, trace.iter = TRUE, na.rm = FALSE)
medpolish(x, eps = 0.01, maxiter = 10, trace.iter = TRUE, na.rm = FALSE)
x |
a numeric matrix. |
eps |
real number greater than 0. A tolerance for convergence: see ‘Details’. |
maxiter |
the maximum number of iterations |
trace.iter |
logical. Should progress in convergence be reported? |
na.rm |
logical. Should missing values be removed? |
The model fitted is additive (constant + rows + columns). The
algorithm works by alternately removing the row and column medians,
and continues until the proportional reduction in the sum
of absolute residuals is less than eps
or until there have been maxiter
iterations.
The sum of absolute residuals is printed at
each iteration of the fitting process, if trace.iter
is TRUE
.
If na.rm
is FALSE
the presence of any NA
value in
x
will cause an error, otherwise NA
values are ignored.
medpolish
returns an object of class medpolish
(see below).
There are printing and plotting methods for this
class, which are invoked via by the generics
print
and plot
.
An object of class medpolish
with the following named components:
overall |
the fitted constant term. |
row |
the fitted row effects. |
col |
the fitted column effects. |
residuals |
the residuals. |
name |
the name of the dataset. |
Tukey, J. W. (1977). Exploratory Data Analysis, Reading Massachusetts: Addison-Wesley.
median
; aov
for a mean
instead of median decomposition.
require(graphics) ## Deaths from sport parachuting; from ABC of EDA, p.224: deaths <- rbind(c(14,15,14), c( 7, 4, 7), c( 8, 2,10), c(15, 9,10), c( 0, 2, 0)) dimnames(deaths) <- list(c("1-24", "25-74", "75-199", "200++", "NA"), paste(1973:1975)) deaths (med.d <- medpolish(deaths)) plot(med.d) ## Check decomposition: all(deaths == med.d$overall + outer(med.d$row,med.d$col, `+`) + med.d$residuals)
require(graphics) ## Deaths from sport parachuting; from ABC of EDA, p.224: deaths <- rbind(c(14,15,14), c( 7, 4, 7), c( 8, 2,10), c(15, 9,10), c( 0, 2, 0)) dimnames(deaths) <- list(c("1-24", "25-74", "75-199", "200++", "NA"), paste(1973:1975)) deaths (med.d <- medpolish(deaths)) plot(med.d) ## Check decomposition: all(deaths == med.d$overall + outer(med.d$row,med.d$col, `+`) + med.d$residuals)
Returns the response, offset, subset, weights or other
special components of a model frame passed as optional arguments to
model.frame
.
model.extract(frame, component) model.offset(x) model.response(data, type = "any") model.weights(x)
model.extract(frame, component) model.offset(x) model.response(data, type = "any") model.weights(x)
frame , x , data
|
a model frame, see |
component |
literal character string or name. The name of a
component to extract, such as |
type |
One of |
model.extract
is provided for compatibility with S, which does
not have the more specific functions. It is also useful to extract
e.g. the etastart
and mustart
components of a
glm
fit.
model.extract(m, "offset")
and model.extract(m, "response")
are equivalent to model.offset(m)
and model.response(m)
respectively. model.offset
sums any terms specified by
offset
terms in the formula or by offset
arguments
in the call producing the model frame: it does check that the offset
is numeric.
model.weights
is slightly different from
model.extract(, "weights")
in not naming the vector it returns.
The specified component of the model frame, usually a vector.
model.response()
now drops a possible "Asis"
class
(stemming from I(.)
).
model.offset
returns NULL
if no offset was specified.
a <- model.frame(cbind(ncases,ncontrols) ~ agegp + tobgp + alcgp, data = esoph) model.extract(a, "response") stopifnot(model.extract(a, "response") == model.response(a)) a <- model.frame(ncases/(ncases+ncontrols) ~ agegp + tobgp + alcgp, data = esoph, weights = ncases+ncontrols) model.response(a) (mw <- model.extract(a, "weights")) stopifnot(identical(unname(mw), model.weights(a))) a <- model.frame(cbind(ncases,ncontrols) ~ agegp, something = tobgp, data = esoph) names(a) stopifnot(model.extract(a, "something") == esoph$tobgp)
a <- model.frame(cbind(ncases,ncontrols) ~ agegp + tobgp + alcgp, data = esoph) model.extract(a, "response") stopifnot(model.extract(a, "response") == model.response(a)) a <- model.frame(ncases/(ncases+ncontrols) ~ agegp + tobgp + alcgp, data = esoph, weights = ncases+ncontrols) model.response(a) (mw <- model.extract(a, "weights")) stopifnot(identical(unname(mw), model.weights(a))) a <- model.frame(cbind(ncases,ncontrols) ~ agegp, something = tobgp, data = esoph) names(a) stopifnot(model.extract(a, "something") == esoph$tobgp)
model.frame
(a generic function) and its methods return a
data.frame
with the variables needed to use
formula
and any ...
arguments.
model.frame(formula, ...) ## Default S3 method: model.frame(formula, data = NULL, subset = NULL, na.action, drop.unused.levels = FALSE, xlev = NULL, ...) ## S3 method for class 'aovlist' model.frame(formula, data = NULL, ...) ## S3 method for class 'glm' model.frame(formula, ...) ## S3 method for class 'lm' model.frame(formula, ...) get_all_vars(formula, data, ...)
model.frame(formula, ...) ## Default S3 method: model.frame(formula, data = NULL, subset = NULL, na.action, drop.unused.levels = FALSE, xlev = NULL, ...) ## S3 method for class 'aovlist' model.frame(formula, data = NULL, ...) ## S3 method for class 'glm' model.frame(formula, ...) ## S3 method for class 'lm' model.frame(formula, ...) get_all_vars(formula, data, ...)
formula |
|
data |
a data frame, list or environment (or object
coercible by |
subset |
a specification of the rows/observations to be used:
defaults to all. This can be any valid indexing vector (see
|
na.action |
an optional (name of a) function for treating missing
values ( |
drop.unused.levels |
should factors have unused levels dropped?
Defaults to |
xlev |
a named list of character vectors giving the full set of levels to be assumed for each factor. |
... |
for For |
Exactly what happens depends on the class and attributes of the object
formula
. If this is an object of fitted-model class such as
"lm"
, the method will either return the saved model frame
used when fitting the model (if any, often selected by argument
model = TRUE
) or pass the call used when fitting on to the
default method. The default method itself can cope with rather
standard model objects such as those of class
"lqs"
from package MASS if no other
arguments are supplied.
The rest of this section applies only to the default method.
If either formula
or data
is already a model frame (a
data frame with a "terms"
attribute) and the other is missing,
the model frame is returned. Unless formula
is a terms object,
as.formula
and then terms
is called on it. (If you wish
to use the keep.order
argument of terms.formula
, pass a
terms object rather than a formula.)
Row names for the model frame are taken from the data
argument
if present, then from the names of the response in the formula (or
rownames if it is a matrix), if there is one.
All the variables in formula
, subset
and in ...
are looked for first in data
and then in the environment of
formula
(see the help for formula()
for further
details) and collected into a data frame. Then the subset
expression is evaluated, and it is used as a row index to the data
frame. Then the na.action
function is applied to the data frame
(and may well add attributes). The levels of any factors in the data
frame are adjusted according to the drop.unused.levels
and
xlev
arguments: if xlev
specifies a factor and a
character variable is found, it is converted to a factor (as from R
2.10.0).
Because variables in the formula are evaluated before rows are dropped
based on subset
, the characteristics of data-dependent bases such
as orthogonal polynomials (i.e. from terms using poly
) or
splines will be computed based on the full data set rather than the
subsetted one.
Unless na.action = NULL
, time-series attributes will be removed
from the variables found (since they will be wrong if NA
s are
removed).
Note that all the variables in the formula are included in the
data frame, even those preceded by -
.
Only variables whose type is raw, logical, integer, real, complex or character can be included in a model frame: this includes classed variables such as factors (whose underlying type is integer), but excludes lists.
get_all_vars
returns a data.frame
containing the
variables used in formula
plus those specified in ...
which are recycled to the number of data frame rows.
Unlike model.frame.default
, it returns the input variables and
not those resulting from function calls in formula
.
A data.frame
containing the variables used in
formula
plus those specified in ...
. It will have
additional attributes, including "terms"
for an object of class
"terms"
derived from formula
,
and possibly "na.action"
giving information on the handling of
NA
s (which will not be present if no special handling was done,
e.g. by na.pass
).
Chambers, J. M. (1992) Data for models. Chapter 3 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
model.matrix
for the ‘design matrix’,
formula
for formulas,
model.extract
to extract components, and
expand.model.frame
for model.frame manipulation.
data.class(model.frame(dist ~ speed, data = cars)) ## using a subset and an extra variable model.frame(dist ~ speed, data = cars, subset = speed < 10, z = log(dist)) ## get_all_vars(): new var.s are recycled (iff length matches: 50 = 2*25) ncars <- get_all_vars(sqrt(dist) ~ I(speed/2), data = cars, newVar = 2:3) stopifnot(is.data.frame(ncars), identical(cars, ncars[,names(cars)]), ncol(ncars) == ncol(cars) + 1)
data.class(model.frame(dist ~ speed, data = cars)) ## using a subset and an extra variable model.frame(dist ~ speed, data = cars, subset = speed < 10, z = log(dist)) ## get_all_vars(): new var.s are recycled (iff length matches: 50 = 2*25) ncars <- get_all_vars(sqrt(dist) ~ I(speed/2), data = cars, newVar = 2:3) stopifnot(is.data.frame(ncars), identical(cars, ncars[,names(cars)]), ncol(ncars) == ncol(cars) + 1)
model.matrix
creates a design (or model) matrix, e.g., by
expanding factors to a set of dummy variables (depending on the
contrasts) and expanding interactions similarly.
model.matrix(object, ...) ## Default S3 method: model.matrix(object, data = environment(object), contrasts.arg = NULL, xlev = NULL, ...) ## S3 method for class 'lm' model.matrix(object, ...)
model.matrix(object, ...) ## Default S3 method: model.matrix(object, data = environment(object), contrasts.arg = NULL, xlev = NULL, ...) ## S3 method for class 'lm' model.matrix(object, ...)
object |
an object of an appropriate class. For the default
method, a model formula or a |
data |
a data frame created with |
contrasts.arg |
a list, whose entries are values (numeric
matrices, |
xlev |
to be used as argument of |
... |
further arguments passed to or from other methods. |
model.matrix
creates a design matrix from the description
given in terms(object)
, using the data in data
which
must supply variables with the same names as would be created by a
call to model.frame(object)
or, more precisely, by evaluating
attr(terms(object), "variables")
. If data
is a data
frame, there may be other columns and the order of columns is not
important. Any character variables are coerced to factors. After
coercion, all the variables used on the right-hand side of the
formula must be logical, integer, numeric or factor.
If contrasts.arg
is specified for a factor it overrides the
default factor coding for that variable and any "contrasts"
attribute set by C
or contrasts
.
Whereas invalid contrasts.arg
s have been ignored always, they are
warned about since R version 3.6.0.
In an interaction term, the variable whose levels vary fastest is the
first one to appear in the formula (and not in the term), so in
~ a + b + b:a
the interaction will have a
varying
fastest.
By convention, if the response variable also appears on the right-hand side of the formula it is dropped (with a warning), although interactions involving the term are retained.
The design matrix for a regression-like model with the specified formula and data.
There is an attribute "assign"
, an integer vector with an entry
for each column in the matrix giving the term in the formula which
gave rise to the column. Value 0
corresponds to the intercept
(if any), and positive values to terms in the order given by the
term.labels
attribute of the terms
structure
corresponding to object
.
If there are any factors in terms in the model, there is an attribute
"contrasts"
, a named list with an entry for each factor. This
specifies the contrasts that would be used in terms in which the
factor is coded by contrasts (in some terms dummy coding may be used),
either as a character vector naming a function or as a numeric matrix.
Chambers, J. M. (1992) Data for models. Chapter 3 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
model.frame
, model.extract
,
terms
sparse.model.matrix
from package
Matrix for creating sparse model matrices, which may
be more efficient in large dimensions.
ff <- log(Volume) ~ log(Height) + log(Girth) utils::str(m <- model.frame(ff, trees)) mat <- model.matrix(ff, m) dd <- data.frame(a = gl(3,4), b = gl(4,1,12)) # balanced 2-way options("contrasts") # typically 'treatment' (for unordered factors) model.matrix(~ a + b, dd) model.matrix(~ a + b, dd, contrasts.arg = list(a = "contr.sum")) model.matrix(~ a + b, dd, contrasts.arg = list(a = "contr.sum", b = contr.poly)) m.orth <- model.matrix(~a+b, dd, contrasts.arg = list(a = "contr.helmert")) crossprod(m.orth) # m.orth is ALMOST orthogonal # invalid contrasts.. ignored with a warning: stopifnot(identical( model.matrix(~ a + b, dd), model.matrix(~ a + b, dd, contrasts.arg = "contr.FOO")))
ff <- log(Volume) ~ log(Height) + log(Girth) utils::str(m <- model.frame(ff, trees)) mat <- model.matrix(ff, m) dd <- data.frame(a = gl(3,4), b = gl(4,1,12)) # balanced 2-way options("contrasts") # typically 'treatment' (for unordered factors) model.matrix(~ a + b, dd) model.matrix(~ a + b, dd, contrasts.arg = list(a = "contr.sum")) model.matrix(~ a + b, dd, contrasts.arg = list(a = "contr.sum", b = contr.poly)) m.orth <- model.matrix(~a+b, dd, contrasts.arg = list(a = "contr.helmert")) crossprod(m.orth) # m.orth is ALMOST orthogonal # invalid contrasts.. ignored with a warning: stopifnot(identical( model.matrix(~ a + b, dd), model.matrix(~ a + b, dd, contrasts.arg = "contr.FOO")))
aov
Model FitComputes summary tables for model fits, especially complex aov
fits.
model.tables(x, ...) ## S3 method for class 'aov' model.tables(x, type = "effects", se = FALSE, cterms, ...) ## S3 method for class 'aovlist' model.tables(x, type = "effects", se = FALSE, ...)
model.tables(x, ...) ## S3 method for class 'aov' model.tables(x, type = "effects", se = FALSE, cterms, ...) ## S3 method for class 'aovlist' model.tables(x, type = "effects", se = FALSE, ...)
x |
a model object, usually produced by |
type |
type of table: currently only |
se |
should standard errors be computed? |
cterms |
A character vector giving the names of the terms for which tables should be computed. The default is all tables. |
... |
further arguments passed to or from other methods. |
For type = "effects"
give tables of the coefficients for each
term, optionally with standard errors.
For type = "means"
give tables of the mean response for each
combinations of levels of the factors in a term.
The "aov"
method cannot be applied to components of a
"aovlist"
fit.
An object of class "tables.aov"
, as list which may contain components
tables |
A list of tables for each requested term. |
n |
The replication information for each term. |
se |
Standard error information. |
The implementation is incomplete, and only the simpler cases have been tested thoroughly.
Weighted aov
fits are not supported.
aov
, proj
,
replications
, TukeyHSD
,
se.contrast
options(contrasts = c("contr.helmert", "contr.treatment")) npk.aov <- aov(yield ~ block + N*P*K, npk) model.tables(npk.aov, "means", se = TRUE) ## as a test, not particularly sensible statistically npk.aovE <- aov(yield ~ N*P*K + Error(block), npk) model.tables(npk.aovE, se = TRUE) model.tables(npk.aovE, "means")
options(contrasts = c("contr.helmert", "contr.treatment")) npk.aov <- aov(yield ~ block + N*P*K, npk) model.tables(npk.aov, "means", se = TRUE) ## as a test, not particularly sensible statistically npk.aovE <- aov(yield ~ N*P*K + Error(block), npk) model.tables(npk.aovE, se = TRUE) model.tables(npk.aovE, "means")
These functions plot seasonal (or other) subseries of a time series. For each season (or other category), a time series is plotted.
monthplot(x, ...) ## S3 method for class 'stl' monthplot(x, labels = NULL, ylab = choice, choice = "seasonal", ...) ## S3 method for class 'StructTS' monthplot(x, labels = NULL, ylab = choice, choice = "sea", ...) ## S3 method for class 'ts' monthplot(x, labels = NULL, times = time(x), phase = cycle(x), ylab = deparse1(substitute(x)), ...) ## Default S3 method: monthplot(x, labels = 1L:12L, ylab = deparse1(substitute(x)), times = seq_along(x), phase = (times - 1L)%%length(labels) + 1L, base = mean, axes = TRUE, type = c("l", "h"), box = TRUE, add = FALSE, col = par("col"), lty = par("lty"), lwd = par("lwd"), col.base = col, lty.base = lty, lwd.base = lwd, ...)
monthplot(x, ...) ## S3 method for class 'stl' monthplot(x, labels = NULL, ylab = choice, choice = "seasonal", ...) ## S3 method for class 'StructTS' monthplot(x, labels = NULL, ylab = choice, choice = "sea", ...) ## S3 method for class 'ts' monthplot(x, labels = NULL, times = time(x), phase = cycle(x), ylab = deparse1(substitute(x)), ...) ## Default S3 method: monthplot(x, labels = 1L:12L, ylab = deparse1(substitute(x)), times = seq_along(x), phase = (times - 1L)%%length(labels) + 1L, base = mean, axes = TRUE, type = c("l", "h"), box = TRUE, add = FALSE, col = par("col"), lty = par("lty"), lwd = par("lwd"), col.base = col, lty.base = lty, lwd.base = lwd, ...)
x |
Time series or related object. |
labels |
Labels to use for each ‘season’. |
ylab |
y label. |
times |
Time of each observation. |
phase |
Indicator for each ‘season’. |
base |
Function to use for reference line for subseries. |
choice |
Which series of an |
... |
Arguments to be passed to the default method or graphical parameters. |
axes |
Should axes be drawn (ignored if |
type |
Type of plot. The default is to join the points with
lines, and |
box |
Should a box be drawn (ignored if |
add |
Should thus just add on an existing plot. |
col , lty , lwd
|
Graphics parameters for the series. |
col.base , lty.base , lwd.base
|
Graphics parameters for the segments used for the reference lines. |
These functions extract subseries from a time series and plot them
all in one frame. The ts
, stl
, and
StructTS
methods use the internally recorded frequency and
start and finish times to set the scale and the seasons. The default
method assumes observations come in groups of 12 (though this can be
changed).
If the labels
are not given but the phase
is given, then
the labels
default to the unique values of the phase
. If
both are given, then the phase
values are assumed to be indices
into the labels
array, i.e., they should be in the range
from 1 to length(labels)
.
These functions are executed for their side effect of drawing a seasonal subseries plot on the current graphical window.
Duncan Murdoch
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
require(graphics) ## The CO2 data fit <- stl(log(co2), s.window = 20, t.window = 20) plot(fit) op <- par(mfrow = c(2,2)) monthplot(co2, ylab = "data", cex.axis = 0.8) monthplot(fit, choice = "seasonal", cex.axis = 0.8) monthplot(fit, choice = "trend", cex.axis = 0.8) monthplot(fit, choice = "remainder", type = "h", cex.axis = 0.8) par(op) ## The CO2 data, grouped quarterly quarter <- (cycle(co2) - 1) %/% 3 monthplot(co2, phase = quarter) ## see also JohnsonJohnson
require(graphics) ## The CO2 data fit <- stl(log(co2), s.window = 20, t.window = 20) plot(fit) op <- par(mfrow = c(2,2)) monthplot(co2, ylab = "data", cex.axis = 0.8) monthplot(fit, choice = "seasonal", cex.axis = 0.8) monthplot(fit, choice = "trend", cex.axis = 0.8) monthplot(fit, choice = "remainder", type = "h", cex.axis = 0.8) par(op) ## The CO2 data, grouped quarterly quarter <- (cycle(co2) - 1) %/% 3 monthplot(co2, phase = quarter) ## see also JohnsonJohnson
Performs Mood's two-sample test for a difference in scale parameters.
mood.test(x, ...) ## Default S3 method: mood.test(x, y, alternative = c("two.sided", "less", "greater"), ...) ## S3 method for class 'formula' mood.test(formula, data, subset, na.action, ...)
mood.test(x, ...) ## Default S3 method: mood.test(x, y, alternative = c("two.sided", "less", "greater"), ...) ## S3 method for class 'formula' mood.test(formula, data, subset, na.action, ...)
x , y
|
numeric vectors of data values. |
alternative |
indicates the alternative hypothesis and must be
one of |
formula |
a formula of the form |
data |
an optional matrix or data frame (or similar: see
|
subset |
an optional vector specifying a subset of observations to be used. |
na.action |
a function which indicates what should happen when
the data contain |
... |
further arguments to be passed to or from methods. |
The underlying model is that the two samples are drawn from
and
, respectively, where
is a
common location parameter and
is a scale parameter.
The null hypothesis is .
There are more useful tests for this problem.
In the case of ties, the formulation of Mielke (1967) is employed.
A list with class "htest"
containing the following components:
statistic |
the value of the test statistic. |
p.value |
the p-value of the test. |
alternative |
a character string describing the alternative hypothesis. You can specify just the initial letter. |
method |
the character string |
data.name |
a character string giving the names of the data. |
William J. Conover (1971), Practical nonparametric statistics. New York: John Wiley & Sons. Pages 234f.
Paul W. Mielke, Jr. (1967). Note on some squared rank tests with existing ties. Technometrics, 9/2, 312–314. doi:10.2307/1266427.
fligner.test
for a rank-based (nonparametric) k-sample
test for homogeneity of variances;
ansari.test
for another rank-based two-sample test for a
difference in scale parameters;
var.test
and bartlett.test
for parametric
tests for the homogeneity in variance.
## Same data as for the Ansari-Bradley test: ## Serum iron determination using Hyland control sera ramsay <- c(111, 107, 100, 99, 102, 106, 109, 108, 104, 99, 101, 96, 97, 102, 107, 113, 116, 113, 110, 98) jung.parekh <- c(107, 108, 106, 98, 105, 103, 110, 105, 104, 100, 96, 108, 103, 104, 114, 114, 113, 108, 106, 99) mood.test(ramsay, jung.parekh) ## Compare this to ansari.test(ramsay, jung.parekh)
## Same data as for the Ansari-Bradley test: ## Serum iron determination using Hyland control sera ramsay <- c(111, 107, 100, 99, 102, 106, 109, 108, 104, 99, 101, 96, 97, 102, 107, 113, 116, 113, 110, 98) jung.parekh <- c(107, 108, 106, 98, 105, 103, 110, 105, 104, 100, 96, 108, 103, 104, 114, 114, 113, 108, 106, 99) mood.test(ramsay, jung.parekh) ## Compare this to ansari.test(ramsay, jung.parekh)
Generate multinomially distributed random number vectors and compute multinomial probabilities.
rmultinom(n, size, prob) dmultinom(x, size = NULL, prob, log = FALSE)
rmultinom(n, size, prob) dmultinom(x, size = NULL, prob, log = FALSE)
x |
vector of length |
n |
number of random vectors to draw. |
size |
integer, say |
prob |
numeric non-negative vector of length |
log |
logical; if TRUE, log probabilities are computed. |
If x
is a -component vector,
dmultinom(x, prob)
is the probability
where is the ‘multinomial coefficient’
and
.
By definition, each component is binomially distributed as
Bin(size, prob[j])
for .
The rmultinom()
algorithm draws binomials from
sequentially, where
(N :=
size
),
(
is
prob
scaled to sum 1),
and for , recursively,
and
.
For rmultinom()
,
an integer matrix where each column is a
random vector generated according to the desired multinomial law, and
hence summing to
size
. Whereas the transposed result
would seem more natural at first, the returned matrix is more
efficient because of columnwise storage.
dmultinom
is currently not vectorized at all and has
no C interface (API); this may be amended in the future.
Distributions for standard distributions, including
dbinom
which is a special case conceptually.
rmultinom(10, size = 12, prob = c(0.1,0.2,0.8)) pr <- c(1,3,6,10) # normalization not necessary for generation rmultinom(10, 20, prob = pr) ## all possible outcomes of Multinom(N = 3, K = 3) X <- t(as.matrix(expand.grid(0:3, 0:3))); X <- X[, colSums(X) <= 3] X <- rbind(X, 3:3 - colSums(X)); dimnames(X) <- list(letters[1:3], NULL) X round(apply(X, 2, function(x) dmultinom(x, prob = c(1,2,5))), 3)
rmultinom(10, size = 12, prob = c(0.1,0.2,0.8)) pr <- c(1,3,6,10) # normalization not necessary for generation rmultinom(10, 20, prob = pr) ## all possible outcomes of Multinom(N = 3, K = 3) X <- t(as.matrix(expand.grid(0:3, 0:3))); X <- X[, colSums(X) <= 3] X <- rbind(X, 3:3 - colSums(X)); dimnames(X) <- list(letters[1:3], NULL) X round(apply(X, 2, function(x) dmultinom(x, prob = c(1,2,5))), 3)
Extract information on the NA action used to create an object.
na.action(object, ...)
na.action(object, ...)
object |
any object whose |
... |
further arguments special methods could require. |
na.action
is a generic function, and na.action.default
its
default method. The latter extracts the "na.action"
component
of a list if present, otherwise the "na.action"
attribute.
When model.frame
is called, it records any information
on NA
handling in a "na.action"
attribute. Most
model-fitting functions return this as a component of their result.
Information from the action which was applied to object
if
NA
s were handled specially, or NULL
.
Chambers, J. M. and Hastie, T. J. (1992) Statistical Models in S. Wadsworth & Brooks/Cole.
options("na.action")
, na.omit
,
na.fail
, also for na.exclude
, na.pass
.
na.action(na.omit(c(1, NA)))
na.action(na.omit(c(1, NA)))
Find the longest consecutive stretch of non-missing values in a time series object. (In the event of a tie, the first such stretch.)
na.contiguous(object, ...)
na.contiguous(object, ...)
object |
a univariate or multivariate time series. |
... |
further arguments passed to or from other methods. |
A time series without missing values. The class of object
will
be preserved.
na.omit
and na.omit.ts
;
na.fail
na.contiguous(presidents)
na.contiguous(presidents)
These generic functions are useful for dealing with NA
s
in e.g., data frames.
na.fail
returns the object if it does not contain any
missing values, and signals an error otherwise.
na.omit
returns the object with incomplete cases removed.
na.pass
returns the object unchanged.
na.fail(object, ...) na.omit(object, ...) na.exclude(object, ...) na.pass(object, ...)
na.fail(object, ...) na.omit(object, ...) na.exclude(object, ...) na.pass(object, ...)
object |
an R object, typically a data frame |
... |
further arguments special methods could require. |
At present these will handle vectors, matrices and data frames comprising vectors and matrices (only).
If na.omit
removes cases, the row numbers of the cases form the
"na.action"
attribute of the result, of class "omit"
.
na.exclude
differs from na.omit
only in the class of the
"na.action"
attribute of the result, which is
"exclude"
. This gives different behaviour in functions making
use of naresid
and napredict
: when
na.exclude
is used the residuals and predictions are padded to
the correct length by inserting NA
s for cases omitted by
na.exclude
.
Chambers, J. M. and Hastie, T. J. (1992) Statistical Models in S. Wadsworth & Brooks/Cole.
na.action
;
options
with argument na.action
for setting NA actions;
and lm
and glm
for functions using these.
na.contiguous
as alternative for time series.
DF <- data.frame(x = c(1, 2, 3), y = c(0, 10, NA)) na.omit(DF) m <- as.matrix(DF) na.omit(m) stopifnot(all(na.omit(1:3) == 1:3)) # does not affect objects with no NA's try(na.fail(DF)) #> Error: missing values in ... options("na.action")
DF <- data.frame(x = c(1, 2, 3), y = c(0, 10, NA)) na.omit(DF) m <- as.matrix(DF) na.omit(m) stopifnot(all(na.omit(1:3) == 1:3)) # does not affect objects with no NA's try(na.fail(DF)) #> Error: missing values in ... options("na.action")
Use missing value information to report the effects of an na.action
.
naprint(x, ...)
naprint(x, ...)
x |
An object produced by an |
... |
further arguments passed to or from other methods. |
This is a generic function, and the exact information differs by
method. naprint.omit
reports the number of rows omitted:
naprint.default
reports an empty string.
A character string providing information on missing values, for example the number.
Use missing value information to adjust residuals and predictions.
naresid(omit, x, ...) napredict(omit, x, ...)
naresid(omit, x, ...) napredict(omit, x, ...)
omit |
an object produced by an |
x |
a vector, data frame, or matrix to be adjusted based upon the missing value information. |
... |
further arguments passed to or from other methods. |
These are utility functions used to allow predict
,
fitted
and residuals
methods for modelling
functions to compensate for the removal of NA
s in the fitting
process. They are used by the default, "lm"
, "glm"
and
"nls"
methods, and by further methods in packages MASS,
rpart and survival. Also used for the scores returned by
factanal
, prcomp
and princomp
.
The default methods do nothing. The default method for the na.exclude
action is to pad the object with NA
s in the correct positions to
have the same number of rows as the original data frame.
Currently naresid
and napredict
are identical, but
future methods need not be. naresid
is used for residuals, and
napredict
for fitted values, predictions and weights
.
These return a similar object to x
.
In the early 2000s, packages rpart and survival5 contained
versions of these functions that had an na.omit
action
equivalent to that now used for na.exclude
.
Density, distribution function, quantile function and random
generation for the negative binomial distribution with parameters
size
and prob
.
dnbinom(x, size, prob, mu, log = FALSE) pnbinom(q, size, prob, mu, lower.tail = TRUE, log.p = FALSE) qnbinom(p, size, prob, mu, lower.tail = TRUE, log.p = FALSE) rnbinom(n, size, prob, mu)
dnbinom(x, size, prob, mu, log = FALSE) pnbinom(q, size, prob, mu, lower.tail = TRUE, log.p = FALSE) qnbinom(p, size, prob, mu, lower.tail = TRUE, log.p = FALSE) rnbinom(n, size, prob, mu)
x |
vector of (non-negative integer) quantiles. |
q |
vector of quantiles. |
p |
vector of probabilities. |
n |
number of observations. If |
size |
target for number of successful trials, or dispersion parameter (the shape parameter of the gamma mixing distribution). Must be strictly positive, need not be integer. |
prob |
probability of success in each trial. |
mu |
alternative parametrization via mean: see ‘Details’. |
log , log.p
|
logical; if TRUE, probabilities p are given as log(p). |
lower.tail |
logical; if TRUE (default), probabilities are
|
The negative binomial distribution with size
and
prob
has density
for ,
and
.
This represents the number of failures which occur in a sequence of
Bernoulli trials before a target number of successes is reached.
The mean is and variance
.
A negative binomial distribution can also arise as a mixture of
Poisson distributions with mean distributed as a gamma distribution
(see pgamma
) with scale parameter (1 - prob)/prob
and shape parameter size
. (This definition allows non-integer
values of size
.)
An alternative parametrization (often used in ecology) is by the
mean mu
(see above), and size
, the dispersion
parameter, where prob
= size/(size+mu)
. The variance
is mu + mu^2/size
in this parametrization.
If an element of x
is not integer, the result of dnbinom
is zero, with a warning.
The case size == 0
is the distribution concentrated at zero.
This is the limiting distribution for size
approaching zero,
even if mu
rather than prob
is held constant. Notice
though, that the mean of the limit distribution is 0, whatever the
value of mu
.
The quantile is defined as the smallest value such that
, where
is the distribution function.
dnbinom
gives the density,
pnbinom
gives the distribution function,
qnbinom
gives the quantile function, and
rnbinom
generates random deviates.
Invalid size
or prob
will result in return value
NaN
, with a warning.
The length of the result is determined by n
for
rnbinom
, and is the maximum of the lengths of the
numerical arguments for the other functions.
The numerical arguments other than n
are recycled to the
length of the result. Only the first elements of the logical
arguments are used.
rnbinom
returns a vector of type integer unless generated
values exceed the maximum representable integer when double
values are returned.
dnbinom
computes via binomial probabilities, using code
contributed by Catherine Loader (see dbinom
).
pnbinom
uses pbeta
.
qnbinom
uses the Cornish–Fisher Expansion to include a skewness
correction to a normal approximation, followed by a search.
rnbinom
uses the derivation as a gamma mixture of Poisson
distributions, see
Devroye, L. (1986) Non-Uniform Random Variate Generation. Springer-Verlag, New York. Page 480.
Distributions for standard distributions, including
dbinom
for the binomial, dpois
for the
Poisson and dgeom
for the geometric distribution, which
is a special case of the negative binomial.
require(graphics) x <- 0:11 dnbinom(x, size = 1, prob = 1/2) * 2^(1 + x) # == 1 126 / dnbinom(0:8, size = 2, prob = 1/2) #- theoretically integer ## Cumulative ('p') = Sum of discrete prob.s ('d'); Relative error : summary(1 - cumsum(dnbinom(x, size = 2, prob = 1/2)) / pnbinom(x, size = 2, prob = 1/2)) x <- 0:15 size <- (1:20)/4 persp(x, size, dnb <- outer(x, size, function(x,s) dnbinom(x, s, prob = 0.4)), xlab = "x", ylab = "s", zlab = "density", theta = 150) title(tit <- "negative binomial density(x,s, pr = 0.4) vs. x & s") image (x, size, log10(dnb), main = paste("log [", tit, "]")) contour(x, size, log10(dnb), add = TRUE) ## Alternative parametrization x1 <- rnbinom(500, mu = 4, size = 1) x2 <- rnbinom(500, mu = 4, size = 10) x3 <- rnbinom(500, mu = 4, size = 100) h1 <- hist(x1, breaks = 20, plot = FALSE) h2 <- hist(x2, breaks = h1$breaks, plot = FALSE) h3 <- hist(x3, breaks = h1$breaks, plot = FALSE) barplot(rbind(h1$counts, h2$counts, h3$counts), beside = TRUE, col = c("red","blue","cyan"), names.arg = round(h1$breaks[-length(h1$breaks)]))
require(graphics) x <- 0:11 dnbinom(x, size = 1, prob = 1/2) * 2^(1 + x) # == 1 126 / dnbinom(0:8, size = 2, prob = 1/2) #- theoretically integer ## Cumulative ('p') = Sum of discrete prob.s ('d'); Relative error : summary(1 - cumsum(dnbinom(x, size = 2, prob = 1/2)) / pnbinom(x, size = 2, prob = 1/2)) x <- 0:15 size <- (1:20)/4 persp(x, size, dnb <- outer(x, size, function(x,s) dnbinom(x, s, prob = 0.4)), xlab = "x", ylab = "s", zlab = "density", theta = 150) title(tit <- "negative binomial density(x,s, pr = 0.4) vs. x & s") image (x, size, log10(dnb), main = paste("log [", tit, "]")) contour(x, size, log10(dnb), add = TRUE) ## Alternative parametrization x1 <- rnbinom(500, mu = 4, size = 1) x2 <- rnbinom(500, mu = 4, size = 10) x3 <- rnbinom(500, mu = 4, size = 100) h1 <- hist(x1, breaks = 20, plot = FALSE) h2 <- hist(x2, breaks = h1$breaks, plot = FALSE) h3 <- hist(x3, breaks = h1$breaks, plot = FALSE) barplot(rbind(h1$counts, h2$counts, h3$counts), beside = TRUE, col = c("red","blue","cyan"), names.arg = round(h1$breaks[-length(h1$breaks)]))
nextn
returns the smallest integer,
greater than or equal to n
, which can be obtained
as a product of powers of the values contained in factors
.
nextn()
is intended to be used to find a suitable length
to zero-pad the argument of fft
so that the transform is computed quickly.
The default value for factors
ensures this.
nextn(n, factors = c(2,3,5))
nextn(n, factors = c(2,3,5))
n |
a vector of integer numbers (of type |
factors |
a vector of positive integer factors (at least |
a vector of the same length
as n
, of type
"integer"
when the values are small enough (determined before
computing them) and "double"
otherwise.
If the factors in factors
are not relative prime,
i.e., have themselves a common factor larger than one, the result may
be wrong in the sense that it may not be the smallest integer.
E.g., nextn(91, c(2,6))
returns 128 instead of 96 as
nextn(91, c(2,3))
returns.
When the resulting N <- nextn(..)
is larger than 2^53
, a
warning with the true 64-bit integer value is signalled, as integers
above that range may not be representable in double precision.
If you really need to deal with such large integers, it may be advisable to use package gmp.
nextn(1001) # 1024 table(nextn(599:630)) n <- 1:100 ; plot(n, nextn(n) - n, type = "o", lwd=2, cex=1/2)
nextn(1001) # 1024 table(nextn(599:630)) n <- 1:100 ; plot(n, nextn(n) - n, type = "o", lwd=2, cex=1/2)
This function carries out a minimization of the function f
using a Newton-type algorithm. See the references for details.
nlm(f, p, ..., hessian = FALSE, typsize = rep(1, length(p)), fscale = 1, print.level = 0, ndigit = 12, gradtol = 1e-6, stepmax = max(1000 * sqrt(sum((p/typsize)^2)), 1000), steptol = 1e-6, iterlim = 100, check.analyticals = TRUE)
nlm(f, p, ..., hessian = FALSE, typsize = rep(1, length(p)), fscale = 1, print.level = 0, ndigit = 12, gradtol = 1e-6, stepmax = max(1000 * sqrt(sum((p/typsize)^2)), 1000), steptol = 1e-6, iterlim = 100, check.analyticals = TRUE)
f |
the function to be minimized, returning a single numeric
value. This should be a function with first argument a vector of
the length of If the function value has an attribute called |
p |
starting parameter values for the minimization. |
... |
additional arguments to be passed to |
hessian |
if |
typsize |
an estimate of the size of each parameter at the minimum. |
fscale |
an estimate of the size of |
print.level |
this argument determines the level of printing
which is done during the minimization process. The default
value of |
ndigit |
the number of significant digits in the function |
gradtol |
a positive scalar giving the tolerance at which the
scaled gradient is considered close enough to zero to
terminate the algorithm. The scaled gradient is a
measure of the relative change in |
stepmax |
a positive scalar which gives the maximum allowable
scaled step length. |
steptol |
A positive scalar providing the minimum allowable relative step length. |
iterlim |
a positive integer specifying the maximum number of iterations to be performed before the program is terminated. |
check.analyticals |
a logical scalar specifying whether the analytic gradients and Hessians, if they are supplied, should be checked against numerical derivatives at the initial parameter values. This can help detect incorrectly formulated gradients or Hessians. |
Note that arguments after ...
must be matched exactly.
If a gradient or hessian is supplied but evaluates to the wrong mode
or length, it will be ignored if check.analyticals = TRUE
(the
default) with a warning. The hessian is not even checked unless the
gradient is present and passes the sanity checks.
The C code for the “perturbed” Cholesky, choldc()
has
had a bug in all R versions before 3.4.1.
From the three methods available in the original source, we always use method “1” which is line search.
The functions supplied should always return finite (including not
NA
and not NaN
) values: for the function value itself
non-finite values are replaced by the maximum positive value with a warning.
A list containing the following components:
minimum |
the value of the estimated minimum of |
estimate |
the point at which the minimum value of
|
gradient |
the gradient at the estimated minimum of |
hessian |
the hessian at the estimated minimum of |
code |
an integer indicating why the optimization process terminated.
|
iterations |
the number of iterations performed. |
The current code is by Saikat DebRoy and the R Core team, using a C translation of Fortran code by Richard H. Jones.
Dennis, J. E. and Schnabel, R. B. (1983). Numerical Methods for Unconstrained Optimization and Nonlinear Equations. Prentice-Hall, Englewood Cliffs, NJ.
Schnabel, R. B., Koontz, J. E. and Weiss, B. E. (1985). A modular system of algorithms for unconstrained minimization. ACM Transactions on Mathematical Software, 11, 419–440. doi:10.1145/6187.6192.
constrOptim
for constrained optimization,
optimize
for one-dimensional
minimization and uniroot
for root finding.
deriv
to calculate analytical derivatives.
For nonlinear regression, nls
may be better.
f <- function(x) sum((x-1:length(x))^2) nlm(f, c(10,10)) nlm(f, c(10,10), print.level = 2) utils::str(nlm(f, c(5), hessian = TRUE)) f <- function(x, a) sum((x-a)^2) nlm(f, c(10,10), a = c(3,5)) f <- function(x, a) { res <- sum((x-a)^2) attr(res, "gradient") <- 2*(x-a) res } nlm(f, c(10,10), a = c(3,5)) ## more examples, including the use of derivatives. ## Not run: demo(nlm)
f <- function(x) sum((x-1:length(x))^2) nlm(f, c(10,10)) nlm(f, c(10,10), print.level = 2) utils::str(nlm(f, c(5), hessian = TRUE)) f <- function(x, a) sum((x-a)^2) nlm(f, c(10,10), a = c(3,5)) f <- function(x, a) { res <- sum((x-a)^2) attr(res, "gradient") <- 2*(x-a) res } nlm(f, c(10,10), a = c(3,5)) ## more examples, including the use of derivatives. ## Not run: demo(nlm)
Unconstrained and box-constrained optimization using PORT routines.
For historical compatibility.
nlminb(start, objective, gradient = NULL, hessian = NULL, ..., scale = 1, control = list(), lower = -Inf, upper = Inf)
nlminb(start, objective, gradient = NULL, hessian = NULL, ..., scale = 1, control = list(), lower = -Inf, upper = Inf)
start |
numeric vector, initial values for the parameters to be optimized. |
objective |
Function to be minimized. Must return a scalar value. The first
argument to |
gradient |
Optional function that takes the same arguments as |
hessian |
Optional function that takes the same arguments as |
... |
Further arguments to be supplied to |
scale |
See PORT documentation (or leave alone). |
control |
A list of control parameters. See below for details. |
lower , upper
|
vectors of lower and upper bounds, replicated to be as long as
|
Any names of start
are passed on to objective
and where
applicable, gradient
and hessian
. The parameter vector
will be coerced to double.
If any of the functions returns NA
or NaN
this is an
error for the gradient and Hessian, and such values for function
evaluation are replaced by +Inf
with a warning.
A list with components:
par |
The best set of parameters found. |
objective |
The value of |
convergence |
An integer code. |
message |
A character string giving any additional information returned by the
optimizer, or |
iterations |
Number of iterations performed. |
evaluations |
Number of objective function and gradient function evaluations |
Possible names in the control
list and their default values
are:
eval.max
Maximum number of evaluations of the objective function allowed. Defaults to 200.
iter.max
Maximum number of iterations allowed. Defaults to 150.
trace
The value of the objective function and the parameters is printed every trace'th iteration. Defaults to 0 which indicates no trace information is to be printed.
abs.tol
Absolute tolerance. Defaults
to 0 so the absolute convergence test is not used. If the objective
function is known to be non-negative, the previous default of
1e-20
would be more appropriate.
rel.tol
Relative tolerance. Defaults to
1e-10
.
x.tol
X tolerance. Defaults to 1.5e-8
.
xf.tol
false convergence tolerance. Defaults to
2.2e-14
.
step.min, step.max
Minimum and maximum step size. Both
default to 1.
.
singular convergence tolerance; defaults to
rel.tol
.
...
an estimated bound on the relative error in the objective function value.
R port: Douglas Bates and Deepayan Sarkar.
Underlying Fortran code by David M. Gay
David M. Gay (1990), Usage summary for selected optimization routines. Computing Science Technical Report 153, AT&T Bell Laboratories, Murray Hill.
optim
(which is preferred) and nlm
.
optimize
for one-dimensional minimization and
constrOptim
for constrained optimization.
x <- rnbinom(100, mu = 10, size = 10) hdev <- function(par) -sum(dnbinom(x, mu = par[1], size = par[2], log = TRUE)) nlminb(c(9, 12), hdev) nlminb(c(20, 20), hdev, lower = 0, upper = Inf) nlminb(c(20, 20), hdev, lower = 0.001, upper = Inf) ## slightly modified from the S-PLUS help page for nlminb # this example minimizes a sum of squares with known solution y sumsq <- function( x, y) {sum((x-y)^2)} y <- rep(1,5) x0 <- rnorm(length(y)) nlminb(start = x0, sumsq, y = y) # now use bounds with a y that has some components outside the bounds y <- c( 0, 2, 0, -2, 0) nlminb(start = x0, sumsq, lower = -1, upper = 1, y = y) # try using the gradient sumsq.g <- function(x, y) 2*(x-y) nlminb(start = x0, sumsq, sumsq.g, lower = -1, upper = 1, y = y) # now use the hessian, too sumsq.h <- function(x, y) diag(2, nrow = length(x)) nlminb(start = x0, sumsq, sumsq.g, sumsq.h, lower = -1, upper = 1, y = y) ## Rest lifted from optim help page fr <- function(x) { ## Rosenbrock Banana function x1 <- x[1] x2 <- x[2] 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 } grr <- function(x) { ## Gradient of 'fr' x1 <- x[1] x2 <- x[2] c(-400 * x1 * (x2 - x1 * x1) - 2 * (1 - x1), 200 * (x2 - x1 * x1)) } nlminb(c(-1.2,1), fr) nlminb(c(-1.2,1), fr, grr) flb <- function(x) { p <- length(x); sum(c(1, rep(4, p-1)) * (x - c(1, x[-p])^2)^2) } ## 25-dimensional box constrained ## par[24] is *not* at boundary nlminb(rep(3, 25), flb, lower = rep(2, 25), upper = rep(4, 25)) ## trying to use a too small tolerance: r <- nlminb(rep(3, 25), flb, control = list(rel.tol = 1e-16)) stopifnot(grepl("rel.tol", r$message))
x <- rnbinom(100, mu = 10, size = 10) hdev <- function(par) -sum(dnbinom(x, mu = par[1], size = par[2], log = TRUE)) nlminb(c(9, 12), hdev) nlminb(c(20, 20), hdev, lower = 0, upper = Inf) nlminb(c(20, 20), hdev, lower = 0.001, upper = Inf) ## slightly modified from the S-PLUS help page for nlminb # this example minimizes a sum of squares with known solution y sumsq <- function( x, y) {sum((x-y)^2)} y <- rep(1,5) x0 <- rnorm(length(y)) nlminb(start = x0, sumsq, y = y) # now use bounds with a y that has some components outside the bounds y <- c( 0, 2, 0, -2, 0) nlminb(start = x0, sumsq, lower = -1, upper = 1, y = y) # try using the gradient sumsq.g <- function(x, y) 2*(x-y) nlminb(start = x0, sumsq, sumsq.g, lower = -1, upper = 1, y = y) # now use the hessian, too sumsq.h <- function(x, y) diag(2, nrow = length(x)) nlminb(start = x0, sumsq, sumsq.g, sumsq.h, lower = -1, upper = 1, y = y) ## Rest lifted from optim help page fr <- function(x) { ## Rosenbrock Banana function x1 <- x[1] x2 <- x[2] 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 } grr <- function(x) { ## Gradient of 'fr' x1 <- x[1] x2 <- x[2] c(-400 * x1 * (x2 - x1 * x1) - 2 * (1 - x1), 200 * (x2 - x1 * x1)) } nlminb(c(-1.2,1), fr) nlminb(c(-1.2,1), fr, grr) flb <- function(x) { p <- length(x); sum(c(1, rep(4, p-1)) * (x - c(1, x[-p])^2)^2) } ## 25-dimensional box constrained ## par[24] is *not* at boundary nlminb(rep(3, 25), flb, lower = rep(2, 25), upper = rep(4, 25)) ## trying to use a too small tolerance: r <- nlminb(rep(3, 25), flb, control = list(rel.tol = 1e-16)) stopifnot(grepl("rel.tol", r$message))
Determine the nonlinear (weighted) least-squares estimates of the parameters of a nonlinear model.
nls(formula, data, start, control, algorithm, trace, subset, weights, na.action, model, lower, upper, ...)
nls(formula, data, start, control, algorithm, trace, subset, weights, na.action, model, lower, upper, ...)
formula |
a nonlinear model formula including variables and parameters. Will be coerced to a formula if necessary. |
data |
an optional data frame in which to evaluate the variables in
|
start |
a named list or named numeric vector of starting
estimates. When |
control |
an optional |
algorithm |
character string specifying the algorithm to use.
The default algorithm is a Gauss-Newton algorithm. Other possible
values are |
trace |
logical value indicating if a trace of the iteration
progress should be printed. Default is |
subset |
an optional vector specifying a subset of observations to be used in the fitting process. |
weights |
an optional numeric vector of (fixed) weights. When present, the objective function is weighted least squares. |
na.action |
a function which indicates what should happen
when the data contain |
model |
logical. If true, the model frame is returned as part of
the object. Default is |
lower , upper
|
vectors of lower and upper bounds, replicated to
be as long as |
... |
Additional optional arguments. None are used at present. |
An nls
object is a type of fitted model object. It has methods
for the generic functions anova
, coef
,
confint
, deviance
,
df.residual
, fitted
,
formula
, logLik
, predict
,
print
, profile
, residuals
,
summary
, vcov
and weights
.
Variables in formula
(and weights
if not missing) are
looked for first in data
, then the environment of
formula
and finally along the search path. Functions in
formula
are searched for first in the environment of
formula
and then along the search path.
Arguments subset
and na.action
are supported only when
all the variables in the formula taken from data
are of the
same length: other cases give a warning.
Note that the anova
method does not check that the
models are nested: this cannot easily be done automatically, so use
with care.
A list of
m |
an |
data |
the expression that was passed to |
call |
the matched call with several components, notably
|
na.action |
the |
dataClasses |
the |
model |
if |
weights |
if |
convInfo |
a list with convergence information. |
control |
the control |
convergence , message
|
for an To use these is deprecated, as they are available from
|
The default settings of nls
generally fail on artificial
“zero-residual” data problems.
The nls
function uses a relative-offset convergence criterion
that compares the numerical imprecision at the current parameter
estimates to the residual sum-of-squares. This performs well on data of
the form
(with
). It fails to indicate convergence on data of the form
because the criterion amounts to
comparing two components of the round-off error.
To avoid a zero-divide in computing the convergence testing value, a
positive constant scaleOffset
should be added to the denominator
sum-of-squares; it is set in control
, as in the example below;
this does not yet apply to algorithm = "port"
.
The algorithm = "port"
code appears unfinished, and does
not even check that the starting value is within the bounds.
Use with caution, especially where bounds are supplied.
Setting warnOnly = TRUE
in the control
argument (see nls.control
) returns a non-converged
object (since R version 2.5.0) which might be useful for further
convergence analysis, but not for inference.
Douglas M. Bates and Saikat DebRoy: David M. Gay for the Fortran code
used by algorithm = "port"
.
Bates, D. M. and Watts, D. G. (1988) Nonlinear Regression Analysis and Its Applications, Wiley
Bates, D. M. and Chambers, J. M. (1992) Nonlinear models. Chapter 10 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
https://netlib.org/port/ for the Port library documentation.
summary.nls
, predict.nls
,
profile.nls
.
Self starting models (with ‘automatic initial values’):
selfStart
.
require(graphics) DNase1 <- subset(DNase, Run == 1) ## using a selfStart model fm1DNase1 <- nls(density ~ SSlogis(log(conc), Asym, xmid, scal), DNase1) summary(fm1DNase1) ## the coefficients only: coef(fm1DNase1) ## including their SE, etc: coef(summary(fm1DNase1)) ## using conditional linearity fm2DNase1 <- nls(density ~ 1/(1 + exp((xmid - log(conc))/scal)), data = DNase1, start = list(xmid = 0, scal = 1), algorithm = "plinear") summary(fm2DNase1) ## without conditional linearity fm3DNase1 <- nls(density ~ Asym/(1 + exp((xmid - log(conc))/scal)), data = DNase1, start = list(Asym = 3, xmid = 0, scal = 1)) summary(fm3DNase1) ## using Port's nl2sol algorithm fm4DNase1 <- nls(density ~ Asym/(1 + exp((xmid - log(conc))/scal)), data = DNase1, start = list(Asym = 3, xmid = 0, scal = 1), algorithm = "port") summary(fm4DNase1) ## weighted nonlinear regression Treated <- Puromycin[Puromycin$state == "treated", ] weighted.MM <- function(resp, conc, Vm, K) { ## Purpose: exactly as white book p. 451 -- RHS for nls() ## Weighted version of Michaelis-Menten model ## ---------------------------------------------------------- ## Arguments: 'y', 'x' and the two parameters (see book) ## ---------------------------------------------------------- ## Author: Martin Maechler, Date: 23 Mar 2001 pred <- (Vm * conc)/(K + conc) (resp - pred) / sqrt(pred) } Pur.wt <- nls( ~ weighted.MM(rate, conc, Vm, K), data = Treated, start = list(Vm = 200, K = 0.1)) summary(Pur.wt) ## Passing arguments using a list that can not be coerced to a data.frame lisTreat <- with(Treated, list(conc1 = conc[1], conc.1 = conc[-1], rate = rate)) weighted.MM1 <- function(resp, conc1, conc.1, Vm, K) { conc <- c(conc1, conc.1) pred <- (Vm * conc)/(K + conc) (resp - pred) / sqrt(pred) } Pur.wt1 <- nls( ~ weighted.MM1(rate, conc1, conc.1, Vm, K), data = lisTreat, start = list(Vm = 200, K = 0.1)) stopifnot(all.equal(coef(Pur.wt), coef(Pur.wt1))) ## Chambers and Hastie (1992) Statistical Models in S (p. 537): ## If the value of the right side [of formula] has an attribute called ## 'gradient' this should be a matrix with the number of rows equal ## to the length of the response and one column for each parameter. weighted.MM.grad <- function(resp, conc1, conc.1, Vm, K) { conc <- c(conc1, conc.1) K.conc <- K+conc dy.dV <- conc/K.conc dy.dK <- -Vm*dy.dV/K.conc pred <- Vm*dy.dV pred.5 <- sqrt(pred) dev <- (resp - pred) / pred.5 Ddev <- -0.5*(resp+pred)/(pred.5*pred) attr(dev, "gradient") <- Ddev * cbind(Vm = dy.dV, K = dy.dK) dev } Pur.wt.grad <- nls( ~ weighted.MM.grad(rate, conc1, conc.1, Vm, K), data = lisTreat, start = list(Vm = 200, K = 0.1)) rbind(coef(Pur.wt), coef(Pur.wt1), coef(Pur.wt.grad)) ## In this example, there seems no advantage to providing the gradient. ## In other cases, there might be. ## The two examples below show that you can fit a model to ## artificial data with noise but not to artificial data ## without noise. x <- 1:10 y <- 2*x + 3 # perfect fit ## terminates in an error, because convergence cannot be confirmed: try(nls(y ~ a + b*x, start = list(a = 0.12345, b = 0.54321))) ## adjusting the convergence test by adding 'scaleOffset' to its denominator RSS: nls(y ~ a + b*x, start = list(a = 0.12345, b = 0.54321), control = list(scaleOffset = 1, printEval=TRUE)) ## Alternatively jittering the "too exact" values, slightly: set.seed(27) yeps <- y + rnorm(length(y), sd = 0.01) # added noise nls(yeps ~ a + b*x, start = list(a = 0.12345, b = 0.54321)) ## the nls() internal cheap guess for starting values can be sufficient: x <- -(1:100)/10 y <- 100 + 10 * exp(x / 2) + rnorm(x)/10 nlmod <- nls(y ~ Const + A * exp(B * x)) plot(x,y, main = "nls(*), data, true function and fit, n=100") curve(100 + 10 * exp(x / 2), col = 4, add = TRUE) lines(x, predict(nlmod), col = 2) ## Here, requiring close convergence, must use more accurate numerical differentiation, ## as this typically gives Error: "step factor .. reduced below 'minFactor' .." try(nlm1 <- update(nlmod, control = list(tol = 1e-7))) o2 <- options(digits = 10) # more accuracy for 'trace' ## central differencing works here typically (PR#18165: not converging on *some*): ctr2 <- nls.control(nDcentral=TRUE, tol = 8e-8, # <- even smaller than above warnOnly = TRUE || # << work around; e.g. needed on some ATLAS-Lapack setups (grepl("^aarch64.*linux", R.version$platform) && grepl("^NixOS", osVersion) )) (nlm2 <- update(nlmod, control = ctr2, trace = TRUE)); options(o2) ## --> convergence tolerance 4.997e-8 (in 11 iter.) ## The muscle dataset in MASS is from an experiment on muscle ## contraction on 21 animals. The observed variables are Strip ## (identifier of muscle), Conc (Cacl concentration) and Length ## (resulting length of muscle section). if(requireNamespace("MASS", quietly = TRUE)) withAutoprint({ ## The non linear model considered is ## Length = alpha + beta*exp(-Conc/theta) + error ## where theta is constant but alpha and beta may vary with Strip. with(MASS::muscle, table(Strip)) # 2, 3 or 4 obs per strip ## We first use the plinear algorithm to fit an overall model, ## ignoring that alpha and beta might vary with Strip. musc.1 <- nls(Length ~ cbind(1, exp(-Conc/th)), MASS::muscle, start = list(th = 1), algorithm = "plinear") summary(musc.1) ## Then we use nls' indexing feature for parameters in non-linear ## models to use the conventional algorithm to fit a model in which ## alpha and beta vary with Strip. The starting values are provided ## by the previously fitted model. ## Note that with indexed parameters, the starting values must be ## given in a list (with names): b <- coef(musc.1) musc.2 <- nls(Length ~ a[Strip] + b[Strip]*exp(-Conc/th), MASS::muscle, start = list(a = rep(b[2], 21), b = rep(b[3], 21), th = b[1])) summary(musc.2) })
require(graphics) DNase1 <- subset(DNase, Run == 1) ## using a selfStart model fm1DNase1 <- nls(density ~ SSlogis(log(conc), Asym, xmid, scal), DNase1) summary(fm1DNase1) ## the coefficients only: coef(fm1DNase1) ## including their SE, etc: coef(summary(fm1DNase1)) ## using conditional linearity fm2DNase1 <- nls(density ~ 1/(1 + exp((xmid - log(conc))/scal)), data = DNase1, start = list(xmid = 0, scal = 1), algorithm = "plinear") summary(fm2DNase1) ## without conditional linearity fm3DNase1 <- nls(density ~ Asym/(1 + exp((xmid - log(conc))/scal)), data = DNase1, start = list(Asym = 3, xmid = 0, scal = 1)) summary(fm3DNase1) ## using Port's nl2sol algorithm fm4DNase1 <- nls(density ~ Asym/(1 + exp((xmid - log(conc))/scal)), data = DNase1, start = list(Asym = 3, xmid = 0, scal = 1), algorithm = "port") summary(fm4DNase1) ## weighted nonlinear regression Treated <- Puromycin[Puromycin$state == "treated", ] weighted.MM <- function(resp, conc, Vm, K) { ## Purpose: exactly as white book p. 451 -- RHS for nls() ## Weighted version of Michaelis-Menten model ## ---------------------------------------------------------- ## Arguments: 'y', 'x' and the two parameters (see book) ## ---------------------------------------------------------- ## Author: Martin Maechler, Date: 23 Mar 2001 pred <- (Vm * conc)/(K + conc) (resp - pred) / sqrt(pred) } Pur.wt <- nls( ~ weighted.MM(rate, conc, Vm, K), data = Treated, start = list(Vm = 200, K = 0.1)) summary(Pur.wt) ## Passing arguments using a list that can not be coerced to a data.frame lisTreat <- with(Treated, list(conc1 = conc[1], conc.1 = conc[-1], rate = rate)) weighted.MM1 <- function(resp, conc1, conc.1, Vm, K) { conc <- c(conc1, conc.1) pred <- (Vm * conc)/(K + conc) (resp - pred) / sqrt(pred) } Pur.wt1 <- nls( ~ weighted.MM1(rate, conc1, conc.1, Vm, K), data = lisTreat, start = list(Vm = 200, K = 0.1)) stopifnot(all.equal(coef(Pur.wt), coef(Pur.wt1))) ## Chambers and Hastie (1992) Statistical Models in S (p. 537): ## If the value of the right side [of formula] has an attribute called ## 'gradient' this should be a matrix with the number of rows equal ## to the length of the response and one column for each parameter. weighted.MM.grad <- function(resp, conc1, conc.1, Vm, K) { conc <- c(conc1, conc.1) K.conc <- K+conc dy.dV <- conc/K.conc dy.dK <- -Vm*dy.dV/K.conc pred <- Vm*dy.dV pred.5 <- sqrt(pred) dev <- (resp - pred) / pred.5 Ddev <- -0.5*(resp+pred)/(pred.5*pred) attr(dev, "gradient") <- Ddev * cbind(Vm = dy.dV, K = dy.dK) dev } Pur.wt.grad <- nls( ~ weighted.MM.grad(rate, conc1, conc.1, Vm, K), data = lisTreat, start = list(Vm = 200, K = 0.1)) rbind(coef(Pur.wt), coef(Pur.wt1), coef(Pur.wt.grad)) ## In this example, there seems no advantage to providing the gradient. ## In other cases, there might be. ## The two examples below show that you can fit a model to ## artificial data with noise but not to artificial data ## without noise. x <- 1:10 y <- 2*x + 3 # perfect fit ## terminates in an error, because convergence cannot be confirmed: try(nls(y ~ a + b*x, start = list(a = 0.12345, b = 0.54321))) ## adjusting the convergence test by adding 'scaleOffset' to its denominator RSS: nls(y ~ a + b*x, start = list(a = 0.12345, b = 0.54321), control = list(scaleOffset = 1, printEval=TRUE)) ## Alternatively jittering the "too exact" values, slightly: set.seed(27) yeps <- y + rnorm(length(y), sd = 0.01) # added noise nls(yeps ~ a + b*x, start = list(a = 0.12345, b = 0.54321)) ## the nls() internal cheap guess for starting values can be sufficient: x <- -(1:100)/10 y <- 100 + 10 * exp(x / 2) + rnorm(x)/10 nlmod <- nls(y ~ Const + A * exp(B * x)) plot(x,y, main = "nls(*), data, true function and fit, n=100") curve(100 + 10 * exp(x / 2), col = 4, add = TRUE) lines(x, predict(nlmod), col = 2) ## Here, requiring close convergence, must use more accurate numerical differentiation, ## as this typically gives Error: "step factor .. reduced below 'minFactor' .." try(nlm1 <- update(nlmod, control = list(tol = 1e-7))) o2 <- options(digits = 10) # more accuracy for 'trace' ## central differencing works here typically (PR#18165: not converging on *some*): ctr2 <- nls.control(nDcentral=TRUE, tol = 8e-8, # <- even smaller than above warnOnly = TRUE || # << work around; e.g. needed on some ATLAS-Lapack setups (grepl("^aarch64.*linux", R.version$platform) && grepl("^NixOS", osVersion) )) (nlm2 <- update(nlmod, control = ctr2, trace = TRUE)); options(o2) ## --> convergence tolerance 4.997e-8 (in 11 iter.) ## The muscle dataset in MASS is from an experiment on muscle ## contraction on 21 animals. The observed variables are Strip ## (identifier of muscle), Conc (Cacl concentration) and Length ## (resulting length of muscle section). if(requireNamespace("MASS", quietly = TRUE)) withAutoprint({ ## The non linear model considered is ## Length = alpha + beta*exp(-Conc/theta) + error ## where theta is constant but alpha and beta may vary with Strip. with(MASS::muscle, table(Strip)) # 2, 3 or 4 obs per strip ## We first use the plinear algorithm to fit an overall model, ## ignoring that alpha and beta might vary with Strip. musc.1 <- nls(Length ~ cbind(1, exp(-Conc/th)), MASS::muscle, start = list(th = 1), algorithm = "plinear") summary(musc.1) ## Then we use nls' indexing feature for parameters in non-linear ## models to use the conventional algorithm to fit a model in which ## alpha and beta vary with Strip. The starting values are provided ## by the previously fitted model. ## Note that with indexed parameters, the starting values must be ## given in a list (with names): b <- coef(musc.1) musc.2 <- nls(Length ~ a[Strip] + b[Strip]*exp(-Conc/th), MASS::muscle, start = list(a = rep(b[2], 21), b = rep(b[3], 21), th = b[1])) summary(musc.2) })
nls
Allow the user to set some characteristics of the nls
nonlinear least squares algorithm.
nls.control(maxiter = 50, tol = 1e-05, minFactor = 1/1024, printEval = FALSE, warnOnly = FALSE, scaleOffset = 0, nDcentral = FALSE)
nls.control(maxiter = 50, tol = 1e-05, minFactor = 1/1024, printEval = FALSE, warnOnly = FALSE, scaleOffset = 0, nDcentral = FALSE)
maxiter |
A positive integer specifying the maximum number of iterations allowed. |
tol |
A positive numeric value specifying the tolerance level for the relative offset convergence criterion. |
minFactor |
A positive numeric value specifying the minimum step-size factor allowed on any step in the iteration. The increment is calculated with a Gauss-Newton algorithm and successively halved until the residual sum of squares has been decreased or until the step-size factor has been reduced below this limit. |
printEval |
a logical specifying whether the number of evaluations (steps in the gradient direction taken each iteration) is printed. |
warnOnly |
a logical specifying whether |
scaleOffset |
a constant to be added to the denominator of the relative
offset convergence criterion calculation to avoid a zero divide in the case
where the fit of a model to data is very close. The default value of
|
nDcentral |
only when numerical derivatives are used:
|
A list
with components
maxiter |
|
tol |
|
minFactor |
|
printEval |
|
warnOnly |
|
scaleOffset |
|
nDcentreal |
with meanings as explained under ‘Arguments’.
Douglas Bates and Saikat DebRoy; John C. Nash for part of the
scaleOffset
option.
Bates, D. M. and Watts, D. G. (1988), Nonlinear Regression Analysis and Its Applications, Wiley.
nls.control(minFactor = 1/2048)
nls.control(minFactor = 1/2048)
Fits the asymptotic regression model, in the form b0 +
b1*(1-exp(-exp(lrc) * x))
to the xy
data.
This can be used as a building block in determining starting estimates
for more complicated models.
NLSstAsymptotic(xy)
NLSstAsymptotic(xy)
xy |
a |
A numeric value of length 3 with components labelled b0
,
b1
, and lrc
. b0
is the estimated intercept on
the y
-axis, b1
is the estimated difference between the
asymptote and the y
-intercept, and lrc
is the estimated
logarithm of the rate constant.
José Pinheiro and Douglas Bates
Lob.329 <- Loblolly[ Loblolly$Seed == "329", ] print(NLSstAsymptotic(sortedXyData(expression(age), expression(height), Lob.329)), digits = 3)
Lob.329 <- Loblolly[ Loblolly$Seed == "329", ] print(NLSstAsymptotic(sortedXyData(expression(age), expression(height), Lob.329)), digits = 3)
Use inverse linear interpolation to approximate the x
value at
which the function represented by xy
is equal to yval
.
NLSstClosestX(xy, yval)
NLSstClosestX(xy, yval)
xy |
a |
yval |
a numeric value on the |
A single numeric value on the x
scale.
José Pinheiro and Douglas Bates
sortedXyData
, NLSstLfAsymptote
,
NLSstRtAsymptote
, selfStart
DNase.2 <- DNase[ DNase$Run == "2", ] DN.srt <- sortedXyData(expression(log(conc)), expression(density), DNase.2) NLSstClosestX(DN.srt, 1.0)
DNase.2 <- DNase[ DNase$Run == "2", ] DN.srt <- sortedXyData(expression(log(conc)), expression(density), DNase.2) NLSstClosestX(DN.srt, 1.0)
Provide an initial guess at the horizontal asymptote on the left side
(i.e., small values of x
) of the graph of y
versus
x
from the xy
object. Primarily used within
initial
functions for self-starting nonlinear regression
models.
NLSstLfAsymptote(xy)
NLSstLfAsymptote(xy)
xy |
a |
A single numeric value estimating the horizontal asymptote for small
x
.
José Pinheiro and Douglas Bates
sortedXyData
,
NLSstClosestX
,
NLSstRtAsymptote
,
selfStart
DNase.2 <- DNase[ DNase$Run == "2", ] DN.srt <- sortedXyData( expression(log(conc)), expression(density), DNase.2 ) NLSstLfAsymptote( DN.srt )
DNase.2 <- DNase[ DNase$Run == "2", ] DN.srt <- sortedXyData( expression(log(conc)), expression(density), DNase.2 ) NLSstLfAsymptote( DN.srt )
Provide an initial guess at the horizontal asymptote on the right side
(i.e., large values of x
) of the graph of y
versus
x
from the xy
object. Primarily used within
initial
functions for self-starting nonlinear regression
models.
NLSstRtAsymptote(xy)
NLSstRtAsymptote(xy)
xy |
a |
A single numeric value estimating the horizontal asymptote for large
x
.
José Pinheiro and Douglas Bates
sortedXyData
,
NLSstClosestX
,
NLSstRtAsymptote
,
selfStart
DNase.2 <- DNase[ DNase$Run == "2", ] DN.srt <- sortedXyData( expression(log(conc)), expression(density), DNase.2 ) NLSstRtAsymptote( DN.srt )
DNase.2 <- DNase[ DNase$Run == "2", ] DN.srt <- sortedXyData( expression(log(conc)), expression(density), DNase.2 ) NLSstRtAsymptote( DN.srt )
Extract the number of ‘observations’ from a model fit. This is
principally intended to be used in computing BIC (see AIC
).
nobs(object, ...) ## Default S3 method: nobs(object, use.fallback = FALSE, ...)
nobs(object, ...) ## Default S3 method: nobs(object, use.fallback = FALSE, ...)
object |
a fitted model object. |
use.fallback |
logical: should fallback methods be used to try to guess the value? |
... |
further arguments to be passed to methods. |
This is a generic function, with an S4 generic in package stats4.
There are methods in this package for objects of classes
"lm"
, "glm"
, "nls"
and
"logLik"
, as well as a default method (which throws an
error, unless use.fallback = TRUE
when it looks for
weights
and residuals
components – use with care!).
The main usage is in determining the appropriate penalty for BIC, but
nobs
is also used by the stepwise fitting methods
step
, add1
and drop1
as a
quick check that different fits have been fitted to the same set of
data (and not, say, that further rows have been dropped because of NAs
in the new predictors).
For lm
, glm
and nls
fits, observations with zero
weight are not included.
A single number, normally an integer. Could be NA
.
AIC
.
Density, distribution function, quantile function and random
generation for the normal distribution with mean equal to mean
and standard deviation equal to sd
.
dnorm(x, mean = 0, sd = 1, log = FALSE) pnorm(q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE) qnorm(p, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE) rnorm(n, mean = 0, sd = 1)
dnorm(x, mean = 0, sd = 1, log = FALSE) pnorm(q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE) qnorm(p, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE) rnorm(n, mean = 0, sd = 1)
x , q
|
vector of quantiles. |
p |
vector of probabilities. |
n |
number of observations. If |
mean |
vector of means. |
sd |
vector of standard deviations. |
log , log.p
|
logical; if TRUE, probabilities p are given as log(p). |
lower.tail |
logical; if TRUE (default), probabilities are
|
If mean
or sd
are not specified they assume the default
values of 0
and 1
, respectively.
The normal distribution has density
where is the mean of the distribution and
the standard deviation.
dnorm
gives the density,
pnorm
gives the distribution function,
qnorm
gives the quantile function, and
rnorm
generates random deviates.
The length of the result is determined by n
for
rnorm
, and is the maximum of the lengths of the
numerical arguments for the other functions.
The numerical arguments other than n
are recycled to the
length of the result. Only the first elements of the logical
arguments are used.
For sd = 0
this gives the limit as sd
decreases to 0, a
point mass at mu
.
sd < 0
is an error and returns NaN
.
For pnorm
, based on
Cody, W. D. (1993) Algorithm 715: SPECFUN – A portable FORTRAN package of special function routines and test drivers. ACM Transactions on Mathematical Software 19, 22–32.
For qnorm
, the code is based on a C translation of
Wichura, M. J. (1988) Algorithm AS 241: The percentage points of the normal distribution. Applied Statistics, 37, 477–484; doi:10.2307/2347330.
which provides precise results up to about 16 digits for
log.p=FALSE
. For log scale probabilities in the extreme tails,
since R version 4.1.0, extensively since 4.3.0, asymptotic expansions
are used which have been derived and explored in
Maechler, M. (2022) Asymptotic tail formulas for gaussian quantiles; DPQ vignette https://CRAN.R-project.org/package=DPQ/vignettes/qnorm-asymp.pdf.
For rnorm
, see RNG for how to select the algorithm and
for references to the supplied methods.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
Johnson, N. L., Kotz, S. and Balakrishnan, N. (1995) Continuous Univariate Distributions, volume 1, chapter 13. Wiley, New York.
Distributions for other standard distributions, including
dlnorm
for the Lognormal distribution.
require(graphics) dnorm(0) == 1/sqrt(2*pi) dnorm(1) == exp(-1/2)/sqrt(2*pi) dnorm(1) == 1/sqrt(2*pi*exp(1)) ## Using "log = TRUE" for an extended range : par(mfrow = c(2,1)) plot(function(x) dnorm(x, log = TRUE), -60, 50, main = "log { Normal density }") curve(log(dnorm(x)), add = TRUE, col = "red", lwd = 2) mtext("dnorm(x, log=TRUE)", adj = 0) mtext("log(dnorm(x))", col = "red", adj = 1) plot(function(x) pnorm(x, log.p = TRUE), -50, 10, main = "log { Normal Cumulative }") curve(log(pnorm(x)), add = TRUE, col = "red", lwd = 2) mtext("pnorm(x, log=TRUE)", adj = 0) mtext("log(pnorm(x))", col = "red", adj = 1) ## if you want the so-called 'error function' erf <- function(x) 2 * pnorm(x * sqrt(2)) - 1 ## (see Abramowitz and Stegun 29.2.29) ## and the so-called 'complementary error function' erfc <- function(x) 2 * pnorm(x * sqrt(2), lower = FALSE) ## and the inverses erfinv <- function (x) qnorm((1 + x)/2)/sqrt(2) erfcinv <- function (x) qnorm(x/2, lower = FALSE)/sqrt(2)
require(graphics) dnorm(0) == 1/sqrt(2*pi) dnorm(1) == exp(-1/2)/sqrt(2*pi) dnorm(1) == 1/sqrt(2*pi*exp(1)) ## Using "log = TRUE" for an extended range : par(mfrow = c(2,1)) plot(function(x) dnorm(x, log = TRUE), -60, 50, main = "log { Normal density }") curve(log(dnorm(x)), add = TRUE, col = "red", lwd = 2) mtext("dnorm(x, log=TRUE)", adj = 0) mtext("log(dnorm(x))", col = "red", adj = 1) plot(function(x) pnorm(x, log.p = TRUE), -50, 10, main = "log { Normal Cumulative }") curve(log(pnorm(x)), add = TRUE, col = "red", lwd = 2) mtext("pnorm(x, log=TRUE)", adj = 0) mtext("log(pnorm(x))", col = "red", adj = 1) ## if you want the so-called 'error function' erf <- function(x) 2 * pnorm(x * sqrt(2)) - 1 ## (see Abramowitz and Stegun 29.2.29) ## and the so-called 'complementary error function' erfc <- function(x) 2 * pnorm(x * sqrt(2), lower = FALSE) ## and the inverses erfinv <- function (x) qnorm((1 + x)/2)/sqrt(2) erfcinv <- function (x) qnorm(x/2, lower = FALSE)/sqrt(2)
numericDeriv
numerically evaluates the gradient of an expression.
numericDeriv(expr, theta, rho = parent.frame(), dir = 1, eps = .Machine$double.eps ^ (1/if(central) 3 else 2), central = FALSE)
numericDeriv(expr, theta, rho = parent.frame(), dir = 1, eps = .Machine$double.eps ^ (1/if(central) 3 else 2), central = FALSE)
expr |
|
theta |
|
rho |
|
dir |
numeric vector of directions, typically with values in
|
eps |
a positive number, to be used as unit step size |
central |
logical indicating if central divided differences
should be computed, i.e., |
This is a front end to the C function numeric_deriv
, which is
described in Writing R Extensions.
The numeric variables must be of type double
and not integer
.
The value of eval(expr, envir = rho)
plus a matrix
attribute "gradient"
. The columns of this matrix are
the derivatives of the value with respect to the variables listed in
theta
.
Saikat DebRoy [email protected];
tweaks and eps
, central
options by R Core Team.
myenv <- new.env() myenv$mean <- 0. myenv$sd <- 1. myenv$x <- seq(-3., 3., length.out = 31) nD <- numericDeriv(quote(pnorm(x, mean, sd)), c("mean", "sd"), myenv) str(nD) ## Visualize : require(graphics) matplot(myenv$x, cbind(c(nD), attr(nD, "gradient")), type="l") abline(h=0, lty=3) ## "gradient" is close to the true derivatives, you don't see any diff.: curve( - dnorm(x), col=2, lty=3, lwd=2, add=TRUE) curve(-x*dnorm(x), col=3, lty=3, lwd=2, add=TRUE) ## # shows 1.609e-8 on most platforms all.equal(attr(nD,"gradient"), with(myenv, cbind(-dnorm(x), -x*dnorm(x))))
myenv <- new.env() myenv$mean <- 0. myenv$sd <- 1. myenv$x <- seq(-3., 3., length.out = 31) nD <- numericDeriv(quote(pnorm(x, mean, sd)), c("mean", "sd"), myenv) str(nD) ## Visualize : require(graphics) matplot(myenv$x, cbind(c(nD), attr(nD, "gradient")), type="l") abline(h=0, lty=3) ## "gradient" is close to the true derivatives, you don't see any diff.: curve( - dnorm(x), col=2, lty=3, lwd=2, add=TRUE) curve(-x*dnorm(x), col=3, lty=3, lwd=2, add=TRUE) ## # shows 1.609e-8 on most platforms all.equal(attr(nD,"gradient"), with(myenv, cbind(-dnorm(x), -x*dnorm(x))))
An offset is a term to be added to a linear predictor, such as in a generalised linear model, with known coefficient 1 rather than an estimated coefficient.
offset(object)
offset(object)
object |
An offset to be included in a model frame |
There can be more than one offset in a model formula, but -
is
not supported for offset
terms (and is equivalent to +
).
The input value.
For examples see glm
and
Insurance
in package MASS.
Test whether two or more samples from normal distributions have the same means. The variances are not necessarily assumed to be equal.
oneway.test(formula, data, subset, na.action, var.equal = FALSE)
oneway.test(formula, data, subset, na.action, var.equal = FALSE)
formula |
a formula of the form |
data |
an optional matrix or data frame (or similar: see
|
subset |
an optional vector specifying a subset of observations to be used. |
na.action |
a function which indicates what should happen when
the data contain |
var.equal |
a logical variable indicating whether to treat the
variances in the samples as equal. If |
If the right-hand side of the formula contains more than one term, their interaction is taken to form the grouping.
A list with class "htest"
containing the following components:
statistic |
the value of the test statistic. |
parameter |
the degrees of freedom of the exact or approximate F distribution of the test statistic. |
p.value |
the p-value of the test. |
method |
a character string indicating the test performed. |
data.name |
a character string giving the names of the data. |
B. L. Welch (1951). On the comparison of several mean values: an alternative approach. Biometrika, 38, 330–336. doi:10.2307/2332579.
The standard t test (t.test
) as the special case for two
samples;
the Kruskal-Wallis test kruskal.test
for a nonparametric
test for equal location parameters in a one-way layout.
## Not assuming equal variances oneway.test(extra ~ group, data = sleep) ## Assuming equal variances oneway.test(extra ~ group, data = sleep, var.equal = TRUE) ## which gives the same result as anova(lm(extra ~ group, data = sleep))
## Not assuming equal variances oneway.test(extra ~ group, data = sleep) ## Assuming equal variances oneway.test(extra ~ group, data = sleep, var.equal = TRUE) ## which gives the same result as anova(lm(extra ~ group, data = sleep))
General-purpose optimization based on Nelder–Mead, quasi-Newton and conjugate-gradient algorithms. It includes an option for box-constrained optimization and simulated annealing.
optim(par, fn, gr = NULL, ..., method = c("Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN", "Brent"), lower = -Inf, upper = Inf, control = list(), hessian = FALSE) optimHess(par, fn, gr = NULL, ..., control = list())
optim(par, fn, gr = NULL, ..., method = c("Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN", "Brent"), lower = -Inf, upper = Inf, control = list(), hessian = FALSE) optimHess(par, fn, gr = NULL, ..., control = list())
par |
Initial values for the parameters to be optimized over. |
fn |
A function to be minimized (or maximized), with first argument the vector of parameters over which minimization is to take place. It should return a scalar result. |
gr |
A function to return the gradient for the For the |
... |
Further arguments to be passed to |
method |
The method to be used. See ‘Details’. Can be abbreviated. |
lower , upper
|
Bounds on the variables for the |
control |
a |
hessian |
Logical. Should a numerically differentiated Hessian matrix be returned? |
Note that arguments after ...
must be matched exactly.
By default optim
performs minimization, but it will maximize
if control$fnscale
is negative. optimHess
is an
auxiliary function to compute the Hessian at a later stage if
hessian = TRUE
was forgotten.
The default method is an implementation of that of Nelder and Mead (1965), that uses only function values and is robust but relatively slow. It will work reasonably well for non-differentiable functions.
Method "BFGS"
is a quasi-Newton method (also known as a variable
metric algorithm), specifically that published simultaneously in 1970
by Broyden, Fletcher, Goldfarb and Shanno.
This uses function values
and gradients to build up a picture of the surface to be optimized.
Method "CG"
is a conjugate gradients method based on that by
Fletcher and Reeves (1964) (but with the option of
Polak–Ribiere or Beale–Sorenson updates).
Conjugate gradient methods will generally
be more fragile than the BFGS method, but as they do not store a
matrix they may be successful in much larger optimization problems.
Method "L-BFGS-B"
is that of Byrd et al. (1995) which
allows box constraints, that is each variable can be given a lower
and/or upper bound. The initial value must satisfy the constraints.
This uses a limited-memory modification of the BFGS quasi-Newton
method. If non-trivial bounds are supplied, this method will be
selected, with a warning.
Nocedal and Wright (1999) is a comprehensive reference for the previous three methods.
Method "SANN"
is by default a variant of simulated annealing
given in Belisle (1992). Simulated-annealing belongs to the class of
stochastic global optimization methods. It uses only function values
but is relatively slow. It will also work for non-differentiable
functions. This implementation uses the Metropolis function for the
acceptance probability. By default the next candidate point is
generated from a Gaussian Markov kernel with scale proportional to the
actual temperature. If a function to generate a new candidate point is
given, method "SANN"
can also be used to solve combinatorial
optimization problems. Temperatures are decreased according to the
logarithmic cooling schedule as given in
Belisle (1992, p. 890);
specifically, the temperature is set to
temp / log(((t-1) %/% tmax)*tmax + exp(1))
, where t
is
the current iteration step and temp
and tmax
are
specifiable via control
, see below. Note that the
"SANN"
method depends critically on the settings of the control
parameters. It is not a general-purpose method but can be very useful
in getting to a good value on a very rough surface.
Method "Brent"
is for one-dimensional problems only, using
optimize()
. It can be useful in cases where
optim()
is used inside other functions where only method
can be specified, such as in mle
from package stats4.
Function fn
can return NA
or Inf
if the function
cannot be evaluated at the supplied value, but the initial value must
have a computable finite value of fn
.
(Except for method "L-BFGS-B"
where the values should always be
finite.)
optim
can be used recursively, and for a single parameter
as well as many. It also accepts a zero-length par
, and just
evaluates the function with that argument.
The control
argument is a list that can supply any of the
following components:
trace
Non-negative integer. If positive,
tracing information on the
progress of the optimization is produced. Higher values may
produce more tracing information: for method "L-BFGS-B"
there are six levels of tracing. (To understand exactly what
these do see the source code: higher levels give more detail.)
fnscale
An overall scaling to be applied to the value
of fn
and gr
during optimization. If negative,
turns the problem into a maximization problem. Optimization is
performed on fn(par)/fnscale
.
parscale
A vector of scaling values for the parameters.
Optimization is performed on par/parscale
and these should be
comparable in the sense that a unit change in any element produces
about a unit change in the scaled value. Not used (nor needed)
for method = "Brent"
.
ndeps
A vector of step sizes for the finite-difference
approximation to the gradient, on par/parscale
scale. Defaults to 1e-3
.
maxit
The maximum number of iterations. Defaults to
100
for the derivative-based methods, and
500
for "Nelder-Mead"
.
For "SANN"
maxit
gives the total number of function
evaluations: there is no other stopping criterion. Defaults to
10000
.
abstol
The absolute convergence tolerance. Only useful for non-negative functions, as a tolerance for reaching zero.
reltol
Relative convergence tolerance. The algorithm
stops if it is unable to reduce the value by a factor of
reltol * (abs(val) + reltol)
at a step. Defaults to
sqrt(.Machine$double.eps)
, typically about 1e-8
.
alpha
, beta
, gamma
Scaling parameters
for the "Nelder-Mead"
method. alpha
is the reflection
factor (default 1.0), beta
the contraction factor (0.5) and
gamma
the expansion factor (2.0).
REPORT
The frequency of reports for the "BFGS"
,
"L-BFGS-B"
and "SANN"
methods if control$trace
is positive. Defaults to every 10 iterations for "BFGS"
and
"L-BFGS-B"
, or every 100 temperatures for "SANN"
.
warn.1d.NelderMead
a logical
indicating
if the (default) "Nelder-Mead"
method should signal a
warning when used for one-dimensional minimization. As the
warning is sometimes inappropriate, you can suppress it by setting
this option to false.
type
for the conjugate-gradients method. Takes value
1
for the Fletcher–Reeves update, 2
for
Polak–Ribiere and 3
for Beale–Sorenson.
lmm
is an integer giving the number of BFGS updates
retained in the "L-BFGS-B"
method, It defaults to 5
.
factr
controls the convergence of the "L-BFGS-B"
method. Convergence occurs when the reduction in the objective is
within this factor of the machine tolerance. Default is 1e7
,
that is a tolerance of about 1e-8
.
pgtol
helps control the convergence of the "L-BFGS-B"
method. It is a tolerance on the projected gradient in the current
search direction. This defaults to zero, when the check is
suppressed.
temp
controls the "SANN"
method. It is the
starting temperature for the cooling schedule. Defaults to
10
.
tmax
is the number of function evaluations at each
temperature for the "SANN"
method. Defaults to 10
.
Any names given to par
will be copied to the vectors passed to
fn
and gr
. Note that no other attributes of par
are copied over.
The parameter vector passed to fn
has special semantics and may
be shared between calls: the function should not change or copy it.
For optim
, a list with components:
par |
The best set of parameters found. |
value |
The value of |
counts |
A two-element integer vector giving the number of calls
to |
convergence |
An integer code.
|
message |
A character string giving any additional information
returned by the optimizer, or |
hessian |
Only if argument |
For optimHess
, the description of the hessian
component
applies.
optim
will work with one-dimensional par
s, but the
default method does not work well (and will warn). Method
"Brent"
uses optimize
and needs bounds to be available;
"BFGS"
often works well enough if not.
The code for methods "Nelder-Mead"
, "BFGS"
and
"CG"
was based originally on Pascal code in Nash (1990) that was
translated by p2c
and then hand-optimized. Dr Nash has agreed
that the code can be made freely available.
The code for method "L-BFGS-B"
is based on Fortran code by Zhu,
Byrd, Lu-Chen and Nocedal obtained from Netlib (file
‘opt/lbfgs_bcm.shar’: another version is in ‘toms/778’).
The code for method "SANN"
was contributed by A. Trapletti.
Belisle, C. J. P. (1992).
Convergence theorems for a class of simulated annealing algorithms on
.
Journal of Applied Probability, 29, 885–895.
doi:10.2307/3214721.
Byrd, R. H., Lu, P., Nocedal, J. and Zhu, C. (1995). A limited memory algorithm for bound constrained optimization. SIAM Journal on Scientific Computing, 16, 1190–1208. doi:10.1137/0916069.
Fletcher, R. and Reeves, C. M. (1964). Function minimization by conjugate gradients. Computer Journal 7, 148–154. doi:10.1093/comjnl/7.2.149.
Nash, J. C. (1990). Compact Numerical Methods for Computers. Linear Algebra and Function Minimisation. Adam Hilger.
Nelder, J. A. and Mead, R. (1965). A simplex algorithm for function minimization. Computer Journal, 7, 308–313. doi:10.1093/comjnl/7.4.308.
Nocedal, J. and Wright, S. J. (1999). Numerical Optimization. Springer.
optimize
for one-dimensional minimization and
constrOptim
for constrained optimization.
require(graphics) fr <- function(x) { ## Rosenbrock Banana function x1 <- x[1] x2 <- x[2] 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 } grr <- function(x) { ## Gradient of 'fr' x1 <- x[1] x2 <- x[2] c(-400 * x1 * (x2 - x1 * x1) - 2 * (1 - x1), 200 * (x2 - x1 * x1)) } optim(c(-1.2,1), fr) (res <- optim(c(-1.2,1), fr, grr, method = "BFGS")) optimHess(res$par, fr, grr) optim(c(-1.2,1), fr, NULL, method = "BFGS", hessian = TRUE) ## These do not converge in the default number of steps optim(c(-1.2,1), fr, grr, method = "CG") optim(c(-1.2,1), fr, grr, method = "CG", control = list(type = 2)) optim(c(-1.2,1), fr, grr, method = "L-BFGS-B") flb <- function(x) { p <- length(x); sum(c(1, rep(4, p-1)) * (x - c(1, x[-p])^2)^2) } ## 25-dimensional box constrained optim(rep(3, 25), flb, NULL, method = "L-BFGS-B", lower = rep(2, 25), upper = rep(4, 25)) # par[24] is *not* at boundary ## "wild" function , global minimum at about -15.81515 fw <- function (x) 10*sin(0.3*x)*sin(1.3*x^2) + 0.00001*x^4 + 0.2*x+80 plot(fw, -50, 50, n = 1000, main = "optim() minimising 'wild function'") res <- optim(50, fw, method = "SANN", control = list(maxit = 20000, temp = 20, parscale = 20)) res ## Now improve locally {typically only by a small bit}: (r2 <- optim(res$par, fw, method = "BFGS")) points(r2$par, r2$value, pch = 8, col = "red", cex = 2) ## Combinatorial optimization: Traveling salesman problem library(stats) # normally loaded eurodistmat <- as.matrix(eurodist) distance <- function(sq) { # Target function sq2 <- embed(sq, 2) sum(eurodistmat[cbind(sq2[,2], sq2[,1])]) } genseq <- function(sq) { # Generate new candidate sequence idx <- seq(2, NROW(eurodistmat)-1) changepoints <- sample(idx, size = 2, replace = FALSE) tmp <- sq[changepoints[1]] sq[changepoints[1]] <- sq[changepoints[2]] sq[changepoints[2]] <- tmp sq } sq <- c(1:nrow(eurodistmat), 1) # Initial sequence: alphabetic distance(sq) # rotate for conventional orientation loc <- -cmdscale(eurodist, add = TRUE)$points x <- loc[,1]; y <- loc[,2] s <- seq_len(nrow(eurodistmat)) tspinit <- loc[sq,] plot(x, y, type = "n", asp = 1, xlab = "", ylab = "", main = "initial solution of traveling salesman problem", axes = FALSE) arrows(tspinit[s,1], tspinit[s,2], tspinit[s+1,1], tspinit[s+1,2], angle = 10, col = "green") text(x, y, labels(eurodist), cex = 0.8) set.seed(123) # chosen to get a good soln relatively quickly res <- optim(sq, distance, genseq, method = "SANN", control = list(maxit = 30000, temp = 2000, trace = TRUE, REPORT = 500)) res # Near optimum distance around 12842 tspres <- loc[res$par,] plot(x, y, type = "n", asp = 1, xlab = "", ylab = "", main = "optim() 'solving' traveling salesman problem", axes = FALSE) arrows(tspres[s,1], tspres[s,2], tspres[s+1,1], tspres[s+1,2], angle = 10, col = "red") text(x, y, labels(eurodist), cex = 0.8) ## 1-D minimization: "Brent" or optimize() being preferred.. but NM may be ok and "unavoidable", ## ---------------- so we can suppress the check+warning : system.time(rO <- optimize(function(x) (x-pi)^2, c(0, 10))) system.time(ro <- optim(1, function(x) (x-pi)^2, control=list(warn.1d.NelderMead = FALSE))) rO$minimum - pi # 0 (perfect), on one platform ro$par - pi # ~= 1.9e-4 on one platform utils::str(ro)
require(graphics) fr <- function(x) { ## Rosenbrock Banana function x1 <- x[1] x2 <- x[2] 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 } grr <- function(x) { ## Gradient of 'fr' x1 <- x[1] x2 <- x[2] c(-400 * x1 * (x2 - x1 * x1) - 2 * (1 - x1), 200 * (x2 - x1 * x1)) } optim(c(-1.2,1), fr) (res <- optim(c(-1.2,1), fr, grr, method = "BFGS")) optimHess(res$par, fr, grr) optim(c(-1.2,1), fr, NULL, method = "BFGS", hessian = TRUE) ## These do not converge in the default number of steps optim(c(-1.2,1), fr, grr, method = "CG") optim(c(-1.2,1), fr, grr, method = "CG", control = list(type = 2)) optim(c(-1.2,1), fr, grr, method = "L-BFGS-B") flb <- function(x) { p <- length(x); sum(c(1, rep(4, p-1)) * (x - c(1, x[-p])^2)^2) } ## 25-dimensional box constrained optim(rep(3, 25), flb, NULL, method = "L-BFGS-B", lower = rep(2, 25), upper = rep(4, 25)) # par[24] is *not* at boundary ## "wild" function , global minimum at about -15.81515 fw <- function (x) 10*sin(0.3*x)*sin(1.3*x^2) + 0.00001*x^4 + 0.2*x+80 plot(fw, -50, 50, n = 1000, main = "optim() minimising 'wild function'") res <- optim(50, fw, method = "SANN", control = list(maxit = 20000, temp = 20, parscale = 20)) res ## Now improve locally {typically only by a small bit}: (r2 <- optim(res$par, fw, method = "BFGS")) points(r2$par, r2$value, pch = 8, col = "red", cex = 2) ## Combinatorial optimization: Traveling salesman problem library(stats) # normally loaded eurodistmat <- as.matrix(eurodist) distance <- function(sq) { # Target function sq2 <- embed(sq, 2) sum(eurodistmat[cbind(sq2[,2], sq2[,1])]) } genseq <- function(sq) { # Generate new candidate sequence idx <- seq(2, NROW(eurodistmat)-1) changepoints <- sample(idx, size = 2, replace = FALSE) tmp <- sq[changepoints[1]] sq[changepoints[1]] <- sq[changepoints[2]] sq[changepoints[2]] <- tmp sq } sq <- c(1:nrow(eurodistmat), 1) # Initial sequence: alphabetic distance(sq) # rotate for conventional orientation loc <- -cmdscale(eurodist, add = TRUE)$points x <- loc[,1]; y <- loc[,2] s <- seq_len(nrow(eurodistmat)) tspinit <- loc[sq,] plot(x, y, type = "n", asp = 1, xlab = "", ylab = "", main = "initial solution of traveling salesman problem", axes = FALSE) arrows(tspinit[s,1], tspinit[s,2], tspinit[s+1,1], tspinit[s+1,2], angle = 10, col = "green") text(x, y, labels(eurodist), cex = 0.8) set.seed(123) # chosen to get a good soln relatively quickly res <- optim(sq, distance, genseq, method = "SANN", control = list(maxit = 30000, temp = 2000, trace = TRUE, REPORT = 500)) res # Near optimum distance around 12842 tspres <- loc[res$par,] plot(x, y, type = "n", asp = 1, xlab = "", ylab = "", main = "optim() 'solving' traveling salesman problem", axes = FALSE) arrows(tspres[s,1], tspres[s,2], tspres[s+1,1], tspres[s+1,2], angle = 10, col = "red") text(x, y, labels(eurodist), cex = 0.8) ## 1-D minimization: "Brent" or optimize() being preferred.. but NM may be ok and "unavoidable", ## ---------------- so we can suppress the check+warning : system.time(rO <- optimize(function(x) (x-pi)^2, c(0, 10))) system.time(ro <- optim(1, function(x) (x-pi)^2, control=list(warn.1d.NelderMead = FALSE))) rO$minimum - pi # 0 (perfect), on one platform ro$par - pi # ~= 1.9e-4 on one platform utils::str(ro)
The function optimize
searches the interval from
lower
to upper
for a minimum or maximum of
the function f
with respect to its first argument.
optimise
is an alias for optimize
.
optimize(f, interval, ..., lower = min(interval), upper = max(interval), maximum = FALSE, tol = .Machine$double.eps^0.25) optimise(f, interval, ..., lower = min(interval), upper = max(interval), maximum = FALSE, tol = .Machine$double.eps^0.25)
optimize(f, interval, ..., lower = min(interval), upper = max(interval), maximum = FALSE, tol = .Machine$double.eps^0.25) optimise(f, interval, ..., lower = min(interval), upper = max(interval), maximum = FALSE, tol = .Machine$double.eps^0.25)
f |
the function to be optimized. The function is
either minimized or maximized over its first argument
depending on the value of |
interval |
a vector containing the end-points of the interval to be searched for the minimum. |
... |
additional named or unnamed arguments to be passed
to |
lower |
the lower end point of the interval to be searched. |
upper |
the upper end point of the interval to be searched. |
maximum |
logical. Should we maximize or minimize (the default)? |
tol |
the desired accuracy. |
Note that arguments after ...
must be matched exactly.
The method used is a combination of golden section search and
successive parabolic interpolation, and was designed for use with
continuous functions. Convergence is never much slower
than that for a Fibonacci search. If f
has a continuous second
derivative which is positive at the minimum (which is not at lower
or
upper
), then convergence is superlinear, and usually of the
order of about 1.324.
The function f
is never evaluated at two points closer together
than , where
is approximately
sqrt(.Machine$double.eps)
and is the final abscissa
optimize()$minimum
.
If f
is a unimodal function and the computed values of f
are always unimodal when separated by at least
, then
approximates the abscissa of the
global minimum of
f
on the interval lower,upper
with an
error less than .
If f
is not unimodal, then optimize()
may approximate a
local, but perhaps non-global, minimum to the same accuracy.
The first evaluation of f
is always at
where
(a,b) = (lower, upper)
and
is the golden section ratio.
Almost always, the second evaluation is at
.
Note that a local minimum inside
will be found as
solution, even when
f
is constant in there, see the last
example.
f
will be called as f(x, ...)
for a numeric value
of x.
The argument passed to f
has special semantics and used to be
shared between calls. The function should not copy it.
A list with components minimum
(or maximum
)
and objective
which give the location of the minimum (or maximum)
and the value of the function at that point.
A C translation of Fortran code https://netlib.org/fmm/fmin.f
(author(s) unstated)
based on the Algol 60 procedure localmin
given in the reference.
Brent, R. (1973) Algorithms for Minimization without Derivatives. Englewood Cliffs N.J.: Prentice-Hall.
require(graphics) f <- function (x, a) (x - a)^2 xmin <- optimize(f, c(0, 1), tol = 0.0001, a = 1/3) xmin ## See where the function is evaluated: optimize(function(x) x^2*(print(x)-1), lower = 0, upper = 10) ## "wrong" solution with unlucky interval and piecewise constant f(): f <- function(x) ifelse(x > -1, ifelse(x < 4, exp(-1/abs(x - 1)), 10), 10) fp <- function(x) { print(x); f(x) } plot(f, -2,5, ylim = 0:1, col = 2) optimize(fp, c(-4, 20)) # doesn't see the minimum optimize(fp, c(-7, 20)) # ok
require(graphics) f <- function (x, a) (x - a)^2 xmin <- optimize(f, c(0, 1), tol = 0.0001, a = 1/3) xmin ## See where the function is evaluated: optimize(function(x) x^2*(print(x)-1), lower = 0, upper = 10) ## "wrong" solution with unlucky interval and piecewise constant f(): f <- function(x) ifelse(x > -1, ifelse(x < 4, exp(-1/abs(x - 1)), 10), 10) fp <- function(x) { print(x); f(x) } plot(f, -2,5, ylim = 0:1, col = 2) optimize(fp, c(-4, 20)) # doesn't see the minimum optimize(fp, c(-7, 20)) # ok
Theses functions return the order (index) or the "label"
attribute for the leaves in a
dendrogram. These indices can then be used to access the appropriate
components of any additional data.
order.dendrogram(x) ## S3 method for class 'dendrogram' labels(object, ...)
order.dendrogram(x) ## S3 method for class 'dendrogram' labels(object, ...)
x , object
|
a dendrogram (see |
... |
additional arguments |
The indices or labels for the leaves in left to right order are retrieved.
A vector with length equal to the number of leaves in the dendrogram
is returned. From r <- order.dendrogram()
, each element is the
index into the original data (from which the dendrogram was computed).
R. Gentleman (order.dendrogram
) and Martin Maechler
(labels.dendrogram
).
set.seed(123) x <- rnorm(10) hc <- hclust(dist(x)) hc$order dd <- as.dendrogram(hc) order.dendrogram(dd) ## the same : stopifnot(hc$order == order.dendrogram(dd)) d2 <- as.dendrogram(hclust(dist(USArrests))) labels(d2) ## in this case the same as stopifnot(identical(labels(d2), rownames(USArrests)[order.dendrogram(d2)]))
set.seed(123) x <- rnorm(10) hc <- hclust(dist(x)) hc$order dd <- as.dendrogram(hc) order.dendrogram(dd) ## the same : stopifnot(hc$order == order.dendrogram(dd)) d2 <- as.dendrogram(hclust(dist(USArrests))) labels(d2) ## in this case the same as stopifnot(identical(labels(d2), rownames(USArrests)[order.dendrogram(d2)]))
Given a set of p-values, returns p-values adjusted using one of several methods.
p.adjust(p, method = p.adjust.methods, n = length(p)) p.adjust.methods # c("holm", "hochberg", "hommel", "bonferroni", "BH", "BY", # "fdr", "none")
p.adjust(p, method = p.adjust.methods, n = length(p)) p.adjust.methods # c("holm", "hochberg", "hommel", "bonferroni", "BH", "BY", # "fdr", "none")
p |
numeric vector of p-values (possibly with
|
method |
correction method, a |
n |
number of comparisons, must be at least |
The adjustment methods include the Bonferroni correction
("bonferroni"
) in which the p-values are multiplied by the
number of comparisons. Less conservative corrections are also
included by
Holm (1979) ("holm"
),
Hochberg (1988) ("hochberg"
),
Hommel (1988) ("hommel"
),
Benjamini & Hochberg (1995) ("BH"
or its alias
"fdr"
), and
Benjamini & Yekutieli (2001) ("BY"
), respectively.
A pass-through option ("none"
) is also included.
The set of methods are contained in the p.adjust.methods
vector
for the benefit of methods that need to have the method as an option
and pass it on to p.adjust
.
The first four methods are designed to give strong control of the family-wise error rate. There seems no reason to use the unmodified Bonferroni correction because it is dominated by Holm's method, which is also valid under arbitrary assumptions.
Hochberg's and Hommel's methods are valid when the hypothesis tests are independent or when they are non-negatively associated ( Sarkar, 1998; Sarkar and Chang, 1997). Hommel's method is more powerful than Hochberg's, but the difference is usually small and the Hochberg p-values are faster to compute.
The "BH"
(aka "fdr"
) and "BY"
methods of
Benjamini, Hochberg, and Yekutieli control the false discovery rate,
the expected proportion of false discoveries amongst the rejected
hypotheses. The false discovery rate is a less stringent condition
than the family-wise error rate, so these methods are more powerful
than the others.
Note that you can set n
larger than length(p)
which
means the unobserved p-values are assumed to be greater than all the
observed p for "bonferroni"
and "holm"
methods and equal
to 1 for the other methods.
A numeric vector of corrected p-values (of the same length as
p
, with names copied from p
).
Benjamini, Y., and Hochberg, Y. (1995). Controlling the false discovery rate: a practical and powerful approach to multiple testing. Journal of the Royal Statistical Society Series B, 57, 289–300. doi:10.1111/j.2517-6161.1995.tb02031.x.
Benjamini, Y., and Yekutieli, D. (2001). The control of the false discovery rate in multiple testing under dependency. Annals of Statistics, 29, 1165–1188. doi:10.1214/aos/1013699998.
Holm, S. (1979). A simple sequentially rejective multiple test procedure. Scandinavian Journal of Statistics, 6, 65–70. https://www.jstor.org/stable/4615733.
Hommel, G. (1988). A stagewise rejective multiple test procedure based on a modified Bonferroni test. Biometrika, 75, 383–386. doi:10.2307/2336190.
Hochberg, Y. (1988). A sharper Bonferroni procedure for multiple tests of significance. Biometrika, 75, 800–803. doi:10.2307/2336325.
Shaffer, J. P. (1995). Multiple hypothesis testing. Annual Review of Psychology, 46, 561–584. doi:10.1146/annurev.ps.46.020195.003021. (An excellent review of the area.)
Sarkar, S. (1998). Some probability inequalities for ordered MTP2 random variables: a proof of Simes conjecture. Annals of Statistics, 26, 494–504. doi:10.1214/aos/1028144846.
Sarkar, S., and Chang, C. K. (1997). The Simes method for multiple hypothesis testing with positively dependent test statistics. Journal of the American Statistical Association, 92, 1601–1608. doi:10.2307/2965431.
Wright, S. P. (1992). Adjusted P-values for simultaneous inference. Biometrics, 48, 1005–1013. doi:10.2307/2532694. (Explains the adjusted P-value approach.)
pairwise.*
functions such as pairwise.t.test
.
require(graphics) set.seed(123) x <- rnorm(50, mean = c(rep(0, 25), rep(3, 25))) p <- 2*pnorm(sort(-abs(x))) round(p, 3) round(p.adjust(p), 3) round(p.adjust(p, "BH"), 3) ## or all of them at once (dropping the "fdr" alias): p.adjust.M <- p.adjust.methods[p.adjust.methods != "fdr"] p.adj <- sapply(p.adjust.M, function(meth) p.adjust(p, meth)) p.adj.60 <- sapply(p.adjust.M, function(meth) p.adjust(p, meth, n = 60)) stopifnot(identical(p.adj[,"none"], p), p.adj <= p.adj.60) round(p.adj, 3) ## or a bit nicer: noquote(apply(p.adj, 2, format.pval, digits = 3)) ## and a graphic: matplot(p, p.adj, ylab="p.adjust(p, meth)", type = "l", asp = 1, lty = 1:6, main = "P-value adjustments") legend(0.7, 0.6, p.adjust.M, col = 1:6, lty = 1:6) ## Can work with NA's: pN <- p; iN <- c(46, 47); pN[iN] <- NA pN.a <- sapply(p.adjust.M, function(meth) p.adjust(pN, meth)) ## The smallest 20 P-values all affected by the NA's : round((pN.a / p.adj)[1:20, ] , 4)
require(graphics) set.seed(123) x <- rnorm(50, mean = c(rep(0, 25), rep(3, 25))) p <- 2*pnorm(sort(-abs(x))) round(p, 3) round(p.adjust(p), 3) round(p.adjust(p, "BH"), 3) ## or all of them at once (dropping the "fdr" alias): p.adjust.M <- p.adjust.methods[p.adjust.methods != "fdr"] p.adj <- sapply(p.adjust.M, function(meth) p.adjust(p, meth)) p.adj.60 <- sapply(p.adjust.M, function(meth) p.adjust(p, meth, n = 60)) stopifnot(identical(p.adj[,"none"], p), p.adj <= p.adj.60) round(p.adj, 3) ## or a bit nicer: noquote(apply(p.adj, 2, format.pval, digits = 3)) ## and a graphic: matplot(p, p.adj, ylab="p.adjust(p, meth)", type = "l", asp = 1, lty = 1:6, main = "P-value adjustments") legend(0.7, 0.6, p.adjust.M, col = 1:6, lty = 1:6) ## Can work with NA's: pN <- p; iN <- c(46, 47); pN[iN] <- NA pN.a <- sapply(p.adjust.M, function(meth) p.adjust(pN, meth)) ## The smallest 20 P-values all affected by the NA's : round((pN.a / p.adj)[1:20, ] , 4)
Combines two vectors into an object of class "Pair"
.
Pair(x, y)
Pair(x, y)
x |
a vector, the 1st element of the pair. |
y |
a vector, the 2nd element of the pair.
Should have the same length as |
A 2-column matrix of class "Pair"
.
Mostly designed as part of the formula interface to paired tests.
t.test
and wilcox.test
Calculate pairwise comparisons between pairs of proportions with correction for multiple testing
pairwise.prop.test(x, n, p.adjust.method = p.adjust.methods, ...)
pairwise.prop.test(x, n, p.adjust.method = p.adjust.methods, ...)
x |
Vector of counts of successes or a matrix with 2 columns giving the counts of successes and failures, respectively. |
n |
Vector of counts of trials; ignored if |
p.adjust.method |
Method for adjusting p values
(see |
... |
Additional arguments to pass to |
Object of class "pairwise.htest"
smokers <- c( 83, 90, 129, 70 ) patients <- c( 86, 93, 136, 82 ) pairwise.prop.test(smokers, patients)
smokers <- c( 83, 90, 129, 70 ) patients <- c( 86, 93, 136, 82 ) pairwise.prop.test(smokers, patients)
Calculate pairwise comparisons between group levels with corrections for multiple testing
pairwise.t.test(x, g, p.adjust.method = p.adjust.methods, pool.sd = !paired, paired = FALSE, alternative = c("two.sided", "less", "greater"), ...)
pairwise.t.test(x, g, p.adjust.method = p.adjust.methods, pool.sd = !paired, paired = FALSE, alternative = c("two.sided", "less", "greater"), ...)
x |
response vector. |
g |
grouping vector or factor. |
p.adjust.method |
Method for adjusting p values (see |
pool.sd |
switch to allow/disallow the use of a pooled SD |
paired |
a logical indicating whether you want paired t-tests. |
alternative |
a character string specifying the alternative
hypothesis, must be one of |
... |
additional arguments to pass to |
The pool.sd
switch calculates a common SD for all
groups and uses that for all comparisons (this can be useful if some
groups are small). This method does not actually call t.test
,
so extra arguments are ignored. Pooling does not generalize to paired tests
so pool.sd
and paired
cannot both be TRUE
.
Only the lower triangle of the matrix of possible comparisons is being
calculated, so setting alternative
to anything other than
"two.sided"
requires that the levels of g
are ordered
sensibly.
Object of class "pairwise.htest"
attach(airquality) Month <- factor(Month, labels = month.abb[5:9]) pairwise.t.test(Ozone, Month) pairwise.t.test(Ozone, Month, p.adjust.method = "bonf") pairwise.t.test(Ozone, Month, pool.sd = FALSE) detach()
attach(airquality) Month <- factor(Month, labels = month.abb[5:9]) pairwise.t.test(Ozone, Month) pairwise.t.test(Ozone, Month, p.adjust.method = "bonf") pairwise.t.test(Ozone, Month, pool.sd = FALSE) detach()
Creates table of p values for pairwise comparisons with corrections for multiple testing.
pairwise.table(compare.levels, level.names, p.adjust.method)
pairwise.table(compare.levels, level.names, p.adjust.method)
compare.levels |
a |
level.names |
names of the group levels |
p.adjust.method |
a character string specifying the method for
multiple testing adjustment; almost always one of
|
Functions that do multiple group comparisons create separate
compare.levels
functions (assumed to be symmetrical in i
and j
) and passes them to this function.
Table of p values in lower triangular form.
Calculate pairwise comparisons between group levels with corrections for multiple testing.
pairwise.wilcox.test(x, g, p.adjust.method = p.adjust.methods, paired = FALSE, ...)
pairwise.wilcox.test(x, g, p.adjust.method = p.adjust.methods, paired = FALSE, ...)
x |
response vector. |
g |
grouping vector or factor. |
p.adjust.method |
method for adjusting p values (see
|
paired |
a logical indicating whether you want a paired test. |
... |
additional arguments to pass to |
Extra arguments that are passed on to wilcox.test
may or may
not be sensible in this context. In particular,
only the lower triangle of the matrix of possible comparisons is being
calculated, so setting alternative
to anything other than
"two.sided"
requires that the levels of g
are ordered
sensibly.
Object of class "pairwise.htest"
attach(airquality) Month <- factor(Month, labels = month.abb[5:9]) ## These give warnings because of ties : pairwise.wilcox.test(Ozone, Month) pairwise.wilcox.test(Ozone, Month, p.adjust.method = "bonf") detach()
attach(airquality) Month <- factor(Month, labels = month.abb[5:9]) ## These give warnings because of ties : pairwise.wilcox.test(Ozone, Month) pairwise.wilcox.test(Ozone, Month, p.adjust.method = "bonf") detach()
Plot method for objects of class "acf"
.
## S3 method for class 'acf' plot(x, ci = 0.95, type = "h", xlab = "Lag", ylab = NULL, ylim = NULL, main = NULL, ci.col = "blue", ci.type = c("white", "ma"), max.mfrow = 6, ask = Npgs > 1 && dev.interactive(), mar = if(nser > 2) c(3,2,2,0.8) else par("mar"), oma = if(nser > 2) c(1,1.2,1,1) else par("oma"), mgp = if(nser > 2) c(1.5,0.6,0) else par("mgp"), xpd = par("xpd"), cex.main = if(nser > 2) 1 else par("cex.main"), verbose = getOption("verbose"), ...)
## S3 method for class 'acf' plot(x, ci = 0.95, type = "h", xlab = "Lag", ylab = NULL, ylim = NULL, main = NULL, ci.col = "blue", ci.type = c("white", "ma"), max.mfrow = 6, ask = Npgs > 1 && dev.interactive(), mar = if(nser > 2) c(3,2,2,0.8) else par("mar"), oma = if(nser > 2) c(1,1.2,1,1) else par("oma"), mgp = if(nser > 2) c(1.5,0.6,0) else par("mgp"), xpd = par("xpd"), cex.main = if(nser > 2) 1 else par("cex.main"), verbose = getOption("verbose"), ...)
x |
an object of class |
ci |
coverage probability for confidence interval. Plotting of
the confidence interval is suppressed if |
type |
the type of plot to be drawn, default to histogram like vertical lines. |
xlab |
the x label of the plot. |
ylab |
the y label of the plot. |
ylim |
numeric of length 2 giving the y limits for the plot. |
main |
overall title for the plot. |
ci.col |
colour to plot the confidence interval lines. |
ci.type |
should the confidence limits assume a white noise
input or for lag |
max.mfrow |
positive integer; for multivariate |
ask |
logical; if |
mar , oma , mgp , xpd , cex.main
|
graphics parameters as in
|
verbose |
logical. Should R report extra information on progress? |
... |
graphics parameters to be passed to the plotting routines. |
The confidence interval plotted in plot.acf
is based on an
uncorrelated series and should be treated with appropriate
caution. Using ci.type = "ma"
may be less potentially
misleading.
acf
which calls plot.acf
by default.
require(graphics) z4 <- ts(matrix(rnorm(400), 100, 4), start = c(1961, 1), frequency = 12) z7 <- ts(matrix(rnorm(700), 100, 7), start = c(1961, 1), frequency = 12) acf(z4) acf(z7, max.mfrow = 7) # squeeze onto 1 page acf(z7) # multi-page
require(graphics) z4 <- ts(matrix(rnorm(400), 100, 4), start = c(1961, 1), frequency = 12) z7 <- ts(matrix(rnorm(700), 100, 7), start = c(1961, 1), frequency = 12) acf(z4) acf(z7, max.mfrow = 7) # squeeze onto 1 page acf(z7) # multi-page
The plot
method for density objects.
## S3 method for class 'density' plot(x, main = NULL, xlab = NULL, ylab = "Density", type = "l", zero.line = TRUE, ...)
## S3 method for class 'density' plot(x, main = NULL, xlab = NULL, ylab = "Density", type = "l", zero.line = TRUE, ...)
x |
a |
main , xlab , ylab , type
|
plotting parameters with useful defaults. |
... |
further plotting parameters. |
zero.line |
logical; if |
None.
"HoltWinters"
objectsProduces a chart of the original time series along with the fitted values. Optionally, predicted values (and their confidence bounds) can also be plotted.
## S3 method for class 'HoltWinters' plot(x, predicted.values = NA, intervals = TRUE, separator = TRUE, col = 1, col.predicted = 2, col.intervals = 4, col.separator = 1, lty = 1, lty.predicted = 1, lty.intervals = 1, lty.separator = 3, ylab = "Observed / Fitted", main = "Holt-Winters filtering", ylim = NULL, ...)
## S3 method for class 'HoltWinters' plot(x, predicted.values = NA, intervals = TRUE, separator = TRUE, col = 1, col.predicted = 2, col.intervals = 4, col.separator = 1, lty = 1, lty.predicted = 1, lty.intervals = 1, lty.separator = 3, ylab = "Observed / Fitted", main = "Holt-Winters filtering", ylim = NULL, ...)
x |
Object of class |
predicted.values |
Predicted values as returned by |
intervals |
If |
separator |
If |
col , lty
|
Color/line type of original data (default: black solid). |
col.predicted , lty.predicted
|
Color/line type of fitted and predicted values (default: red solid). |
col.intervals , lty.intervals
|
Color/line type of prediction intervals (default: blue solid). |
col.separator , lty.separator
|
Color/line type of observed/predicted values separator (default: black dashed). |
ylab |
Label of the y-axis. |
main |
Main title. |
ylim |
Limits of the y-axis. If |
... |
Other graphics parameters. |
David Meyer [email protected]
C. C. Holt (1957) Forecasting trends and seasonals by exponentially weighted moving averages, ONR Research Memorandum, Carnegie Institute of Technology 52.
P. R. Winters (1960). Forecasting sales by exponentially weighted moving averages. Management Science, 6, 324–342. doi:10.1287/mnsc.6.3.324.
HoltWinters
, predict.HoltWinters
isoreg
ObjectsThe plot
and lines
method for
R objects of class isoreg
.
## S3 method for class 'isoreg' plot(x, plot.type = c("single", "row.wise", "col.wise"), main = paste("Isotonic regression", deparse(x$call)), main2 = "Cumulative Data and Convex Minorant", xlab = "x0", ylab = "x$y", par.fit = list(col = "red", cex = 1.5, pch = 13, lwd = 1.5), mar = if (both) 0.1 + c(3.5, 2.5, 1, 1) else par("mar"), mgp = if (both) c(1.6, 0.7, 0) else par("mgp"), grid = length(x$x) < 12, ...) ## S3 method for class 'isoreg' lines(x, col = "red", lwd = 1.5, do.points = FALSE, cex = 1.5, pch = 13, ...)
## S3 method for class 'isoreg' plot(x, plot.type = c("single", "row.wise", "col.wise"), main = paste("Isotonic regression", deparse(x$call)), main2 = "Cumulative Data and Convex Minorant", xlab = "x0", ylab = "x$y", par.fit = list(col = "red", cex = 1.5, pch = 13, lwd = 1.5), mar = if (both) 0.1 + c(3.5, 2.5, 1, 1) else par("mar"), mgp = if (both) c(1.6, 0.7, 0) else par("mgp"), grid = length(x$x) < 12, ...) ## S3 method for class 'isoreg' lines(x, col = "red", lwd = 1.5, do.points = FALSE, cex = 1.5, pch = 13, ...)
x |
an |
plot.type |
character indicating which type of plot is desired. The first (default) only draws the data and the fit, where the others add a plot of the cumulative data and fit. Can be abbreviated. |
main |
main title of plot, see |
main2 |
title for second (cumulative) plot. |
xlab , ylab
|
x- and y- axis annotation. |
par.fit |
a |
mar , mgp
|
graphical parameters, see |
grid |
logical indicating if grid lines should be drawn. If
true, |
do.points |
for |
col , lwd , cex , pch
|
graphical arguments for |
... |
further arguments passed to and from methods. |
isoreg
for computation of isoreg
objects.
require(graphics) utils::example(isoreg) # for the examples there plot(y3, main = "simple plot(.) + lines(<isoreg>)") lines(ir3) ## 'same' plot as above, "proving" that only ranks of 'x' are important plot(isoreg(2^(1:9), c(1,0,4,3,3,5,4,2,0)), plot.type = "row", log = "x") plot(ir3, plot.type = "row", ylab = "y3") plot(isoreg(y3 - 4), plot.type = "r", ylab = "y3 - 4") plot(ir4, plot.type = "ro", ylab = "y4", xlab = "x = 1:n") ## experiment a bit with these (C-c C-j): plot(isoreg(sample(9), y3), plot.type = "row") plot(isoreg(sample(9), y3), plot.type = "col.wise") plot(ir <- isoreg(sample(10), sample(10, replace = TRUE)), plot.type = "r")
require(graphics) utils::example(isoreg) # for the examples there plot(y3, main = "simple plot(.) + lines(<isoreg>)") lines(ir3) ## 'same' plot as above, "proving" that only ranks of 'x' are important plot(isoreg(2^(1:9), c(1,0,4,3,3,5,4,2,0)), plot.type = "row", log = "x") plot(ir3, plot.type = "row", ylab = "y3") plot(isoreg(y3 - 4), plot.type = "r", ylab = "y3 - 4") plot(ir4, plot.type = "ro", ylab = "y4", xlab = "x = 1:n") ## experiment a bit with these (C-c C-j): plot(isoreg(sample(9), y3), plot.type = "row") plot(isoreg(sample(9), y3), plot.type = "col.wise") plot(ir <- isoreg(sample(10), sample(10, replace = TRUE)), plot.type = "r")
lm
ObjectSix plots (selectable by which
) are currently available: a plot
of residuals against fitted values, a Scale-Location plot of
against fitted values, a Q-Q plot of residuals, a
plot of Cook's distances versus row labels, a plot of residuals
against leverages, and a plot of Cook's distances against
leverage/(1-leverage). By default, the first three and
5
are
provided.
## S3 method for class 'lm' plot(x, which = c(1,2,3,5), caption = list("Residuals vs Fitted", "Q-Q Residuals", "Scale-Location", "Cook's distance", "Residuals vs Leverage", expression("Cook's dist vs Leverage* " * h[ii] / (1 - h[ii]))), panel = if(add.smooth) function(x, y, ...) panel.smooth(x, y, iter=iter.smooth, ...) else points, sub.caption = NULL, main = "", ask = prod(par("mfcol")) < length(which) && dev.interactive(), ..., id.n = 3, labels.id = names(residuals(x)), cex.id = 0.75, qqline = TRUE, cook.levels = c(0.5, 1.0), cook.col = 8, cook.lty = 2, cook.legendChanges = list(), add.smooth = getOption("add.smooth"), iter.smooth = if(isGlm) 0 else 3, label.pos = c(4,2), cex.caption = 1, cex.oma.main = 1.25 , extend.ylim.f = 0.08 )
## S3 method for class 'lm' plot(x, which = c(1,2,3,5), caption = list("Residuals vs Fitted", "Q-Q Residuals", "Scale-Location", "Cook's distance", "Residuals vs Leverage", expression("Cook's dist vs Leverage* " * h[ii] / (1 - h[ii]))), panel = if(add.smooth) function(x, y, ...) panel.smooth(x, y, iter=iter.smooth, ...) else points, sub.caption = NULL, main = "", ask = prod(par("mfcol")) < length(which) && dev.interactive(), ..., id.n = 3, labels.id = names(residuals(x)), cex.id = 0.75, qqline = TRUE, cook.levels = c(0.5, 1.0), cook.col = 8, cook.lty = 2, cook.legendChanges = list(), add.smooth = getOption("add.smooth"), iter.smooth = if(isGlm) 0 else 3, label.pos = c(4,2), cex.caption = 1, cex.oma.main = 1.25 , extend.ylim.f = 0.08 )
x |
|
which |
a subset of the numbers
See also ‘Details’ below. |
caption |
captions to appear above the plots;
|
panel |
panel function. The useful alternative to
|
sub.caption |
common title—above the figures if there are more
than one; used as |
main |
title to each plot—in addition to |
ask |
logical; if |
... |
other parameters to be passed through to plotting functions. |
id.n |
number of points to be labelled in each plot, starting with the most extreme. |
labels.id |
vector of labels, from which the labels for extreme
points will be chosen. |
cex.id |
magnification of point labels. |
qqline |
logical indicating if a |
cook.levels |
levels of Cook's distance at which to draw contours. |
cook.col , cook.lty
|
color and line type to use for these contour lines. |
cook.legendChanges |
a |
add.smooth |
logical indicating if a smoother should be added to
most plots; see also |
iter.smooth |
the number of robustness iterations, the argument
|
label.pos |
positioning of labels, for the left half and right half of the graph respectively, for plots 1-3, 5, 6. |
cex.caption |
controls the size of |
cex.oma.main |
controls the size of the |
extend.ylim.f |
a numeric vector of length 1 or 2, to be used in
|
sub.caption
—by default the function call—is shown as
a subtitle (under the x-axis title) on each plot when plots are on
separate pages, or as a subtitle in the outer margin (if any) when
there are multiple plots per page.
The ‘Scale-Location’ plot (which=3
), also called ‘Spread-Location’ or
‘S-L’ plot, takes the square root of the absolute residuals in
order to diminish skewness ( is much less skewed
than
for Gaussian zero-mean
).
The ‘S-L’, the Q-Q, and the Residual-Leverage (which=5
) plot use
standardized residuals which have identical variance (under the
hypothesis). They are given as
where the ‘leverages’
are the diagonal entries
of the hat matrix,
influence()$hat
(see also hat
), and
where the Residual-Leverage plot uses the standardized Pearson residuals
(residuals.glm(type = "pearson")
) for .
The Residual-Leverage plot (which=5
) shows contours of equal Cook's distance,
for values of cook.levels
(by default 0.5 and 1) and omits
cases with leverage one with a warning. If the leverages are constant
(as is typically the case in a balanced aov
situation)
the plot uses factor level combinations instead of the leverages for
the x-axis. (The factor levels are ordered by mean fitted value.)
In the Cook's distance vs leverage/(1-leverage) (= “leverage*”)
plot (which=6
), contours of
standardized residuals (rstandard(.)
) that are equal in
magnitude are lines through the origin. These lines are labelled with
the magnitudes. The x-axis is labeled with the (non equidistant)
leverages .
For the glm
case, the Q-Q plot is based on the absolute value
of the standardized deviance residuals. When the saddlepoint
approximation applies, these have an approximate half-normal
distribution. The saddlepoint approximation is exact for the normal
and inverse Gaussian family, and holds approximately for the Gamma
family with small dispersion (large shape) and for the Poisson and
binomial families with large counts (Dunn and Smyth 2018).
John Maindonald and Martin Maechler.
Belsley, D. A., Kuh, E. and Welsch, R. E. (1980). Regression Diagnostics. New York: Wiley.
Cook, R. D. and Weisberg, S. (1982). Residuals and Influence in Regression. London: Chapman and Hall.
Firth, D. (1991) Generalized Linear Models. In Hinkley, D. V. and Reid, N. and Snell, E. J., eds: Pp. 55-82 in Statistical Theory and Modelling. In Honour of Sir David Cox, FRS. London: Chapman and Hall.
Hinkley, D. V. (1975). On power transformations to symmetry. Biometrika, 62, 101–111. doi:10.2307/2334491.
McCullagh, P. and Nelder, J. A. (1989). Generalized Linear Models. London: Chapman and Hall.
Dunn, P.K. and Smyth G.K. (2018) Generalized Linear Models with Examples in R. New York: Springer-Verlag.
termplot
, lm.influence
,
cooks.distance
, hatvalues
.
require(graphics) ## Analysis of the life-cycle savings data ## given in Belsley, Kuh and Welsch. lm.SR <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings) plot(lm.SR) ## 4 plots on 1 page; ## allow room for printing model formula in outer margin: par(mfrow = c(2, 2), oma = c(0, 0, 2, 0)) -> opar plot(lm.SR) plot(lm.SR, id.n = NULL) # no id's plot(lm.SR, id.n = 5, labels.id = NULL) # 5 id numbers ## Was default in R <= 2.1.x: ## Cook's distances instead of Residual-Leverage plot plot(lm.SR, which = 1:4) ## All the above fit a smooth curve where applicable ## by default unless "add.smooth" is changed. ## Give a smoother curve by increasing the lowess span : plot(lm.SR, panel = function(x, y) panel.smooth(x, y, span = 1)) par(mfrow = c(2,1)) # same oma as above plot(lm.SR, which = 1:2, sub.caption = "Saving Rates, n=50, p=5") ## Cook's distance tweaking par(mfrow = c(2,3)) # same oma ... plot(lm.SR, which = 1:6, cook.col = "royalblue") ## A case where over plotting of the "legend" is to be avoided: if(dev.interactive(TRUE)) getOption("device")(height = 6, width = 4) par(mfrow = c(3,1), mar = c(5,5,4,2)/2 +.1, mgp = c(1.4, .5, 0)) plot(lm.SR, which = 5, extend.ylim.f = c(0.2, 0.08)) plot(lm.SR, which = 5, cook.lty = "dotdash", cook.legendChanges = list(x = "bottomright", legend = "Cook")) plot(lm.SR, which = 5, cook.legendChanges = NULL) # no "legend" par(opar) # reset par()s
require(graphics) ## Analysis of the life-cycle savings data ## given in Belsley, Kuh and Welsch. lm.SR <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings) plot(lm.SR) ## 4 plots on 1 page; ## allow room for printing model formula in outer margin: par(mfrow = c(2, 2), oma = c(0, 0, 2, 0)) -> opar plot(lm.SR) plot(lm.SR, id.n = NULL) # no id's plot(lm.SR, id.n = 5, labels.id = NULL) # 5 id numbers ## Was default in R <= 2.1.x: ## Cook's distances instead of Residual-Leverage plot plot(lm.SR, which = 1:4) ## All the above fit a smooth curve where applicable ## by default unless "add.smooth" is changed. ## Give a smoother curve by increasing the lowess span : plot(lm.SR, panel = function(x, y) panel.smooth(x, y, span = 1)) par(mfrow = c(2,1)) # same oma as above plot(lm.SR, which = 1:2, sub.caption = "Saving Rates, n=50, p=5") ## Cook's distance tweaking par(mfrow = c(2,3)) # same oma ... plot(lm.SR, which = 1:6, cook.col = "royalblue") ## A case where over plotting of the "legend" is to be avoided: if(dev.interactive(TRUE)) getOption("device")(height = 6, width = 4) par(mfrow = c(3,1), mar = c(5,5,4,2)/2 +.1, mgp = c(1.4, .5, 0)) plot(lm.SR, which = 5, extend.ylim.f = c(0.2, 0.08)) plot(lm.SR, which = 5, cook.lty = "dotdash", cook.legendChanges = list(x = "bottomright", legend = "Cook")) plot(lm.SR, which = 5, cook.legendChanges = NULL) # no "legend" par(opar) # reset par()s
Plot the ridge functions for a projection pursuit regression
(ppr
) fit.
## S3 method for class 'ppr' plot(x, ask, type = "o", cex = 1/2, main = quote(bquote( "term"[.(i)]*":" ~~ hat(beta[.(i)]) == .(bet.i))), xlab = quote(bquote(bold(alpha)[.(i)]^T * bold(x))), ylab = "", ...)
## S3 method for class 'ppr' plot(x, ask, type = "o", cex = 1/2, main = quote(bquote( "term"[.(i)]*":" ~~ hat(beta[.(i)]) == .(bet.i))), xlab = quote(bquote(bold(alpha)[.(i)]^T * bold(x))), ylab = "", ...)
x |
an R object of class |
ask |
the graphics parameter |
type |
the type of line (see |
cex |
plot symbol expansion factor (relative to
|
main , xlab , ylab
|
axis annotations, see also
|
... |
further graphical parameters, passed to
|
None
A series of plots are drawn on the current graphical device, one for each term in the fit.
require(graphics) rock1 <- within(rock, { area1 <- area/10000; peri1 <- peri/10000 }) par(mfrow = c(3,2)) # maybe: , pty = "s" rock.ppr <- ppr(log(perm) ~ area1 + peri1 + shape, data = rock1, nterms = 2, max.terms = 5) plot(rock.ppr, main = "ppr(log(perm)~ ., nterms=2, max.terms=5)") plot(update(rock.ppr, bass = 5), main = "update(..., bass = 5)") plot(update(rock.ppr, sm.method = "gcv", gcvpen = 2), main = "update(..., sm.method=\"gcv\", gcvpen=2)")
require(graphics) rock1 <- within(rock, { area1 <- area/10000; peri1 <- peri/10000 }) par(mfrow = c(3,2)) # maybe: , pty = "s" rock.ppr <- ppr(log(perm) ~ area1 + peri1 + shape, data = rock1, nterms = 2, max.terms = 5) plot(rock.ppr, main = "ppr(log(perm)~ ., nterms=2, max.terms=5)") plot(update(rock.ppr, bass = 5), main = "update(..., bass = 5)") plot(update(rock.ppr, sm.method = "gcv", gcvpen = 2), main = "update(..., sm.method=\"gcv\", gcvpen=2)")
plot
and pairs
methods for objects of
class "profile"
.
## S3 method for class 'profile' plot(x, ...) ## S3 method for class 'profile' pairs(x, colours = 2:3, which = names(x), ...)
## S3 method for class 'profile' plot(x, ...) ## S3 method for class 'profile' pairs(x, colours = 2:3, which = names(x), ...)
x |
an object inheriting from class |
colours |
colours to be used for the mean curves conditional on
|
which |
names or number of parameters in pairs plot |
... |
arguments passed to or from other methods. |
This is the main plot
method for objects created by
profile.glm
. It can also be called on objects created
by profile.nls
, but they have a specific method,
plot.profile.nls
.
The pairs
method shows, for each pair of parameters x and
y, two curves intersecting at the maximum likelihood estimate, which
give the loci of the points at which the tangents to the contours of
the bivariate profile likelihood become vertical and horizontal,
respectively. In the case of an exactly bivariate normal profile
likelihood, these two curves would be straight lines giving the
conditional means of y|x and x|y, and the contours would be exactly
elliptical. The which
argument allows you to select a subset
of parameters; the default corresponds to the set of parameters that have
been profiled.
Originally, D. M. Bates and W. N. Venables for S (in 1996). Taken from MASS where these functions were re-written by B. D. Ripley for R (by 1998).
## see ?profile.glm for another example using glm fits. ## a version of example(profile.nls) from R >= 2.8.0 fm1 <- nls(demand ~ SSasympOrig(Time, A, lrc), data = BOD) pr1 <- profile(fm1, alphamax = 0.1) stats:::plot.profile(pr1) ## override dispatch to plot.profile.nls pairs(pr1) # a little odd since the parameters are highly correlated ## an example from ?nls x <- -(1:100)/10 y <- 100 + 10 * exp(x / 2) + rnorm(x)/10 nlmod <- nls(y ~ Const + A * exp(B * x), start=list(Const=100, A=10, B=1)) pairs(profile(nlmod)) ## example from Dobson (1990) (see ?glm) counts <- c(18,17,15,20,10,20,25,13,12) outcome <- gl(3,1,9) treatment <- gl(3,3) ## this example is only formally a Poisson model. It is really a ## comparison of 3 multinomials. Only the interaction parameters are of ## interest. glm.D93i <- glm(counts ~ outcome * treatment, family = poisson()) pr1 <- profile(glm.D93i) pr2 <- profile(glm.D93i, which=6:9) plot(pr1) plot(pr2) pairs(pr1) pairs(pr2)
## see ?profile.glm for another example using glm fits. ## a version of example(profile.nls) from R >= 2.8.0 fm1 <- nls(demand ~ SSasympOrig(Time, A, lrc), data = BOD) pr1 <- profile(fm1, alphamax = 0.1) stats:::plot.profile(pr1) ## override dispatch to plot.profile.nls pairs(pr1) # a little odd since the parameters are highly correlated ## an example from ?nls x <- -(1:100)/10 y <- 100 + 10 * exp(x / 2) + rnorm(x)/10 nlmod <- nls(y ~ Const + A * exp(B * x), start=list(Const=100, A=10, B=1)) pairs(profile(nlmod)) ## example from Dobson (1990) (see ?glm) counts <- c(18,17,15,20,10,20,25,13,12) outcome <- gl(3,1,9) treatment <- gl(3,3) ## this example is only formally a Poisson model. It is really a ## comparison of 3 multinomials. Only the interaction parameters are of ## interest. glm.D93i <- glm(counts ~ outcome * treatment, family = poisson()) pr1 <- profile(glm.D93i) pr2 <- profile(glm.D93i, which=6:9) plot(pr1) plot(pr2) pairs(pr1) pairs(pr2)
profile.nls
ObjectDisplays a series of plots of the profile t function and interpolated
confidence intervals for the parameters in a nonlinear regression
model that has been fit with nls
and profiled with
profile.nls
.
## S3 method for class 'profile.nls' plot(x, levels, conf = c(99, 95, 90, 80, 50)/100, absVal = TRUE, ylab = NULL, lty = 2, ...)
## S3 method for class 'profile.nls' plot(x, levels, conf = c(99, 95, 90, 80, 50)/100, absVal = TRUE, ylab = NULL, lty = 2, ...)
x |
an object of class |
levels |
levels, on the scale of the absolute value of a t
statistic, at which to interpolate intervals. Usually |
conf |
a numeric vector of confidence levels for profile-based
confidence intervals on the parameters.
Defaults to |
absVal |
a logical value indicating whether or not the plots
should be on the scale of the absolute value of the profile t.
Defaults to |
lty |
the line type to be used for axis and dropped lines. |
ylab , ...
|
other arguments to the |
The plots are produced in a set of hard-coded colours, but as these
are coded by number their effect can be changed by setting the
palette
. Colour 1 is used for the axes and 4 for the
profile itself. Colours 3 and 6 are used for the axis line at zero and
the horizontal/vertical lines dropping to the axes.
Douglas M. Bates and Saikat DebRoy
Bates, D.M. and Watts, D.G. (1988), Nonlinear Regression Analysis and Its Applications, Wiley (chapter 6)
require(graphics) # obtain the fitted object fm1 <- nls(demand ~ SSasympOrig(Time, A, lrc), data = BOD) # get the profile for the fitted model pr1 <- profile(fm1, alphamax = 0.05) opar <- par(mfrow = c(2,2), oma = c(1.1, 0, 1.1, 0), las = 1) plot(pr1, conf = c(95, 90, 80, 50)/100) plot(pr1, conf = c(95, 90, 80, 50)/100, absVal = FALSE) mtext("Confidence intervals based on the profile sum of squares", side = 3, outer = TRUE) mtext("BOD data - confidence levels of 50%, 80%, 90% and 95%", side = 1, outer = TRUE) par(opar)
require(graphics) # obtain the fitted object fm1 <- nls(demand ~ SSasympOrig(Time, A, lrc), data = BOD) # get the profile for the fitted model pr1 <- profile(fm1, alphamax = 0.05) opar <- par(mfrow = c(2,2), oma = c(1.1, 0, 1.1, 0), las = 1) plot(pr1, conf = c(95, 90, 80, 50)/100) plot(pr1, conf = c(95, 90, 80, 50)/100, absVal = FALSE) mtext("Confidence intervals based on the profile sum of squares", side = 3, outer = TRUE) mtext("BOD data - confidence levels of 50%, 80%, 90% and 95%", side = 1, outer = TRUE) par(opar)
Plotting method for objects of class "spec"
. For multivariate
time series it plots the marginal spectra of the series or pairs plots
of the coherency and phase of the cross-spectra.
## S3 method for class 'spec' plot(x, add = FALSE, ci = 0.95, log = c("yes", "dB", "no"), xlab = "frequency", ylab = NULL, type = "l", ci.col = "blue", ci.lty = 3, main = NULL, sub = NULL, plot.type = c("marginal", "coherency", "phase"), ...) plot.spec.phase(x, ci = 0.95, xlab = "frequency", ylab = "phase", ylim = c(-pi, pi), type = "l", main = NULL, ci.col = "blue", ci.lty = 3, ...) plot.spec.coherency(x, ci = 0.95, xlab = "frequency", ylab = "squared coherency", ylim = c(0, 1), type = "l", main = NULL, ci.col = "blue", ci.lty = 3, ...)
## S3 method for class 'spec' plot(x, add = FALSE, ci = 0.95, log = c("yes", "dB", "no"), xlab = "frequency", ylab = NULL, type = "l", ci.col = "blue", ci.lty = 3, main = NULL, sub = NULL, plot.type = c("marginal", "coherency", "phase"), ...) plot.spec.phase(x, ci = 0.95, xlab = "frequency", ylab = "phase", ylim = c(-pi, pi), type = "l", main = NULL, ci.col = "blue", ci.lty = 3, ...) plot.spec.coherency(x, ci = 0.95, xlab = "frequency", ylab = "squared coherency", ylim = c(0, 1), type = "l", main = NULL, ci.col = "blue", ci.lty = 3, ...)
x |
an object of class |
add |
logical. If |
ci |
coverage probability for confidence interval. Plotting of
the confidence bar/limits is omitted unless |
log |
If |
xlab |
the x label of the plot. |
ylab |
the y label of the plot. If missing a suitable label will be constructed. |
type |
the type of plot to be drawn, defaults to lines. |
ci.col |
colour for plotting confidence bar or confidence intervals for coherency and phase. |
ci.lty |
line type for confidence intervals for coherency and phase. |
main |
overall title for the plot. If missing, a suitable title is constructed. |
sub |
a subtitle for the plot. Only used for |
plot.type |
For multivariate time series, the type of plot required. Only the first character is needed. |
ylim , ...
|
Graphical parameters. |
Method of the generic plot
for stepfun
objects and utility for plotting piecewise constant functions.
## S3 method for class 'stepfun' plot(x, xval, xlim, ylim = range(c(y, Fn.kn)), xlab = "x", ylab = "f(x)", main = NULL, add = FALSE, verticals = TRUE, do.points = (n < 1000), pch = par("pch"), col = par("col"), col.points = col, cex.points = par("cex"), col.hor = col, col.vert = col, lty = par("lty"), lwd = par("lwd"), ...) ## S3 method for class 'stepfun' lines(x, ...)
## S3 method for class 'stepfun' plot(x, xval, xlim, ylim = range(c(y, Fn.kn)), xlab = "x", ylab = "f(x)", main = NULL, add = FALSE, verticals = TRUE, do.points = (n < 1000), pch = par("pch"), col = par("col"), col.points = col, cex.points = par("cex"), col.hor = col, col.vert = col, lty = par("lty"), lwd = par("lwd"), ...) ## S3 method for class 'stepfun' lines(x, ...)
x |
an R object inheriting from |
xval |
numeric vector of abscissa values at which to evaluate
|
xlim , ylim
|
limits for the plot region: see
|
xlab , ylab
|
labels for x and y axis. |
main |
main title. |
add |
logical; if |
verticals |
logical; if |
do.points |
logical; if |
pch |
character; point character if |
col |
default color of all points and lines. |
col.points |
character or integer code; color of points if
|
cex.points |
numeric; character expansion factor if |
col.hor |
color of horizontal lines. |
col.vert |
color of vertical lines. |
lty , lwd
|
line type and thickness for all lines. |
... |
A list with two components
t |
abscissa (x) values, including the two outermost ones. |
y |
y values ‘in between’ the |
Martin Maechler [email protected], 1990, 1993; ported to R, 1997.
ecdf
for empirical distribution functions as
special step functions,
approxfun
and splinefun
.
require(graphics) y0 <- c(1,2,4,3) sfun0 <- stepfun(1:3, y0, f = 0) sfun.2 <- stepfun(1:3, y0, f = .2) sfun1 <- stepfun(1:3, y0, right = TRUE) tt <- seq(0, 3, by = 0.1) op <- par(mfrow = c(2,2)) plot(sfun0); plot(sfun0, xval = tt, add = TRUE, col.hor = "bisque") plot(sfun.2);plot(sfun.2, xval = tt, add = TRUE, col = "orange") # all colors plot(sfun1);lines(sfun1, xval = tt, col.hor = "coral") ##-- This is revealing : plot(sfun0, verticals = FALSE, main = "stepfun(x, y0, f=f) for f = 0, .2, 1") for(i in 1:3) lines(list(sfun0, sfun.2, stepfun(1:3, y0, f = 1))[[i]], col = i) legend(2.5, 1.9, paste("f =", c(0, 0.2, 1)), col = 1:3, lty = 1, y.intersp = 1) par(op) # Extend and/or restrict 'viewport': plot(sfun0, xlim = c(0,5), ylim = c(0, 3.5), main = "plot(stepfun(*), xlim= . , ylim = .)") ##-- this works too (automatic call to ecdf(.)): plot.stepfun(rt(50, df = 3), col.vert = "gray20")
require(graphics) y0 <- c(1,2,4,3) sfun0 <- stepfun(1:3, y0, f = 0) sfun.2 <- stepfun(1:3, y0, f = .2) sfun1 <- stepfun(1:3, y0, right = TRUE) tt <- seq(0, 3, by = 0.1) op <- par(mfrow = c(2,2)) plot(sfun0); plot(sfun0, xval = tt, add = TRUE, col.hor = "bisque") plot(sfun.2);plot(sfun.2, xval = tt, add = TRUE, col = "orange") # all colors plot(sfun1);lines(sfun1, xval = tt, col.hor = "coral") ##-- This is revealing : plot(sfun0, verticals = FALSE, main = "stepfun(x, y0, f=f) for f = 0, .2, 1") for(i in 1:3) lines(list(sfun0, sfun.2, stepfun(1:3, y0, f = 1))[[i]], col = i) legend(2.5, 1.9, paste("f =", c(0, 0.2, 1)), col = 1:3, lty = 1, y.intersp = 1) par(op) # Extend and/or restrict 'viewport': plot(sfun0, xlim = c(0,5), ylim = c(0, 3.5), main = "plot(stepfun(*), xlim= . , ylim = .)") ##-- this works too (automatic call to ecdf(.)): plot.stepfun(rt(50, df = 3), col.vert = "gray20")
Plotting method for objects inheriting from class "ts"
.
## S3 method for class 'ts' plot(x, y = NULL, plot.type = c("multiple", "single"), xy.labels, xy.lines, panel = lines, nc, yax.flip = FALSE, mar.multi = c(0, 5.1, 0, if(yax.flip) 5.1 else 2.1), oma.multi = c(6, 0, 5, 0), axes = TRUE, ...) ## S3 method for class 'ts' lines(x, ...)
## S3 method for class 'ts' plot(x, y = NULL, plot.type = c("multiple", "single"), xy.labels, xy.lines, panel = lines, nc, yax.flip = FALSE, mar.multi = c(0, 5.1, 0, if(yax.flip) 5.1 else 2.1), oma.multi = c(6, 0, 5, 0), axes = TRUE, ...) ## S3 method for class 'ts' lines(x, ...)
x , y
|
time series objects, usually inheriting from class |
plot.type |
for multivariate time series, should the series by plotted separately (with a common time axis) or on a single plot? Can be abbreviated. |
xy.labels |
logical, indicating if |
xy.lines |
logical, indicating if |
panel |
a |
nc |
the number of columns to use when |
yax.flip |
logical indicating if the y-axis (ticks and numbering)
should flip from side 2 (left) to 4 (right) from series to series
when |
mar.multi , oma.multi
|
the (default) |
axes |
logical indicating if x- and y- axes should be drawn. |
... |
additional graphical arguments, see |
If y
is missing, this function creates a time series
plot, for multivariate series of one of two kinds depending on
plot.type
.
If y
is present, both x
and y
must be univariate,
and a scatter plot y ~ x
will be drawn, enhanced by
using text
if xy.labels
is
TRUE
or character
, and lines
if
xy.lines
is TRUE
.
ts
for basic time series construction and access
functionality.
require(graphics) ## Multivariate z <- ts(matrix(rt(200 * 8, df = 3), 200, 8), start = c(1961, 1), frequency = 12) plot(z, yax.flip = TRUE) plot(z, axes = FALSE, ann = FALSE, frame.plot = TRUE, mar.multi = c(0,0,0,0), oma.multi = c(1,1,5,1)) title("plot(ts(..), axes=FALSE, ann=FALSE, frame.plot=TRUE, mar..., oma...)") z <- window(z[,1:3], end = c(1969,12)) plot(z, type = "b") # multiple plot(z, plot.type = "single", lty = 1:3, col = 4:2) ## A phase plot: plot(nhtemp, lag(nhtemp, 1), cex = .8, col = "blue", main = "Lag plot of New Haven temperatures") ## xy.lines and xy.labels are FALSE for large series: plot(lag(sunspots, 1), sunspots, pch = ".") SMI <- EuStockMarkets[, "SMI"] plot(lag(SMI, 1), SMI, pch = ".") plot(lag(SMI, 20), SMI, pch = ".", log = "xy", main = "4 weeks lagged SMI stocks -- log scale", xy.lines = TRUE)
require(graphics) ## Multivariate z <- ts(matrix(rt(200 * 8, df = 3), 200, 8), start = c(1961, 1), frequency = 12) plot(z, yax.flip = TRUE) plot(z, axes = FALSE, ann = FALSE, frame.plot = TRUE, mar.multi = c(0,0,0,0), oma.multi = c(1,1,5,1)) title("plot(ts(..), axes=FALSE, ann=FALSE, frame.plot=TRUE, mar..., oma...)") z <- window(z[,1:3], end = c(1969,12)) plot(z, type = "b") # multiple plot(z, plot.type = "single", lty = 1:3, col = 4:2) ## A phase plot: plot(nhtemp, lag(nhtemp, 1), cex = .8, col = "blue", main = "Lag plot of New Haven temperatures") ## xy.lines and xy.labels are FALSE for large series: plot(lag(sunspots, 1), sunspots, pch = ".") SMI <- EuStockMarkets[, "SMI"] plot(lag(SMI, 1), SMI, pch = ".") plot(lag(SMI, 20), SMI, pch = ".", log = "xy", main = "4 weeks lagged SMI stocks -- log scale", xy.lines = TRUE)
Density, distribution function, quantile function and random
generation for the Poisson distribution with parameter lambda
.
dpois(x, lambda, log = FALSE) ppois(q, lambda, lower.tail = TRUE, log.p = FALSE) qpois(p, lambda, lower.tail = TRUE, log.p = FALSE) rpois(n, lambda)
dpois(x, lambda, log = FALSE) ppois(q, lambda, lower.tail = TRUE, log.p = FALSE) qpois(p, lambda, lower.tail = TRUE, log.p = FALSE) rpois(n, lambda)
x |
vector of (non-negative integer) quantiles. |
q |
vector of quantiles. |
p |
vector of probabilities. |
n |
number of random values to return. |
lambda |
vector of (non-negative) means. |
log , log.p
|
logical; if TRUE, probabilities p are given as log(p). |
lower.tail |
logical; if TRUE (default), probabilities are
|
The Poisson distribution has density
for .
The mean and variance are
.
Note that is really a limit case (setting
) resulting in a point mass at
, see also the example.
If an element of x
is not integer, the result of dpois
is zero, with a warning.
is computed using Loader's algorithm, see the reference in
dbinom
.
The quantile is right continuous: qpois(p, lambda)
is the smallest
integer such that
.
Setting lower.tail = FALSE
allows to get much more precise
results when the default, lower.tail = TRUE
would return 1, see
the example below.
dpois
gives the (log) density,
ppois
gives the (log) distribution function,
qpois
gives the quantile function, and
rpois
generates random deviates.
Invalid lambda
will result in return value NaN
, with a warning.
The length of the result is determined by n
for
rpois
, and is the maximum of the lengths of the
numerical arguments for the other functions.
The numerical arguments other than n
are recycled to the
length of the result. Only the first elements of the logical
arguments are used.
rpois
returns a vector of type integer unless generated
values exceed the maximum representable integer when double
values are returned.
dpois
uses C code contributed by Catherine Loader
(see dbinom
).
ppois
uses pgamma
.
qpois
uses the Cornish–Fisher Expansion to include a skewness
correction to a normal approximation, followed by a search.
rpois
uses
Ahrens, J. H. and Dieter, U. (1982). Computer generation of Poisson deviates from modified normal distributions. ACM Transactions on Mathematical Software, 8, 163–179.
Distributions for other standard distributions, including
dbinom
for the binomial and dnbinom
for
the negative binomial distribution.
require(graphics) -log(dpois(0:7, lambda = 1) * gamma(1+ 0:7)) # == 1 Ni <- rpois(50, lambda = 4); table(factor(Ni, 0:max(Ni))) 1 - ppois(10*(15:25), lambda = 100) # becomes 0 (cancellation) ppois(10*(15:25), lambda = 100, lower.tail = FALSE) # no cancellation par(mfrow = c(2, 1)) x <- seq(-0.01, 5, 0.01) plot(x, ppois(x, 1), type = "s", ylab = "F(x)", main = "Poisson(1) CDF") plot(x, pbinom(x, 100, 0.01), type = "s", ylab = "F(x)", main = "Binomial(100, 0.01) CDF") ## The (limit) case lambda = 0 : stopifnot(identical(dpois(0,0), 1), identical(ppois(0,0), 1), identical(qpois(1,0), 0))
require(graphics) -log(dpois(0:7, lambda = 1) * gamma(1+ 0:7)) # == 1 Ni <- rpois(50, lambda = 4); table(factor(Ni, 0:max(Ni))) 1 - ppois(10*(15:25), lambda = 100) # becomes 0 (cancellation) ppois(10*(15:25), lambda = 100, lower.tail = FALSE) # no cancellation par(mfrow = c(2, 1)) x <- seq(-0.01, 5, 0.01) plot(x, ppois(x, 1), type = "s", ylab = "F(x)", main = "Poisson(1) CDF") plot(x, pbinom(x, 100, 0.01), type = "s", ylab = "F(x)", main = "Binomial(100, 0.01) CDF") ## The (limit) case lambda = 0 : stopifnot(identical(dpois(0,0), 1), identical(ppois(0,0), 1), identical(qpois(1,0), 0))
Performs an exact test of a simple null hypothesis about the rate parameter in Poisson distribution, or for the ratio between two rate parameters.
poisson.test(x, T = 1, r = 1, alternative = c("two.sided", "less", "greater"), conf.level = 0.95)
poisson.test(x, T = 1, r = 1, alternative = c("two.sided", "less", "greater"), conf.level = 0.95)
x |
number of events. A vector of length one or two. |
T |
time base for event count. A vector of length one or two. |
r |
hypothesized rate or rate ratio |
alternative |
indicates the alternative hypothesis and must be
one of |
conf.level |
confidence level for the returned confidence interval. |
Confidence intervals are computed similarly to those of
binom.test
in the one-sample case, and using
binom.test
in the two sample case.
A list with class "htest"
containing the following components:
statistic |
the number of events (in the first sample if there are two.) |
parameter |
the corresponding expected count |
p.value |
the p-value of the test. |
conf.int |
a confidence interval for the rate or rate ratio. |
estimate |
the estimated rate or rate ratio. |
null.value |
the rate or rate ratio under the null,
|
alternative |
a character string describing the alternative hypothesis. |
method |
the character string |
data.name |
a character string giving the names of the data. |
The rate parameter in Poisson data is often given based on a
“time on test” or similar quantity (person-years, population
size, or expected number of cases from mortality tables). This is the
role of the T
argument.
The one-sample case is effectively the binomial test with a very large
n
. The two sample case is converted to a binomial test by
conditioning on the total event count, and the rate ratio is directly
related to the odds in that binomial distribution.
### These are paraphrased from data sets in the ISwR package ## SMR, Welsh Nickel workers poisson.test(137, 24.19893) ## eba1977, compare Fredericia to other three cities for ages 55-59 poisson.test(c(11, 6+8+7), c(800, 1083+1050+878))
### These are paraphrased from data sets in the ISwR package ## SMR, Welsh Nickel workers poisson.test(137, 24.19893) ## eba1977, compare Fredericia to other three cities for ages 55-59 poisson.test(c(11, 6+8+7), c(800, 1083+1050+878))
Returns or evaluates orthogonal polynomials of degree 1 to
degree
over the specified set of points x
: these are all
orthogonal to the constant polynomial of degree 0. Alternatively,
evaluate raw polynomials.
poly(x, ..., degree = 1, coefs = NULL, raw = FALSE, simple = FALSE) polym (..., degree = 1, coefs = NULL, raw = FALSE) ## S3 method for class 'poly' predict(object, newdata, ...)
poly(x, ..., degree = 1, coefs = NULL, raw = FALSE, simple = FALSE) polym (..., degree = 1, coefs = NULL, raw = FALSE) ## S3 method for class 'poly' predict(object, newdata, ...)
x , newdata
|
a numeric vector or an object with |
degree |
the degree of the polynomial. Must be less than the
number of unique points when |
coefs |
for prediction, coefficients from a previous fit. |
raw |
if true, use raw and not orthogonal polynomials. |
simple |
logical indicating if a simple matrix (with no further
|
object |
an object inheriting from class |
... |
|
Although formally degree
should be named (as it follows
...
), an unnamed second argument of length 1 will be
interpreted as the degree, such that poly(x, 3)
can be used in
formulas.
The orthogonal polynomial is summarized by the coefficients, which can
be used to evaluate it via the three-term recursion given in Kennedy
& Gentle (1980, pp. 343–4), and used in the predict
part of
the code.
poly
using ...
is just a convenience wrapper for
polym
: coef
is ignored. Conversely, if polym
is
called with a single argument in ...
it is a wrapper for
poly
.
For poly
and polym()
(when simple=FALSE
and
coefs=NULL
as per default):
A matrix with rows corresponding to points in x
and columns
corresponding to the degree, with attributes "degree"
specifying
the degrees of the columns and (unless raw = TRUE
)
"coefs"
which contains the centering and normalization
constants used in constructing the orthogonal polynomials and
class c("poly", "matrix")
.
For poly(*, simple=TRUE)
, polym(*, coefs=<non-NULL>)
,
and predict.poly()
: a matrix.
This routine is intended for statistical purposes such as
contr.poly
: it does not attempt to orthogonalize to
machine accuracy.
R Core Team. Keith Jewell (Campden BRI Group, UK) contributed improvements for correct prediction on subsets.
Chambers, J. M. and Hastie, T. J. (1992) Statistical Models in S. Wadsworth & Brooks/Cole.
Kennedy, W. J. Jr and Gentle, J. E. (1980) Statistical Computing Marcel Dekker.
cars
for an example of polynomial regression.
od <- options(digits = 3) # avoid too much visual clutter (z <- poly(1:10, 3)) predict(z, seq(2, 4, 0.5)) zapsmall(poly(seq(4, 6, 0.5), 3, coefs = attr(z, "coefs"))) zm <- zapsmall(polym ( 1:4, c(1, 4:6), degree = 3)) # or just poly(): (z1 <- zapsmall(poly(cbind(1:4, c(1, 4:6)), degree = 3))) ## they are the same : stopifnot(all.equal(zm, z1, tolerance = 1e-15)) ## poly(<matrix>, df) --- used to fail till July 14 (vive la France!), 2017: m2 <- cbind(1:4, c(1, 4:6)) pm2 <- zapsmall(poly(m2, 3)) # "unnamed degree = 3" stopifnot(all.equal(pm2, zm, tolerance = 1e-15)) options(od)
od <- options(digits = 3) # avoid too much visual clutter (z <- poly(1:10, 3)) predict(z, seq(2, 4, 0.5)) zapsmall(poly(seq(4, 6, 0.5), 3, coefs = attr(z, "coefs"))) zm <- zapsmall(polym ( 1:4, c(1, 4:6), degree = 3)) # or just poly(): (z1 <- zapsmall(poly(cbind(1:4, c(1, 4:6)), degree = 3))) ## they are the same : stopifnot(all.equal(zm, z1, tolerance = 1e-15)) ## poly(<matrix>, df) --- used to fail till July 14 (vive la France!), 2017: m2 <- cbind(1:4, c(1, 4:6)) pm2 <- zapsmall(poly(m2, 3)) # "unnamed degree = 3" stopifnot(all.equal(pm2, zm, tolerance = 1e-15)) options(od)
Creates a link object based on the link function
.
power(lambda = 1)
power(lambda = 1)
lambda |
a real number. |
If lambda
is non-positive, it is taken as zero, and the log
link is obtained. The default lambda = 1
gives the identity
link.
A list with components linkfun
, linkinv
,
mu.eta
, and valideta
.
See make.link
for information on their meaning.
Chambers, J. M. and Hastie, T. J. (1992) Statistical Models in S. Wadsworth & Brooks/Cole.
To raise a number to a power, see Arithmetic
.
To calculate the power of a test, see various functions in the
stats package, e.g., power.t.test
.
power() quasi(link = power(1/3))[c("linkfun", "linkinv")]
power() quasi(link = power(1/3))[c("linkfun", "linkinv")]
Compute power of test or determine parameters to obtain target power.
power.anova.test(groups = NULL, n = NULL, between.var = NULL, within.var = NULL, sig.level = 0.05, power = NULL)
power.anova.test(groups = NULL, n = NULL, between.var = NULL, within.var = NULL, sig.level = 0.05, power = NULL)
groups |
Number of groups |
n |
Number of observations (per group) |
between.var |
Between group variance |
within.var |
Within group variance |
sig.level |
Significance level (Type I error probability) |
power |
Power of test (1 minus Type II error probability) |
Exactly one of the parameters groups
, n
, between.var
,
power
, within.var
, and sig.level
must be passed as NULL,
and that parameter is determined from the others. Notice that
sig.level
has non-NULL default so NULL must be explicitly
passed if you want it computed.
Object of class "power.htest"
, a list of the arguments
(including the computed one) augmented with method
and
note
elements.
uniroot
is used to solve power equation for unknowns, so
you may see errors from it, notably about inability to bracket the
root when invalid arguments are given.
Claus Ekstrøm
power.anova.test(groups = 4, n = 5, between.var = 1, within.var = 3) # Power = 0.3535594 power.anova.test(groups = 4, between.var = 1, within.var = 3, power = .80) # n = 11.92613 ## Assume we have prior knowledge of the group means: groupmeans <- c(120, 130, 140, 150) power.anova.test(groups = length(groupmeans), between.var = var(groupmeans), within.var = 500, power = .90) # n = 15.18834
power.anova.test(groups = 4, n = 5, between.var = 1, within.var = 3) # Power = 0.3535594 power.anova.test(groups = 4, between.var = 1, within.var = 3, power = .80) # n = 11.92613 ## Assume we have prior knowledge of the group means: groupmeans <- c(120, 130, 140, 150) power.anova.test(groups = length(groupmeans), between.var = var(groupmeans), within.var = 500, power = .90) # n = 15.18834
Compute the power of the two-sample test for proportions, or determine parameters to obtain a target power.
power.prop.test(n = NULL, p1 = NULL, p2 = NULL, sig.level = 0.05, power = NULL, alternative = c("two.sided", "one.sided"), strict = FALSE, tol = .Machine$double.eps^0.25)
power.prop.test(n = NULL, p1 = NULL, p2 = NULL, sig.level = 0.05, power = NULL, alternative = c("two.sided", "one.sided"), strict = FALSE, tol = .Machine$double.eps^0.25)
n |
number of observations (per group) |
p1 |
probability in one group |
p2 |
probability in other group |
sig.level |
significance level (Type I error probability) |
power |
power of test (1 minus Type II error probability) |
alternative |
one- or two-sided test. Can be abbreviated. |
strict |
use strict interpretation in two-sided case |
tol |
numerical tolerance used in root finding, the default providing (at least) four significant digits. |
Exactly one of the parameters n
, p1
, p2
,
power
, and sig.level
must be passed as NULL, and that
parameter is determined from the others. Notice that sig.level
has a non-NULL default so NULL
must be explicitly passed if you
want it computed.
If strict = TRUE
is used, the power will include the probability of
rejection in the opposite direction of the true effect, in the two-sided
case. Without this the power will be half the significance level if the
true difference is zero.
Note that not all conditions can be satisfied, e.g., for
power.prop.test(n=30, p1=0.90, p2=NULL, power=0.8, strict=TRUE)
there is no proportion p2
between p1 = 0.9
and 1, as
you'd need a sample size of at least to yield the
desired power for
.
For these impossible conditions, currently a warning
(warning
) is signalled which may become an error
(stop
) in the future.
Object of class "power.htest"
, a list of the arguments
(including the computed one) augmented with method
and
note
elements.
uniroot
is used to solve power equation for unknowns, so
you may see errors from it, notably about inability to bracket the
root when invalid arguments are given. If one of p1
and
p2
is computed, then is assumed and will hold,
but if you specify both,
is allowed.
Peter Dalgaard. Based on previous work by Claus Ekstrøm
power.prop.test(n = 50, p1 = .50, p2 = .75) ## => power = 0.740 power.prop.test(p1 = .50, p2 = .75, power = .90) ## => n = 76.7 power.prop.test(n = 50, p1 = .5, power = .90) ## => p2 = 0.8026 power.prop.test(n = 50, p1 = .5, p2 = 0.9, power = .90, sig.level=NULL) ## => sig.l = 0.00131 power.prop.test(p1 = .5, p2 = 0.501, sig.level=.001, power=0.90) ## => n = 10451937 try( power.prop.test(n=30, p1=0.90, p2=NULL, power=0.8) ) # a warning (which may become an error) ## Reason: power.prop.test( p1=0.90, p2= 1.0, power=0.8) ##-> n = 73.37
power.prop.test(n = 50, p1 = .50, p2 = .75) ## => power = 0.740 power.prop.test(p1 = .50, p2 = .75, power = .90) ## => n = 76.7 power.prop.test(n = 50, p1 = .5, power = .90) ## => p2 = 0.8026 power.prop.test(n = 50, p1 = .5, p2 = 0.9, power = .90, sig.level=NULL) ## => sig.l = 0.00131 power.prop.test(p1 = .5, p2 = 0.501, sig.level=.001, power=0.90) ## => n = 10451937 try( power.prop.test(n=30, p1=0.90, p2=NULL, power=0.8) ) # a warning (which may become an error) ## Reason: power.prop.test( p1=0.90, p2= 1.0, power=0.8) ##-> n = 73.37
Compute the power of the one- or two- sample t test, or determine parameters to obtain a target power.
power.t.test(n = NULL, delta = NULL, sd = 1, sig.level = 0.05, power = NULL, type = c("two.sample", "one.sample", "paired"), alternative = c("two.sided", "one.sided"), strict = FALSE, tol = .Machine$double.eps^0.25)
power.t.test(n = NULL, delta = NULL, sd = 1, sig.level = 0.05, power = NULL, type = c("two.sample", "one.sample", "paired"), alternative = c("two.sided", "one.sided"), strict = FALSE, tol = .Machine$double.eps^0.25)
n |
number of observations (per group) |
delta |
true difference in means |
sd |
standard deviation |
sig.level |
significance level (Type I error probability) |
power |
power of test (1 minus Type II error probability) |
type |
string specifying the type of t test. Can be abbreviated. |
alternative |
one- or two-sided test. Can be abbreviated. |
strict |
use strict interpretation in two-sided case |
tol |
numerical tolerance used in root finding, the default providing (at least) four significant digits. |
Exactly one of the parameters n
, delta
, power
,
sd
, and sig.level
must be passed as NULL
, and that
parameter is determined from the others. Notice that the last two have
non-NULL defaults, so NULL must be explicitly passed if you want to
compute them.
If strict = TRUE
is used, the power will include the probability of
rejection in the opposite direction of the true effect, in the two-sided
case. Without this the power will be half the significance level if the
true difference is zero.
Object of class "power.htest"
, a list of the arguments
(including the computed one) augmented with method
and
note
elements.
uniroot
is used to solve the power equation for unknowns, so
you may see errors from it, notably about inability to bracket the
root when invalid arguments are given.
Peter Dalgaard. Based on previous work by Claus Ekstrøm
power.t.test(n = 20, delta = 1) power.t.test(power = .90, delta = 1) power.t.test(power = .90, delta = 1, alternative = "one.sided")
power.t.test(n = 20, delta = 1) power.t.test(power = .90, delta = 1) power.t.test(power = .90, delta = 1, alternative = "one.sided")
Computes the Phillips-Perron test for the null hypothesis that
x
has a unit root against a stationary alternative.
PP.test(x, lshort = TRUE)
PP.test(x, lshort = TRUE)
x |
a numeric vector or univariate time series. |
lshort |
a logical indicating whether the short or long version of the truncation lag parameter is used. |
The general regression equation which incorporates a constant and a
linear trend is used and the corrected t-statistic for a first order
autoregressive coefficient equals one is computed. To estimate
sigma^2
the Newey-West estimator is used. If lshort
is TRUE
, then the truncation lag parameter is set to
trunc(4*(n/100)^0.25)
, otherwise
trunc(12*(n/100)^0.25)
is used. The p-values are
interpolated from Table 4.2, page 103 of
Banerjee et al. (1993).
Missing values are not handled.
A list with class "htest"
containing the following components:
statistic |
the value of the test statistic. |
parameter |
the truncation lag parameter. |
p.value |
the p-value of the test. |
method |
a character string indicating what type of test was performed. |
data.name |
a character string giving the name of the data. |
A. Trapletti
A. Banerjee, J. J. Dolado, J. W. Galbraith, and D. F. Hendry (1993). Cointegration, Error Correction, and the Econometric Analysis of Non-Stationary Data. Oxford University Press, Oxford.
P. Perron (1988). Trends and random walks in macroeconomic time series. Journal of Economic Dynamics and Control, 12, 297–332. doi:10.1016/0165-1889(88)90043-7.
x <- rnorm(1000) PP.test(x) y <- cumsum(x) # has unit root PP.test(y)
x <- rnorm(1000) PP.test(x) y <- cumsum(x) # has unit root PP.test(y)
Generates the sequence of probability points
(1:m - a)/(m + (1-a)-a)
where m
is either n
, if length(n)==1
, or
length(n)
.
ppoints(n, a = if(n <= 10) 3/8 else 1/2)
ppoints(n, a = if(n <= 10) 3/8 else 1/2)
n |
either the number of points generated or a vector of observations. |
a |
the offset fraction to be used; typically in |
If , the resulting values are within
(excluding boundaries).
In any case, the resulting sequence is symmetric in
, i.e.,
p + rev(p) == 1
.
ppoints()
is used in qqplot
and qqnorm
to generate
the set of probabilities at which to evaluate the inverse distribution.
The choice of a
follows the documentation of the function of the
same name in Becker et al. (1988), and appears to have been
motivated by results from Blom (1958) on approximations to expect normal
order statistics (see also quantile
).
The probability points for the continuous sample quantile types 5 to 9
(see quantile
) can be obtained by taking a
as,
respectively, 1/2, 0, 1, 1/3, and 3/8.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
Blom, G. (1958) Statistical Estimates and Transformed Beta Variables. Wiley
ppoints(4) # the same as ppoints(1:4) ppoints(10) ppoints(10, a = 1/2) ## Visualize including the fractions : require(graphics) p.ppoints <- function(n, ..., add = FALSE, col = par("col")) { pn <- ppoints(n, ...) if(add) points(pn, pn, col = col) else { tit <- match.call(); tit[[1]] <- quote(ppoints) plot(pn,pn, main = deparse(tit), col=col, xlim = 0:1, ylim = 0:1, xaxs = "i", yaxs = "i") abline(0, 1, col = adjustcolor(1, 1/4), lty = 3) } if(!add && requireNamespace("MASS", quietly = TRUE)) text(pn, pn, as.character(MASS::fractions(pn)), adj = c(0,0)-1/4, cex = 3/4, xpd = NA, col=col) abline(h = pn, v = pn, col = adjustcolor(col, 1/2), lty = 2, lwd = 1/2) } p.ppoints(4) p.ppoints(10) p.ppoints(10, a = 1/2) p.ppoints(21) p.ppoints(8) ; p.ppoints(8, a = 1/2, add=TRUE, col="tomato")
ppoints(4) # the same as ppoints(1:4) ppoints(10) ppoints(10, a = 1/2) ## Visualize including the fractions : require(graphics) p.ppoints <- function(n, ..., add = FALSE, col = par("col")) { pn <- ppoints(n, ...) if(add) points(pn, pn, col = col) else { tit <- match.call(); tit[[1]] <- quote(ppoints) plot(pn,pn, main = deparse(tit), col=col, xlim = 0:1, ylim = 0:1, xaxs = "i", yaxs = "i") abline(0, 1, col = adjustcolor(1, 1/4), lty = 3) } if(!add && requireNamespace("MASS", quietly = TRUE)) text(pn, pn, as.character(MASS::fractions(pn)), adj = c(0,0)-1/4, cex = 3/4, xpd = NA, col=col) abline(h = pn, v = pn, col = adjustcolor(col, 1/2), lty = 2, lwd = 1/2) } p.ppoints(4) p.ppoints(10) p.ppoints(10, a = 1/2) p.ppoints(21) p.ppoints(8) ; p.ppoints(8, a = 1/2, add=TRUE, col="tomato")
Fit a projection pursuit regression model.
ppr(x, ...) ## S3 method for class 'formula' ppr(formula, data, weights, subset, na.action, contrasts = NULL, ..., model = FALSE) ## Default S3 method: ppr(x, y, weights = rep(1, n), ww = rep(1, q), nterms, max.terms = nterms, optlevel = 2, sm.method = c("supsmu", "spline", "gcvspline"), bass = 0, span = 0, df = 5, gcvpen = 1, trace = FALSE, ...)
ppr(x, ...) ## S3 method for class 'formula' ppr(formula, data, weights, subset, na.action, contrasts = NULL, ..., model = FALSE) ## Default S3 method: ppr(x, y, weights = rep(1, n), ww = rep(1, q), nterms, max.terms = nterms, optlevel = 2, sm.method = c("supsmu", "spline", "gcvspline"), bass = 0, span = 0, df = 5, gcvpen = 1, trace = FALSE, ...)
formula |
a formula specifying one or more numeric response variables and the explanatory variables. |
x |
numeric matrix of explanatory variables. Rows represent observations, and columns represent variables. Missing values are not accepted. |
y |
numeric matrix of response variables. Rows represent observations, and columns represent variables. Missing values are not accepted. |
nterms |
number of terms to include in the final model. |
data |
a data frame (or similar: see |
weights |
a vector of weights |
ww |
a vector of weights for each response, so the fit criterion is
the sum over case |
subset |
an index vector specifying the cases to be used in the training sample. (NOTE: If given, this argument must be named.) |
na.action |
a function to specify the action to be taken if |
contrasts |
the contrasts to be used when any factor explanatory variables are coded. |
max.terms |
maximum number of terms to choose from when building the model. |
optlevel |
integer from 0 to 3 which determines the thoroughness of an optimization routine in the SMART program. See the ‘Details’ section. |
sm.method |
the method used for smoothing the ridge functions. The default is
to use Friedman's super smoother Can be abbreviated. |
bass |
super smoother bass tone control used with automatic span selection
(see |
span |
super smoother span control (see |
df |
if |
gcvpen |
if |
trace |
logical indicating if each spline fit should produce
diagnostic output (about |
... |
arguments to be passed to or from other methods. |
model |
logical. If true, the model frame is returned. |
The basic method is given by Friedman (1984) and based on his code. This code has been shown to be extremely sensitive to the Fortran compiler used.
The algorithm first adds up to max.terms
ridge terms one at a
time; it will use less if it is unable to find a term to add that makes
sufficient difference. It then removes the least
important term at each step until nterms
terms
are left.
The levels of optimization (argument optlevel
)
differ in how thoroughly the models are refitted during this process.
At level 0 the existing ridge terms are not refitted. At level 1
the projection directions are not refitted, but the ridge
functions and the regression coefficients are.
Levels 2 and 3 refit all the terms and are equivalent for one
response; level 3 is more careful to re-balance the contributions
from each regressor at each step and so is a little less likely to
converge to a saddle point of the sum of squares criterion.
A list with the following components, many of which are for use by the method functions.
call |
the matched call |
p |
the number of explanatory variables (after any coding) |
q |
the number of response variables |
mu |
the argument |
ml |
the argument |
gof |
the overall residual (weighted) sum of squares for the selected model |
gofn |
the overall residual (weighted) sum of squares against the
number of terms, up to |
df |
the argument |
edf |
if |
xnames |
the names of the explanatory variables |
ynames |
the names of the response variables |
alpha |
a matrix of the projection directions, with a column for each ridge term |
beta |
a matrix of the coefficients applied for each response to the ridge terms: the rows are the responses and the columns the ridge terms |
yb |
the weighted means of each response |
ys |
the overall scale factor used: internally the responses are
divided by |
fitted.values |
the fitted values, as a matrix if |
residuals |
the residuals, as a matrix if |
smod |
internal work array, which includes the ridge functions evaluated at the training set points. |
model |
(only if |
Friedman (1984): converted to double precision and added interface to smoothing splines by B. D. Ripley, originally for the MASS package.
Friedman, J. H. and Stuetzle, W. (1981). Projection pursuit regression. Journal of the American Statistical Association, 76, 817–823. doi:10.2307/2287576.
Friedman, J. H. (1984). SMART User's Guide. Laboratory for Computational Statistics, Stanford University Technical Report No. 1.
Venables, W. N. and Ripley, B. D. (2002). Modern Applied Statistics with S. Springer.
plot.ppr
, supsmu
, smooth.spline
require(graphics) # Note: your numerical values may differ attach(rock) area1 <- area/10000; peri1 <- peri/10000 rock.ppr <- ppr(log(perm) ~ area1 + peri1 + shape, data = rock, nterms = 2, max.terms = 5) rock.ppr # Call: # ppr.formula(formula = log(perm) ~ area1 + peri1 + shape, data = rock, # nterms = 2, max.terms = 5) # # Goodness of fit: # 2 terms 3 terms 4 terms 5 terms # 8.737806 5.289517 4.745799 4.490378 summary(rock.ppr) # ..... (same as above) # ..... # # Projection direction vectors ('alpha'): # term 1 term 2 # area1 0.34357179 0.37071027 # peri1 -0.93781471 -0.61923542 # shape 0.04961846 0.69218595 # # Coefficients of ridge terms: # term 1 term 2 # 1.6079271 0.5460971 par(mfrow = c(3,2)) # maybe: , pty = "s") plot(rock.ppr, main = "ppr(log(perm)~ ., nterms=2, max.terms=5)") plot(update(rock.ppr, bass = 5), main = "update(..., bass = 5)") plot(update(rock.ppr, sm.method = "gcv", gcvpen = 2), main = "update(..., sm.method=\"gcv\", gcvpen=2)") cbind(perm = rock$perm, prediction = round(exp(predict(rock.ppr)), 1)) detach()
require(graphics) # Note: your numerical values may differ attach(rock) area1 <- area/10000; peri1 <- peri/10000 rock.ppr <- ppr(log(perm) ~ area1 + peri1 + shape, data = rock, nterms = 2, max.terms = 5) rock.ppr # Call: # ppr.formula(formula = log(perm) ~ area1 + peri1 + shape, data = rock, # nterms = 2, max.terms = 5) # # Goodness of fit: # 2 terms 3 terms 4 terms 5 terms # 8.737806 5.289517 4.745799 4.490378 summary(rock.ppr) # ..... (same as above) # ..... # # Projection direction vectors ('alpha'): # term 1 term 2 # area1 0.34357179 0.37071027 # peri1 -0.93781471 -0.61923542 # shape 0.04961846 0.69218595 # # Coefficients of ridge terms: # term 1 term 2 # 1.6079271 0.5460971 par(mfrow = c(3,2)) # maybe: , pty = "s") plot(rock.ppr, main = "ppr(log(perm)~ ., nterms=2, max.terms=5)") plot(update(rock.ppr, bass = 5), main = "update(..., bass = 5)") plot(update(rock.ppr, sm.method = "gcv", gcvpen = 2), main = "update(..., sm.method=\"gcv\", gcvpen=2)") cbind(perm = rock$perm, prediction = round(exp(predict(rock.ppr)), 1)) detach()
Performs a principal components analysis on the given data matrix
and returns the results as an object of class prcomp
.
prcomp(x, ...) ## S3 method for class 'formula' prcomp(formula, data = NULL, subset, na.action, ...) ## Default S3 method: prcomp(x, retx = TRUE, center = TRUE, scale. = FALSE, tol = NULL, rank. = NULL, ...) ## S3 method for class 'prcomp' predict(object, newdata, ...)
prcomp(x, ...) ## S3 method for class 'formula' prcomp(formula, data = NULL, subset, na.action, ...) ## Default S3 method: prcomp(x, retx = TRUE, center = TRUE, scale. = FALSE, tol = NULL, rank. = NULL, ...) ## S3 method for class 'prcomp' predict(object, newdata, ...)
formula |
a formula with no response variable, referring only to numeric variables. |
data |
an optional data frame (or similar: see
|
subset |
an optional vector used to select rows (observations) of the
data matrix |
na.action |
a function which indicates what should happen
when the data contain |
... |
arguments passed to or from other methods. If |
x |
a numeric or complex matrix (or data frame) which provides the data for the principal components analysis. |
retx |
a logical value indicating whether the rotated variables should be returned. |
center |
a logical value indicating whether the variables
should be shifted to be zero centered. Alternately, a vector of
length equal the number of columns of |
scale. |
a logical value indicating whether the variables should
be scaled to have unit variance before the analysis takes
place. The default is |
tol |
a value indicating the magnitude below which components
should be omitted. (Components are omitted if their
standard deviations are less than or equal to |
rank. |
optionally, a number specifying the maximal rank, i.e.,
maximal number of principal components to be used. Can be set as
alternative or in addition to |
object |
object of class inheriting from |
newdata |
An optional data frame or matrix in which to look for
variables with which to predict. If omitted, the scores are used.
If the original fit used a formula or a data frame or a matrix with
column names, |
The calculation is done by a singular value decomposition of the
(centered and possibly scaled) data matrix, not by using
eigen
on the covariance matrix. This
is generally the preferred method for numerical accuracy. The
print
method for these objects prints the results in a nice
format and the plot
method produces a scree plot.
Unlike princomp
, variances are computed with the usual
divisor .
Note that scale = TRUE
cannot be used if there are zero or
constant (for center = TRUE
) variables.
prcomp
returns a list with class "prcomp"
containing the following components:
sdev |
the standard deviations of the principal components (i.e., the square roots of the eigenvalues of the covariance/correlation matrix, though the calculation is actually done with the singular values of the data matrix). |
rotation |
the matrix of variable loadings (i.e., a matrix
whose columns contain the eigenvectors). The function
|
x |
if |
center , scale
|
the centering and scaling used, or |
The signs of the columns of the rotation matrix are arbitrary, and so may differ between different programs for PCA, and even between different builds of R.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
Mardia, K. V., J. T. Kent, and J. M. Bibby (1979) Multivariate Analysis, London: Academic Press.
Venables, W. N. and B. D. Ripley (2002) Modern Applied Statistics with S, Springer-Verlag.
biplot.prcomp
, screeplot
,
princomp
, cor
, cov
,
svd
, eigen
.
C <- chol(S <- toeplitz(.9 ^ (0:31))) # Cov.matrix and its root all.equal(S, crossprod(C)) set.seed(17) X <- matrix(rnorm(32000), 1000, 32) Z <- X %*% C ## ==> cov(Z) ~= C'C = S all.equal(cov(Z), S, tolerance = 0.08) pZ <- prcomp(Z, tol = 0.1) summary(pZ) # only ~14 PCs (out of 32) ## or choose only 3 PCs more directly: pz3 <- prcomp(Z, rank. = 3) summary(pz3) # same numbers as the first 3 above stopifnot(ncol(pZ$rotation) == 14, ncol(pz3$rotation) == 3, all.equal(pz3$sdev, pZ$sdev, tolerance = 1e-15)) # exactly equal typically ## signs are random require(graphics) ## the variances of the variables in the ## USArrests data vary by orders of magnitude, so scaling is appropriate prcomp(USArrests) # inappropriate prcomp(USArrests, scale. = TRUE) prcomp(~ Murder + Assault + Rape, data = USArrests, scale. = TRUE) plot(prcomp(USArrests)) summary(prcomp(USArrests, scale. = TRUE)) biplot(prcomp(USArrests, scale. = TRUE))
C <- chol(S <- toeplitz(.9 ^ (0:31))) # Cov.matrix and its root all.equal(S, crossprod(C)) set.seed(17) X <- matrix(rnorm(32000), 1000, 32) Z <- X %*% C ## ==> cov(Z) ~= C'C = S all.equal(cov(Z), S, tolerance = 0.08) pZ <- prcomp(Z, tol = 0.1) summary(pZ) # only ~14 PCs (out of 32) ## or choose only 3 PCs more directly: pz3 <- prcomp(Z, rank. = 3) summary(pz3) # same numbers as the first 3 above stopifnot(ncol(pZ$rotation) == 14, ncol(pz3$rotation) == 3, all.equal(pz3$sdev, pZ$sdev, tolerance = 1e-15)) # exactly equal typically ## signs are random require(graphics) ## the variances of the variables in the ## USArrests data vary by orders of magnitude, so scaling is appropriate prcomp(USArrests) # inappropriate prcomp(USArrests, scale. = TRUE) prcomp(~ Murder + Assault + Rape, data = USArrests, scale. = TRUE) plot(prcomp(USArrests)) summary(prcomp(USArrests, scale. = TRUE)) biplot(prcomp(USArrests, scale. = TRUE))
predict
is a generic function for predictions from the results of
various model fitting functions. The function invokes particular
methods which depend on the class
of
the first argument.
predict (object, ...)
predict (object, ...)
object |
a model object for which prediction is desired. |
... |
additional arguments affecting the predictions produced. |
Most prediction methods which are similar to those for linear models
have an argument newdata
specifying the first place to look for
explanatory variables to be used for prediction. Some considerable
attempts are made to match up the columns in newdata
to those
used for fitting, for example that they are of comparable types and
that any factors have the same level set in the same order (or can be
transformed to be so).
Time series prediction methods in package stats have an argument
n.ahead
specifying how many time steps ahead to predict.
Many methods have a logical argument se.fit
saying if standard
errors are to returned.
The form of the value returned by predict
depends on the
class of its argument. See the documentation of the
particular methods for details of what is produced by that method.
Chambers, J. M. and Hastie, T. J. (1992) Statistical Models in S. Wadsworth & Brooks/Cole.
predict.glm
,
predict.lm
,
predict.loess
,
predict.nls
,
predict.poly
,
predict.princomp
,
predict.smooth.spline
.
SafePrediction for prediction from (univariable) polynomial and spline fits.
For time-series prediction,
predict.ar
,
predict.Arima
,
predict.arima0
,
predict.HoltWinters
,
predict.StructTS
.
require(utils) ## All the "predict" methods found ## NB most of the methods in the standard packages are hidden. ## Output will depend on what namespaces are (or have been) loaded. for(fn in methods("predict")) try({ f <- eval(substitute(getAnywhere(fn)$objs[[1]], list(fn = fn))) cat(fn, ":\n\t", deparse(args(f)), "\n") }, silent = TRUE)
require(utils) ## All the "predict" methods found ## NB most of the methods in the standard packages are hidden. ## Output will depend on what namespaces are (or have been) loaded. for(fn in methods("predict")) try({ f <- eval(substitute(getAnywhere(fn)$objs[[1]], list(fn = fn))) cat(fn, ":\n\t", deparse(args(f)), "\n") }, silent = TRUE)
Forecast from models fitted by arima
.
## S3 method for class 'Arima' predict(object, n.ahead = 1, newxreg = NULL, se.fit = TRUE, ...)
## S3 method for class 'Arima' predict(object, n.ahead = 1, newxreg = NULL, se.fit = TRUE, ...)
object |
The result of an |
n.ahead |
The number of steps ahead for which prediction is required. |
newxreg |
New values of |
se.fit |
Logical: should standard errors of prediction be returned? |
... |
arguments passed to or from other methods. |
Finite-history prediction is used, via KalmanForecast
.
This is only statistically efficient if the MA part of the fit is
invertible, so predict.Arima
will give a warning for
non-invertible MA models.
The standard errors of prediction exclude the uncertainty in the estimation of the ARMA model and the regression coefficients. According to Harvey (1993, pp. 58–9) the effect is small.
A time series of predictions, or if se.fit = TRUE
, a list
with components pred
, the predictions, and se
,
the estimated standard errors. Both components are time series.
Durbin, J. and Koopman, S. J. (2001). Time Series Analysis by State Space Methods. Oxford University Press.
Harvey, A. C. and McKenzie, C. R. (1982). Algorithm AS 182: An algorithm for finite sample prediction from ARIMA processes. Applied Statistics, 31, 180–187. doi:10.2307/2347987.
Harvey, A. C. (1993). Time Series Models, 2nd Edition. Harvester Wheatsheaf. Sections 3.3 and 4.4.
od <- options(digits = 5) # avoid too much spurious accuracy predict(arima(lh, order = c(3,0,0)), n.ahead = 12) (fit <- arima(USAccDeaths, order = c(0,1,1), seasonal = list(order = c(0,1,1)))) predict(fit, n.ahead = 6) options(od)
od <- options(digits = 5) # avoid too much spurious accuracy predict(arima(lh, order = c(3,0,0)), n.ahead = 12) (fit <- arima(USAccDeaths, order = c(0,1,1), seasonal = list(order = c(0,1,1)))) predict(fit, n.ahead = 6) options(od)
Obtains predictions and optionally estimates standard errors of those predictions from a fitted generalized linear model object.
## S3 method for class 'glm' predict(object, newdata = NULL, type = c("link", "response", "terms"), se.fit = FALSE, dispersion = NULL, terms = NULL, na.action = na.pass, ...)
## S3 method for class 'glm' predict(object, newdata = NULL, type = c("link", "response", "terms"), se.fit = FALSE, dispersion = NULL, terms = NULL, na.action = na.pass, ...)
object |
a fitted object of class inheriting from |
newdata |
optionally, a data frame in which to look for variables with which to predict. If omitted, the fitted linear predictors are used. |
type |
the type of prediction required. The default is on the
scale of the linear predictors; the alternative The value of this argument can be abbreviated. |
se.fit |
logical switch indicating if standard errors are required. |
dispersion |
the dispersion of the GLM fit to be assumed in
computing the standard errors. If omitted, that returned by
|
terms |
with |
na.action |
function determining what should be done with missing
values in |
... |
further arguments passed to or from other methods. |
If newdata
is omitted the predictions are based on the data
used for the fit. In that case how cases with missing values in the
original fit is determined by the na.action
argument of that
fit. If na.action = na.omit
omitted cases will not appear in
the residuals, whereas if na.action = na.exclude
they will
appear (in predictions and standard errors), with residual value
NA
. See also napredict
.
If se.fit = FALSE
, a vector or matrix of predictions.
For type = "terms"
this is a matrix with a column per term, and
may have an attribute "constant"
.
If se.fit = TRUE
, a list with components
fit |
Predictions, as for |
se.fit |
Estimated standard errors. |
residual.scale |
A scalar giving the square root of the dispersion used in computing the standard errors. |
Variables are first looked for in newdata
and then searched for
in the usual way (which will include the environment of the formula
used in the fit). A warning will be given if the
variables found are not of the same length as those in newdata
if it was supplied.
require(graphics) ## example from Venables and Ripley (2002, pp. 190-2.) ldose <- rep(0:5, 2) numdead <- c(1, 4, 9, 13, 18, 20, 0, 2, 6, 10, 12, 16) sex <- factor(rep(c("M", "F"), c(6, 6))) SF <- cbind(numdead, numalive = 20-numdead) budworm.lg <- glm(SF ~ sex*ldose, family = binomial) summary(budworm.lg) plot(c(1,32), c(0,1), type = "n", xlab = "dose", ylab = "prob", log = "x") text(2^ldose, numdead/20, as.character(sex)) ld <- seq(0, 5, 0.1) lines(2^ld, predict(budworm.lg, data.frame(ldose = ld, sex = factor(rep("M", length(ld)), levels = levels(sex))), type = "response")) lines(2^ld, predict(budworm.lg, data.frame(ldose = ld, sex = factor(rep("F", length(ld)), levels = levels(sex))), type = "response"))
require(graphics) ## example from Venables and Ripley (2002, pp. 190-2.) ldose <- rep(0:5, 2) numdead <- c(1, 4, 9, 13, 18, 20, 0, 2, 6, 10, 12, 16) sex <- factor(rep(c("M", "F"), c(6, 6))) SF <- cbind(numdead, numalive = 20-numdead) budworm.lg <- glm(SF ~ sex*ldose, family = binomial) summary(budworm.lg) plot(c(1,32), c(0,1), type = "n", xlab = "dose", ylab = "prob", log = "x") text(2^ldose, numdead/20, as.character(sex)) ld <- seq(0, 5, 0.1) lines(2^ld, predict(budworm.lg, data.frame(ldose = ld, sex = factor(rep("M", length(ld)), levels = levels(sex))), type = "response")) lines(2^ld, predict(budworm.lg, data.frame(ldose = ld, sex = factor(rep("F", length(ld)), levels = levels(sex))), type = "response"))
Computes predictions and prediction intervals for models fitted by the Holt-Winters method.
## S3 method for class 'HoltWinters' predict(object, n.ahead = 1, prediction.interval = FALSE, level = 0.95, ...)
## S3 method for class 'HoltWinters' predict(object, n.ahead = 1, prediction.interval = FALSE, level = 0.95, ...)
object |
An object of class |
n.ahead |
Number of future periods to predict. |
prediction.interval |
logical. If |
level |
Confidence level for the prediction interval. |
... |
arguments passed to or from other methods. |
A time series of the predicted values. If prediction intervals are
requested, a multiple time series is returned with columns fit
,
lwr
and upr
for the predicted values and the lower and
upper bounds respectively.
David Meyer [email protected]
C. C. Holt (1957) Forecasting trends and seasonals by exponentially weighted moving averages, ONR Research Memorandum, Carnegie Institute of Technology 52.
P. R. Winters (1960). Forecasting sales by exponentially weighted moving averages. Management Science, 6, 324–342. doi:10.1287/mnsc.6.3.324.
require(graphics) m <- HoltWinters(co2) p <- predict(m, 50, prediction.interval = TRUE) plot(m, p)
require(graphics) m <- HoltWinters(co2) p <- predict(m, 50, prediction.interval = TRUE) plot(m, p)
Predicted values based on linear model object.
## S3 method for class 'lm' predict(object, newdata, se.fit = FALSE, scale = NULL, df = Inf, interval = c("none", "confidence", "prediction"), level = 0.95, type = c("response", "terms"), terms = NULL, na.action = na.pass, pred.var = res.var/weights, weights = 1, rankdeficient = c("warnif", "simple", "non-estim", "NA", "NAwarn"), tol = 1e-6, verbose = FALSE, ...)
## S3 method for class 'lm' predict(object, newdata, se.fit = FALSE, scale = NULL, df = Inf, interval = c("none", "confidence", "prediction"), level = 0.95, type = c("response", "terms"), terms = NULL, na.action = na.pass, pred.var = res.var/weights, weights = 1, rankdeficient = c("warnif", "simple", "non-estim", "NA", "NAwarn"), tol = 1e-6, verbose = FALSE, ...)
object |
Object of class inheriting from |
newdata |
An optional data frame in which to look for variables with which to predict. If omitted, the fitted values are used. |
se.fit |
A switch indicating if standard errors are required. |
scale |
Scale parameter for std.err. calculation. |
df |
Degrees of freedom for scale. |
interval |
Type of interval calculation. Can be abbreviated. |
level |
Tolerance/confidence level. |
type |
Type of prediction (response or model term). Can be abbreviated. |
terms |
If |
na.action |
function determining what should be done with missing
values in |
pred.var |
the variance(s) for future observations to be assumed for prediction intervals. See ‘Details’. |
weights |
variance weights for prediction. This can be a numeric
vector or a one-sided model formula. In the latter case, it is
interpreted as an expression evaluated in |
rankdeficient |
a
|
tol |
non-negative number determining how non-estimability is determined in rank deficient cases. |
verbose |
|
... |
further arguments passed to or from other methods. |
predict.lm
produces predicted values, obtained by evaluating
the regression function in the frame newdata
(which defaults to
model.frame(object)
). If the logical se.fit
is
TRUE
, standard errors of the predictions are calculated. If
the numeric argument scale
is set (with optional df
), it
is used as the residual standard deviation in the computation of the
standard errors, otherwise this is extracted from the model fit.
Setting intervals
specifies computation of confidence or
prediction (tolerance) intervals at the specified level
, sometimes
referred to as narrow vs. wide intervals.
If the fit is rank-deficient, some of the columns of the design matrix
will have been dropped during the lm
computations, and
corresponding coef()
components set to NA
.
Prediction from such a fit only makes sense if newdata
is
contained in the same subspace as the original data. Other
newdata
entries (rows) are non-estimable
. This is now
checked (up to numerical tolerance tol
) unless rankdeficient
== "simple"
, which corresponds to previous behaviour, warns always and
predicts using the non-NA
coefficients with the corresponding
columns of the design matrix. The new default option,
rankdeficient == "warnif"
checks if there are
“non-estimable” cases (up to tolerance tol
) and only warns
in that case. All further rankdeficient
options also check and
either predict NA
or mark the non-estimable cases differently.
If newdata
is omitted the predictions are based on the data
used for the fit. In that case how cases with missing values in the
original fit are handled is determined by the na.action
argument of that
fit. If na.action = na.omit
omitted cases will not appear in
the predictions, whereas if na.action = na.exclude
they will
appear (in predictions, standard errors or interval limits),
with value NA
. See also napredict
.
The prediction intervals are for a single observation at each case in
newdata
(or by default, the data used for the fit) with error
variance(s) pred.var
. This can be a multiple of res.var
,
the estimated value of : the default is to assume that
future observations have the same error variance as those
used for fitting. If
weights
is supplied, the inverse of this
is used as a scale factor. For a weighted fit, if the prediction
is for the original data frame, weights
defaults to the weights
used for the model fit, with a warning since it might not be the
intended result. If the fit was weighted and newdata
is given, the
default is to assume constant prediction variance, with a warning.
predict.lm
produces a vector of predictions or a matrix of
predictions and bounds with column names fit
, lwr
, and
upr
if interval
is set. For type = "terms"
this
is a matrix with a column per term and may have an attribute
"constant"
.
If se.fit
is
TRUE
, a list with the following components is returned:
fit |
vector or matrix as above |
se.fit |
standard error of predicted means |
residual.scale |
residual standard deviations |
df |
degrees of freedom for residual |
Variables are first looked for in newdata
and then searched for
in the usual way (which will include the environment of the formula
used in the fit). A warning will be given if the
variables found are not of the same length as those in newdata
if it was supplied.
Notice that prediction variances and prediction intervals always refer to future observations, possibly corresponding to the same predictors as used for the fit. The variance of the residuals will be smaller.
Strictly speaking, the formula used for prediction limits assumes that
the degrees of freedom for the fit are the same as those for the
residual variance. This may not be the case if res.var
is
not obtained from the fit.
The model fitting function lm
, predict
.
SafePrediction for prediction from (univariable) polynomial and spline fits.
require(graphics) ## Predictions x <- rnorm(15) y <- x + rnorm(15) predict(lm(y ~ x)) new <- data.frame(x = seq(-3, 3, 0.5)) predict(lm(y ~ x), new, se.fit = TRUE) pred.w.plim <- predict(lm(y ~ x), new, interval = "prediction") pred.w.clim <- predict(lm(y ~ x), new, interval = "confidence") matplot(new$x, cbind(pred.w.clim, pred.w.plim[,-1]), lty = c(1,2,2,3,3), type = "l", ylab = "predicted y") ## Prediction intervals, special cases ## The first three of these throw warnings w <- 1 + x^2 fit <- lm(y ~ x) wfit <- lm(y ~ x, weights = w) predict(fit, interval = "prediction") predict(wfit, interval = "prediction") predict(wfit, new, interval = "prediction") predict(wfit, new, interval = "prediction", weights = (new$x)^2) predict(wfit, new, interval = "prediction", weights = ~x^2) ##-- From aov(.) example ---- predict(.. terms) npk.aov <- aov(yield ~ block + N*P*K, npk) (termL <- attr(terms(npk.aov), "term.labels")) (pt <- predict(npk.aov, type = "terms")) pt. <- predict(npk.aov, type = "terms", terms = termL[1:4]) stopifnot(all.equal(pt[,1:4], pt., tolerance = 1e-12, check.attributes = FALSE))
require(graphics) ## Predictions x <- rnorm(15) y <- x + rnorm(15) predict(lm(y ~ x)) new <- data.frame(x = seq(-3, 3, 0.5)) predict(lm(y ~ x), new, se.fit = TRUE) pred.w.plim <- predict(lm(y ~ x), new, interval = "prediction") pred.w.clim <- predict(lm(y ~ x), new, interval = "confidence") matplot(new$x, cbind(pred.w.clim, pred.w.plim[,-1]), lty = c(1,2,2,3,3), type = "l", ylab = "predicted y") ## Prediction intervals, special cases ## The first three of these throw warnings w <- 1 + x^2 fit <- lm(y ~ x) wfit <- lm(y ~ x, weights = w) predict(fit, interval = "prediction") predict(wfit, interval = "prediction") predict(wfit, new, interval = "prediction") predict(wfit, new, interval = "prediction", weights = (new$x)^2) predict(wfit, new, interval = "prediction", weights = ~x^2) ##-- From aov(.) example ---- predict(.. terms) npk.aov <- aov(yield ~ block + N*P*K, npk) (termL <- attr(terms(npk.aov), "term.labels")) (pt <- predict(npk.aov, type = "terms")) pt. <- predict(npk.aov, type = "terms", terms = termL[1:4]) stopifnot(all.equal(pt[,1:4], pt., tolerance = 1e-12, check.attributes = FALSE))
Predictions from a loess
fit, optionally with standard errors.
## S3 method for class 'loess' predict(object, newdata = NULL, se = FALSE, na.action = na.pass, ...)
## S3 method for class 'loess' predict(object, newdata = NULL, se = FALSE, na.action = na.pass, ...)
object |
an object fitted by |
newdata |
an optional data frame in which to look for variables with which to predict, or a matrix or vector containing exactly the variables needs for prediction. If missing, the original data points are used. |
se |
should standard errors be computed? |
na.action |
function determining what should be done with missing
values in data frame |
... |
arguments passed to or from other methods. |
The standard errors calculation se = TRUE
is slower than
prediction, notably as it needs a relatively large workspace (memory),
notably matrices of dimension where
span
, i.e., se = TRUE
is
and hence stops when the sample size
is larger than about 40'600
(for default
span = 0.75
).
When the fit was made using surface = "interpolate"
(the
default), predict.loess
will not extrapolate – so points outside
an axis-aligned hypercube enclosing the original data will have
missing (NA
) predictions and standard errors.
If se = FALSE
, a vector giving the prediction for each row of
newdata
(or the original data). If se = TRUE
, a list
containing components
fit |
the predicted values. |
se |
an estimated standard error for each predicted value. |
residual.scale |
the estimated scale of the residuals used in computing the standard errors. |
df |
an estimate of the effective degrees of freedom used in estimating the residual scale, intended for use with t-based confidence intervals. |
If newdata
was the result of a call to
expand.grid
, the predictions (and s.e.'s if requested)
will be an array of the appropriate dimensions.
Predictions from infinite inputs will be NA
since loess
does not support extrapolation.
Variables are first looked for in newdata
and then searched for
in the usual way (which will include the environment of the formula
used in the fit). A warning will be given if the
variables found are not of the same length as those in newdata
if it was supplied.
B. D. Ripley, based on the cloess
package of Cleveland,
Grosse and Shyu.
cars.lo <- loess(dist ~ speed, cars) predict(cars.lo, data.frame(speed = seq(5, 30, 1)), se = TRUE) # to get extrapolation cars.lo2 <- loess(dist ~ speed, cars, control = loess.control(surface = "direct")) predict(cars.lo2, data.frame(speed = seq(5, 30, 1)), se = TRUE)
cars.lo <- loess(dist ~ speed, cars) predict(cars.lo, data.frame(speed = seq(5, 30, 1)), se = TRUE) # to get extrapolation cars.lo2 <- loess(dist ~ speed, cars, control = loess.control(surface = "direct")) predict(cars.lo2, data.frame(speed = seq(5, 30, 1)), se = TRUE)
predict.nls
produces predicted values, obtained by evaluating
the regression function in the frame newdata
. If the logical
se.fit
is TRUE
, standard errors of the predictions are
calculated. If the numeric argument scale
is set (with
optional df
), it is used as the residual standard deviation in
the computation of the standard errors, otherwise this is extracted
from the model fit. Setting intervals
specifies computation of
confidence or prediction (tolerance) intervals at the specified
level
.
At present se.fit
and interval
are ignored.
## S3 method for class 'nls' predict(object, newdata , se.fit = FALSE, scale = NULL, df = Inf, interval = c("none", "confidence", "prediction"), level = 0.95, ...)
## S3 method for class 'nls' predict(object, newdata , se.fit = FALSE, scale = NULL, df = Inf, interval = c("none", "confidence", "prediction"), level = 0.95, ...)
object |
An object that inherits from class |
newdata |
A named list or data frame in which to look for variables with
which to predict. If |
se.fit |
A logical value indicating if the standard errors of the
predictions should be calculated. Defaults to |
scale |
A numeric scalar. If it is set (with optional
|
df |
A positive numeric scalar giving the number of degrees of
freedom for the |
interval |
A character string indicating if prediction intervals or a confidence interval on the mean responses are to be calculated. At present this argument is ignored. |
level |
A numeric scalar between 0 and 1 giving the confidence level for the intervals (if any) to be calculated. At present this argument is ignored. |
... |
Additional optional arguments. At present no optional arguments are used. |
predict.nls
produces a vector of predictions.
When implemented, interval
will produce a matrix of
predictions and bounds with column names fit
, lwr
, and
upr
. When implemented, if se.fit
is
TRUE
, a list with the following components will be returned:
fit |
vector or matrix as above |
se.fit |
standard error of predictions |
residual.scale |
residual standard deviations |
df |
degrees of freedom for residual |
Variables are first looked for in newdata
and then searched for
in the usual way (which will include the environment of the formula
used in the fit). A warning will be given if the
variables found are not of the same length as those in newdata
if it was supplied.
The model fitting function nls
,
predict
.
require(graphics) fm <- nls(demand ~ SSasympOrig(Time, A, lrc), data = BOD) predict(fm) # fitted values at observed times ## Form data plot and smooth line for the predictions opar <- par(las = 1) plot(demand ~ Time, data = BOD, col = 4, main = "BOD data and fitted first-order curve", xlim = c(0,7), ylim = c(0, 20) ) tt <- seq(0, 8, length.out = 101) lines(tt, predict(fm, list(Time = tt))) par(opar)
require(graphics) fm <- nls(demand ~ SSasympOrig(Time, A, lrc), data = BOD) predict(fm) # fitted values at observed times ## Form data plot and smooth line for the predictions opar <- par(las = 1) plot(demand ~ Time, data = BOD, col = 4, main = "BOD data and fitted first-order curve", xlim = c(0,7), ylim = c(0, 20) ) tt <- seq(0, 8, length.out = 101) lines(tt, predict(fm, list(Time = tt))) par(opar)
Predict a smoothing spline fit at new points, return the derivative if desired. The predicted fit is linear beyond the original data.
## S3 method for class 'smooth.spline' predict(object, x, deriv = 0, ...)
## S3 method for class 'smooth.spline' predict(object, x, deriv = 0, ...)
object |
a fit from |
x |
the new values of x. |
deriv |
integer; the order of the derivative required. |
... |
further arguments passed to or from other methods. |
A list with components
x |
The input |
y |
The fitted values or derivatives at |
require(graphics) attach(cars) cars.spl <- smooth.spline(speed, dist, df = 6.4) ## "Proof" that the derivatives are okay, by comparing with approximation diff.quot <- function(x, y) { ## Difference quotient (central differences where available) n <- length(x); i1 <- 1:2; i2 <- (n-1):n c(diff(y[i1]) / diff(x[i1]), (y[-i1] - y[-i2]) / (x[-i1] - x[-i2]), diff(y[i2]) / diff(x[i2])) } xx <- unique(sort(c(seq(0, 30, by = .2), kn <- unique(speed)))) i.kn <- match(kn, xx) # indices of knots within xx op <- par(mfrow = c(2,2)) plot(speed, dist, xlim = range(xx), main = "Smooth.spline & derivatives") lines(pp <- predict(cars.spl, xx), col = "red") points(kn, pp$y[i.kn], pch = 3, col = "dark red") mtext("s(x)", col = "red") for(d in 1:3){ n <- length(pp$x) plot(pp$x, diff.quot(pp$x,pp$y), type = "l", xlab = "x", ylab = "", col = "blue", col.main = "red", main = paste0("s" ,paste(rep("'", d), collapse = ""), "(x)")) mtext("Difference quotient approx.(last)", col = "blue") lines(pp <- predict(cars.spl, xx, deriv = d), col = "red") points(kn, pp$y[i.kn], pch = 3, col = "dark red") abline(h = 0, lty = 3, col = "gray") } detach(); par(op)
require(graphics) attach(cars) cars.spl <- smooth.spline(speed, dist, df = 6.4) ## "Proof" that the derivatives are okay, by comparing with approximation diff.quot <- function(x, y) { ## Difference quotient (central differences where available) n <- length(x); i1 <- 1:2; i2 <- (n-1):n c(diff(y[i1]) / diff(x[i1]), (y[-i1] - y[-i2]) / (x[-i1] - x[-i2]), diff(y[i2]) / diff(x[i2])) } xx <- unique(sort(c(seq(0, 30, by = .2), kn <- unique(speed)))) i.kn <- match(kn, xx) # indices of knots within xx op <- par(mfrow = c(2,2)) plot(speed, dist, xlim = range(xx), main = "Smooth.spline & derivatives") lines(pp <- predict(cars.spl, xx), col = "red") points(kn, pp$y[i.kn], pch = 3, col = "dark red") mtext("s(x)", col = "red") for(d in 1:3){ n <- length(pp$x) plot(pp$x, diff.quot(pp$x,pp$y), type = "l", xlab = "x", ylab = "", col = "blue", col.main = "red", main = paste0("s" ,paste(rep("'", d), collapse = ""), "(x)")) mtext("Difference quotient approx.(last)", col = "blue") lines(pp <- predict(cars.spl, xx, deriv = d), col = "red") points(kn, pp$y[i.kn], pch = 3, col = "dark red") abline(h = 0, lty = 3, col = "gray") } detach(); par(op)
Compute an object to be used for plots relating to the given model object.
preplot(object, ...)
preplot(object, ...)
object |
a fitted model object. |
... |
additional arguments for specific methods. |
Only the generic function is currently provided in base R, but some add-on packages have methods. Principally here for S compatibility.
An object set up to make a plot that describes object
.
princomp
performs a principal components analysis on the given
numeric data matrix and returns the results as an object of class
princomp
.
princomp(x, ...) ## S3 method for class 'formula' princomp(formula, data = NULL, subset, na.action, ...) ## Default S3 method: princomp(x, cor = FALSE, scores = TRUE, covmat = NULL, subset = rep_len(TRUE, nrow(as.matrix(x))), fix_sign = TRUE, ...) ## S3 method for class 'princomp' predict(object, newdata, ...)
princomp(x, ...) ## S3 method for class 'formula' princomp(formula, data = NULL, subset, na.action, ...) ## Default S3 method: princomp(x, cor = FALSE, scores = TRUE, covmat = NULL, subset = rep_len(TRUE, nrow(as.matrix(x))), fix_sign = TRUE, ...) ## S3 method for class 'princomp' predict(object, newdata, ...)
formula |
a formula with no response variable, referring only to numeric variables. |
data |
an optional data frame (or similar: see
|
subset |
an optional vector used to select rows (observations) of the
data matrix |
na.action |
a function which indicates what should happen
when the data contain |
x |
a numeric matrix or data frame which provides the data for the principal components analysis. |
cor |
a logical value indicating whether the calculation should use the correlation matrix or the covariance matrix. (The correlation matrix can only be used if there are no constant variables.) |
scores |
a logical value indicating whether the score on each principal component should be calculated. |
covmat |
a covariance matrix, or a covariance list as returned by
|
fix_sign |
Should the signs of the loadings and scores be chosen so that the first element of each loading is non-negative? |
... |
arguments passed to or from other methods. If |
object |
Object of class inheriting from |
newdata |
An optional data frame or matrix in which to look for
variables with which to predict. If omitted, the scores are used.
If the original fit used a formula or a data frame or a matrix with
column names, |
princomp
is a generic function with "formula"
and
"default"
methods.
The calculation is done using eigen
on the correlation
or covariance matrix, as determined by cor
. (This was
done for compatibility with the S-PLUS result.) A preferred method of
calculation is to use svd
on x
, as is done in
prcomp
.
Note that the default calculation uses divisor N
for the
covariance matrix.
The print
method for these objects prints the
results in a nice format and the plot
method produces
a scree plot (screeplot
). There is also a
biplot
method.
If x
is a formula then the standard NA-handling is applied to
the scores (if requested): see napredict
.
princomp
only handles so-called R-mode PCA, that is feature
extraction of variables. If a data matrix is supplied (possibly via a
formula) it is required that there are at least as many units as
variables. For Q-mode PCA use prcomp
.
princomp
returns a list with class "princomp"
containing the following components:
sdev |
the standard deviations of the principal components. |
loadings |
the matrix of variable loadings (i.e., a matrix
whose columns contain the eigenvectors). This is of class
|
center |
the means that were subtracted. |
scale |
the scalings applied to each variable. |
n.obs |
the number of observations. |
scores |
if |
call |
the matched call. |
na.action |
If relevant. |
The signs of the columns of the loadings and scores are arbitrary, and
so may differ between different programs for PCA, and even between
different builds of R: fix_sign = TRUE
alleviates that.
Mardia, K. V., J. T. Kent and J. M. Bibby (1979). Multivariate Analysis, London: Academic Press.
Venables, W. N. and B. D. Ripley (2002). Modern Applied Statistics with S, Springer-Verlag.
summary.princomp
, screeplot
,
biplot.princomp
,
prcomp
, cor
, cov
,
eigen
.
require(graphics) ## The variances of the variables in the ## USArrests data vary by orders of magnitude, so scaling is appropriate (pc.cr <- princomp(USArrests)) # inappropriate princomp(USArrests, cor = TRUE) # =^= prcomp(USArrests, scale=TRUE) ## Similar, but different: ## The standard deviations differ by a factor of sqrt(49/50) summary(pc.cr <- princomp(USArrests, cor = TRUE)) loadings(pc.cr) # note that blank entries are small but not zero ## The signs of the columns of the loadings are arbitrary plot(pc.cr) # shows a screeplot. biplot(pc.cr) ## Formula interface princomp(~ ., data = USArrests, cor = TRUE) ## NA-handling USArrests[1, 2] <- NA pc.cr <- princomp(~ Murder + Assault + UrbanPop, data = USArrests, na.action = na.exclude, cor = TRUE) pc.cr$scores[1:5, ] ## (Simple) Robust PCA: ## Classical: (pc.cl <- princomp(stackloss)) ## Robust: (pc.rob <- princomp(stackloss, covmat = MASS::cov.rob(stackloss)))
require(graphics) ## The variances of the variables in the ## USArrests data vary by orders of magnitude, so scaling is appropriate (pc.cr <- princomp(USArrests)) # inappropriate princomp(USArrests, cor = TRUE) # =^= prcomp(USArrests, scale=TRUE) ## Similar, but different: ## The standard deviations differ by a factor of sqrt(49/50) summary(pc.cr <- princomp(USArrests, cor = TRUE)) loadings(pc.cr) # note that blank entries are small but not zero ## The signs of the columns of the loadings are arbitrary plot(pc.cr) # shows a screeplot. biplot(pc.cr) ## Formula interface princomp(~ ., data = USArrests, cor = TRUE) ## NA-handling USArrests[1, 2] <- NA pc.cr <- princomp(~ Murder + Assault + UrbanPop, data = USArrests, na.action = na.exclude, cor = TRUE) pc.cr$scores[1:5, ] ## (Simple) Robust PCA: ## Classical: (pc.cl <- princomp(stackloss)) ## Robust: (pc.rob <- princomp(stackloss, covmat = MASS::cov.rob(stackloss)))
Printing objects of class "htest"
or "power.htest"
,
respectively, by simple print
methods.
## S3 method for class 'htest' print(x, digits = getOption("digits"), prefix = "\t", ...) ## S3 method for class 'power.htest' print(x, digits = getOption("digits"), ...)
## S3 method for class 'htest' print(x, digits = getOption("digits"), prefix = "\t", ...) ## S3 method for class 'power.htest' print(x, digits = getOption("digits"), ...)
x |
object of class |
digits |
number of significant digits to be used. |
prefix |
string, passed to |
... |
further arguments to be passed to or from methods. |
Both print
methods traditionally have not obeyed the
digits
argument properly. They now do, the htest
method mostly in expressions like max(1, digits - 2)
.
A power.htest
object is just a named list of numbers and
character strings, supplemented with method
and note
elements. The method
is displayed as a title, the note
as a footnote, and the remaining elements are given in an aligned
‘name = value’ format.
the argument x
, invisibly, as for all print
methods.
Peter Dalgaard
(ptt <- power.t.test(n = 20, delta = 1)) print(ptt, digits = 4) # using less digits than default print(ptt, digits = 12) # using more " " "
(ptt <- power.t.test(n = 20, delta = 1)) print(ptt, digits = 4) # using less digits than default print(ptt, digits = 12) # using more " " "
Notably for calendar related time series objects,
format
and print
methods showing years,
months and or quarters respectively.
## S3 method for class 'ts' print(x, calendar, ...) .preformat.ts(x, calendar, ...)
## S3 method for class 'ts' print(x, calendar, ...) .preformat.ts(x, calendar, ...)
x |
a time series object. |
calendar |
enable/disable the display of information about
month names, quarter names or year when printing. The default is
|
... |
The print
method for "ts"
objects prints a
header (basically of tsp(x)
), if calendar
is
false, and then prints the result of .preformat.ts(x, *)
, which
is typically a matrix
with rownames
built
from the calendar times where applicable.
print(ts(1:10, frequency = 7, start = c(12, 2)), calendar = TRUE) print(sunsp.1 <- window(sunspot.month, end=c(1756, 12))) m <- .preformat.ts(sunsp.1) # a character matrix
print(ts(1:10, frequency = 7, start = c(12, 2)), calendar = TRUE) print(sunsp.1 <- window(sunspot.month, end=c(1756, 12))) m <- .preformat.ts(sunsp.1) # a character matrix
Utility function to be used in higher-level print
methods, such as those for summary.lm
,
summary.glm
and anova
. The
goal is to provide a flexible interface with smart defaults such
that often, only x
needs to be specified.
printCoefmat(x, digits = max(3, getOption("digits") - 2), signif.stars = getOption("show.signif.stars"), signif.legend = signif.stars, dig.tst = max(1, min(5, digits - 1)), cs.ind = 1L:k, tst.ind = k + 1L, zap.ind = integer(), P.values = NULL, has.Pvalue = nc >= 4L && length(cn <- colnames(x)) && substr(cn[nc], 1L, 3L) %in% c("Pr(", "p-v"), eps.Pvalue = .Machine$double.eps, na.print = "NA", quote = FALSE, right = TRUE, ...)
printCoefmat(x, digits = max(3, getOption("digits") - 2), signif.stars = getOption("show.signif.stars"), signif.legend = signif.stars, dig.tst = max(1, min(5, digits - 1)), cs.ind = 1L:k, tst.ind = k + 1L, zap.ind = integer(), P.values = NULL, has.Pvalue = nc >= 4L && length(cn <- colnames(x)) && substr(cn[nc], 1L, 3L) %in% c("Pr(", "p-v"), eps.Pvalue = .Machine$double.eps, na.print = "NA", quote = FALSE, right = TRUE, ...)
x |
a numeric matrix like object, to be printed. |
digits |
minimum number of significant digits to be used for most numbers. |
signif.stars |
logical; if |
signif.legend |
logical; if |
dig.tst |
minimum number of significant digits for the test statistics,
see |
cs.ind |
indices (integer) of column numbers which are (like) coefficients and standard errors to be formatted together. |
tst.ind |
indices (integer) of column numbers for test statistics. |
zap.ind |
indices (integer) of column numbers which should be
formatted by |
P.values |
logical or |
has.Pvalue |
logical; if |
eps.Pvalue |
number, passed to |
na.print |
a character string to code |
quote , right , ...
|
further arguments passed to
|
Invisibly returns its argument, x
.
Martin Maechler
print.summary.lm
,
format.pval
,
format
.
cmat <- cbind(rnorm(3, 10), sqrt(rchisq(3, 12))) cmat <- cbind(cmat, cmat[, 1]/cmat[, 2]) cmat <- cbind(cmat, 2*pnorm(-cmat[, 3])) colnames(cmat) <- c("Estimate", "Std.Err", "Z value", "Pr(>z)") printCoefmat(cmat[, 1:3]) printCoefmat(cmat) op <- options(show.coef.Pvalues = FALSE) printCoefmat(cmat, digits = 2) printCoefmat(cmat, digits = 2, P.values = TRUE) options(op) # restore
cmat <- cbind(rnorm(3, 10), sqrt(rchisq(3, 12))) cmat <- cbind(cmat, cmat[, 1]/cmat[, 2]) cmat <- cbind(cmat, 2*pnorm(-cmat[, 3])) colnames(cmat) <- c("Estimate", "Std.Err", "Z value", "Pr(>z)") printCoefmat(cmat[, 1:3]) printCoefmat(cmat) op <- options(show.coef.Pvalues = FALSE) printCoefmat(cmat, digits = 2) printCoefmat(cmat, digits = 2, P.values = TRUE) options(op) # restore
Investigates the behavior of the objective function near the solution
represented by fitted
.
See documentation on method functions for further details.
profile(fitted, ...)
profile(fitted, ...)
fitted |
the original fitted model object. |
... |
additional parameters. See documentation on individual methods. |
A list with an element for each parameter being profiled. See the individual methods for further details.
profile.nls
, profile.glm
...
For profiling R code, see Rprof
.
glm
ObjectsInvestigates the profile log-likelihood function for a fitted model of
class "glm"
.
## S3 method for class 'glm' profile(fitted, which = 1:p, alpha = 0.01, maxsteps = 10, del = zmax/5, trace = FALSE, test = c("LRT", "Rao"), ...)
## S3 method for class 'glm' profile(fitted, which = 1:p, alpha = 0.01, maxsteps = 10, del = zmax/5, trace = FALSE, test = c("LRT", "Rao"), ...)
fitted |
the original fitted model object. |
which |
the original model parameters which should be profiled. This can be a numeric or character vector. By default, all parameters are profiled. |
alpha |
highest significance level allowed for the profile z-statistics. |
maxsteps |
maximum number of points to be used for profiling each parameter. |
del |
suggested change on the scale of the profile t-statistics. Default value chosen to allow profiling at about 10 parameter values. |
trace |
logical: should the progress of profiling be reported? |
test |
profile Likelihood Ratio test or Rao Score test. |
... |
further arguments passed to or from other methods. |
The profile z-statistic is defined either as (case test = "LRT"
) the square root of change in
deviance with an
appropriate sign, or (case test = "Rao"
) as the similarly
signed square root of the Rao Score test
statistic. The latter is defined as the squared gradient of the profile log likelihood
divided by the profile Fisher information, but more conveniently
calculated via the deviance of a Gaussian GLM fitted to the residuals
of the profiled model.
A list of classes "profile.glm"
and "profile"
with an
element for each parameter being profiled. The elements are
data-frames with two variables
par.vals |
a matrix of parameter values for each fitted model. |
tau or z |
the profile t or z-statistics (the name depends on whether there is an estimated dispersion parameter.) |
Originally, D. M. Bates and W. N. Venables. (For S in 1996.)
options(contrasts = c("contr.treatment", "contr.poly")) ldose <- rep(0:5, 2) numdead <- c(1, 4, 9, 13, 18, 20, 0, 2, 6, 10, 12, 16) sex <- factor(rep(c("M", "F"), c(6, 6))) SF <- cbind(numdead, numalive = 20 - numdead) budworm.lg <- glm(SF ~ sex*ldose, family = binomial) pr1 <- profile(budworm.lg) plot(pr1) pairs(pr1)
options(contrasts = c("contr.treatment", "contr.poly")) ldose <- rep(0:5, 2) numdead <- c(1, 4, 9, 13, 18, 20, 0, 2, 6, 10, 12, 16) sex <- factor(rep(c("M", "F"), c(6, 6))) SF <- cbind(numdead, numalive = 20 - numdead) budworm.lg <- glm(SF ~ sex*ldose, family = binomial) pr1 <- profile(budworm.lg) plot(pr1) pairs(pr1)
nls
ObjectsInvestigates the profile log-likelihood function for a fitted model of
class "nls"
.
## S3 method for class 'nls' profile(fitted, which = 1:npar, maxpts = 100, alphamax = 0.01, delta.t = cutoff/5, ...)
## S3 method for class 'nls' profile(fitted, which = 1:npar, maxpts = 100, alphamax = 0.01, delta.t = cutoff/5, ...)
fitted |
the original fitted model object. |
which |
the original model parameters which should be profiled. This can be a numeric or character vector. By default, all non-linear parameters are profiled. |
maxpts |
maximum number of points to be used for profiling each parameter. |
alphamax |
highest significance level allowed for the profile t-statistics. |
delta.t |
suggested change on the scale of the profile t-statistics. Default value chosen to allow profiling at about 10 parameter values. |
... |
further arguments passed to or from other methods. |
The profile t-statistics is defined as the square root of change in sum-of-squares divided by residual standard error with an appropriate sign.
A list with an element for each parameter being profiled. The elements are data-frames with two variables
par.vals |
a matrix of parameter values for each fitted model. |
tau |
the profile t-statistics. |
Of the original version, Douglas M. Bates and Saikat DebRoy
Bates, D. M. and Watts, D. G. (1988), Nonlinear Regression Analysis and Its Applications, Wiley (chapter 6).
nls
, profile
, plot.profile.nls
# obtain the fitted object fm1 <- nls(demand ~ SSasympOrig(Time, A, lrc), data = BOD) # get the profile for the fitted model: default level is too extreme pr1 <- profile(fm1, alphamax = 0.05) # profiled values for the two parameters pr1$A pr1$lrc # see also example(plot.profile.nls)
# obtain the fitted object fm1 <- nls(demand ~ SSasympOrig(Time, A, lrc), data = BOD) # get the profile for the fitted model: default level is too extreme pr1 <- profile(fm1, alphamax = 0.05) # profiled values for the two parameters pr1$A pr1$lrc # see also example(plot.profile.nls)
proj
returns a matrix or list of matrices giving the projections
of the data onto the terms of a linear model. It is most frequently
used for aov
models.
proj(object, ...) ## S3 method for class 'aov' proj(object, onedf = FALSE, unweighted.scale = FALSE, ...) ## S3 method for class 'aovlist' proj(object, onedf = FALSE, unweighted.scale = FALSE, ...) ## Default S3 method: proj(object, onedf = TRUE, ...) ## S3 method for class 'lm' proj(object, onedf = FALSE, unweighted.scale = FALSE, ...)
proj(object, ...) ## S3 method for class 'aov' proj(object, onedf = FALSE, unweighted.scale = FALSE, ...) ## S3 method for class 'aovlist' proj(object, onedf = FALSE, unweighted.scale = FALSE, ...) ## Default S3 method: proj(object, onedf = TRUE, ...) ## S3 method for class 'lm' proj(object, onedf = FALSE, unweighted.scale = FALSE, ...)
object |
An object of class |
onedf |
A logical flag. If |
unweighted.scale |
If the fit producing |
... |
Swallow and ignore any other arguments. |
A projection is given for each stratum of the object, so for aov
models with an Error
term the result is a list of projections.
A projection matrix or (for multi-stratum objects) a list of projection matrices.
Each projection is a matrix with a row for each observations and
either a column for each term (onedf = FALSE
) or for each
coefficient (onedf = TRUE
). Projection matrices from the
default method have orthogonal columns representing the projection of
the response onto the column space of the Q matrix from the QR
decomposition. The fitted values are the sum of the projections, and
the sum of squares for each column is the reduction in sum of squares
from fitting that column (after those to the left of it).
The methods for lm
and aov
models add a column to the
projection matrix giving the residuals (the projection of the data
onto the orthogonal complement of the model space).
Strictly, when onedf = FALSE
the result is not a projection,
but the columns represent sums of projections onto the columns of the
model matrix corresponding to that term. In this case the matrix does
not depend on the coding used.
The design was inspired by the S function of the same name described in Chambers et al. (1992).
Chambers, J. M., Freeny, A and Heiberger, R. M. (1992) Analysis of variance; designed experiments. Chapter 5 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
N <- c(0,1,0,1,1,1,0,0,0,1,1,0,1,1,0,0,1,0,1,0,1,1,0,0) P <- c(1,1,0,0,0,1,0,1,1,1,0,0,0,1,0,1,1,0,0,1,0,1,1,0) K <- c(1,0,0,1,0,1,1,0,0,1,0,1,0,1,1,0,0,0,1,1,1,0,1,0) yield <- c(49.5,62.8,46.8,57.0,59.8,58.5,55.5,56.0,62.8,55.8,69.5, 55.0, 62.0,48.8,45.5,44.2,52.0,51.5,49.8,48.8,57.2,59.0,53.2,56.0) npk <- data.frame(block = gl(6,4), N = factor(N), P = factor(P), K = factor(K), yield = yield) npk.aov <- aov(yield ~ block + N*P*K, npk) proj(npk.aov) ## as a test, not particularly sensible options(contrasts = c("contr.helmert", "contr.treatment")) npk.aovE <- aov(yield ~ N*P*K + Error(block), npk) proj(npk.aovE)
N <- c(0,1,0,1,1,1,0,0,0,1,1,0,1,1,0,0,1,0,1,0,1,1,0,0) P <- c(1,1,0,0,0,1,0,1,1,1,0,0,0,1,0,1,1,0,0,1,0,1,1,0) K <- c(1,0,0,1,0,1,1,0,0,1,0,1,0,1,1,0,0,0,1,1,1,0,1,0) yield <- c(49.5,62.8,46.8,57.0,59.8,58.5,55.5,56.0,62.8,55.8,69.5, 55.0, 62.0,48.8,45.5,44.2,52.0,51.5,49.8,48.8,57.2,59.0,53.2,56.0) npk <- data.frame(block = gl(6,4), N = factor(N), P = factor(P), K = factor(K), yield = yield) npk.aov <- aov(yield ~ block + N*P*K, npk) proj(npk.aov) ## as a test, not particularly sensible options(contrasts = c("contr.helmert", "contr.treatment")) npk.aovE <- aov(yield ~ N*P*K + Error(block), npk) proj(npk.aovE)
prop.test
can be used for testing the null that the
proportions (probabilities of success) in several groups are the
same, or that they equal certain given values.
prop.test(x, n, p = NULL, alternative = c("two.sided", "less", "greater"), conf.level = 0.95, correct = TRUE)
prop.test(x, n, p = NULL, alternative = c("two.sided", "less", "greater"), conf.level = 0.95, correct = TRUE)
x |
a vector of counts of successes, a one-dimensional table with two entries, or a two-dimensional table (or matrix) with 2 columns, giving the counts of successes and failures, respectively. |
n |
a vector of counts of trials; ignored if |
p |
a vector of probabilities of success. The length of
|
alternative |
a character string specifying the alternative
hypothesis, must be one of |
conf.level |
confidence level of the returned confidence interval. Must be a single number between 0 and 1. Only used when testing the null that a single proportion equals a given value, or that two proportions are equal; ignored otherwise. |
correct |
a logical indicating whether Yates' continuity correction should be applied where possible. |
Only groups with finite numbers of successes and failures are used. Counts of successes and failures must be nonnegative and hence not greater than the corresponding numbers of trials which must be positive. All finite counts should be integers.
If p
is NULL
and there is more than one group, the null
tested is that the proportions in each group are the same. If there
are two groups, the alternatives are that the probability of success
in the first group is less than, not equal to, or greater than the
probability of success in the second group, as specified by
alternative
. A confidence interval for the difference of
proportions with confidence level as specified by conf.level
and clipped to is returned. Continuity correction is
used only if it does not exceed the difference of the sample
proportions in absolute value. Otherwise, if there are more than 2
groups, the alternative is always
"two.sided"
, the returned
confidence interval is NULL
, and continuity correction is never
used.
If there is only one group, then the null tested is that the
underlying probability of success is p
, or .5 if p
is
not given. The alternative is that the probability of success is less
than, not equal to, or greater than p
or 0.5, respectively, as
specified by alternative
. A confidence interval for the
underlying proportion with confidence level as specified by
conf.level
and clipped to is returned. Continuity
correction is used only if it does not exceed the difference between
sample and null proportions in absolute value. The confidence interval
is computed by inverting the score test.
Finally, if p
is given and there are more than 2 groups, the
null tested is that the underlying probabilities of success are those
given by p
. The alternative is always "two.sided"
, the
returned confidence interval is NULL
, and continuity correction
is never used.
A list with class "htest"
containing the following
components:
statistic |
the value of Pearson's chi-squared test statistic. |
parameter |
the degrees of freedom of the approximate chi-squared distribution of the test statistic. |
p.value |
the p-value of the test. |
estimate |
a vector with the sample proportions |
conf.int |
a confidence interval for the true proportion if
there is one group, or for the difference in proportions if
there are 2 groups and |
null.value |
the value of |
alternative |
a character string describing the alternative. |
method |
a character string indicating the method used, and whether Yates' continuity correction was applied. |
data.name |
a character string giving the names of the data. |
Wilson, E.B. (1927). Probable inference, the law of succession, and statistical inference. Journal of the American Statistical Association, 22, 209–212. doi:10.2307/2276774.
Newcombe R.G. (1998). Two-Sided Confidence Intervals for the Single Proportion: Comparison of Seven Methods. Statistics in Medicine, 17, 857–872. doi:10.1002/(SICI)1097-0258(19980430)17:8<857::AID-SIM777>3.0.CO;2-E.
Newcombe R.G. (1998). Interval Estimation for the Difference Between Independent Proportions: Comparison of Eleven Methods. Statistics in Medicine, 17, 873–890. doi:10.1002/(SICI)1097-0258(19980430)17:8<873::AID-SIM779>3.0.CO;2-I.
binom.test
for an exact test of a binomial
hypothesis.
heads <- rbinom(1, size = 100, prob = .5) prop.test(heads, 100) # continuity correction TRUE by default prop.test(heads, 100, correct = FALSE) ## Data from Fleiss (1981), p. 139. ## H0: The null hypothesis is that the four populations from which ## the patients were drawn have the same true proportion of smokers. ## A: The alternative is that this proportion is different in at ## least one of the populations. smokers <- c( 83, 90, 129, 70 ) patients <- c( 86, 93, 136, 82 ) prop.test(smokers, patients)
heads <- rbinom(1, size = 100, prob = .5) prop.test(heads, 100) # continuity correction TRUE by default prop.test(heads, 100, correct = FALSE) ## Data from Fleiss (1981), p. 139. ## H0: The null hypothesis is that the four populations from which ## the patients were drawn have the same true proportion of smokers. ## A: The alternative is that this proportion is different in at ## least one of the populations. smokers <- c( 83, 90, 129, 70 ) patients <- c( 86, 93, 136, 82 ) prop.test(smokers, patients)
Performs chi-squared test for trend in proportions, i.e., a test
asymptotically optimal for local alternatives where the log odds vary
in proportion with score
. By default, score
is chosen
as the group numbers.
prop.trend.test(x, n, score = seq_along(x))
prop.trend.test(x, n, score = seq_along(x))
x |
Number of events |
n |
Number of trials |
score |
Group score |
An object of class "htest"
with title, test statistic, p-value,
etc.
This really should get integrated with prop.test
Peter Dalgaard
smokers <- c( 83, 90, 129, 70 ) patients <- c( 86, 93, 136, 82 ) prop.test(smokers, patients) prop.trend.test(smokers, patients) prop.trend.test(smokers, patients, c(0,0,0,1))
smokers <- c( 83, 90, 129, 70 ) patients <- c( 86, 93, 136, 82 ) prop.test(smokers, patients) prop.trend.test(smokers, patients) prop.trend.test(smokers, patients, c(0,0,0,1))
qqnorm
is a generic function the default method of which
produces a normal QQ plot of the values in y
.
qqline
adds a line to a “theoretical”, by default
normal, quantile-quantile plot which passes through the probs
quantiles, by default the first and third quartiles.
qqplot
produces a QQ plot of two datasets. If conf.level
is
given, a confidence band for a function transforming the distribution of
x
into the distribution of y
is plotted based on
Switzer (1976). The QQ plot can be understood as an estimate of such a
treatment function. If exact = NULL
(the default), an
exact confidence band is computed if the product of
the sample sizes is less than 10000, with or
without ties. Otherwise, asymptotic distributions are used whose approximations may
be inaccurate in small samples. Monte-Carlo approximations based on
B
random permutations are computed when simulate = TRUE
.
Confidence bands are in agreement with Smirnov's test, that is, the
bisecting line is covered by the band iff the null of both samples coming
from the same distribution cannot be rejected at the same level.
Graphical parameters may be given as arguments to qqnorm
,
qqplot
and qqline
.
qqnorm(y, ...) ## Default S3 method: qqnorm(y, ylim, main = "Normal Q-Q Plot", xlab = "Theoretical Quantiles", ylab = "Sample Quantiles", plot.it = TRUE, datax = FALSE, ...) qqline(y, datax = FALSE, distribution = qnorm, probs = c(0.25, 0.75), qtype = 7, ...) qqplot(x, y, plot.it = TRUE, xlab = deparse1(substitute(x)), ylab = deparse1(substitute(y)), ..., conf.level = NULL, conf.args = list(exact = NULL, simulate.p.value = FALSE, B = 2000, col = NA, border = NULL))
qqnorm(y, ...) ## Default S3 method: qqnorm(y, ylim, main = "Normal Q-Q Plot", xlab = "Theoretical Quantiles", ylab = "Sample Quantiles", plot.it = TRUE, datax = FALSE, ...) qqline(y, datax = FALSE, distribution = qnorm, probs = c(0.25, 0.75), qtype = 7, ...) qqplot(x, y, plot.it = TRUE, xlab = deparse1(substitute(x)), ylab = deparse1(substitute(y)), ..., conf.level = NULL, conf.args = list(exact = NULL, simulate.p.value = FALSE, B = 2000, col = NA, border = NULL))
x |
The first sample for |
y |
The second or only data sample. |
xlab , ylab , main
|
plot labels. The |
plot.it |
logical. Should the result be plotted? |
datax |
logical. Should data values be on the x-axis? |
distribution |
quantile function for reference theoretical distribution. |
probs |
numeric vector of length two, representing probabilities. Corresponding quantile pairs define the line drawn. |
qtype |
the |
ylim , ...
|
graphical parameters. |
conf.level |
confidence level of the band. The default, |
conf.args |
list of arguments defining confidence band computation
and visualisation: |
For qqnorm
and qqplot
, a list with components
x |
The x coordinates of the points that were/would be plotted |
y |
The original |
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988). The New S Language. Wadsworth & Brooks/Cole.
Switzer, P. (1976). Confidence procedures for two-sample problems. Biometrika, 63(1), 13–25. doi:10.1093/biomet/63.1.13.
ppoints
, used by qqnorm
to generate
approximations to expected order statistics for a normal distribution.
require(graphics) y <- rt(200, df = 5) qqnorm(y); qqline(y, col = 2) qqplot(y, rt(300, df = 5)) qqnorm(precip, ylab = "Precipitation [in/yr] for 70 US cities") ## "QQ-Chisquare" : -------------------------- y <- rchisq(500, df = 3) ## Q-Q plot for Chi^2 data against true theoretical distribution: qqplot(qchisq(ppoints(500), df = 3), y, main = expression("Q-Q plot for" ~~ {chi^2}[nu == 3])) qqline(y, distribution = function(p) qchisq(p, df = 3), probs = c(0.1, 0.6), col = 2) mtext("qqline(*, dist = qchisq(., df=3), prob = c(0.1, 0.6))") ## (Note that the above uses ppoints() with a = 1/2, giving the ## probability points for quantile type 5: so theoretically, using ## qqline(qtype = 5) might be preferable.) ## Figure 1 in Switzer (1976), knee angle data switzer <- data.frame( angle = c(-31, -30, -25, -25, -23, -23, -22, -20, -20, -18, -18, -18, -16, -15, -15, -14, -13, -11, -10, - 9, - 8, - 7, - 7, - 7, - 6, - 6, - 4, - 4, - 3, - 2, - 2, - 1, 1, 1, 4, 5, 11, 12, 16, 34, -31, -20, -18, -16, -16, -16, -15, -14, -14, -14, -14, -13, -13, -11, -11, -10, - 9, - 9, - 8, - 7, - 7, - 6, - 6, -5, - 5, - 5, - 4, - 2, - 2, - 2, 0, 0, 1, 1, 2, 4, 5, 5, 6, 17), sex = gl(2, 40, labels = c("Female", "Male"))) ks.test(angle ~ sex, data = switzer) d <- with(switzer, split(angle, sex)) with(d, qqplot(Female, Male, pch = 19, xlim = c(-31, 31), ylim = c(-31, 31), conf.level = 0.945, conf.args = list(col = "lightgrey", exact = TRUE)) ) abline(a = 0, b = 1) ## agreement with ks.test set.seed(1) x <- rnorm(50) y <- rnorm(50, mean = .5, sd = .95) ex <- TRUE ### p = 0.112 (pval <- ks.test(x, y, exact = ex)$p.value) ## 88.8% confidence band with bisecting line ## touching the lower bound qqplot(x, y, pch = 19, conf.level = 1 - pval, conf.args = list(exact = ex, col = "lightgrey")) abline(a = 0, b = 1)
require(graphics) y <- rt(200, df = 5) qqnorm(y); qqline(y, col = 2) qqplot(y, rt(300, df = 5)) qqnorm(precip, ylab = "Precipitation [in/yr] for 70 US cities") ## "QQ-Chisquare" : -------------------------- y <- rchisq(500, df = 3) ## Q-Q plot for Chi^2 data against true theoretical distribution: qqplot(qchisq(ppoints(500), df = 3), y, main = expression("Q-Q plot for" ~~ {chi^2}[nu == 3])) qqline(y, distribution = function(p) qchisq(p, df = 3), probs = c(0.1, 0.6), col = 2) mtext("qqline(*, dist = qchisq(., df=3), prob = c(0.1, 0.6))") ## (Note that the above uses ppoints() with a = 1/2, giving the ## probability points for quantile type 5: so theoretically, using ## qqline(qtype = 5) might be preferable.) ## Figure 1 in Switzer (1976), knee angle data switzer <- data.frame( angle = c(-31, -30, -25, -25, -23, -23, -22, -20, -20, -18, -18, -18, -16, -15, -15, -14, -13, -11, -10, - 9, - 8, - 7, - 7, - 7, - 6, - 6, - 4, - 4, - 3, - 2, - 2, - 1, 1, 1, 4, 5, 11, 12, 16, 34, -31, -20, -18, -16, -16, -16, -15, -14, -14, -14, -14, -13, -13, -11, -11, -10, - 9, - 9, - 8, - 7, - 7, - 6, - 6, -5, - 5, - 5, - 4, - 2, - 2, - 2, 0, 0, 1, 1, 2, 4, 5, 5, 6, 17), sex = gl(2, 40, labels = c("Female", "Male"))) ks.test(angle ~ sex, data = switzer) d <- with(switzer, split(angle, sex)) with(d, qqplot(Female, Male, pch = 19, xlim = c(-31, 31), ylim = c(-31, 31), conf.level = 0.945, conf.args = list(col = "lightgrey", exact = TRUE)) ) abline(a = 0, b = 1) ## agreement with ks.test set.seed(1) x <- rnorm(50) y <- rnorm(50, mean = .5, sd = .95) ex <- TRUE ### p = 0.112 (pval <- ks.test(x, y, exact = ex)$p.value) ## 88.8% confidence band with bisecting line ## touching the lower bound qqplot(x, y, pch = 19, conf.level = 1 - pval, conf.args = list(exact = ex, col = "lightgrey")) abline(a = 0, b = 1)
Performs a Quade test with unreplicated blocked data.
quade.test(y, ...) ## Default S3 method: quade.test(y, groups, blocks, ...) ## S3 method for class 'formula' quade.test(formula, data, subset, na.action, ...)
quade.test(y, ...) ## Default S3 method: quade.test(y, groups, blocks, ...) ## S3 method for class 'formula' quade.test(formula, data, subset, na.action, ...)
y |
either a numeric vector of data values, or a data matrix. |
groups |
a vector giving the group for the corresponding elements
of |
blocks |
a vector giving the block for the corresponding elements
of |
formula |
a formula of the form |
data |
an optional matrix or data frame (or similar: see
|
subset |
an optional vector specifying a subset of observations to be used. |
na.action |
a function which indicates what should happen when
the data contain |
... |
further arguments to be passed to or from methods. |
quade.test
can be used for analyzing unreplicated complete
block designs (i.e., there is exactly one observation in y
for each combination of levels of groups
and blocks
)
where the normality assumption may be violated.
The null hypothesis is that apart from an effect of blocks
,
the location parameter of y
is the same in each of the
groups
.
If y
is a matrix, groups
and blocks
are obtained
from the column and row indices, respectively. NA
's are not
allowed in groups
or blocks
; if y
contains
NA
's, corresponding blocks are removed.
A list with class "htest"
containing the following components:
statistic |
the value of Quade's F statistic. |
parameter |
a vector with the numerator and denominator degrees of freedom of the approximate F distribution of the test statistic. |
p.value |
the p-value of the test. |
method |
the character string |
data.name |
a character string giving the names of the data. |
D. Quade (1979), Using weighted rankings in the analysis of complete blocks with additive block effects. Journal of the American Statistical Association 74, 680–683.
William J. Conover (1999), Practical nonparametric statistics. New York: John Wiley & Sons. Pages 373–380.
## Conover (1999, p. 375f): ## Numbers of five brands of a new hand lotion sold in seven stores ## during one week. y <- matrix(c( 5, 4, 7, 10, 12, 1, 3, 1, 0, 2, 16, 12, 22, 22, 35, 5, 4, 3, 5, 4, 10, 9, 7, 13, 10, 19, 18, 28, 37, 58, 10, 7, 6, 8, 7), nrow = 7, byrow = TRUE, dimnames = list(Store = as.character(1:7), Brand = LETTERS[1:5])) y (qTst <- quade.test(y)) ## Show equivalence of different versions of test : utils::str(dy <- as.data.frame(as.table(y))) qT. <- quade.test(Freq ~ Brand|Store, data = dy) qT.$data.name <- qTst$data.name stopifnot(all.equal(qTst, qT., tolerance = 1e-15)) dys <- dy[order(dy[,"Freq"]),] qTs <- quade.test(Freq ~ Brand|Store, data = dys) qTs$data.name <- qTst$data.name stopifnot(all.equal(qTst, qTs, tolerance = 1e-15))
## Conover (1999, p. 375f): ## Numbers of five brands of a new hand lotion sold in seven stores ## during one week. y <- matrix(c( 5, 4, 7, 10, 12, 1, 3, 1, 0, 2, 16, 12, 22, 22, 35, 5, 4, 3, 5, 4, 10, 9, 7, 13, 10, 19, 18, 28, 37, 58, 10, 7, 6, 8, 7), nrow = 7, byrow = TRUE, dimnames = list(Store = as.character(1:7), Brand = LETTERS[1:5])) y (qTst <- quade.test(y)) ## Show equivalence of different versions of test : utils::str(dy <- as.data.frame(as.table(y))) qT. <- quade.test(Freq ~ Brand|Store, data = dy) qT.$data.name <- qTst$data.name stopifnot(all.equal(qTst, qT., tolerance = 1e-15)) dys <- dy[order(dy[,"Freq"]),] qTs <- quade.test(Freq ~ Brand|Store, data = dys) qTs$data.name <- qTst$data.name stopifnot(all.equal(qTst, qTs, tolerance = 1e-15))
The generic function quantile
produces sample quantiles
corresponding to the given probabilities.
The smallest observation corresponds to a probability of 0 and the
largest to a probability of 1.
quantile(x, ...) ## Default S3 method: quantile(x, probs = seq(0, 1, 0.25), na.rm = FALSE, names = TRUE, type = 7, digits = 7, ...)
quantile(x, ...) ## Default S3 method: quantile(x, probs = seq(0, 1, 0.25), na.rm = FALSE, names = TRUE, type = 7, digits = 7, ...)
x |
numeric vector whose sample quantiles are wanted, or an
object of a class for which a method has been defined (see also
‘details’). |
probs |
numeric vector of probabilities with values in
|
na.rm |
logical; if true, any |
names |
logical; if true, the result has a |
type |
an integer between 1 and 9 selecting one of the nine quantile algorithms detailed below to be used. |
digits |
used only when |
... |
further arguments passed to or from other methods. |
A vector of length length(probs)
is returned;
if names = TRUE
, it has a names
attribute.
NA
and NaN
values in probs
are
propagated to the result.
The default method works with classed objects sufficiently like
numeric vectors that sort
and (not needed by types 1 and 3)
addition of elements and multiplication by a number work correctly.
Note that as this is in a namespace, the copy of sort
in
base will be used, not some S4 generic of that name. Also note
that that is no check on the ‘correctly’, and so
e.g. quantile
can be applied to complex vectors which (apart
from ties) will be ordered on their real parts.
There is a method for the date-time classes (see
"POSIXt"
). Types 1 and 3 can be used for class
"Date"
and for ordered factors.
quantile
returns estimates of underlying distribution quantiles
based on one or two order statistics from the supplied elements in
x
at probabilities in probs
. One of the nine quantile
algorithms discussed in Hyndman and Fan (1996), selected by
type
, is employed.
All sample quantiles are defined as weighted averages of
consecutive order statistics. Sample quantiles of type
are defined by:
where ,
,
is the
-th order statistic,
is the
sample size, the value of
is a function of
and
,
and
is a constant determined by the sample quantile type.
Discontinuous sample quantile types 1, 2, and 3
For types 1, 2 and 3, is a discontinuous
function of
, with
when
and
, and
when
.
Inverse of empirical distribution function.
if
, and 1 otherwise.
Similar to type 1 but with averaging at discontinuities.
if
, and 1 otherwise (SAS default, see
Wicklin (2017)).
Nearest even order statistic (SAS default till ca. 2010).
if
and
is even,
and 1 otherwise.
Continuous sample quantile types 4 through 9
For types 4 through 9, is a continuous function
of
, with
and
given below. The
sample quantiles can be obtained equivalently by linear interpolation
between the points
where
is the
-th order statistic. Specific expressions for
are given below.
.
.
That is, linear interpolation of the empirical cdf.
.
.
That is a piecewise linear function where the knots are the values
midway through the steps of the empirical cdf. This is popular
amongst hydrologists.
.
.
Thus
.
This is used by Minitab and by SPSS.
.
.
In this case,
.
This is used by S.
.
.
Then
.
The resulting quantile estimates are approximately median-unbiased
regardless of the distribution of
x
.
.
.
The resulting quantile estimates are approximately unbiased for
the expected order statistics if
x
is normally distributed.
Further details are provided in Hyndman and Fan (1996) who recommended type 8. The default method is type 7, as used by S and by R < 2.0.0. Makkonen argues for type 6, also as already proposed by Weibull in 1939. The Wikipedia page contains further information about availability of these 9 types in software.
of the version used in R >= 2.0.0, Ivan Frohne and Rob J Hyndman.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
Hyndman, R. J. and Fan, Y. (1996) Sample quantiles in statistical packages, American Statistician 50, 361–365. doi:10.2307/2684934.
Wicklin, R. (2017) Sample quantiles: A comparison of 9 definitions; SAS Blog. https://blogs.sas.com/content/iml/2017/05/24/definitions-sample-quantiles.html
Wikipedia: https://en.wikipedia.org/wiki/Quantile#Estimating_quantiles_from_a_sample
ecdf
for empirical distributions of which
quantile
is an inverse;
boxplot.stats
and fivenum
for computing
other versions of quartiles, etc.
quantile(x <- rnorm(1001)) # Extremes & Quartiles by default quantile(x, probs = c(0.1, 0.5, 1, 2, 5, 10, 50, NA)/100) ### Compare different types quantAll <- function(x, prob, ...) t(vapply(1:9, function(typ) quantile(x, probs = prob, type = typ, ...), quantile(x, prob, type=1, ...))) p <- c(0.1, 0.5, 1, 2, 5, 10, 50)/100 signif(quantAll(x, p), 4) ## 0% and 100% are equal to min(), max() for all types: stopifnot(t(quantAll(x, prob=0:1)) == range(x)) ## for complex numbers: z <- complex(real = x, imaginary = -10*x) signif(quantAll(z, p), 4)
quantile(x <- rnorm(1001)) # Extremes & Quartiles by default quantile(x, probs = c(0.1, 0.5, 1, 2, 5, 10, 50, NA)/100) ### Compare different types quantAll <- function(x, prob, ...) t(vapply(1:9, function(typ) quantile(x, probs = prob, type = typ, ...), quantile(x, prob, type=1, ...))) p <- c(0.1, 0.5, 1, 2, 5, 10, 50)/100 signif(quantAll(x, p), 4) ## 0% and 100% are equal to min(), max() for all types: stopifnot(t(quantAll(x, prob=0:1)) == range(x)) ## for complex numbers: z <- complex(real = x, imaginary = -10*x) signif(quantAll(z, p), 4)
Generate random 2-way tables with given marginals using Patefield's algorithm.
r2dtable(n, r, c)
r2dtable(n, r, c)
n |
a non-negative numeric giving the number of tables to be drawn. |
r |
a non-negative vector of length at least 2 giving the row
totals, to be coerced to |
c |
a non-negative vector of length at least 2 giving the column
totals, to be coerced to |
A list of length n
containing the generated tables as its
components.
Patefield, W. M. (1981). Algorithm AS 159: An efficient method of generating r x c tables with given row and column totals. Applied Statistics, 30, 91–97. doi:10.2307/2346669.
## Fisher's Tea Drinker data. TeaTasting <- matrix(c(3, 1, 1, 3), nrow = 2, dimnames = list(Guess = c("Milk", "Tea"), Truth = c("Milk", "Tea"))) ## Simulate permutation test for independence based on the maximum ## Pearson residuals (rather than their sum). rowTotals <- rowSums(TeaTasting) colTotals <- colSums(TeaTasting) nOfCases <- sum(rowTotals) expected <- outer(rowTotals, colTotals) / nOfCases maxSqResid <- function(x) max((x - expected) ^ 2 / expected) simMaxSqResid <- sapply(r2dtable(1000, rowTotals, colTotals), maxSqResid) sum(simMaxSqResid >= maxSqResid(TeaTasting)) / 1000 ## Fisher's exact test gives p = 0.4857 ...
## Fisher's Tea Drinker data. TeaTasting <- matrix(c(3, 1, 1, 3), nrow = 2, dimnames = list(Guess = c("Milk", "Tea"), Truth = c("Milk", "Tea"))) ## Simulate permutation test for independence based on the maximum ## Pearson residuals (rather than their sum). rowTotals <- rowSums(TeaTasting) colTotals <- colSums(TeaTasting) nOfCases <- sum(rowTotals) expected <- outer(rowTotals, colTotals) / nOfCases maxSqResid <- function(x) max((x - expected) ^ 2 / expected) simMaxSqResid <- sapply(r2dtable(1000, rowTotals, colTotals), maxSqResid) sum(simMaxSqResid >= maxSqResid(TeaTasting)) / 1000 ## Fisher's exact test gives p = 0.4857 ...
Read, write and coerce ‘flat’ (contingency) tables, aka
ftable
s.
read.ftable(file, sep = "", quote = "\"", row.var.names, col.vars, skip = 0) write.ftable(x, file = "", quote = TRUE, append = FALSE, digits = getOption("digits"), sep = " ", ...) ## S3 method for class 'ftable' format(x, quote = TRUE, digits = getOption("digits"), method = c("non.compact", "row.compact", "col.compact", "compact"), lsep = " | ", justify = c("left", "right"), ...) ## S3 method for class 'ftable' print(x, digits = getOption("digits"), ...)
read.ftable(file, sep = "", quote = "\"", row.var.names, col.vars, skip = 0) write.ftable(x, file = "", quote = TRUE, append = FALSE, digits = getOption("digits"), sep = " ", ...) ## S3 method for class 'ftable' format(x, quote = TRUE, digits = getOption("digits"), method = c("non.compact", "row.compact", "col.compact", "compact"), lsep = " | ", justify = c("left", "right"), ...) ## S3 method for class 'ftable' print(x, digits = getOption("digits"), ...)
file |
either a character string naming a file or a |
sep |
the field separator string. Values on each line of the file are separated by this string. |
quote |
a character string giving the set of quoting characters
for |
row.var.names |
a character vector with the names of the row variables, in case these cannot be determined automatically. |
col.vars |
a list giving the names and levels of the column variables, in case these cannot be determined automatically. |
skip |
the number of lines of the data file to skip before beginning to read data. |
x |
an object of class |
append |
logical. If |
digits |
an integer giving the number of significant digits to
use for (the cell entries of) |
method |
string specifying how the
|
lsep |
only for |
justify |
|
... |
further arguments to be passed to or
from methods; for |
read.ftable
reads in a flat-like contingency table from a
file. If the file contains the written representation of a flat
table (more precisely, a header with all information on names and
levels of column variables, followed by a line with the names of the
row variables), no further arguments are needed. Similarly, flat
tables with only one column variable the name of which is the only
entry in the first line are handled automatically. Other variants can
be dealt with by skipping all header information using skip
,
and providing the names of the row variables and the names and levels
of the column variable using row.var.names
and col.vars
,
respectively. See the examples below.
Note that flat tables are characterized by their ‘ragged’
display of row (and maybe also column) labels. If the full grid of
levels of the row variables is given, one should instead use
read.table
to read in the data, and create the
contingency table from this using xtabs
.
write.ftable
writes a flat table to a file, which is useful for
generating ‘pretty’ ASCII representations of contingency
tables. Different versions are available via the method
argument, which may be useful, for example, for constructing LaTeX tables.
Agresti, A. (1990) Categorical data analysis. New York: Wiley.
ftable
for more information on flat contingency tables.
## Agresti (1990), page 157, Table 5.8. ## Not in ftable standard format, but o.k. file <- tempfile() cat(" Intercourse\n", "Race Gender Yes No\n", "White Male 43 134\n", " Female 26 149\n", "Black Male 29 23\n", " Female 22 36\n", file = file) file.show(file) ft1 <- read.ftable(file) ft1 unlink(file) ## Agresti (1990), page 297, Table 8.16. ## Almost o.k., but misses the name of the row variable. file <- tempfile() cat(" \"Tonsil Size\"\n", " \"Not Enl.\" \"Enl.\" \"Greatly Enl.\"\n", "Noncarriers 497 560 269\n", "Carriers 19 29 24\n", file = file) file.show(file) ft <- read.ftable(file, skip = 2, row.var.names = "Status", col.vars = list("Tonsil Size" = c("Not Enl.", "Enl.", "Greatly Enl."))) ft unlink(file) ft22 <- ftable(Titanic, row.vars = 2:1, col.vars = 4:3) write.ftable(ft22, quote = FALSE) # is the same as print(ft22)#method="non.compact" is default print(ft22, method="row.compact") print(ft22, method="col.compact") print(ft22, method="compact") ## using 'justify' and 'quote' : format(ftable(wool + tension ~ breaks, warpbreaks), justify = "none", quote = FALSE)
## Agresti (1990), page 157, Table 5.8. ## Not in ftable standard format, but o.k. file <- tempfile() cat(" Intercourse\n", "Race Gender Yes No\n", "White Male 43 134\n", " Female 26 149\n", "Black Male 29 23\n", " Female 22 36\n", file = file) file.show(file) ft1 <- read.ftable(file) ft1 unlink(file) ## Agresti (1990), page 297, Table 8.16. ## Almost o.k., but misses the name of the row variable. file <- tempfile() cat(" \"Tonsil Size\"\n", " \"Not Enl.\" \"Enl.\" \"Greatly Enl.\"\n", "Noncarriers 497 560 269\n", "Carriers 19 29 24\n", file = file) file.show(file) ft <- read.ftable(file, skip = 2, row.var.names = "Status", col.vars = list("Tonsil Size" = c("Not Enl.", "Enl.", "Greatly Enl."))) ft unlink(file) ft22 <- ftable(Titanic, row.vars = 2:1, col.vars = 4:3) write.ftable(ft22, quote = FALSE) # is the same as print(ft22)#method="non.compact" is default print(ft22, method="row.compact") print(ft22, method="col.compact") print(ft22, method="compact") ## using 'justify' and 'quote' : format(ftable(wool + tension ~ breaks, warpbreaks), justify = "none", quote = FALSE)
Draws rectangles around the branches of a dendrogram highlighting the corresponding clusters. First the dendrogram is cut at a certain level, then a rectangle is drawn around selected branches.
rect.hclust(tree, k = NULL, which = NULL, x = NULL, h = NULL, border = 2, cluster = NULL)
rect.hclust(tree, k = NULL, which = NULL, x = NULL, h = NULL, border = 2, cluster = NULL)
tree |
an object of the type produced by |
k , h
|
Scalar. Cut the dendrogram such that either exactly
|
which , x
|
A vector selecting the clusters around which a
rectangle should be drawn. |
border |
Vector with border colors for the rectangles. |
cluster |
Optional vector with cluster memberships as returned by
|
(Invisibly) returns a list where each element contains a vector of data points contained in the respective cluster.
require(graphics) hca <- hclust(dist(USArrests)) plot(hca) rect.hclust(hca, k = 3, border = "red") x <- rect.hclust(hca, h = 50, which = c(2,7), border = 3:4) x
require(graphics) hca <- hclust(dist(USArrests)) plot(hca) rect.hclust(hca, k = 3, border = "red") x <- rect.hclust(hca, h = 50, which = c(2,7), border = 3:4) x
The levels of a factor are re-ordered so that the level specified by
ref
is first and the others are moved down. This is useful
for contr.treatment
contrasts which take the first level as
the reference.
relevel(x, ref, ...)
relevel(x, ref, ...)
x |
an unordered factor. |
ref |
the reference level, typically a string. |
... |
additional arguments for future methods. |
This, as reorder()
, is a special case of simply calling
factor(x, levels = levels(x)[....])
.
A factor of the same length as x
.
factor
, contr.treatment
,
levels
, reorder
.
warpbreaks$tension <- relevel(warpbreaks$tension, ref = "M") summary(lm(breaks ~ wool + tension, data = warpbreaks))
warpbreaks$tension <- relevel(warpbreaks$tension, ref = "M") summary(lm(breaks ~ wool + tension, data = warpbreaks))
reorder
is a generic function. The "default"
method
treats its first argument as a categorical variable, and reorders its
levels based on the values of a second variable, usually numeric.
reorder(x, ...) ## Default S3 method: reorder(x, X, FUN = mean, ..., order = is.ordered(x), decreasing = FALSE)
reorder(x, ...) ## Default S3 method: reorder(x, X, FUN = mean, ..., order = is.ordered(x), decreasing = FALSE)
x |
an atomic vector, usually a |
X |
a vector of the same length as |
FUN |
a |
... |
optional: extra arguments supplied to |
order |
logical, whether return value will be an ordered factor rather than a factor. |
decreasing |
logical, whether the levels will be ordered in increasing or decreasing order. |
This, as relevel()
, is a special case of simply calling
factor(x, levels = levels(x)[....])
.
A factor or an ordered factor (depending on the value of
order
), with the order of the levels determined by
FUN
applied to X
grouped by x
. By default, the
levels are ordered such that the values returned by FUN
are in increasing order. Empty levels will be dropped.
Additionally, the values of FUN
applied to the subsets of
X
(in the original order of the levels of x
) is returned
as the "scores"
attribute.
Deepayan Sarkar [email protected]
reorder.dendrogram
, levels
,
relevel
.
require(graphics) bymedian <- with(InsectSprays, reorder(spray, count, median)) boxplot(count ~ bymedian, data = InsectSprays, xlab = "Type of spray", ylab = "Insect count", main = "InsectSprays data", varwidth = TRUE, col = "lightgray") bymedianR <- with(InsectSprays, reorder(spray, count, median, decreasing=TRUE)) stopifnot(exprs = { identical(attr(bymedian, "scores") -> sc, attr(bymedianR,"scores")) identical(nms <- names(sc), LETTERS[1:6]) identical(levels(bymedian ), nms[isc <- order(sc)]) identical(levels(bymedianR), nms[rev(isc)]) })
require(graphics) bymedian <- with(InsectSprays, reorder(spray, count, median)) boxplot(count ~ bymedian, data = InsectSprays, xlab = "Type of spray", ylab = "Insect count", main = "InsectSprays data", varwidth = TRUE, col = "lightgray") bymedianR <- with(InsectSprays, reorder(spray, count, median, decreasing=TRUE)) stopifnot(exprs = { identical(attr(bymedian, "scores") -> sc, attr(bymedianR,"scores")) identical(nms <- names(sc), LETTERS[1:6]) identical(levels(bymedian ), nms[isc <- order(sc)]) identical(levels(bymedianR), nms[rev(isc)]) })
A method for the generic function reorder
.
There are many different orderings of a dendrogram that are consistent with the structure imposed. This function takes a dendrogram and a vector of values and reorders the dendrogram in the order of the supplied vector, maintaining the constraints on the dendrogram.
## S3 method for class 'dendrogram' reorder(x, wts, agglo.FUN = sum, ...)
## S3 method for class 'dendrogram' reorder(x, wts, agglo.FUN = sum, ...)
x |
the (dendrogram) object to be reordered |
wts |
numeric weights (arbitrary values) for reordering. |
agglo.FUN |
a function for weights agglomeration, see below. |
... |
additional arguments |
Using the weights wts
, the leaves of the dendrogram are
reordered so as to be in an order as consistent as possible with the
weights. At each node, the branches are ordered in
increasing weights where the weight of a branch is defined as
where
is
agglo.FUN
and is the
weight of the
-th sub branch.
A dendrogram where each node has a further attribute value
with
its corresponding weight.
R. Gentleman and M. Maechler
rev.dendrogram
which simply reverses the nodes'
order; heatmap
, cophenetic
.
require(graphics) set.seed(123) x <- rnorm(10) hc <- hclust(dist(x)) dd <- as.dendrogram(hc) dd.reorder <- reorder(dd, 10:1) plot(dd, main = "random dendrogram 'dd'") op <- par(mfcol = 1:2) plot(dd.reorder, main = "reorder(dd, 10:1)") plot(reorder(dd, 10:1, agglo.FUN = mean), main = "reorder(dd, 10:1, mean)") par(op)
require(graphics) set.seed(123) x <- rnorm(10) hc <- hclust(dist(x)) dd <- as.dendrogram(hc) dd.reorder <- reorder(dd, 10:1) plot(dd, main = "random dendrogram 'dd'") op <- par(mfcol = 1:2) plot(dd.reorder, main = "reorder(dd, 10:1)") plot(reorder(dd, 10:1, agglo.FUN = mean), main = "reorder(dd, 10:1, mean)") par(op)
Returns a vector or a list of the number of replicates for each term in the formula.
replications(formula, data = NULL, na.action)
replications(formula, data = NULL, na.action)
formula |
a formula or a terms object or a data frame. |
data |
a data frame used to find the objects in |
na.action |
function for handling missing values. Defaults to
a |
If formula
is a data frame and data
is missing,
formula
is used for data
with the formula ~ .
.
Any character vectors in the formula are coerced to factors.
A vector or list with one entry for each term in the formula giving the number(s) of replications for each level. If all levels are balanced (have the same number of replications) the result is a vector, otherwise it is a list with a component for each terms, as a vector, matrix or array as required.
A test for balance is !is.list(replications(formula,data))
.
The design was inspired by the S function of the same name described in Chambers et al. (1992).
Chambers, J. M., Freeny, A and Heiberger, R. M. (1992) Analysis of variance; designed experiments. Chapter 5 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
## From Venables and Ripley (2002) p.165. N <- c(0,1,0,1,1,1,0,0,0,1,1,0,1,1,0,0,1,0,1,0,1,1,0,0) P <- c(1,1,0,0,0,1,0,1,1,1,0,0,0,1,0,1,1,0,0,1,0,1,1,0) K <- c(1,0,0,1,0,1,1,0,0,1,0,1,0,1,1,0,0,0,1,1,1,0,1,0) yield <- c(49.5,62.8,46.8,57.0,59.8,58.5,55.5,56.0,62.8,55.8,69.5, 55.0, 62.0,48.8,45.5,44.2,52.0,51.5,49.8,48.8,57.2,59.0,53.2,56.0) npk <- data.frame(block = gl(6,4), N = factor(N), P = factor(P), K = factor(K), yield = yield) replications(~ . - yield, npk)
## From Venables and Ripley (2002) p.165. N <- c(0,1,0,1,1,1,0,0,0,1,1,0,1,1,0,0,1,0,1,0,1,1,0,0) P <- c(1,1,0,0,0,1,0,1,1,1,0,0,0,1,0,1,1,0,0,1,0,1,1,0) K <- c(1,0,0,1,0,1,1,0,0,1,0,1,0,1,1,0,0,0,1,1,1,0,1,0) yield <- c(49.5,62.8,46.8,57.0,59.8,58.5,55.5,56.0,62.8,55.8,69.5, 55.0, 62.0,48.8,45.5,44.2,52.0,51.5,49.8,48.8,57.2,59.0,53.2,56.0) npk <- data.frame(block = gl(6,4), N = factor(N), P = factor(P), K = factor(K), yield = yield) replications(~ . - yield, npk)
This function reshapes a data frame between ‘wide’ format (with repeated measurements in separate columns of the same row) and ‘long’ format (with the repeated measurements in separate rows).
reshape(data, varying = NULL, v.names = NULL, timevar = "time", idvar = "id", ids = 1:NROW(data), times = seq_along(varying[[1]]), drop = NULL, direction, new.row.names = NULL, sep = ".", split = if (sep == "") { list(regexp = "[A-Za-z][0-9]", include = TRUE) } else { list(regexp = sep, include = FALSE, fixed = TRUE)} ) ### Typical usage for converting from long to wide format: # reshape(data, direction = "wide", # idvar = "___", timevar = "___", # mandatory # v.names = c(___), # time-varying variables # varying = list(___)) # auto-generated if missing ### Typical usage for converting from wide to long format: ### If names of wide-format variables are in a 'nice' format # reshape(data, direction = "long", # varying = c(___), # vector # sep) # to help guess 'v.names' and 'times' ### To specify long-format variable names explicitly # reshape(data, direction = "long", # varying = ___, # list / matrix / vector (use with care) # v.names = ___, # vector of variable names in long format # timevar, times, # name / values of constructed time variable # idvar, ids) # name / values of constructed id variable
reshape(data, varying = NULL, v.names = NULL, timevar = "time", idvar = "id", ids = 1:NROW(data), times = seq_along(varying[[1]]), drop = NULL, direction, new.row.names = NULL, sep = ".", split = if (sep == "") { list(regexp = "[A-Za-z][0-9]", include = TRUE) } else { list(regexp = sep, include = FALSE, fixed = TRUE)} ) ### Typical usage for converting from long to wide format: # reshape(data, direction = "wide", # idvar = "___", timevar = "___", # mandatory # v.names = c(___), # time-varying variables # varying = list(___)) # auto-generated if missing ### Typical usage for converting from wide to long format: ### If names of wide-format variables are in a 'nice' format # reshape(data, direction = "long", # varying = c(___), # vector # sep) # to help guess 'v.names' and 'times' ### To specify long-format variable names explicitly # reshape(data, direction = "long", # varying = ___, # list / matrix / vector (use with care) # v.names = ___, # vector of variable names in long format # timevar, times, # name / values of constructed time variable # idvar, ids) # name / values of constructed id variable
data |
a data frame |
varying |
names of sets of variables in the wide format that
correspond to single variables in long format
(‘time-varying’). This is canonically a list of vectors of
variable names, but it can optionally be a matrix of names, or a
single vector of names. In each case, when |
v.names |
names of variables in the long format that correspond to multiple variables in the wide format. See ‘Details’. |
timevar |
the variable in long format that differentiates multiple records from the same group or individual. If more than one record matches, the first will be taken (with a warning). |
idvar |
Names of one or more variables in long format that identify multiple records from the same group/individual. These variables may also be present in wide format. |
ids |
the values to use for a newly created |
times |
the values to use for a newly created |
drop |
a vector of names of variables to drop before reshaping. |
direction |
character string, partially matched to either
|
new.row.names |
character or |
sep |
A character vector of length 1, indicating a separating
character in the variable names in the wide format. This is used for
guessing |
split |
A list with three components, |
Although reshape()
can be used in a variety of contexts, the
motivating application is data from longitudinal studies, and the
arguments of this function are named and described in those terms. A
longitudinal study is characterized by repeated measurements of the
same variable(s), e.g., height and weight, on each unit being studied
(e.g., individual persons) at different time points (which are assumed
to be the same for all units). These variables are called time-varying
variables. The study may include other variables that are measured
only once for each unit and do not vary with time (e.g., gender and
race); these are called time-constant variables.
A ‘wide’ format representation of a longitudinal dataset will have one record (row) for each unit, typically with some time-constant variables that occupy single columns, and some time-varying variables that occupy multiple columns (one column for each time point). A ‘long’ format representation of the same dataset will have multiple records (rows) for each individual, with the time-constant variables being constant across these records and the time-varying variables varying across the records. The ‘long’ format dataset will have two additional variables: a ‘time’ variable identifying which time point each record comes from, and an ‘id’ variable showing which records refer to the same unit.
The type of conversion (long to wide or wide to long) is determined by
the direction
argument, which is mandatory unless the
data
argument is the result of a previous call to
reshape
. In that case, the operation can be reversed simply
using reshape(data)
(the other arguments are stored as
attributes on the data frame).
Conversion from long to wide format with direction = "wide"
is
the simpler operation, and is mainly useful in the context of
multivariate analysis where data is often expected as a wide-format
matrix. In this case, the time variable timevar
and id variable
idvar
must be specified. All other variables are assumed to be
time-varying, unless the time-varying variables are explicitly
specified via the v.names
argument. A warning is issued if
time-constant variables are not actually constant.
Each time-varying variable is expanded into multiple variables in the
wide format. The names of these expanded variables are generated
automatically, unless they are specified as the varying
argument in the form of a list (or matrix) with one component (or row)
for each time-varying variable. If varying
is a vector of
names, it is implicitly converted into a matrix, with one row for each
time-varying variable. Use this option with care if there are multiple
time-varying variables, as the ordering (by column, the default in the
matrix
constructor) may be unintuitive, whereas the
explicit list or matrix form is unambiguous.
Conversion from wide to long with direction = "long"
is the
more common operation as most (univariate) statistical modeling
functions expect data in the long format. In the simpler case where
there is only one time-varying variable, the corresponding columns in
the wide format input can be specified as the varying
argument,
which can be either a vector of column names or the corresponding
column indices. The name of the corresponding variable in the long
format output combining these columns can be optionally specified as
the v.names
argument, and the name of the time variables as the
timevar
argument. The values to use as the time values
corresponding to the different columns in the wide format can be
specified as the times
argument. If v.names
is
unspecified, the function will attempt to guess v.names
and
times
from varying
(an explicitly specified times
argument is unused in that case). The default expects variable names
like x.1
, x.2
, where sep = "."
specifies to
split at the dot and drop it from the name. To have alphabetic
followed by numeric times use sep = ""
.
Multiple time-varying variables can be specified in two ways, either
with varying
as an atomic vector as above, or as a list (or a
matrix). The first form is useful (and mandatory) if the automatic
variable name splitting as described above is used; this requires the
names of all time-varying variables to be suitably formatted in the
same manner, and v.names
to be unspecified. If varying
is a list (with one component for each time-varying variable) or a
matrix (one row for each time-varying variable), variable name
splitting is not attempted, and v.names
and times
will
generally need to be specified, although they will default to,
respectively, the first variable name in each set, and sequential
times.
Also, guessing is not attempted if v.names
is given explicitly,
even if varying
is an atomic vector. In that case, the number
of time-varying variables is taken to be the length of v.names
,
and varying
is implicitly converted into a matrix, with one row
for each time-varying variable. As in the case of long to wide
conversion, the matrix is filled up by column, so careful attention needs
to be paid to the order of variable names (or indices) in
varying
, which is taken to be like x.1
, y.1
,
x.2
, y.2
(i.e., variables corresponding to the same time
point need to be grouped together).
The split
argument should not usually be necessary. The
split$regexp
component is passed to either
strsplit
or regexpr
, where the latter is
used if split$include
is TRUE
, in which case the
splitting occurs after the first character of the matched string. In
the strsplit
case, the separator is not included in the
result, and it is possible to specify fixed-string matching using
split$fixed
.
The reshaped data frame with added attributes to simplify reshaping back to the original form.
stack
, aperm
;
relist
for reshaping the result of
unlist
. xtabs
and
as.data.frame.table
for creating contingency tables and
converting them back to data frames.
summary(Indometh) # data in long format ## long to wide (direction = "wide") requires idvar and timevar at a minimum reshape(Indometh, direction = "wide", idvar = "Subject", timevar = "time") ## can also explicitly specify name of combined variable wide <- reshape(Indometh, direction = "wide", idvar = "Subject", timevar = "time", v.names = "conc", sep= "_") wide ## reverse transformation reshape(wide, direction = "long") reshape(wide, idvar = "Subject", varying = list(2:12), v.names = "conc", direction = "long") ## times need not be numeric df <- data.frame(id = rep(1:4, rep(2,4)), visit = I(rep(c("Before","After"), 4)), x = rnorm(4), y = runif(4)) df reshape(df, timevar = "visit", idvar = "id", direction = "wide") ## warns that y is really varying reshape(df, timevar = "visit", idvar = "id", direction = "wide", v.names = "x") ## unbalanced 'long' data leads to NA fill in 'wide' form df2 <- df[1:7, ] df2 reshape(df2, timevar = "visit", idvar = "id", direction = "wide") ## Alternative regular expressions for guessing names df3 <- data.frame(id = 1:4, age = c(40,50,60,50), dose1 = c(1,2,1,2), dose2 = c(2,1,2,1), dose4 = c(3,3,3,3)) reshape(df3, direction = "long", varying = 3:5, sep = "") ## an example that isn't longitudinal data state.x77 <- as.data.frame(state.x77) long <- reshape(state.x77, idvar = "state", ids = row.names(state.x77), times = names(state.x77), timevar = "Characteristic", varying = list(names(state.x77)), direction = "long") reshape(long, direction = "wide") reshape(long, direction = "wide", new.row.names = unique(long$state)) ## multiple id variables df3 <- data.frame(school = rep(1:3, each = 4), class = rep(9:10, 6), time = rep(c(1,1,2,2), 3), score = rnorm(12)) wide <- reshape(df3, idvar = c("school", "class"), direction = "wide") wide ## transform back reshape(wide)
summary(Indometh) # data in long format ## long to wide (direction = "wide") requires idvar and timevar at a minimum reshape(Indometh, direction = "wide", idvar = "Subject", timevar = "time") ## can also explicitly specify name of combined variable wide <- reshape(Indometh, direction = "wide", idvar = "Subject", timevar = "time", v.names = "conc", sep= "_") wide ## reverse transformation reshape(wide, direction = "long") reshape(wide, idvar = "Subject", varying = list(2:12), v.names = "conc", direction = "long") ## times need not be numeric df <- data.frame(id = rep(1:4, rep(2,4)), visit = I(rep(c("Before","After"), 4)), x = rnorm(4), y = runif(4)) df reshape(df, timevar = "visit", idvar = "id", direction = "wide") ## warns that y is really varying reshape(df, timevar = "visit", idvar = "id", direction = "wide", v.names = "x") ## unbalanced 'long' data leads to NA fill in 'wide' form df2 <- df[1:7, ] df2 reshape(df2, timevar = "visit", idvar = "id", direction = "wide") ## Alternative regular expressions for guessing names df3 <- data.frame(id = 1:4, age = c(40,50,60,50), dose1 = c(1,2,1,2), dose2 = c(2,1,2,1), dose4 = c(3,3,3,3)) reshape(df3, direction = "long", varying = 3:5, sep = "") ## an example that isn't longitudinal data state.x77 <- as.data.frame(state.x77) long <- reshape(state.x77, idvar = "state", ids = row.names(state.x77), times = names(state.x77), timevar = "Characteristic", varying = list(names(state.x77)), direction = "long") reshape(long, direction = "wide") reshape(long, direction = "wide", new.row.names = unique(long$state)) ## multiple id variables df3 <- data.frame(school = rep(1:3, each = 4), class = rep(9:10, 6), time = rep(c(1,1,2,2), 3), score = rnorm(12)) wide <- reshape(df3, idvar = c("school", "class"), direction = "wide") wide ## transform back reshape(wide)
residuals
is a generic function which extracts model residuals
from objects returned by modeling functions.
resid
is an alias for residuals
,
abbreviated to encourage users to access object components through
an accessor function rather than by directly referencing an object
slot.
All object classes which are returned by model fitting functions
should provide a residuals
method. (Note that the method is
for ‘residuals’ and not ‘resid’.)
Methods can make use of naresid
methods to compensate
for the omission of missing values. The default, nls
and
smooth.spline
methods do.
residuals(object, ...) resid(object, ...)
residuals(object, ...) resid(object, ...)
object |
an object for which the extraction of model residuals is meaningful. |
... |
other arguments. |
Residuals extracted from the object object
.
Chambers, J. M. and Hastie, T. J. (1992) Statistical Models in S. Wadsworth & Brooks/Cole.
coefficients
, fitted.values
,
glm
, lm
.
influence.measures for standardized (rstandard
)
and studentized (rstudent
) residuals.
Compute running medians of odd span. This is the ‘most robust’ scatter plot smoothing possible. For efficiency (and historical reason), you can use one of two different algorithms giving identical results.
runmed(x, k, endrule = c("median", "keep", "constant"), algorithm = NULL, na.action = c("+Big_alternate", "-Big_alternate", "na.omit", "fail"), print.level = 0)
runmed(x, k, endrule = c("median", "keep", "constant"), algorithm = NULL, na.action = c("+Big_alternate", "-Big_alternate", "na.omit", "fail"), print.level = 0)
x |
numeric vector, the ‘dependent’ variable to be smoothed. |
k |
integer width of median window; must be odd. Turlach had a
default of |
endrule |
character string indicating how the values at the beginning and the end (of the data) should be treated. Can be abbreviated. Possible values are:
|
algorithm |
character string (partially matching |
na.action |
character string determining the behavior in the case of
|
print.level |
integer, indicating verboseness of algorithm; should rarely be changed by average users. |
Apart from the end values, the result y = runmed(x, k)
simply has
y[j] = median(x[(j-k2):(j+k2)])
(k = 2*k2+1
), computed very
efficiently.
The two algorithms are internally entirely different:
"Turlach"
is the Härdle–Steiger
algorithm (see Ref.) as implemented by Berwin Turlach.
A tree algorithm is used, ensuring performance where
n = length(x)
which is
asymptotically optimal.
"Stuetzle"
is the (older) Stuetzle–Friedman implementation
which makes use of median updating when one observation
enters and one leaves the smoothing window. While this performs as
which is slower asymptotically, it is
considerably faster for small
or
.
Note that, both algorithms (and the smoothEnds()
utility)
now “work” also when x
contains non-finite entries
(Inf
, NaN
, and
NA
):
"Turlach"
.......
"Stuetzle"
currently simply works by applying the underlying math library (‘libm’) arithmetic for the non-finite numbers; this may optionally change in the future.
Currently long vectors are only supported for algorithm = "Stuetzle"
.
vector of smoothed values of the same length as x
with an
attr
ibute k
containing (the ‘oddified’)
k
.
Martin Maechler [email protected], based on Fortran code from Werner Stuetzle and S-PLUS and C code from Berwin Turlach.
Härdle, W. and Steiger, W. (1995) Algorithm AS 296: Optimal median smoothing, Applied Statistics 44, 258–264. doi:10.2307/2986349.
Jerome H. Friedman and Werner Stuetzle (1982) Smoothing of Scatterplots; Report, Dep. Statistics, Stanford U., Project Orion 003.
smoothEnds
which implements Tukey's end point rule and
is called by default from runmed(*, endrule = "median")
.
smooth
uses running
medians of 3 for its compound smoothers.
require(graphics) utils::example(nhtemp) myNHT <- as.vector(nhtemp) myNHT[20] <- 2 * nhtemp[20] plot(myNHT, type = "b", ylim = c(48, 60), main = "Running Medians Example") lines(runmed(myNHT, 7), col = "red") ## special: multiple y values for one x plot(cars, main = "'cars' data and runmed(dist, 3)") lines(cars, col = "light gray", type = "c") with(cars, lines(speed, runmed(dist, k = 3), col = 2)) ## nice quadratic with a few outliers y <- ys <- (-20:20)^2 y [c(1,10,21,41)] <- c(150, 30, 400, 450) all(y == runmed(y, 1)) # 1-neighbourhood <==> interpolation plot(y) ## lines(y, lwd = .1, col = "light gray") lines(lowess(seq(y), y, f = 0.3), col = "brown") lines(runmed(y, 7), lwd = 2, col = "blue") lines(runmed(y, 11), lwd = 2, col = "red") ## Lowess is not robust y <- ys ; y[21] <- 6666 ; x <- seq(y) col <- c("black", "brown","blue") plot(y, col = col[1]) lines(lowess(x, y, f = 0.3), col = col[2]) lines(runmed(y, 7), lwd = 2, col = col[3]) legend(length(y),max(y), c("data", "lowess(y, f = 0.3)", "runmed(y, 7)"), xjust = 1, col = col, lty = c(0, 1, 1), pch = c(1,NA,NA)) ## An example with initial NA's - used to fail badly (notably for "Turlach"): x15 <- c(rep(NA, 4), c(9, 9, 4, 22, 6, 1, 7, 5, 2, 8, 3)) rS15 <- cbind(Sk.3 = runmed(x15, k = 3, algorithm="S"), Sk.7 = runmed(x15, k = 7, algorithm="S"), Sk.11= runmed(x15, k =11, algorithm="S")) rT15 <- cbind(Tk.3 = runmed(x15, k = 3, algorithm="T", print.level=1), Tk.7 = runmed(x15, k = 7, algorithm="T", print.level=1), Tk.9 = runmed(x15, k = 9, algorithm="T", print.level=1), Tk.11= runmed(x15, k =11, algorithm="T", print.level=1)) cbind(x15, rS15, rT15) # result for k=11 maybe a bit surprising .. Tv <- rT15[-(1:3),] stopifnot(3 <= Tv, Tv <= 9, 5 <= Tv[1:10,]) matplot(y = cbind(x15, rT15), type = "b", ylim = c(1,9), pch=1:5, xlab = NA, main = "runmed(x15, k, algo = \"Turlach\")") mtext(paste("x15 <-", deparse(x15))) points(x15, cex=2) legend("bottomleft", legend=c("data", paste("k = ", c(3,7,9,11))), bty="n", col=1:5, lty=1:5, pch=1:5)
require(graphics) utils::example(nhtemp) myNHT <- as.vector(nhtemp) myNHT[20] <- 2 * nhtemp[20] plot(myNHT, type = "b", ylim = c(48, 60), main = "Running Medians Example") lines(runmed(myNHT, 7), col = "red") ## special: multiple y values for one x plot(cars, main = "'cars' data and runmed(dist, 3)") lines(cars, col = "light gray", type = "c") with(cars, lines(speed, runmed(dist, k = 3), col = 2)) ## nice quadratic with a few outliers y <- ys <- (-20:20)^2 y [c(1,10,21,41)] <- c(150, 30, 400, 450) all(y == runmed(y, 1)) # 1-neighbourhood <==> interpolation plot(y) ## lines(y, lwd = .1, col = "light gray") lines(lowess(seq(y), y, f = 0.3), col = "brown") lines(runmed(y, 7), lwd = 2, col = "blue") lines(runmed(y, 11), lwd = 2, col = "red") ## Lowess is not robust y <- ys ; y[21] <- 6666 ; x <- seq(y) col <- c("black", "brown","blue") plot(y, col = col[1]) lines(lowess(x, y, f = 0.3), col = col[2]) lines(runmed(y, 7), lwd = 2, col = col[3]) legend(length(y),max(y), c("data", "lowess(y, f = 0.3)", "runmed(y, 7)"), xjust = 1, col = col, lty = c(0, 1, 1), pch = c(1,NA,NA)) ## An example with initial NA's - used to fail badly (notably for "Turlach"): x15 <- c(rep(NA, 4), c(9, 9, 4, 22, 6, 1, 7, 5, 2, 8, 3)) rS15 <- cbind(Sk.3 = runmed(x15, k = 3, algorithm="S"), Sk.7 = runmed(x15, k = 7, algorithm="S"), Sk.11= runmed(x15, k =11, algorithm="S")) rT15 <- cbind(Tk.3 = runmed(x15, k = 3, algorithm="T", print.level=1), Tk.7 = runmed(x15, k = 7, algorithm="T", print.level=1), Tk.9 = runmed(x15, k = 9, algorithm="T", print.level=1), Tk.11= runmed(x15, k =11, algorithm="T", print.level=1)) cbind(x15, rS15, rT15) # result for k=11 maybe a bit surprising .. Tv <- rT15[-(1:3),] stopifnot(3 <= Tv, Tv <= 9, 5 <= Tv[1:10,]) matplot(y = cbind(x15, rT15), type = "b", ylim = c(1,9), pch=1:5, xlab = NA, main = "runmed(x15, k, algo = \"Turlach\")") mtext(paste("x15 <-", deparse(x15))) points(x15, cex=2) legend("bottomleft", legend=c("data", paste("k = ", c(3,7,9,11))), bty="n", col=1:5, lty=1:5, pch=1:5)
Generate n
random matrices, distributed according to the
Wishart distribution with parameters Sigma
and df
,
.
rWishart(n, df, Sigma)
rWishart(n, df, Sigma)
n |
integer sample size. |
df |
numeric parameter, “degrees of freedom”. |
Sigma |
positive definite ( |
If is
a sample of
independent multivariate Gaussians with mean (vector) 0, and
covariance matrix
, the distribution of
is
.
Consequently, the expectation of is
Further, if Sigma
is scalar (), the Wishart
distribution is a scaled chi-squared (
)
distribution with
df
degrees of freedom,
.
The component wise variance is
a numeric array
, say R
, of dimension
, where each
R[,,i]
is a
positive definite matrix, a realization of the Wishart distribution
.
Douglas Bates
Mardia, K. V., J. T. Kent, and J. M. Bibby (1979) Multivariate Analysis, London: Academic Press.
## Artificial S <- toeplitz((10:1)/10) set.seed(11) R <- rWishart(1000, 20, S) dim(R) # 10 10 1000 mR <- apply(R, 1:2, mean) # ~= E[ Wish(S, 20) ] = 20 * S stopifnot(all.equal(mR, 20*S, tolerance = .009)) ## See Details, the variance is Va <- 20*(S^2 + tcrossprod(diag(S))) vR <- apply(R, 1:2, var) stopifnot(all.equal(vR, Va, tolerance = 1/16))
## Artificial S <- toeplitz((10:1)/10) set.seed(11) R <- rWishart(1000, 20, S) dim(R) # 10 10 1000 mR <- apply(R, 1:2, mean) # ~= E[ Wish(S, 20) ] = 20 * S stopifnot(all.equal(mR, 20*S, tolerance = .009)) ## See Details, the variance is Va <- 20*(S^2 + tcrossprod(diag(S))) vR <- apply(R, 1:2, var) stopifnot(all.equal(vR, Va, tolerance = 1/16))
loess
Plot and add a smooth curve computed by loess
to a scatter plot.
scatter.smooth(x, y = NULL, span = 2/3, degree = 1, family = c("symmetric", "gaussian"), xlab = NULL, ylab = NULL, ylim = range(y, pred$y, na.rm = TRUE), evaluation = 50, ..., lpars = list()) loess.smooth(x, y, span = 2/3, degree = 1, family = c("symmetric", "gaussian"), evaluation = 50, ...)
scatter.smooth(x, y = NULL, span = 2/3, degree = 1, family = c("symmetric", "gaussian"), xlab = NULL, ylab = NULL, ylim = range(y, pred$y, na.rm = TRUE), evaluation = 50, ..., lpars = list()) loess.smooth(x, y, span = 2/3, degree = 1, family = c("symmetric", "gaussian"), evaluation = 50, ...)
x , y
|
the |
span |
smoothness parameter for |
degree |
degree of local polynomial used. |
family |
if |
xlab |
label for x axis. |
ylab |
label for y axis. |
ylim |
the y limits of the plot. |
evaluation |
number of points at which to evaluate the smooth curve. |
... |
For |
lpars |
loess.smooth
is an auxiliary function which evaluates the
loess
smooth at evaluation
equally spaced points
covering the range of x
.
For scatter.smooth
, none.
For loess.smooth
, a list with two components, x
(the
grid of evaluation points) and y
(the smoothed values at the
grid points).
loess
; smoothScatter
for scatter plots
with smoothed density color representation.
require(graphics) with(cars, scatter.smooth(speed, dist)) ## or with dotted thick smoothed line results : with(cars, scatter.smooth(speed, dist, lpars = list(col = "red", lwd = 3, lty = 3)))
require(graphics) with(cars, scatter.smooth(speed, dist)) ## or with dotted thick smoothed line results : with(cars, scatter.smooth(speed, dist, lpars = list(col = "red", lwd = 3, lty = 3)))
screeplot.default
plots the variances against the number of the
principal component. This is also the plot
method for classes
"princomp"
and "prcomp"
.
screeplot(x, ...) ## Default S3 method: screeplot(x, npcs = min(10, length(x$sdev)), type = c("barplot", "lines"), main = deparse1(substitute(x)), ...)
screeplot(x, ...) ## Default S3 method: screeplot(x, npcs = min(10, length(x$sdev)), type = c("barplot", "lines"), main = deparse1(substitute(x)), ...)
x |
an object containing a |
npcs |
the number of components to be plotted. |
type |
the type of plot. Can be abbreviated. |
main , ...
|
graphics parameters. |
Mardia, K. V., J. T. Kent and J. M. Bibby (1979). Multivariate Analysis, London: Academic Press.
Venables, W. N. and B. D. Ripley (2002). Modern Applied Statistics with S, Springer-Verlag.
require(graphics) ## The variances of the variables in the ## USArrests data vary by orders of magnitude, so scaling is appropriate (pc.cr <- princomp(USArrests, cor = TRUE)) # inappropriate screeplot(pc.cr) fit <- princomp(covmat = Harman74.cor) screeplot(fit) screeplot(fit, npcs = 24, type = "lines")
require(graphics) ## The variances of the variables in the ## USArrests data vary by orders of magnitude, so scaling is appropriate (pc.cr <- princomp(USArrests, cor = TRUE)) # inappropriate screeplot(pc.cr) fit <- princomp(covmat = Harman74.cor) screeplot(fit) screeplot(fit, npcs = 24, type = "lines")
This function computes the standard deviation of the values in
x
.
If na.rm
is TRUE
then missing values are removed before
computation proceeds.
sd(x, na.rm = FALSE)
sd(x, na.rm = FALSE)
x |
a numeric vector or an R object but not a
|
na.rm |
logical. Should missing values be removed? |
Like var
this uses denominator .
The standard deviation of a length-one or zero-length vector is NA
.
var
for its square, and mad
, the most
robust alternative.
sd(1:2) ^ 2
sd(1:2) ^ 2
Returns the standard errors for one or more contrasts in an aov
object.
se.contrast(object, ...) ## S3 method for class 'aov' se.contrast(object, contrast.obj, coef = contr.helmert(ncol(contrast))[, 1], data = NULL, ...)
se.contrast(object, ...) ## S3 method for class 'aov' se.contrast(object, contrast.obj, coef = contr.helmert(ncol(contrast))[, 1], data = NULL, ...)
object |
A suitable fit, usually from |
contrast.obj |
The contrasts for which standard errors are requested. This can be specified via a list or via a matrix. A single contrast can be specified by a list of logical vectors giving the cells to be contrasted. Multiple contrasts should be specified by a matrix, each column of which is a numerical contrast vector (summing to zero). |
coef |
used when |
data |
The data frame used to evaluate |
... |
further arguments passed to or from other methods. |
Contrasts are usually used to test if certain means are
significantly different; it can be easier to use se.contrast
than compute them directly from the coefficients.
In multistratum models, the contrasts can appear in more than one
stratum, in which case the standard errors are computed in the lowest
stratum and adjusted for efficiencies and comparisons between
strata. (See the comments in the note in the help for
aov
about using orthogonal contrasts.) Such standard
errors are often conservative.
Suitable matrices for use with coef
can be found by
calling contrasts
and indexing the columns by a factor.
A vector giving the standard errors for each contrast.
## From Venables and Ripley (2002) p.165. N <- c(0,1,0,1,1,1,0,0,0,1,1,0,1,1,0,0,1,0,1,0,1,1,0,0) P <- c(1,1,0,0,0,1,0,1,1,1,0,0,0,1,0,1,1,0,0,1,0,1,1,0) K <- c(1,0,0,1,0,1,1,0,0,1,0,1,0,1,1,0,0,0,1,1,1,0,1,0) yield <- c(49.5,62.8,46.8,57.0,59.8,58.5,55.5,56.0,62.8,55.8,69.5, 55.0, 62.0,48.8,45.5,44.2,52.0,51.5,49.8,48.8,57.2,59.0,53.2,56.0) npk <- data.frame(block = gl(6,4), N = factor(N), P = factor(P), K = factor(K), yield = yield) ## Set suitable contrasts. options(contrasts = c("contr.helmert", "contr.poly")) npk.aov1 <- aov(yield ~ block + N + K, data = npk) se.contrast(npk.aov1, list(N == "0", N == "1"), data = npk) # or via a matrix cont <- matrix(c(-1,1), 2, 1, dimnames = list(NULL, "N")) se.contrast(npk.aov1, cont[N, , drop = FALSE]/12, data = npk) ## test a multi-stratum model npk.aov2 <- aov(yield ~ N + K + Error(block/(N + K)), data = npk) se.contrast(npk.aov2, list(N == "0", N == "1")) ## an example looking at an interaction contrast ## Dataset from R.E. Kirk (1995) ## 'Experimental Design: procedures for the behavioral sciences' score <- c(12, 8,10, 6, 8, 4,10,12, 8, 6,10,14, 9, 7, 9, 5,11,12, 7,13, 9, 9, 5,11, 8, 7, 3, 8,12,10,13,14,19, 9,16,14) A <- gl(2, 18, labels = c("a1", "a2")) B <- rep(gl(3, 6, labels = c("b1", "b2", "b3")), 2) fit <- aov(score ~ A*B) cont <- c(1, -1)[A] * c(1, -1, 0)[B] sum(cont) # 0 sum(cont*score) # value of the contrast se.contrast(fit, as.matrix(cont)) (t.stat <- sum(cont*score)/se.contrast(fit, as.matrix(cont))) summary(fit, split = list(B = 1:2), expand.split = TRUE) ## t.stat^2 is the F value on the A:B: C1 line (with Helmert contrasts) ## Now look at all three interaction contrasts cont <- c(1, -1)[A] * cbind(c(1, -1, 0), c(1, 0, -1), c(0, 1, -1))[B,] se.contrast(fit, cont) # same, due to balance. rm(A, B, score) ## multi-stratum example where efficiencies play a role ## An example from Yates (1932), ## a 2^3 design in 2 blocks replicated 4 times Block <- gl(8, 4) A <- factor(c(0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1, 0,1,0,1,0,1,0,1,0,1,0,1)) B <- factor(c(0,0,1,1,0,0,1,1,0,1,0,1,1,0,1,0,0,0,1,1, 0,0,1,1,0,0,1,1,0,0,1,1)) C <- factor(c(0,1,1,0,1,0,0,1,0,0,1,1,0,0,1,1,0,1,0,1, 1,0,1,0,0,0,1,1,1,1,0,0)) Yield <- c(101, 373, 398, 291, 312, 106, 265, 450, 106, 306, 324, 449, 272, 89, 407, 338, 87, 324, 279, 471, 323, 128, 423, 334, 131, 103, 445, 437, 324, 361, 302, 272) aovdat <- data.frame(Block, A, B, C, Yield) fit <- aov(Yield ~ A + B * C + Error(Block), data = aovdat) cont1 <- c(-1, 1)[A]/32 # Helmert contrasts cont2 <- c(-1, 1)[B] * c(-1, 1)[C]/32 cont <- cbind(A = cont1, BC = cont2) colSums(cont*Yield) # values of the contrasts se.contrast(fit, as.matrix(cont)) # comparison with lme library(nlme) fit2 <- lme(Yield ~ A + B*C, random = ~1 | Block, data = aovdat) summary(fit2)$tTable # same estimates, similar (but smaller) se's.
## From Venables and Ripley (2002) p.165. N <- c(0,1,0,1,1,1,0,0,0,1,1,0,1,1,0,0,1,0,1,0,1,1,0,0) P <- c(1,1,0,0,0,1,0,1,1,1,0,0,0,1,0,1,1,0,0,1,0,1,1,0) K <- c(1,0,0,1,0,1,1,0,0,1,0,1,0,1,1,0,0,0,1,1,1,0,1,0) yield <- c(49.5,62.8,46.8,57.0,59.8,58.5,55.5,56.0,62.8,55.8,69.5, 55.0, 62.0,48.8,45.5,44.2,52.0,51.5,49.8,48.8,57.2,59.0,53.2,56.0) npk <- data.frame(block = gl(6,4), N = factor(N), P = factor(P), K = factor(K), yield = yield) ## Set suitable contrasts. options(contrasts = c("contr.helmert", "contr.poly")) npk.aov1 <- aov(yield ~ block + N + K, data = npk) se.contrast(npk.aov1, list(N == "0", N == "1"), data = npk) # or via a matrix cont <- matrix(c(-1,1), 2, 1, dimnames = list(NULL, "N")) se.contrast(npk.aov1, cont[N, , drop = FALSE]/12, data = npk) ## test a multi-stratum model npk.aov2 <- aov(yield ~ N + K + Error(block/(N + K)), data = npk) se.contrast(npk.aov2, list(N == "0", N == "1")) ## an example looking at an interaction contrast ## Dataset from R.E. Kirk (1995) ## 'Experimental Design: procedures for the behavioral sciences' score <- c(12, 8,10, 6, 8, 4,10,12, 8, 6,10,14, 9, 7, 9, 5,11,12, 7,13, 9, 9, 5,11, 8, 7, 3, 8,12,10,13,14,19, 9,16,14) A <- gl(2, 18, labels = c("a1", "a2")) B <- rep(gl(3, 6, labels = c("b1", "b2", "b3")), 2) fit <- aov(score ~ A*B) cont <- c(1, -1)[A] * c(1, -1, 0)[B] sum(cont) # 0 sum(cont*score) # value of the contrast se.contrast(fit, as.matrix(cont)) (t.stat <- sum(cont*score)/se.contrast(fit, as.matrix(cont))) summary(fit, split = list(B = 1:2), expand.split = TRUE) ## t.stat^2 is the F value on the A:B: C1 line (with Helmert contrasts) ## Now look at all three interaction contrasts cont <- c(1, -1)[A] * cbind(c(1, -1, 0), c(1, 0, -1), c(0, 1, -1))[B,] se.contrast(fit, cont) # same, due to balance. rm(A, B, score) ## multi-stratum example where efficiencies play a role ## An example from Yates (1932), ## a 2^3 design in 2 blocks replicated 4 times Block <- gl(8, 4) A <- factor(c(0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1, 0,1,0,1,0,1,0,1,0,1,0,1)) B <- factor(c(0,0,1,1,0,0,1,1,0,1,0,1,1,0,1,0,0,0,1,1, 0,0,1,1,0,0,1,1,0,0,1,1)) C <- factor(c(0,1,1,0,1,0,0,1,0,0,1,1,0,0,1,1,0,1,0,1, 1,0,1,0,0,0,1,1,1,1,0,0)) Yield <- c(101, 373, 398, 291, 312, 106, 265, 450, 106, 306, 324, 449, 272, 89, 407, 338, 87, 324, 279, 471, 323, 128, 423, 334, 131, 103, 445, 437, 324, 361, 302, 272) aovdat <- data.frame(Block, A, B, C, Yield) fit <- aov(Yield ~ A + B * C + Error(Block), data = aovdat) cont1 <- c(-1, 1)[A]/32 # Helmert contrasts cont2 <- c(-1, 1)[B] * c(-1, 1)[C]/32 cont <- cbind(A = cont1, BC = cont2) colSums(cont*Yield) # values of the contrasts se.contrast(fit, as.matrix(cont)) # comparison with lme library(nlme) fit2 <- lme(Yield ~ A + B*C, random = ~1 | Block, data = aovdat) summary(fit2)$tTable # same estimates, similar (but smaller) se's.
Construct self-starting nonlinear models to be used in
nls
, etc. Via function initial
to compute
approximate parameter values from data, such models are
“self-starting”, i.e., do not need a start
argument in,
e.g., nls()
.
selfStart(model, initial, parameters, template)
selfStart(model, initial, parameters, template)
model |
a function object defining a nonlinear model or
a nonlinear |
initial |
a function object, taking arguments |
parameters |
a character vector specifying the terms on the right
hand side of |
template |
an optional prototype for the calling sequence of the
returned object, passed as the |
nls()
calls getInitial
and the
initial
function for these self-starting models.
This function is generic; methods functions can be written to handle specific classes of objects.
a function
object of class "selfStart"
, for the
formula
method obtained by applying deriv
to the right hand side of the model
formula. An
initial
attribute (defined by the initial
argument) is
added to the function to calculate starting estimates for the
parameters in the model automatically.
José Pinheiro and Douglas Bates
Each of the following are "selfStart"
models (with examples)
SSasymp
, SSasympOff
, SSasympOrig
,
SSbiexp
, SSfol
, SSfpl
,
SSgompertz
, SSlogis
, SSmicmen
,
SSweibull
.
Further, package nlme's nlsList
.
## self-starting logistic model ## The "initializer" (finds initial values for parameters from data): initLogis <- function(mCall, data, LHS, ...) { xy <- sortedXyData(mCall[["x"]], LHS, data) if(nrow(xy) < 4) stop("too few distinct input values to fit a logistic model") z <- xy[["y"]] ## transform to proportion, i.e. in (0,1) : rng <- range(z); dz <- diff(rng) z <- (z - rng[1L] + 0.05 * dz)/(1.1 * dz) xy[["z"]] <- log(z/(1 - z)) # logit transformation aux <- coef(lm(x ~ z, xy)) pars <- coef(nls(y ~ 1/(1 + exp((xmid - x)/scal)), data = xy, start = list(xmid = aux[[1L]], scal = aux[[2L]]), algorithm = "plinear", ...)) setNames(pars [c(".lin", "xmid", "scal")], mCall[c("Asym", "xmid", "scal")]) } mySSlogis <- selfStart(~ Asym/(1 + exp((xmid - x)/scal)), initial = initLogis, parameters = c("Asym", "xmid", "scal")) getInitial(weight ~ mySSlogis(Time, Asym, xmid, scal), data = subset(ChickWeight, Chick == 1)) # 'first.order.log.model' is a function object defining a first order # compartment model # 'first.order.log.initial' is a function object which calculates initial # values for the parameters in 'first.order.log.model' # # self-starting first order compartment model ## Not run: SSfol <- selfStart(first.order.log.model, first.order.log.initial) ## End(Not run) ## Explore the self-starting models already available in R's "stats": pos.st <- which("package:stats" == search()) mSS <- apropos("^SS..", where = TRUE, ignore.case = FALSE) (mSS <- unname(mSS[names(mSS) == pos.st])) fSS <- sapply(mSS, get, pos = pos.st, mode = "function") all(sapply(fSS, inherits, "selfStart")) # -> TRUE ## Show the argument list of each self-starting function: str(fSS, give.attr = FALSE)
## self-starting logistic model ## The "initializer" (finds initial values for parameters from data): initLogis <- function(mCall, data, LHS, ...) { xy <- sortedXyData(mCall[["x"]], LHS, data) if(nrow(xy) < 4) stop("too few distinct input values to fit a logistic model") z <- xy[["y"]] ## transform to proportion, i.e. in (0,1) : rng <- range(z); dz <- diff(rng) z <- (z - rng[1L] + 0.05 * dz)/(1.1 * dz) xy[["z"]] <- log(z/(1 - z)) # logit transformation aux <- coef(lm(x ~ z, xy)) pars <- coef(nls(y ~ 1/(1 + exp((xmid - x)/scal)), data = xy, start = list(xmid = aux[[1L]], scal = aux[[2L]]), algorithm = "plinear", ...)) setNames(pars [c(".lin", "xmid", "scal")], mCall[c("Asym", "xmid", "scal")]) } mySSlogis <- selfStart(~ Asym/(1 + exp((xmid - x)/scal)), initial = initLogis, parameters = c("Asym", "xmid", "scal")) getInitial(weight ~ mySSlogis(Time, Asym, xmid, scal), data = subset(ChickWeight, Chick == 1)) # 'first.order.log.model' is a function object defining a first order # compartment model # 'first.order.log.initial' is a function object which calculates initial # values for the parameters in 'first.order.log.model' # # self-starting first order compartment model ## Not run: SSfol <- selfStart(first.order.log.model, first.order.log.initial) ## End(Not run) ## Explore the self-starting models already available in R's "stats": pos.st <- which("package:stats" == search()) mSS <- apropos("^SS..", where = TRUE, ignore.case = FALSE) (mSS <- unname(mSS[names(mSS) == pos.st])) fSS <- sapply(mSS, get, pos = pos.st, mode = "function") all(sapply(fSS, inherits, "selfStart")) # -> TRUE ## Show the argument list of each self-starting function: str(fSS, give.attr = FALSE)
This is a convenience function that sets the names on an object and returns the object. It is most useful at the end of a function definition where one is creating the object to be returned and would prefer not to store it under a name just so the names can be assigned.
setNames(object = nm, nm)
setNames(object = nm, nm)
object |
an object for which a |
nm |
a character vector of names to assign to the object |
An object of the same sort as object
with the new names assigned.
Douglas M. Bates and Saikat DebRoy
unname
for removing names.
setNames( 1:3, c("foo", "bar", "baz") ) # this is just a short form of tmp <- 1:3 names(tmp) <- c("foo", "bar", "baz") tmp ## special case of character vector, using default setNames(nm = c("First", "2nd"))
setNames( 1:3, c("foo", "bar", "baz") ) # this is just a short form of tmp <- 1:3 names(tmp) <- c("foo", "bar", "baz") tmp ## special case of character vector, using default setNames(nm = c("First", "2nd"))
Performs the Shapiro-Wilk test of normality.
shapiro.test(x)
shapiro.test(x)
x |
a numeric vector of data values. Missing values are allowed, but the number of non-missing values must be between 3 and 5000. |
A list with class "htest"
containing the following components:
statistic |
the value of the Shapiro-Wilk statistic. |
p.value |
an approximate p-value for the test. This is
said in Royston (1995) to be adequate for |
method |
the character string |
data.name |
a character string giving the name(s) of the data. |
The algorithm used is a C translation of the Fortran code described in
Royston (1995).
The calculation of the p value is exact for , otherwise
approximations are used, separately for
and
.
Patrick Royston (1982).
An extension of Shapiro and Wilk's test for normality to large
samples.
Applied Statistics, 31, 115–124.
doi:10.2307/2347973.
Patrick Royston (1982).
Algorithm AS 181: The test for Normality.
Applied Statistics, 31, 176–180.
doi:10.2307/2347986.
Patrick Royston (1995).
Remark AS R94: A remark on Algorithm AS 181: The test for
normality.
Applied Statistics, 44, 547–551.
doi:10.2307/2986146.
qqnorm
for producing a normal quantile-quantile plot.
shapiro.test(rnorm(100, mean = 5, sd = 3)) shapiro.test(runif(100, min = 2, max = 4))
shapiro.test(rnorm(100, mean = 5, sd = 3)) shapiro.test(runif(100, min = 2, max = 4))
Extract the estimated standard deviation of the errors, the
“residual standard deviation” (misnamed also
“residual standard error”, e.g., in
summary.lm()
's output, from a fitted model).
Many classical statistical models have a scale parameter,
typically the standard deviation of a zero-mean normal (or Gaussian)
random variable which is denoted as .
sigma(.)
extracts the estimated parameter from a fitted
model, i.e., .
sigma(object, ...) ## Default S3 method: sigma(object, use.fallback = TRUE, ...)
sigma(object, ...) ## Default S3 method: sigma(object, use.fallback = TRUE, ...)
object |
an R object, typically resulting from a model fitting
function such as |
use.fallback |
logical, passed to |
... |
potentially further arguments passed to and from
methods. Passed to |
The stats package provides the S3 generic, a default method,
and a method for objects of class "glm"
.
The default method is correct typically for (asymptotically / approximately)
generalized gaussian (“least squares”) problems, since it is
defined as
sigma.default <- function (object, use.fallback = TRUE, ...) sqrt( deviance(object, ...) / (NN - PP) )
where NN <- nobs(object, use.fallback = use.fallback)
and PP <- sum(!is.na(coef(object)))
– where in older R
versions this was length(coef(object))
which is too large in
case of undetermined coefficients, e.g., for rank deficient model fits.
Typically a number, the estimated standard deviation of the errors (“residual standard deviation”) for Gaussian models, and—less interpretably—the square root of the residual deviance per degree of freedom in more general models.
Very strictly speaking, (“
hat”)
is actually
.
For generalized linear models (class "glm"
), the
sigma.glm
method returns the square root of the dispersion
parameter (See summary.glm
). For families with free
dispersion parameter, is estimated from the root mean
square of the Pearson residuals. For families with fixed dispersion,
sigma
is not estimated from the residuals but extracted directly
from the family of the fitted model. Consequently, for binomial or
Poisson GLMs, sigma
is exactly 1.
For multivariate linear models (class "mlm"
), a vector
of sigmas is returned, each corresponding to one column of .
The misnomer “Residual standard error” has been part of too many R (and S) outputs to be easily changed there.
deviance
, nobs
, vcov
,
summary.glm
.
## -- lm() ------------------------------ lm1 <- lm(Fertility ~ . , data = swiss) sigma(lm1) # ~= 7.165 = "Residual standard error" printed from summary(lm1) stopifnot(all.equal(sigma(lm1), summary(lm1)$sigma, tolerance=1e-15)) ## -- nls() ----------------------------- DNase1 <- subset(DNase, Run == 1) fm.DN1 <- nls(density ~ SSlogis(log(conc), Asym, xmid, scal), DNase1) sigma(fm.DN1) # ~= 0.01919 as from summary(..) stopifnot(all.equal(sigma(fm.DN1), summary(fm.DN1)$sigma, tolerance=1e-15)) ## -- glm() ----------------------------- ## -- a) Binomial -- Example from MASS ldose <- rep(0:5, 2) numdead <- c(1, 4, 9, 13, 18, 20, 0, 2, 6, 10, 12, 16) sex <- factor(rep(c("M", "F"), c(6, 6))) SF <- cbind(numdead, numalive = 20-numdead) sigma(budworm.lg <- glm(SF ~ sex*ldose, family = binomial)) ## -- b) Poisson -- from ?glm : ## Dobson (1990) Page 93: Randomized Controlled Trial : counts <- c(18,17,15,20,10,20,25,13,12) outcome <- gl(3,1,9) treatment <- gl(3,3) sigma(glm.D93 <- glm(counts ~ outcome + treatment, family = poisson())) ## equal to sqrt(summary(glm.D93)$dispersion) # == 1 ## and the *Quasi*poisson's dispersion sigma(glm.qD93 <- update(glm.D93, family = quasipoisson())) sigma (glm.qD93)^2 # 1.2933 equal to summary(glm.qD93)$dispersion # == 1.2933 ## -- Multivariate lm() "mlm" ----------- utils::example("SSD", echo=FALSE) sigma(mlmfit) # is the same as {but more efficient than} sqrt(diag(estVar(mlmfit)))
## -- lm() ------------------------------ lm1 <- lm(Fertility ~ . , data = swiss) sigma(lm1) # ~= 7.165 = "Residual standard error" printed from summary(lm1) stopifnot(all.equal(sigma(lm1), summary(lm1)$sigma, tolerance=1e-15)) ## -- nls() ----------------------------- DNase1 <- subset(DNase, Run == 1) fm.DN1 <- nls(density ~ SSlogis(log(conc), Asym, xmid, scal), DNase1) sigma(fm.DN1) # ~= 0.01919 as from summary(..) stopifnot(all.equal(sigma(fm.DN1), summary(fm.DN1)$sigma, tolerance=1e-15)) ## -- glm() ----------------------------- ## -- a) Binomial -- Example from MASS ldose <- rep(0:5, 2) numdead <- c(1, 4, 9, 13, 18, 20, 0, 2, 6, 10, 12, 16) sex <- factor(rep(c("M", "F"), c(6, 6))) SF <- cbind(numdead, numalive = 20-numdead) sigma(budworm.lg <- glm(SF ~ sex*ldose, family = binomial)) ## -- b) Poisson -- from ?glm : ## Dobson (1990) Page 93: Randomized Controlled Trial : counts <- c(18,17,15,20,10,20,25,13,12) outcome <- gl(3,1,9) treatment <- gl(3,3) sigma(glm.D93 <- glm(counts ~ outcome + treatment, family = poisson())) ## equal to sqrt(summary(glm.D93)$dispersion) # == 1 ## and the *Quasi*poisson's dispersion sigma(glm.qD93 <- update(glm.D93, family = quasipoisson())) sigma (glm.qD93)^2 # 1.2933 equal to summary(glm.qD93)$dispersion # == 1.2933 ## -- Multivariate lm() "mlm" ----------- utils::example("SSD", echo=FALSE) sigma(mlmfit) # is the same as {but more efficient than} sqrt(diag(estVar(mlmfit)))
Density, distribution function, quantile function and random
generation for the distribution of the Wilcoxon Signed Rank statistic
obtained from a sample with size n
.
dsignrank(x, n, log = FALSE) psignrank(q, n, lower.tail = TRUE, log.p = FALSE) qsignrank(p, n, lower.tail = TRUE, log.p = FALSE) rsignrank(nn, n)
dsignrank(x, n, log = FALSE) psignrank(q, n, lower.tail = TRUE, log.p = FALSE) qsignrank(p, n, lower.tail = TRUE, log.p = FALSE) rsignrank(nn, n)
x , q
|
vector of quantiles. |
p |
vector of probabilities. |
nn |
number of observations. If |
n |
number(s) of observations in the sample(s). A positive integer, or a vector of such integers. |
log , log.p
|
logical; if TRUE, probabilities p are given as log(p). |
lower.tail |
logical; if TRUE (default), probabilities are
|
This distribution is obtained as follows. Let x
be a sample of
size n
from a continuous distribution symmetric about the
origin. Then the Wilcoxon signed rank statistic is the sum of the
ranks of the absolute values x[i]
for which x[i]
is
positive. This statistic takes values between and
, and its mean and variance are
and
, respectively.
If either of the first two arguments is a vector, the recycling rule is used to do the calculations for all combinations of the two up to the length of the longer vector.
dsignrank
gives the density,
psignrank
gives the distribution function,
qsignrank
gives the quantile function, and
rsignrank
generates random deviates.
The length of the result is determined by nn
for
rsignrank
, and is the maximum of the lengths of the
numerical arguments for the other functions.
The numerical arguments other than nn
are recycled to the
length of the result. Only the first elements of the logical
arguments are used.
Kurt Hornik; efficiency improvement by Ivo Ugrina.
wilcox.test
to calculate the statistic from data, find p
values and so on.
Distributions for standard distributions, including
dwilcox
for the distribution of two-sample
Wilcoxon rank sum statistic.
require(graphics) par(mfrow = c(2,2)) for(n in c(4:5,10,40)) { x <- seq(0, n*(n+1)/2, length.out = 501) plot(x, dsignrank(x, n = n), type = "l", main = paste0("dsignrank(x, n = ", n, ")")) }
require(graphics) par(mfrow = c(2,2)) for(n in c(4:5,10,40)) { x <- seq(0, n*(n+1)/2, length.out = 501) plot(x, dsignrank(x, n = n), type = "l", main = paste0("dsignrank(x, n = ", n, ")")) }
Simulate one or more responses from the distribution corresponding to a fitted model object.
simulate(object, nsim = 1, seed = NULL, ...)
simulate(object, nsim = 1, seed = NULL, ...)
object |
an object representing a fitted model. |
nsim |
number of response vectors to simulate. Defaults to |
seed |
an object specifying if and how the random number
generator should be initialized (‘seeded’). |
... |
additional optional arguments. |
This is a generic function. Consult the individual modeling functions for details on how to use this function.
Package stats has a method for "lm"
objects which is used
for lm
and glm
fits. There is a method
for fits from glm.nb
in package MASS, and hence the
case of negative binomial families is not covered by the "lm"
method.
The methods for linear models fitted by lm
or glm(family
= "gaussian")
assume that any weights which have been supplied are
inversely proportional to the error variance. For other GLMs the
(optional) simulate
component of the family
object is used—there is no appropriate simulation method for
‘quasi’ models as they are specified only up to two moments.
For binomial and Poisson GLMs the dispersion is fixed at one. Integer
prior weights can be interpreted as meaning that
observation
is an average of
observations, which is
natural for binomials specified as proportions but less so for a
Poisson, for which prior weights are ignored with a warning.
For a gamma GLM the shape parameter is estimated by maximum likelihood
(using function gamma.shape
in package
MASS). The interpretation of weights is as multipliers to a
basic shape parameter, since dispersion is inversely proportional to
shape.
For an inverse gaussian GLM the model assumed is
(see
https://en.wikipedia.org/wiki/Inverse_Gaussian_distribution)
where
is estimated by the inverse of the dispersion
estimate for the fit. The variance is
and
hence inversely proportional to the prior weights. The simulation is
done by function
rinvGauss
from the
SuppDists package, which must be installed.
Typically, a list of length nsim
of simulated responses. Where
appropriate the result can be a data frame (which is a special type of
list).
For the "lm"
method, the result is a data frame with an
attribute "seed"
. If argument seed
is NULL
, the
attribute is the value of .Random.seed
before the
simulation was started; otherwise it is the value of the argument with
a "kind"
attribute with value as.list(RNGkind())
.
RNG
about random number generation in R,
fitted.values
and residuals
for related methods;
glm
, lm
for model fitting.
There are further examples in the ‘simulate.R’ tests file in the sources for package stats.
x <- 1:5 mod1 <- lm(c(1:3, 7, 6) ~ x) S1 <- simulate(mod1, nsim = 4) ## repeat the simulation: .Random.seed <- attr(S1, "seed") identical(S1, simulate(mod1, nsim = 4)) S2 <- simulate(mod1, nsim = 200, seed = 101) rowMeans(S2) # should be about the same as fitted(mod1) ## repeat identically: (sseed <- attr(S2, "seed")) # seed; RNGkind as attribute stopifnot(identical(S2, simulate(mod1, nsim = 200, seed = sseed))) ## To be sure about the proper RNGkind, e.g., after RNGversion("2.7.0") ## first set the RNG kind, then simulate do.call(RNGkind, attr(sseed, "kind")) identical(S2, simulate(mod1, nsim = 200, seed = sseed)) ## Binomial GLM examples yb1 <- matrix(c(4, 4, 5, 7, 8, 6, 6, 5, 3, 2), ncol = 2) modb1 <- glm(yb1 ~ x, family = binomial) S3 <- simulate(modb1, nsim = 4) # each column of S3 is a two-column matrix. x2 <- sort(runif(100)) yb2 <- rbinom(100, prob = plogis(2*(x2-1)), size = 1) yb2 <- factor(1 + yb2, labels = c("failure", "success")) modb2 <- glm(yb2 ~ x2, family = binomial) S4 <- simulate(modb2, nsim = 4) # each column of S4 is a factor
x <- 1:5 mod1 <- lm(c(1:3, 7, 6) ~ x) S1 <- simulate(mod1, nsim = 4) ## repeat the simulation: .Random.seed <- attr(S1, "seed") identical(S1, simulate(mod1, nsim = 4)) S2 <- simulate(mod1, nsim = 200, seed = 101) rowMeans(S2) # should be about the same as fitted(mod1) ## repeat identically: (sseed <- attr(S2, "seed")) # seed; RNGkind as attribute stopifnot(identical(S2, simulate(mod1, nsim = 200, seed = sseed))) ## To be sure about the proper RNGkind, e.g., after RNGversion("2.7.0") ## first set the RNG kind, then simulate do.call(RNGkind, attr(sseed, "kind")) identical(S2, simulate(mod1, nsim = 200, seed = sseed)) ## Binomial GLM examples yb1 <- matrix(c(4, 4, 5, 7, 8, 6, 6, 5, 3, 2), ncol = 2) modb1 <- glm(yb1 ~ x, family = binomial) S3 <- simulate(modb1, nsim = 4) # each column of S3 is a two-column matrix. x2 <- sort(runif(100)) yb2 <- rbinom(100, prob = plogis(2*(x2-1)), size = 1) yb2 <- factor(1 + yb2, labels = c("failure", "success")) modb2 <- glm(yb2 ~ x2, family = binomial) S4 <- simulate(modb2, nsim = 4) # each column of S4 is a factor
Distribution function, quantile function and random generation for the distribution of the Smirnov statistic.
psmirnov(q, sizes, z = NULL, alternative = c("two.sided", "less", "greater"), exact = TRUE, simulate = FALSE, B = 2000, lower.tail = TRUE, log.p = FALSE) qsmirnov(p, sizes, z = NULL, alternative = c("two.sided", "less", "greater"), exact = TRUE, simulate = FALSE, B = 2000) rsmirnov(n, sizes, z = NULL, alternative = c("two.sided", "less", "greater"))
psmirnov(q, sizes, z = NULL, alternative = c("two.sided", "less", "greater"), exact = TRUE, simulate = FALSE, B = 2000, lower.tail = TRUE, log.p = FALSE) qsmirnov(p, sizes, z = NULL, alternative = c("two.sided", "less", "greater"), exact = TRUE, simulate = FALSE, B = 2000) rsmirnov(n, sizes, z = NULL, alternative = c("two.sided", "less", "greater"))
q |
a numeric vector of quantiles. |
p |
a numeric vector of probabilities. |
sizes |
an integer vector of length two giving the sample sizes. |
z |
a numeric vector of the pooled data values in both samples when the exact conditional distribution of the Smirnov statistic given the data shall be computed. |
alternative |
one of |
exact |
|
simulate |
a logical indicating whether to compute the distribution function by Monte Carlo simulation. |
B |
an integer specifying the number of replicates used in the Monte Carlo test. |
lower.tail |
a logical, if |
log.p |
a logical, if |
n |
an integer giving number of observations. |
For samples and
with respective sizes
and
and empirical cumulative distribution functions
and
, the Smirnov statistic is
in the two-sided case,
in the one-sided "greater"
case, and
in the one-sided "less"
case.
These statistics are used in the Smirnov test of the null that
and
were drawn from the same distribution, see
ks.test
.
If the underlying common distribution function is continuous,
the distribution of the test statistics does not depend on
,
and has a simple asymptotic approximation. For arbitrary
, one
can compute the conditional distribution given the pooled data values
of
and
, either exactly (feasible provided that
the product
of the sample sizes is “small enough”) or
approximately Monte Carlo simulation. If the pooled data values
are not specified, a pooled sample without ties is assumed.
psmirnov
gives the distribution function,
qsmirnov
gives the quantile function, and
rsmirnov
generates random deviates.
ks.test
for references on the algorithms used for
computing exact distributions.
Tukey's smoothers, 3RS3R, 3RSS, 3R, etc.
smooth(x, kind = c("3RS3R", "3RSS", "3RSR", "3R", "3", "S"), twiceit = FALSE, endrule = c("Tukey", "copy"), do.ends = FALSE)
smooth(x, kind = c("3RS3R", "3RSS", "3RSR", "3R", "3", "S"), twiceit = FALSE, endrule = c("Tukey", "copy"), do.ends = FALSE)
x |
a vector or time series |
kind |
a character string indicating the kind of smoother required;
defaults to |
twiceit |
logical, indicating if the result should be ‘twiced’.
Twicing a smoother |
endrule |
a character string indicating the rule for smoothing at the
boundary. Either |
do.ends |
logical, indicating if the 3-splitting of ties should
also happen at the boundaries (ends). This is only used for
|
3
is Tukey's short notation for running median
s
of length 3,
3R
stands for Repeated 3
until
convergence, and
S
for Splitting of horizontal stretches of length 2 or 3.
Hence, 3RS3R
is a concatenation of 3R
, S
and 3R
, 3RSS
similarly,
whereas 3RSR
means first 3R
and then (S and 3)
Repeated until convergence – which
can be bad.
An object of class "tukeysmooth"
(which has print
and
summary
methods) and is a vector or time series containing the
smoothed values with additional attributes.
Note that there are other smoothing methods which provide rather better results. These were designed for hand calculations and may be used mainly for didactical purposes.
Since R version 1.2, smooth
does really implement
Tukey's end-point rule correctly (see argument endrule
).
kind = "3RSR"
had been the default till R-1.1,
but it can have very bad properties, see the examples.
Note that repeated application of smooth(*)
does
smooth more, for the "3RS*"
kinds.
Tukey, J. W. (1977). Exploratory Data Analysis, Reading Massachusetts: Addison-Wesley.
runmed
for running medians;
lowess
and loess
;
supsmu
and
smooth.spline
.
require(graphics) ## see also demo(smooth) ! x1 <- c(4, 1, 3, 6, 6, 4, 1, 6, 2, 4, 2) # very artificial (x3R <- smooth(x1, "3R")) # 2 iterations of "3" smooth(x3R, kind = "S") sm.3RS <- function(x, ...) smooth(smooth(x, "3R", ...), "S", ...) y <- c(1, 1, 19:1) plot(y, main = "misbehaviour of \"3RSR\"", col.main = 3) lines(sm.3RS(y)) lines(smooth(y)) lines(smooth(y, "3RSR"), col = 3, lwd = 2) # the horror x <- c(8:10, 10, 0, 0, 9, 9) plot(x, main = "breakdown of 3R and S and hence 3RSS") matlines(cbind(smooth(x, "3R"), smooth(x, "S"), smooth(x, "3RSS"), smooth(x))) presidents[is.na(presidents)] <- 0 # silly summary(sm3 <- smooth(presidents, "3R")) summary(sm2 <- smooth(presidents,"3RSS")) summary(sm <- smooth(presidents)) all.equal(c(sm2), c(smooth(smooth(sm3, "S"), "S"))) # 3RSS === 3R S S all.equal(c(sm), c(smooth(smooth(sm3, "S"), "3R"))) # 3RS3R === 3R S 3R plot(presidents, main = "smooth(presidents0, *) : 3R and default 3RS3R") lines(sm3, col = 3, lwd = 1.5) lines(sm, col = 2, lwd = 1.25)
require(graphics) ## see also demo(smooth) ! x1 <- c(4, 1, 3, 6, 6, 4, 1, 6, 2, 4, 2) # very artificial (x3R <- smooth(x1, "3R")) # 2 iterations of "3" smooth(x3R, kind = "S") sm.3RS <- function(x, ...) smooth(smooth(x, "3R", ...), "S", ...) y <- c(1, 1, 19:1) plot(y, main = "misbehaviour of \"3RSR\"", col.main = 3) lines(sm.3RS(y)) lines(smooth(y)) lines(smooth(y, "3RSR"), col = 3, lwd = 2) # the horror x <- c(8:10, 10, 0, 0, 9, 9) plot(x, main = "breakdown of 3R and S and hence 3RSS") matlines(cbind(smooth(x, "3R"), smooth(x, "S"), smooth(x, "3RSS"), smooth(x))) presidents[is.na(presidents)] <- 0 # silly summary(sm3 <- smooth(presidents, "3R")) summary(sm2 <- smooth(presidents,"3RSS")) summary(sm <- smooth(presidents)) all.equal(c(sm2), c(smooth(smooth(sm3, "S"), "S"))) # 3RSS === 3R S S all.equal(c(sm), c(smooth(smooth(sm3, "S"), "3R"))) # 3RS3R === 3R S 3R plot(presidents, main = "smooth(presidents0, *) : 3R and default 3RS3R") lines(sm3, col = 3, lwd = 1.5) lines(sm, col = 2, lwd = 1.25)
Fits a cubic smoothing spline to the supplied data.
smooth.spline(x, y = NULL, w = NULL, df, spar = NULL, lambda = NULL, cv = FALSE, all.knots = FALSE, nknots = .nknots.smspl, keep.data = TRUE, df.offset = 0, penalty = 1, control.spar = list(), tol = 1e-6 * IQR(x), keep.stuff = FALSE) .nknots.smspl(n)
smooth.spline(x, y = NULL, w = NULL, df, spar = NULL, lambda = NULL, cv = FALSE, all.knots = FALSE, nknots = .nknots.smspl, keep.data = TRUE, df.offset = 0, penalty = 1, control.spar = list(), tol = 1e-6 * IQR(x), keep.stuff = FALSE) .nknots.smspl(n)
x |
a vector giving the values of the predictor variable, or a list or a two-column matrix specifying x and y. |
y |
responses. If |
w |
optional vector of weights of the same length as |
df |
the desired equivalent number of degrees of freedom (trace of
the smoother matrix). Must be in |
spar |
smoothing parameter, typically (but not necessarily) in
|
lambda |
if desired, the internal (design-dependent) smoothing
parameter |
cv |
ordinary leave-one-out ( |
all.knots |
if Alternatively, a strictly increasing |
nknots |
integer or |
keep.data |
logical specifying if the input data should be kept
in the result. If |
df.offset |
allows the degrees of freedom to be increased by
|
penalty |
the coefficient of the penalty for degrees of freedom in the GCV criterion. |
control.spar |
optional list with named components controlling the
root finding when the smoothing parameter Note that this is partly experimental and may change with general spar computation improvements!
Note that |
tol |
a tolerance for sameness or uniqueness of the |
keep.stuff |
an experimental |
n |
for |
Neither x
nor y
are allowed to containing missing or
infinite values.
The x
vector should contain at least four distinct values.
‘Distinct’ here is controlled by tol
: values which are
regarded as the same are replaced by the first of their values and the
corresponding y
and w
are pooled accordingly.
Unless lambda
has been specified instead of spar
,
the computational used (as a function of
) is
where
,
is the matrix given by
,
is given by
,
is the diagonal matrix of weights (scaled such that
its trace is
, the original number of observations)
and
is the
-th B-spline.
Note that with these definitions, , and the B-spline
basis representation
(i.e.,
is
the vector of spline coefficients), the penalized log likelihood is
, and hence
is the solution of the (ridge regression)
.
If spar
and lambda
are missing or NULL
, the value
of df
is used to determine the degree of smoothing. If
df
is missing as well, leave-one-out cross-validation (ordinary
or ‘generalized’ as determined by cv
) is used to
determine .
Note that from the above relation,
spar
is .
Note however that currently the results may become very unreliable
for spar
values smaller than about -1 or -2. The same may
happen for values larger than 2 or so. Don't think of setting
spar
or the controls low
and high
outside such a
safe range, unless you know what you are doing!
Similarly, specifying lambda
instead of spar
is
delicate, notably as the range of “safe” values for
lambda
is not scale-invariant and hence entirely data dependent.
The ‘generalized’ cross-validation method GCV will work correctly when
there are duplicated points in x
. However, it is ambiguous what
leave-one-out cross-validation means with duplicated points, and the
internal code uses an approximation that involves leaving out groups
of duplicated points. cv = TRUE
is best avoided in that case.
An object of class "smooth.spline"
with components
x |
the distinct |
y |
the fitted values corresponding to |
w |
the weights used at the unique values of |
yin |
the y values used at the unique |
tol |
the |
data |
only if |
n |
an integer; the (original) sample size. |
lev |
(when |
cv |
the |
cv.crit |
cross-validation score, ‘generalized’ or true, depending
on |
pen.crit |
the penalized criterion, a non-negative number; simply
the (weighted) residual sum of squares (RSS), |
crit |
the criterion value minimized in the underlying
|
df |
equivalent degrees of freedom used. Note that (currently)
this value may become quite imprecise when the true |
spar |
the value of |
ratio |
(when |
lambda |
the value of |
iparms |
named integer(3) vector where |
auxMat |
experimental; when |
fit |
list for use by
|
call |
the matched call. |
method(class = "smooth.spline")
shows a
hatvalues()
method based on the lev
vector above.
The number of unique x
values, , are
determined by the
tol
argument, equivalently to
nx <- length(x) - sum(duplicated( round((x - mean(x)) / tol) ))
The default all.knots = FALSE
and nknots = .nknots.smspl
,
entails using only
knots instead of
for
. This cuts
speed and memory requirements, but not drastically anymore since R
version 1.5.1 where it is only
where
is the number of knots.
In this case where not all unique x
values are
used as knots, the result is a regression spline rather than a
smoothing spline in the strict
sense, but very close unless a small smoothing parameter (or large
df
) is used.
R implementation by B. D. Ripley and Martin Maechler
(spar/lambda
, etc).
This function is based on code in the GAMFIT
Fortran program by
T. Hastie and R. Tibshirani (originally taken from
http://lib.stat.cmu.edu/general/gamfit)
which makes use of spline code by Finbarr O'Sullivan. Its design
parallels the smooth.spline
function of Chambers & Hastie (1992).
Chambers, J. M. and Hastie, T. J. (1992) Statistical Models in S, Wadsworth & Brooks/Cole.
Green, P. J. and Silverman, B. W. (1994) Nonparametric Regression and Generalized Linear Models: A Roughness Penalty Approach. Chapman and Hall.
Hastie, T. J. and Tibshirani, R. J. (1990) Generalized Additive Models. Chapman and Hall.
predict.smooth.spline
for evaluating the spline
and its derivatives.
require(graphics) plot(dist ~ speed, data = cars, main = "data(cars) & smoothing splines") cars.spl <- with(cars, smooth.spline(speed, dist)) cars.spl ## This example has duplicate points, so avoid cv = TRUE lines(cars.spl, col = "blue") ss10 <- smooth.spline(cars[,"speed"], cars[,"dist"], df = 10) lines(ss10, lty = 2, col = "red") legend(5,120,c(paste("default [C.V.] => df =",round(cars.spl$df,1)), "s( * , df = 10)"), col = c("blue","red"), lty = 1:2, bg = 'bisque') ## Residual (Tukey Anscombe) plot: plot(residuals(cars.spl) ~ fitted(cars.spl)) abline(h = 0, col = "gray") ## consistency check: stopifnot(all.equal(cars$dist, fitted(cars.spl) + residuals(cars.spl))) ## The chosen inner knots in original x-scale : with(cars.spl$fit, min + range * knot[-c(1:3, nk+1 +1:3)]) # == unique(cars$speed) ## Visualize the behavior of .nknots.smspl() nKnots <- Vectorize(.nknots.smspl) ; c.. <- adjustcolor("gray20",.5) curve(nKnots, 1, 250, n=250) abline(0,1, lty=2, col=c..); text(90,90,"y = x", col=c.., adj=-.25) abline(h=100,lty=2); abline(v=200, lty=2) n <- c(1:799, seq(800, 3490, by=10), seq(3500, 10000, by = 50)) plot(n, nKnots(n), type="l", main = "Vectorize(.nknots.smspl) (n)") abline(0,1, lty=2, col=c..); text(180,180,"y = x", col=c..) n0 <- c(50, 200, 800, 3200); c0 <- adjustcolor("blue3", .5) lines(n0, nKnots(n0), type="h", col=c0) axis(1, at=n0, line=-2, col.ticks=c0, col=NA, col.axis=c0) axis(4, at=.nknots.smspl(10000), line=-.5, col=c..,col.axis=c.., las=1) ##-- artificial example y18 <- c(1:3, 5, 4, 7:3, 2*(2:5), rep(10, 4)) xx <- seq(1, length(y18), length.out = 201) (s2 <- smooth.spline(y18)) # GCV (s02 <- smooth.spline(y18, spar = 0.2)) (s02. <- smooth.spline(y18, spar = 0.2, cv = NA)) plot(y18, main = deparse(s2$call), col.main = 2) lines(s2, col = "gray"); lines(predict(s2, xx), col = 2) lines(predict(s02, xx), col = 3); mtext(deparse(s02$call), col = 3) ## Specifying 'lambda' instead of usual spar : (s2. <- smooth.spline(y18, lambda = s2$lambda, tol = s2$tol)) ## The following shows the problematic behavior of 'spar' searching: (s2 <- smooth.spline(y18, control = list(trace = TRUE, tol = 1e-6, low = -1.5))) (s2m <- smooth.spline(y18, cv = TRUE, control = list(trace = TRUE, tol = 1e-6, low = -1.5))) ## both above do quite similarly (Df = 8.5 +- 0.2)
require(graphics) plot(dist ~ speed, data = cars, main = "data(cars) & smoothing splines") cars.spl <- with(cars, smooth.spline(speed, dist)) cars.spl ## This example has duplicate points, so avoid cv = TRUE lines(cars.spl, col = "blue") ss10 <- smooth.spline(cars[,"speed"], cars[,"dist"], df = 10) lines(ss10, lty = 2, col = "red") legend(5,120,c(paste("default [C.V.] => df =",round(cars.spl$df,1)), "s( * , df = 10)"), col = c("blue","red"), lty = 1:2, bg = 'bisque') ## Residual (Tukey Anscombe) plot: plot(residuals(cars.spl) ~ fitted(cars.spl)) abline(h = 0, col = "gray") ## consistency check: stopifnot(all.equal(cars$dist, fitted(cars.spl) + residuals(cars.spl))) ## The chosen inner knots in original x-scale : with(cars.spl$fit, min + range * knot[-c(1:3, nk+1 +1:3)]) # == unique(cars$speed) ## Visualize the behavior of .nknots.smspl() nKnots <- Vectorize(.nknots.smspl) ; c.. <- adjustcolor("gray20",.5) curve(nKnots, 1, 250, n=250) abline(0,1, lty=2, col=c..); text(90,90,"y = x", col=c.., adj=-.25) abline(h=100,lty=2); abline(v=200, lty=2) n <- c(1:799, seq(800, 3490, by=10), seq(3500, 10000, by = 50)) plot(n, nKnots(n), type="l", main = "Vectorize(.nknots.smspl) (n)") abline(0,1, lty=2, col=c..); text(180,180,"y = x", col=c..) n0 <- c(50, 200, 800, 3200); c0 <- adjustcolor("blue3", .5) lines(n0, nKnots(n0), type="h", col=c0) axis(1, at=n0, line=-2, col.ticks=c0, col=NA, col.axis=c0) axis(4, at=.nknots.smspl(10000), line=-.5, col=c..,col.axis=c.., las=1) ##-- artificial example y18 <- c(1:3, 5, 4, 7:3, 2*(2:5), rep(10, 4)) xx <- seq(1, length(y18), length.out = 201) (s2 <- smooth.spline(y18)) # GCV (s02 <- smooth.spline(y18, spar = 0.2)) (s02. <- smooth.spline(y18, spar = 0.2, cv = NA)) plot(y18, main = deparse(s2$call), col.main = 2) lines(s2, col = "gray"); lines(predict(s2, xx), col = 2) lines(predict(s02, xx), col = 3); mtext(deparse(s02$call), col = 3) ## Specifying 'lambda' instead of usual spar : (s2. <- smooth.spline(y18, lambda = s2$lambda, tol = s2$tol)) ## The following shows the problematic behavior of 'spar' searching: (s2 <- smooth.spline(y18, control = list(trace = TRUE, tol = 1e-6, low = -1.5))) (s2m <- smooth.spline(y18, cv = TRUE, control = list(trace = TRUE, tol = 1e-6, low = -1.5))) ## both above do quite similarly (Df = 8.5 +- 0.2)
Smooth end points of a vector y
using subsequently smaller
medians and Tukey's end point rule at the very end. (of odd span),
smoothEnds(y, k = 3)
smoothEnds(y, k = 3)
y |
dependent variable to be smoothed (vector). |
k |
width of largest median window; must be odd. |
smoothEnds
is used to only do the ‘end point smoothing’,
i.e., change at most the observations closer to the beginning/end
than half the window k
. The first and last value are computed using
Tukey's end point rule, i.e.,
sm[1] = median(y[1], sm[2], 3*sm[2] - 2*sm[3], na.rm=TRUE)
.
In R versions 3.6.0 and earlier, missing values (NA
)
in y
typically lead to an error, whereas now the equivalent of
median(*, na.rm=TRUE)
is used.
vector of smoothed values, the same length as y
.
Martin Maechler
John W. Tukey (1977) Exploratory Data Analysis, Addison.
Velleman, P.F., and Hoaglin, D.C. (1981) ABC of EDA (Applications, Basics, and Computing of Exploratory Data Analysis); Duxbury.
runmed(*, endrule = "median")
which calls
smoothEnds()
.
require(graphics) y <- ys <- (-20:20)^2 y [c(1,10,21,41)] <- c(100, 30, 400, 470) s7k <- runmed(y, 7, endrule = "keep") s7. <- runmed(y, 7, endrule = "const") s7m <- runmed(y, 7) col3 <- c("midnightblue","blue","steelblue") plot(y, main = "Running Medians -- runmed(*, k=7, endrule = X)") lines(ys, col = "light gray") matlines(cbind(s7k, s7.,s7m), lwd = 1.5, lty = 1, col = col3) eRules <- c("keep","constant","median") legend("topleft", paste("endrule", eRules, sep = " = "), col = col3, lwd = 1.5, lty = 1, bty = "n") stopifnot(identical(s7m, smoothEnds(s7k, 7))) ## With missing values (for R >= 3.6.1): yN <- y; yN[c(2,40)] <- NA rN <- sapply(eRules, function(R) runmed(yN, 7, endrule=R)) matlines(rN, type = "b", pch = 4, lwd = 3, lty=2, col = adjustcolor(c("red", "orange4", "orange1"), 0.5)) yN[c(1, 20:21)] <- NA # additionally rN. <- sapply(eRules, function(R) runmed(yN, 7, endrule=R)) head(rN., 4); tail(rN.) # more NA's too, still not *so* many: stopifnot(exprs = { !anyNA(rN[,2:3]) identical(which(is.na(rN[,"keep"])), c(2L, 40L)) identical(which(is.na(rN.), arr.ind=TRUE, useNames=FALSE), cbind(c(1:2,40L), 1L)) identical(rN.[38:41, "median"], c(289,289, 397, 470)) })
require(graphics) y <- ys <- (-20:20)^2 y [c(1,10,21,41)] <- c(100, 30, 400, 470) s7k <- runmed(y, 7, endrule = "keep") s7. <- runmed(y, 7, endrule = "const") s7m <- runmed(y, 7) col3 <- c("midnightblue","blue","steelblue") plot(y, main = "Running Medians -- runmed(*, k=7, endrule = X)") lines(ys, col = "light gray") matlines(cbind(s7k, s7.,s7m), lwd = 1.5, lty = 1, col = col3) eRules <- c("keep","constant","median") legend("topleft", paste("endrule", eRules, sep = " = "), col = col3, lwd = 1.5, lty = 1, bty = "n") stopifnot(identical(s7m, smoothEnds(s7k, 7))) ## With missing values (for R >= 3.6.1): yN <- y; yN[c(2,40)] <- NA rN <- sapply(eRules, function(R) runmed(yN, 7, endrule=R)) matlines(rN, type = "b", pch = 4, lwd = 3, lty=2, col = adjustcolor(c("red", "orange4", "orange1"), 0.5)) yN[c(1, 20:21)] <- NA # additionally rN. <- sapply(eRules, function(R) runmed(yN, 7, endrule=R)) head(rN., 4); tail(rN.) # more NA's too, still not *so* many: stopifnot(exprs = { !anyNA(rN[,2:3]) identical(which(is.na(rN[,"keep"])), c(2L, 40L)) identical(which(is.na(rN.), arr.ind=TRUE, useNames=FALSE), cbind(c(1:2,40L), 1L)) identical(rN.[38:41, "median"], c(289,289, 397, 470)) })
sortedXyData
ObjectThis is a constructor function for the class of sortedXyData
objects. These objects are mostly used in the initial
function for a self-starting nonlinear regression model, which will be
of the selfStart
class.
sortedXyData(x, y, data)
sortedXyData(x, y, data)
x |
a numeric vector or an expression that will evaluate in
|
y |
a numeric vector or an expression that will evaluate in
|
data |
an optional data frame in which to evaluate expressions
for |
A sortedXyData
object. This is a data frame with exactly
two numeric columns, named x
and y
. The rows are
sorted so the x
column is in increasing order. Duplicate
x
values are eliminated by averaging the corresponding y
values.
José Pinheiro and Douglas Bates
selfStart
, NLSstClosestX
,
NLSstLfAsymptote
, NLSstRtAsymptote
DNase.2 <- DNase[ DNase$Run == "2", ] sortedXyData( expression(log(conc)), expression(density), DNase.2 )
DNase.2 <- DNase[ DNase$Run == "2", ] sortedXyData( expression(log(conc)), expression(density), DNase.2 )
Fits an AR model to x
(or uses the existing fit) and computes
(and by default plots) the spectral density of the fitted model.
spec.ar(x, n.freq, order = NULL, plot = TRUE, na.action = na.fail, method = "yule-walker", ...)
spec.ar(x, n.freq, order = NULL, plot = TRUE, na.action = na.fail, method = "yule-walker", ...)
x |
A univariate (not yet:or multivariate) time series or the
result of a fit by |
n.freq |
The number of points at which to plot. |
order |
The order of the AR model to be fitted. If omitted, the order is chosen by AIC. |
plot |
Plot the periodogram? |
na.action |
|
method |
|
... |
Graphical arguments passed to |
An object of class "spec"
.
The result is returned invisibly if plot
is true.
Some authors, for example Thomson (1990), warn strongly that AR spectra can be misleading.
The multivariate case is not yet implemented.
Thompson, D.J. (1990). Time series analysis of Holocene climate data. Philosophical Transactions of the Royal Society of London Series A, 330, 601–616. doi:10.1098/rsta.1990.0041.
Venables, W.N. and Ripley, B.D. (2002) Modern Applied Statistics with S. Fourth edition. Springer. (Especially page 402.)
require(graphics) spec.ar(lh) spec.ar(ldeaths) spec.ar(ldeaths, method = "burg") spec.ar(log(lynx)) spec.ar(log(lynx), method = "burg", add = TRUE, col = "purple") spec.ar(log(lynx), method = "mle", add = TRUE, col = "forest green") spec.ar(log(lynx), method = "ols", add = TRUE, col = "blue")
require(graphics) spec.ar(lh) spec.ar(ldeaths) spec.ar(ldeaths, method = "burg") spec.ar(log(lynx)) spec.ar(log(lynx), method = "burg", add = TRUE, col = "purple") spec.ar(log(lynx), method = "mle", add = TRUE, col = "forest green") spec.ar(log(lynx), method = "ols", add = TRUE, col = "blue")
spec.pgram
calculates the periodogram using a fast Fourier
transform, and optionally smooths the result with a series of
modified Daniell smoothers (moving averages giving half weight to
the end values).
spec.pgram(x, spans = NULL, kernel, taper = 0.1, pad = 0, fast = TRUE, demean = FALSE, detrend = TRUE, plot = TRUE, na.action = na.fail, ...)
spec.pgram(x, spans = NULL, kernel, taper = 0.1, pad = 0, fast = TRUE, demean = FALSE, detrend = TRUE, plot = TRUE, na.action = na.fail, ...)
x |
univariate or multivariate time series. |
spans |
vector of odd integers giving the widths of modified Daniell smoothers to be used to smooth the periodogram. |
kernel |
alternatively, a kernel smoother of class
|
taper |
specifies the proportion of data to taper. A split cosine bell taper is applied to this proportion of the data at the beginning and end of the series. |
pad |
proportion of data to pad. Zeros are added to the end of
the series to increase its length by the proportion |
fast |
logical; if |
demean |
logical. If |
detrend |
logical. If |
plot |
plot the periodogram? |
na.action |
|
... |
graphical arguments passed to |
The raw periodogram is not a consistent estimator of the spectral density, but adjacent values are asymptotically independent. Hence a consistent estimator can be derived by smoothing the raw periodogram, assuming that the spectral density is smooth.
The series will be automatically padded with zeros until the series
length is a highly composite number in order to help the Fast Fourier
Transform. This is controlled by the fast
and not the pad
argument.
The periodogram at zero is in theory zero as the mean of the series is removed (but this may be affected by tapering): it is replaced by an interpolation of adjacent values during smoothing, and no value is returned for that frequency.
A list object of class "spec"
(see spectrum
)
with the following additional components:
kernel |
The |
df |
The distribution of the spectral density estimate can be
approximated by a (scaled) chi square distribution with |
bandwidth |
The equivalent bandwidth of the kernel smoother as defined by Bloomfield (1976, page 201). |
taper |
The value of the |
pad |
The value of the |
detrend |
The value of the |
demean |
The value of the |
The result is returned invisibly if plot
is true.
Originally Martyn Plummer; kernel smoothing by Adrian Trapletti, synthesis by B.D. Ripley
Bloomfield, P. (1976) Fourier Analysis of Time Series: An Introduction. Wiley.
Brockwell, P.J. and Davis, R.A. (1991) Time Series: Theory and Methods. Second edition. Springer.
Venables, W.N. and Ripley, B.D. (2002) Modern Applied Statistics with S. Fourth edition. Springer. (Especially pp. 392–7.)
spectrum
, spec.taper
,
plot.spec
, fft
require(graphics) ## Examples from Venables & Ripley spectrum(ldeaths) spectrum(ldeaths, spans = c(3,5)) spectrum(ldeaths, spans = c(5,7)) spectrum(mdeaths, spans = c(3,3)) spectrum(fdeaths, spans = c(3,3)) ## bivariate example mfdeaths.spc <- spec.pgram(ts.union(mdeaths, fdeaths), spans = c(3,3)) # plots marginal spectra: now plot coherency and phase plot(mfdeaths.spc, plot.type = "coherency") plot(mfdeaths.spc, plot.type = "phase") ## now impose a lack of alignment mfdeaths.spc <- spec.pgram(ts.intersect(mdeaths, lag(fdeaths, 4)), spans = c(3,3), plot = FALSE) plot(mfdeaths.spc, plot.type = "coherency") plot(mfdeaths.spc, plot.type = "phase") stocks.spc <- spectrum(EuStockMarkets, kernel("daniell", c(30,50)), plot = FALSE) plot(stocks.spc, plot.type = "marginal") # the default type plot(stocks.spc, plot.type = "coherency") plot(stocks.spc, plot.type = "phase") sales.spc <- spectrum(ts.union(BJsales, BJsales.lead), kernel("modified.daniell", c(5,7))) plot(sales.spc, plot.type = "coherency") plot(sales.spc, plot.type = "phase")
require(graphics) ## Examples from Venables & Ripley spectrum(ldeaths) spectrum(ldeaths, spans = c(3,5)) spectrum(ldeaths, spans = c(5,7)) spectrum(mdeaths, spans = c(3,3)) spectrum(fdeaths, spans = c(3,3)) ## bivariate example mfdeaths.spc <- spec.pgram(ts.union(mdeaths, fdeaths), spans = c(3,3)) # plots marginal spectra: now plot coherency and phase plot(mfdeaths.spc, plot.type = "coherency") plot(mfdeaths.spc, plot.type = "phase") ## now impose a lack of alignment mfdeaths.spc <- spec.pgram(ts.intersect(mdeaths, lag(fdeaths, 4)), spans = c(3,3), plot = FALSE) plot(mfdeaths.spc, plot.type = "coherency") plot(mfdeaths.spc, plot.type = "phase") stocks.spc <- spectrum(EuStockMarkets, kernel("daniell", c(30,50)), plot = FALSE) plot(stocks.spc, plot.type = "marginal") # the default type plot(stocks.spc, plot.type = "coherency") plot(stocks.spc, plot.type = "phase") sales.spc <- spectrum(ts.union(BJsales, BJsales.lead), kernel("modified.daniell", c(5,7))) plot(sales.spc, plot.type = "coherency") plot(sales.spc, plot.type = "phase")
Apply a cosine-bell taper to a time series.
spec.taper(x, p = 0.1)
spec.taper(x, p = 0.1)
x |
A univariate or multivariate time series |
p |
The proportion to be tapered at each end of the series, either a scalar (giving the proportion for all series) or a vector of the length of the number of series (giving the proportion for each series). |
The cosine-bell taper is applied to the first and last p[i]
observations of time series x[, i]
.
A new time series object.
The spectrum
function estimates the spectral density of a
time series.
spectrum(x, ..., method = c("pgram", "ar"))
spectrum(x, ..., method = c("pgram", "ar"))
x |
A univariate or multivariate time series. |
method |
String specifying the method used to estimate the
spectral density. Allowed methods are |
... |
Further arguments to specific spec methods or
|
spectrum
is a wrapper function which calls the methods
spec.pgram
and spec.ar
.
The spectrum here is defined (for historical compatibility) with
scaling 1/frequency(x)
. This makes the spectral density
a density over the range (-frequency(x)/2, +frequency(x)/2]
,
whereas a more common scaling is and range
(e.g., Bloomfield) or 1 and range
.
If available, a confidence interval will be plotted by
plot.spec
: this is asymmetric, and the width of the centre
mark indicates the equivalent bandwidth.
An object of class "spec"
, which is a list containing at
least the following components:
freq |
vector of frequencies at which the spectral density is estimated. (Possibly approximate Fourier frequencies.) The units are the reciprocal of cycles per unit time (and not per observation spacing): see ‘Details’ below. |
spec |
Vector (for univariate series) or matrix (for multivariate
series) of estimates of the spectral density at frequencies
corresponding to |
coh |
|
phase |
|
series |
The name of the time series. |
snames |
For multivariate input, the names of the component series. |
method |
The method used to calculate the spectrum. |
The result is returned invisibly if plot
is true.
The default plot for objects of class "spec"
is quite complex,
including an error bar and default title, subtitle and axis
labels. The defaults can all be overridden by supplying the
appropriate graphical parameters.
Martyn Plummer, B.D. Ripley
Bloomfield, P. (1976) Fourier Analysis of Time Series: An Introduction. Wiley.
Brockwell, P. J. and Davis, R. A. (1991) Time Series: Theory and Methods. Second edition. Springer.
Venables, W. N. and Ripley, B. D. (2002) Modern Applied Statistics with S-PLUS. Fourth edition. Springer. (Especially pages 392–7.)
spec.ar
,
spec.pgram
;
plot.spec
.
require(graphics) ## Examples from Venables & Ripley ## spec.pgram par(mfrow = c(2,2)) spectrum(lh) spectrum(lh, spans = 3) spectrum(lh, spans = c(3,3)) spectrum(lh, spans = c(3,5)) spectrum(ldeaths) spectrum(ldeaths, spans = c(3,3)) spectrum(ldeaths, spans = c(3,5)) spectrum(ldeaths, spans = c(5,7)) spectrum(ldeaths, spans = c(5,7), log = "dB", ci = 0.8) # for multivariate examples see the help for spec.pgram ## spec.ar spectrum(lh, method = "ar") spectrum(ldeaths, method = "ar")
require(graphics) ## Examples from Venables & Ripley ## spec.pgram par(mfrow = c(2,2)) spectrum(lh) spectrum(lh, spans = 3) spectrum(lh, spans = c(3,3)) spectrum(lh, spans = c(3,5)) spectrum(ldeaths) spectrum(ldeaths, spans = c(3,3)) spectrum(ldeaths, spans = c(3,5)) spectrum(ldeaths, spans = c(5,7)) spectrum(ldeaths, spans = c(5,7), log = "dB", ci = 0.8) # for multivariate examples see the help for spec.pgram ## spec.ar spectrum(lh, method = "ar") spectrum(ldeaths, method = "ar")
Perform cubic (or Hermite) spline interpolation of given data points, returning either a list of points obtained by the interpolation or a function performing the interpolation.
splinefun(x, y = NULL, method = c("fmm", "periodic", "natural", "monoH.FC", "hyman"), ties = mean) spline(x, y = NULL, n = 3*length(x), method = "fmm", xmin = min(x), xmax = max(x), xout, ties = mean) splinefunH(x, y, m)
splinefun(x, y = NULL, method = c("fmm", "periodic", "natural", "monoH.FC", "hyman"), ties = mean) spline(x, y = NULL, n = 3*length(x), method = "fmm", xmin = min(x), xmax = max(x), xout, ties = mean) splinefunH(x, y, m)
x , y
|
vectors giving the coordinates of the points to be
interpolated. Alternatively a single plotting structure can be
specified: see
|
m |
(for |
method |
specifies the type of spline to be used. Possible
values are |
n |
if |
xmin , xmax
|
left-hand and right-hand endpoint of the
interpolation interval (when |
xout |
an optional set of values specifying where interpolation is to take place. |
ties |
handling of tied |
The inputs can contain missing values which are deleted, so at least
one complete (x, y)
pair is required.
If method = "fmm"
, the spline used is that of
Forsythe, Malcolm and Moler
(an exact cubic is fitted through the four points at each
end of the data, and this is used to determine the end conditions).
Natural splines are used when method = "natural"
, and periodic
splines when method = "periodic"
.
The method "monoH.FC"
computes a monotone Hermite spline
according to the method of Fritsch and Carlson. It does so by
determining slopes such that the Hermite spline, determined by
, is monotone (increasing or
decreasing) iff the data are.
Method "hyman"
computes a monotone cubic spline using
Hyman filtering of an method = "fmm"
fit for strictly monotonic
inputs.
These interpolation splines can also be used for extrapolation, that is
prediction at points outside the range of x
. Extrapolation
makes little sense for method = "fmm"
; for natural splines it
is linear using the slope of the interpolating curve at the nearest
data point.
spline
returns a list containing components x
and
y
which give the ordinates where interpolation took place and
the interpolated values.
splinefun
returns a function with formal arguments x
and
deriv
, the latter defaulting to zero. This function
can be used to evaluate the interpolating cubic spline
(deriv
= 0), or its derivatives (deriv
= 1, 2, 3) at the
points x
, where the spline function interpolates the data
points originally specified. It uses data stored in its environment
when it was created, the details of which are subject to change.
The value returned by splinefun
contains references to the code
in the current version of R: it is not intended to be saved and
loaded into a different R session. This is safer in R >= 3.0.0.
R Core Team.
Simon Wood for the original code for Hyman filtering.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988). The New S Language. Wadsworth & Brooks/Cole.
Dougherty, R. L., Edelman, A. and Hyman, J. M. (1989) Positivity-, monotonicity-, or convexity-preserving cubic and quintic Hermite interpolation. Mathematics of Computation, 52, 471–494. doi:10.1090/S0025-5718-1989-0962209-1.
Forsythe, G. E., Malcolm, M. A. and Moler, C. B. (1977). Computer Methods for Mathematical Computations. Wiley.
Fritsch, F. N. and Carlson, R. E. (1980). Monotone piecewise cubic interpolation. SIAM Journal on Numerical Analysis, 17, 238–246. doi:10.1137/0717021.
Hyman, J. M. (1983). Accurate monotonicity preserving cubic interpolation. SIAM Journal on Scientific and Statistical Computing, 4, 645–654. doi:10.1137/0904045.
approx
and approxfun
for constant and
linear interpolation.
Package splines, especially interpSpline
and periodicSpline
for interpolation splines.
That package also generates spline bases that can be used for
regression splines.
smooth.spline
for smoothing splines.
require(graphics) op <- par(mfrow = c(2,1), mgp = c(2,.8,0), mar = 0.1+c(3,3,3,1)) n <- 9 x <- 1:n y <- rnorm(n) plot(x, y, main = paste("spline[fun](.) through", n, "points")) lines(spline(x, y)) lines(spline(x, y, n = 201), col = 2) y <- (x-6)^2 plot(x, y, main = "spline(.) -- 3 methods") lines(spline(x, y, n = 201), col = 2) lines(spline(x, y, n = 201, method = "natural"), col = 3) lines(spline(x, y, n = 201, method = "periodic"), col = 4) legend(6, 25, c("fmm","natural","periodic"), col = 2:4, lty = 1) y <- sin((x-0.5)*pi) f <- splinefun(x, y) ls(envir = environment(f)) splinecoef <- get("z", envir = environment(f)) curve(f(x), 1, 10, col = "green", lwd = 1.5) points(splinecoef, col = "purple", cex = 2) curve(f(x, deriv = 1), 1, 10, col = 2, lwd = 1.5) curve(f(x, deriv = 2), 1, 10, col = 2, lwd = 1.5, n = 401) curve(f(x, deriv = 3), 1, 10, col = 2, lwd = 1.5, n = 401) par(op) ## Manual spline evaluation --- demo the coefficients : .x <- splinecoef$x u <- seq(3, 6, by = 0.25) (ii <- findInterval(u, .x)) dx <- u - .x[ii] f.u <- with(splinecoef, y[ii] + dx*(b[ii] + dx*(c[ii] + dx* d[ii]))) stopifnot(all.equal(f(u), f.u)) ## An example with ties (non-unique x values): set.seed(1); x <- round(rnorm(30), 1); y <- sin(pi * x) + rnorm(30)/10 plot(x, y, main = "spline(x,y) when x has ties") lines(spline(x, y, n = 201), col = 2) ## visualizes the non-unique ones: tx <- table(x); mx <- as.numeric(names(tx[tx > 1])) ry <- matrix(unlist(tapply(y, match(x, mx), range, simplify = FALSE)), ncol = 2, byrow = TRUE) segments(mx, ry[, 1], mx, ry[, 2], col = "blue", lwd = 2) ## Another example with sorted x, but ties: set.seed(8); x <- sort(round(rnorm(30), 1)); y <- round(sin(pi * x) + rnorm(30)/10, 3) summary(diff(x) == 0) # -> 7 duplicated x-values str(spline(x, y, n = 201, ties="ordered")) # all '$y' entries are NaN ## The default (ties=mean) is ok, but most efficient to use instead is sxyo <- spline(x, y, n = 201, ties= list("ordered", mean)) sapply(sxyo, summary)# all fine now plot(x, y, main = "spline(x,y, ties=list(\"ordered\", mean)) for when x has ties") lines(sxyo, col="blue") ## An example of monotone interpolation n <- 20 set.seed(11) x. <- sort(runif(n)) ; y. <- cumsum(abs(rnorm(n))) plot(x., y.) curve(splinefun(x., y.)(x), add = TRUE, col = 2, n = 1001) curve(splinefun(x., y., method = "monoH.FC")(x), add = TRUE, col = 3, n = 1001) curve(splinefun(x., y., method = "hyman") (x), add = TRUE, col = 4, n = 1001) legend("topleft", paste0("splinefun( \"", c("fmm", "monoH.FC", "hyman"), "\" )"), col = 2:4, lty = 1, bty = "n") ## and one from Fritsch and Carlson (1980), Dougherty et al (1989) x. <- c(7.09, 8.09, 8.19, 8.7, 9.2, 10, 12, 15, 20) f <- c(0, 2.76429e-5, 4.37498e-2, 0.169183, 0.469428, 0.943740, 0.998636, 0.999919, 0.999994) s0 <- splinefun(x., f) s1 <- splinefun(x., f, method = "monoH.FC") s2 <- splinefun(x., f, method = "hyman") plot(x., f, ylim = c(-0.2, 1.2)) curve(s0(x), add = TRUE, col = 2, n = 1001) -> m0 curve(s1(x), add = TRUE, col = 3, n = 1001) curve(s2(x), add = TRUE, col = 4, n = 1001) legend("right", paste0("splinefun( \"", c("fmm", "monoH.FC", "hyman"), "\" )"), col = 2:4, lty = 1, bty = "n") ## they seem identical, but are not quite: xx <- m0$x plot(xx, s1(xx) - s2(xx), type = "l", col = 2, lwd = 2, main = "Difference monoH.FC - hyman"); abline(h = 0, lty = 3) x <- xx[xx < 10.2] ## full range: x <- xx .. does not show enough ccol <- adjustcolor(2:4, 0.8) matplot(x, cbind(s0(x, deriv = 2), s1(x, deriv = 2), s2(x, deriv = 2))^2, lwd = 2, col = ccol, type = "l", ylab = quote({{f*second}(x)}^2), main = expression({{f*second}(x)}^2 ~" for the three 'splines'")) legend("topright", paste0("splinefun( \"", c("fmm", "monoH.FC", "hyman"), "\" )"), lwd = 2, col = ccol, lty = 1:3, bty = "n") ## --> "hyman" has slightly smaller Integral f''(x)^2 dx than "FC", ## here, and both are 'much worse' than the regular fmm spline.
require(graphics) op <- par(mfrow = c(2,1), mgp = c(2,.8,0), mar = 0.1+c(3,3,3,1)) n <- 9 x <- 1:n y <- rnorm(n) plot(x, y, main = paste("spline[fun](.) through", n, "points")) lines(spline(x, y)) lines(spline(x, y, n = 201), col = 2) y <- (x-6)^2 plot(x, y, main = "spline(.) -- 3 methods") lines(spline(x, y, n = 201), col = 2) lines(spline(x, y, n = 201, method = "natural"), col = 3) lines(spline(x, y, n = 201, method = "periodic"), col = 4) legend(6, 25, c("fmm","natural","periodic"), col = 2:4, lty = 1) y <- sin((x-0.5)*pi) f <- splinefun(x, y) ls(envir = environment(f)) splinecoef <- get("z", envir = environment(f)) curve(f(x), 1, 10, col = "green", lwd = 1.5) points(splinecoef, col = "purple", cex = 2) curve(f(x, deriv = 1), 1, 10, col = 2, lwd = 1.5) curve(f(x, deriv = 2), 1, 10, col = 2, lwd = 1.5, n = 401) curve(f(x, deriv = 3), 1, 10, col = 2, lwd = 1.5, n = 401) par(op) ## Manual spline evaluation --- demo the coefficients : .x <- splinecoef$x u <- seq(3, 6, by = 0.25) (ii <- findInterval(u, .x)) dx <- u - .x[ii] f.u <- with(splinecoef, y[ii] + dx*(b[ii] + dx*(c[ii] + dx* d[ii]))) stopifnot(all.equal(f(u), f.u)) ## An example with ties (non-unique x values): set.seed(1); x <- round(rnorm(30), 1); y <- sin(pi * x) + rnorm(30)/10 plot(x, y, main = "spline(x,y) when x has ties") lines(spline(x, y, n = 201), col = 2) ## visualizes the non-unique ones: tx <- table(x); mx <- as.numeric(names(tx[tx > 1])) ry <- matrix(unlist(tapply(y, match(x, mx), range, simplify = FALSE)), ncol = 2, byrow = TRUE) segments(mx, ry[, 1], mx, ry[, 2], col = "blue", lwd = 2) ## Another example with sorted x, but ties: set.seed(8); x <- sort(round(rnorm(30), 1)); y <- round(sin(pi * x) + rnorm(30)/10, 3) summary(diff(x) == 0) # -> 7 duplicated x-values str(spline(x, y, n = 201, ties="ordered")) # all '$y' entries are NaN ## The default (ties=mean) is ok, but most efficient to use instead is sxyo <- spline(x, y, n = 201, ties= list("ordered", mean)) sapply(sxyo, summary)# all fine now plot(x, y, main = "spline(x,y, ties=list(\"ordered\", mean)) for when x has ties") lines(sxyo, col="blue") ## An example of monotone interpolation n <- 20 set.seed(11) x. <- sort(runif(n)) ; y. <- cumsum(abs(rnorm(n))) plot(x., y.) curve(splinefun(x., y.)(x), add = TRUE, col = 2, n = 1001) curve(splinefun(x., y., method = "monoH.FC")(x), add = TRUE, col = 3, n = 1001) curve(splinefun(x., y., method = "hyman") (x), add = TRUE, col = 4, n = 1001) legend("topleft", paste0("splinefun( \"", c("fmm", "monoH.FC", "hyman"), "\" )"), col = 2:4, lty = 1, bty = "n") ## and one from Fritsch and Carlson (1980), Dougherty et al (1989) x. <- c(7.09, 8.09, 8.19, 8.7, 9.2, 10, 12, 15, 20) f <- c(0, 2.76429e-5, 4.37498e-2, 0.169183, 0.469428, 0.943740, 0.998636, 0.999919, 0.999994) s0 <- splinefun(x., f) s1 <- splinefun(x., f, method = "monoH.FC") s2 <- splinefun(x., f, method = "hyman") plot(x., f, ylim = c(-0.2, 1.2)) curve(s0(x), add = TRUE, col = 2, n = 1001) -> m0 curve(s1(x), add = TRUE, col = 3, n = 1001) curve(s2(x), add = TRUE, col = 4, n = 1001) legend("right", paste0("splinefun( \"", c("fmm", "monoH.FC", "hyman"), "\" )"), col = 2:4, lty = 1, bty = "n") ## they seem identical, but are not quite: xx <- m0$x plot(xx, s1(xx) - s2(xx), type = "l", col = 2, lwd = 2, main = "Difference monoH.FC - hyman"); abline(h = 0, lty = 3) x <- xx[xx < 10.2] ## full range: x <- xx .. does not show enough ccol <- adjustcolor(2:4, 0.8) matplot(x, cbind(s0(x, deriv = 2), s1(x, deriv = 2), s2(x, deriv = 2))^2, lwd = 2, col = ccol, type = "l", ylab = quote({{f*second}(x)}^2), main = expression({{f*second}(x)}^2 ~" for the three 'splines'")) legend("topright", paste0("splinefun( \"", c("fmm", "monoH.FC", "hyman"), "\" )"), lwd = 2, col = ccol, lty = 1:3, bty = "n") ## --> "hyman" has slightly smaller Integral f''(x)^2 dx than "FC", ## here, and both are 'much worse' than the regular fmm spline.
nls
Asymptotic ModelThis selfStart
model evaluates the asymptotic regression
function and its gradient. It has an initial
attribute that
will evaluate initial estimates of the parameters Asym
, R0
,
and lrc
for a given set of data.
Note that SSweibull()
generalizes this asymptotic model
with an extra parameter.
SSasymp(input, Asym, R0, lrc)
SSasymp(input, Asym, R0, lrc)
input |
a numeric vector of values at which to evaluate the model. |
Asym |
a numeric parameter representing the horizontal asymptote on
the right side (very large values of |
R0 |
a numeric parameter representing the response when
|
lrc |
a numeric parameter representing the natural logarithm of the rate constant. |
a numeric vector of the same length as input
. It is the value of
the expression Asym+(R0-Asym)*exp(-exp(lrc)*input)
. If all of
the arguments Asym
, R0
, and lrc
are
names of objects, the gradient matrix with respect to these names is
attached as an attribute named gradient
.
José Pinheiro and Douglas Bates
Lob.329 <- Loblolly[ Loblolly$Seed == "329", ] SSasymp( Lob.329$age, 100, -8.5, -3.2 ) # response only local({ Asym <- 100 ; resp0 <- -8.5 ; lrc <- -3.2 SSasymp( Lob.329$age, Asym, resp0, lrc) # response _and_ gradient }) getInitial(height ~ SSasymp( age, Asym, resp0, lrc), data = Lob.329) ## Initial values are in fact the converged values fm1 <- nls(height ~ SSasymp( age, Asym, resp0, lrc), data = Lob.329) summary(fm1) ## Visualize the SSasymp() model parametrization : xx <- seq(-.3, 5, length.out = 101) ## Asym + (R0-Asym) * exp(-exp(lrc)* x) : yy <- 5 - 4 * exp(-xx / exp(3/4)) stopifnot( all.equal(yy, SSasymp(xx, Asym = 5, R0 = 1, lrc = -3/4)) ) require(graphics) op <- par(mar = c(0, .2, 4.1, 0)) plot(xx, yy, type = "l", axes = FALSE, ylim = c(0,5.2), xlim = c(-.3, 5), xlab = "", ylab = "", lwd = 2, main = quote("Parameters in the SSasymp model " ~ {f[phi](x) == phi[1] + (phi[2]-phi[1])*~e^{-e^{phi[3]}*~x}})) mtext(quote(list(phi[1] == "Asym", phi[2] == "R0", phi[3] == "lrc"))) usr <- par("usr") arrows(usr[1], 0, usr[2], 0, length = 0.1, angle = 25) arrows(0, usr[3], 0, usr[4], length = 0.1, angle = 25) text(usr[2] - 0.2, 0.1, "x", adj = c(1, 0)) text( -0.1, usr[4], "y", adj = c(1, 1)) abline(h = 5, lty = 3) arrows(c(0.35, 0.65), 1, c(0 , 1 ), 1, length = 0.08, angle = 25); text(0.5, 1, quote(1)) y0 <- 1 + 4*exp(-3/4) ; t.5 <- log(2) / exp(-3/4) ; AR2 <- 3 # (Asym + R0)/2 segments(c(1, 1), c( 1, y0), c(1, 0), c(y0, 1), lty = 2, lwd = 0.75) text(1.1, 1/2+y0/2, quote((phi[1]-phi[2])*e^phi[3]), adj = c(0,.5)) axis(2, at = c(1,AR2,5), labels= expression(phi[2], frac(phi[1]+phi[2],2), phi[1]), pos=0, las=1) arrows(c(.6,t.5-.6), AR2, c(0, t.5 ), AR2, length = 0.08, angle = 25) text( t.5/2, AR2, quote(t[0.5])) text( t.5 +.4, AR2, quote({f(t[0.5]) == frac(phi[1]+phi[2],2)}~{} %=>% {}~~ {t[0.5] == frac(log(2), e^{phi[3]})}), adj = c(0, 0.5)) par(op)
Lob.329 <- Loblolly[ Loblolly$Seed == "329", ] SSasymp( Lob.329$age, 100, -8.5, -3.2 ) # response only local({ Asym <- 100 ; resp0 <- -8.5 ; lrc <- -3.2 SSasymp( Lob.329$age, Asym, resp0, lrc) # response _and_ gradient }) getInitial(height ~ SSasymp( age, Asym, resp0, lrc), data = Lob.329) ## Initial values are in fact the converged values fm1 <- nls(height ~ SSasymp( age, Asym, resp0, lrc), data = Lob.329) summary(fm1) ## Visualize the SSasymp() model parametrization : xx <- seq(-.3, 5, length.out = 101) ## Asym + (R0-Asym) * exp(-exp(lrc)* x) : yy <- 5 - 4 * exp(-xx / exp(3/4)) stopifnot( all.equal(yy, SSasymp(xx, Asym = 5, R0 = 1, lrc = -3/4)) ) require(graphics) op <- par(mar = c(0, .2, 4.1, 0)) plot(xx, yy, type = "l", axes = FALSE, ylim = c(0,5.2), xlim = c(-.3, 5), xlab = "", ylab = "", lwd = 2, main = quote("Parameters in the SSasymp model " ~ {f[phi](x) == phi[1] + (phi[2]-phi[1])*~e^{-e^{phi[3]}*~x}})) mtext(quote(list(phi[1] == "Asym", phi[2] == "R0", phi[3] == "lrc"))) usr <- par("usr") arrows(usr[1], 0, usr[2], 0, length = 0.1, angle = 25) arrows(0, usr[3], 0, usr[4], length = 0.1, angle = 25) text(usr[2] - 0.2, 0.1, "x", adj = c(1, 0)) text( -0.1, usr[4], "y", adj = c(1, 1)) abline(h = 5, lty = 3) arrows(c(0.35, 0.65), 1, c(0 , 1 ), 1, length = 0.08, angle = 25); text(0.5, 1, quote(1)) y0 <- 1 + 4*exp(-3/4) ; t.5 <- log(2) / exp(-3/4) ; AR2 <- 3 # (Asym + R0)/2 segments(c(1, 1), c( 1, y0), c(1, 0), c(y0, 1), lty = 2, lwd = 0.75) text(1.1, 1/2+y0/2, quote((phi[1]-phi[2])*e^phi[3]), adj = c(0,.5)) axis(2, at = c(1,AR2,5), labels= expression(phi[2], frac(phi[1]+phi[2],2), phi[1]), pos=0, las=1) arrows(c(.6,t.5-.6), AR2, c(0, t.5 ), AR2, length = 0.08, angle = 25) text( t.5/2, AR2, quote(t[0.5])) text( t.5 +.4, AR2, quote({f(t[0.5]) == frac(phi[1]+phi[2],2)}~{} %=>% {}~~ {t[0.5] == frac(log(2), e^{phi[3]})}), adj = c(0, 0.5)) par(op)
nls
Asymptotic Model with an OffsetThis selfStart
model evaluates an alternative parametrization
of the asymptotic
regression function and the gradient with respect to those parameters.
It has an initial
attribute that creates initial estimates of the parameters
Asym
, lrc
, and c0
.
SSasympOff(input, Asym, lrc, c0)
SSasympOff(input, Asym, lrc, c0)
input |
a numeric vector of values at which to evaluate the model. |
Asym |
a numeric parameter representing the horizontal asymptote on
the right side (very large values of |
lrc |
a numeric parameter representing the natural logarithm of the rate constant. |
c0 |
a numeric parameter representing the |
a numeric vector of the same length as input
. It is the value of
the expression Asym*(1 - exp(-exp(lrc)*(input - c0)))
. If all of
the arguments Asym
, lrc
, and c0
are
names of objects, the gradient matrix with respect to these names is
attached as an attribute named gradient
.
José Pinheiro and Douglas Bates
nls
, selfStart
;
example(SSasympOff)
gives graph showing the SSasympOff
parametrization.
CO2.Qn1 <- CO2[CO2$Plant == "Qn1", ] SSasympOff(CO2.Qn1$conc, 32, -4, 43) # response only local({ Asym <- 32; lrc <- -4; c0 <- 43 SSasympOff(CO2.Qn1$conc, Asym, lrc, c0) # response and gradient }) getInitial(uptake ~ SSasympOff(conc, Asym, lrc, c0), data = CO2.Qn1) ## Initial values are in fact the converged values fm1 <- nls(uptake ~ SSasympOff(conc, Asym, lrc, c0), data = CO2.Qn1) summary(fm1) ## Visualize the SSasympOff() model parametrization : xx <- seq(0.25, 8, by=1/16) yy <- 5 * (1 - exp(-(xx - 3/4)*0.4)) stopifnot( all.equal(yy, SSasympOff(xx, Asym = 5, lrc = log(0.4), c0 = 3/4)) ) require(graphics) op <- par(mar = c(0, 0, 4.0, 0)) plot(xx, yy, type = "l", axes = FALSE, ylim = c(-.5,6), xlim = c(-1, 8), xlab = "", ylab = "", lwd = 2, main = "Parameters in the SSasympOff model") mtext(quote(list(phi[1] == "Asym", phi[2] == "lrc", phi[3] == "c0"))) usr <- par("usr") arrows(usr[1], 0, usr[2], 0, length = 0.1, angle = 25) arrows(0, usr[3], 0, usr[4], length = 0.1, angle = 25) text(usr[2] - 0.2, 0.1, "x", adj = c(1, 0)) text( -0.1, usr[4], "y", adj = c(1, 1)) abline(h = 5, lty = 3) arrows(-0.8, c(2.1, 2.9), -0.8, c(0 , 5 ), length = 0.1, angle = 25) text (-0.8, 2.5, quote(phi[1])) segments(3/4, -.2, 3/4, 1.6, lty = 2) text (3/4, c(-.3, 1.7), quote(phi[3])) arrows(c(1.1, 1.4), -.15, c(3/4, 7/4), -.15, length = 0.07, angle = 25) text (3/4 + 1/2, -.15, quote(1)) segments(c(3/4, 7/4, 7/4), c(0, 0, 2), # 5 * exp(log(0.4)) = 2 c(7/4, 7/4, 3/4), c(0, 2, 0), lty = 2, lwd = 2) text( 7/4 +.1, 2./2, quote(phi[1]*e^phi[2]), adj = c(0, .5)) par(op)
CO2.Qn1 <- CO2[CO2$Plant == "Qn1", ] SSasympOff(CO2.Qn1$conc, 32, -4, 43) # response only local({ Asym <- 32; lrc <- -4; c0 <- 43 SSasympOff(CO2.Qn1$conc, Asym, lrc, c0) # response and gradient }) getInitial(uptake ~ SSasympOff(conc, Asym, lrc, c0), data = CO2.Qn1) ## Initial values are in fact the converged values fm1 <- nls(uptake ~ SSasympOff(conc, Asym, lrc, c0), data = CO2.Qn1) summary(fm1) ## Visualize the SSasympOff() model parametrization : xx <- seq(0.25, 8, by=1/16) yy <- 5 * (1 - exp(-(xx - 3/4)*0.4)) stopifnot( all.equal(yy, SSasympOff(xx, Asym = 5, lrc = log(0.4), c0 = 3/4)) ) require(graphics) op <- par(mar = c(0, 0, 4.0, 0)) plot(xx, yy, type = "l", axes = FALSE, ylim = c(-.5,6), xlim = c(-1, 8), xlab = "", ylab = "", lwd = 2, main = "Parameters in the SSasympOff model") mtext(quote(list(phi[1] == "Asym", phi[2] == "lrc", phi[3] == "c0"))) usr <- par("usr") arrows(usr[1], 0, usr[2], 0, length = 0.1, angle = 25) arrows(0, usr[3], 0, usr[4], length = 0.1, angle = 25) text(usr[2] - 0.2, 0.1, "x", adj = c(1, 0)) text( -0.1, usr[4], "y", adj = c(1, 1)) abline(h = 5, lty = 3) arrows(-0.8, c(2.1, 2.9), -0.8, c(0 , 5 ), length = 0.1, angle = 25) text (-0.8, 2.5, quote(phi[1])) segments(3/4, -.2, 3/4, 1.6, lty = 2) text (3/4, c(-.3, 1.7), quote(phi[3])) arrows(c(1.1, 1.4), -.15, c(3/4, 7/4), -.15, length = 0.07, angle = 25) text (3/4 + 1/2, -.15, quote(1)) segments(c(3/4, 7/4, 7/4), c(0, 0, 2), # 5 * exp(log(0.4)) = 2 c(7/4, 7/4, 3/4), c(0, 2, 0), lty = 2, lwd = 2) text( 7/4 +.1, 2./2, quote(phi[1]*e^phi[2]), adj = c(0, .5)) par(op)
nls
Asymptotic Model through the OriginThis selfStart
model evaluates the asymptotic regression
function through the origin and its gradient. It has an
initial
attribute that will evaluate initial estimates of the
parameters Asym
and lrc
for a given set of data.
SSasympOrig(input, Asym, lrc)
SSasympOrig(input, Asym, lrc)
input |
a numeric vector of values at which to evaluate the model. |
Asym |
a numeric parameter representing the horizontal asymptote. |
lrc |
a numeric parameter representing the natural logarithm of the rate constant. |
a numeric vector of the same length as input
. It is the value of
the expression Asym*(1 - exp(-exp(lrc)*input))
. If all of
the arguments Asym
and lrc
are
names of objects, the gradient matrix with respect to these names is
attached as an attribute named gradient
.
José Pinheiro and Douglas Bates
Lob.329 <- Loblolly[ Loblolly$Seed == "329", ] SSasympOrig(Lob.329$age, 100, -3.2) # response only local({ Asym <- 100; lrc <- -3.2 SSasympOrig(Lob.329$age, Asym, lrc) # response and gradient }) getInitial(height ~ SSasympOrig(age, Asym, lrc), data = Lob.329) ## Initial values are in fact the converged values fm1 <- nls(height ~ SSasympOrig(age, Asym, lrc), data = Lob.329) summary(fm1) ## Visualize the SSasympOrig() model parametrization : xx <- seq(0, 5, length.out = 101) yy <- 5 * (1- exp(-xx * log(2))) stopifnot( all.equal(yy, SSasympOrig(xx, Asym = 5, lrc = log(log(2)))) ) require(graphics) op <- par(mar = c(0, 0, 3.5, 0)) plot(xx, yy, type = "l", axes = FALSE, ylim = c(0,5), xlim = c(-1/4, 5), xlab = "", ylab = "", lwd = 2, main = quote("Parameters in the SSasympOrig model"~~ f[phi](x))) mtext(quote(list(phi[1] == "Asym", phi[2] == "lrc"))) usr <- par("usr") arrows(usr[1], 0, usr[2], 0, length = 0.1, angle = 25) arrows(0, usr[3], 0, usr[4], length = 0.1, angle = 25) text(usr[2] - 0.2, 0.1, "x", adj = c(1, 0)) text( -0.1, usr[4], "y", adj = c(1, 1)) abline(h = 5, lty = 3) axis(2, at = 5*c(1/2,1), labels= expression(frac(phi[1],2), phi[1]), pos=0, las=1) arrows(c(.3,.7), 5/2, c(0, 1 ), 5/2, length = 0.08, angle = 25) text( 0.5, 5/2, quote(t[0.5])) text( 1 +.4, 5/2, quote({f(t[0.5]) == frac(phi[1],2)}~{} %=>% {}~~{t[0.5] == frac(log(2), e^{phi[2]})}), adj = c(0, 0.5)) par(op)
Lob.329 <- Loblolly[ Loblolly$Seed == "329", ] SSasympOrig(Lob.329$age, 100, -3.2) # response only local({ Asym <- 100; lrc <- -3.2 SSasympOrig(Lob.329$age, Asym, lrc) # response and gradient }) getInitial(height ~ SSasympOrig(age, Asym, lrc), data = Lob.329) ## Initial values are in fact the converged values fm1 <- nls(height ~ SSasympOrig(age, Asym, lrc), data = Lob.329) summary(fm1) ## Visualize the SSasympOrig() model parametrization : xx <- seq(0, 5, length.out = 101) yy <- 5 * (1- exp(-xx * log(2))) stopifnot( all.equal(yy, SSasympOrig(xx, Asym = 5, lrc = log(log(2)))) ) require(graphics) op <- par(mar = c(0, 0, 3.5, 0)) plot(xx, yy, type = "l", axes = FALSE, ylim = c(0,5), xlim = c(-1/4, 5), xlab = "", ylab = "", lwd = 2, main = quote("Parameters in the SSasympOrig model"~~ f[phi](x))) mtext(quote(list(phi[1] == "Asym", phi[2] == "lrc"))) usr <- par("usr") arrows(usr[1], 0, usr[2], 0, length = 0.1, angle = 25) arrows(0, usr[3], 0, usr[4], length = 0.1, angle = 25) text(usr[2] - 0.2, 0.1, "x", adj = c(1, 0)) text( -0.1, usr[4], "y", adj = c(1, 1)) abline(h = 5, lty = 3) axis(2, at = 5*c(1/2,1), labels= expression(frac(phi[1],2), phi[1]), pos=0, las=1) arrows(c(.3,.7), 5/2, c(0, 1 ), 5/2, length = 0.08, angle = 25) text( 0.5, 5/2, quote(t[0.5])) text( 1 +.4, 5/2, quote({f(t[0.5]) == frac(phi[1],2)}~{} %=>% {}~~{t[0.5] == frac(log(2), e^{phi[2]})}), adj = c(0, 0.5)) par(op)
nls
Biexponential ModelThis selfStart
model evaluates the biexponential model function
and its gradient. It has an initial
attribute that
creates initial estimates of the parameters A1
, lrc1
,
A2
, and lrc2
.
SSbiexp(input, A1, lrc1, A2, lrc2)
SSbiexp(input, A1, lrc1, A2, lrc2)
input |
a numeric vector of values at which to evaluate the model. |
A1 |
a numeric parameter representing the multiplier of the first exponential. |
lrc1 |
a numeric parameter representing the natural logarithm of the rate constant of the first exponential. |
A2 |
a numeric parameter representing the multiplier of the second exponential. |
lrc2 |
a numeric parameter representing the natural logarithm of the rate constant of the second exponential. |
a numeric vector of the same length as input
. It is the value of
the expression
A1*exp(-exp(lrc1)*input)+A2*exp(-exp(lrc2)*input)
.
If all of the arguments A1
, lrc1
, A2
, and
lrc2
are names of objects, the gradient matrix with respect to
these names is attached as an attribute named gradient
.
José Pinheiro and Douglas Bates
Indo.1 <- Indometh[Indometh$Subject == 1, ] SSbiexp( Indo.1$time, 3, 1, 0.6, -1.3 ) # response only A1 <- 3; lrc1 <- 1; A2 <- 0.6; lrc2 <- -1.3 SSbiexp( Indo.1$time, A1, lrc1, A2, lrc2 ) # response and gradient print(getInitial(conc ~ SSbiexp(time, A1, lrc1, A2, lrc2), data = Indo.1), digits = 5) ## Initial values are in fact the converged values fm1 <- nls(conc ~ SSbiexp(time, A1, lrc1, A2, lrc2), data = Indo.1) summary(fm1) ## Show the model components visually require(graphics) xx <- seq(0, 5, length.out = 101) y1 <- 3.5 * exp(-4*xx) y2 <- 1.5 * exp(-xx) plot(xx, y1 + y2, type = "l", lwd=2, ylim = c(-0.2,6), xlim = c(0, 5), main = "Components of the SSbiexp model") lines(xx, y1, lty = 2, col="tomato"); abline(v=0, h=0, col="gray40") lines(xx, y2, lty = 3, col="blue2" ) legend("topright", c("y1+y2", "y1 = 3.5 * exp(-4*x)", "y2 = 1.5 * exp(-x)"), lty=1:3, col=c("black","tomato","blue2"), bty="n") axis(2, pos=0, at = c(3.5, 1.5), labels = c("A1","A2"), las=2) ## and how you could have got their sum via SSbiexp(): ySS <- SSbiexp(xx, 3.5, log(4), 1.5, log(1)) ## --- --- stopifnot(all.equal(y1+y2, ySS, tolerance = 1e-15)) ## Show a no-noise example datN <- data.frame(time = (0:600)/64) datN$conc <- predict(fm1, newdata=datN) plot(conc ~ time, data=datN) # perfect, no noise ## Fails by default (scaleOffset=0) on most platforms {also after increasing maxiter !} ## Not run: nls(conc ~ SSbiexp(time, A1, lrc1, A2, lrc2), data = datN, trace=TRUE) ## End(Not run) fmX1 <- nls(conc ~ SSbiexp(time, A1, lrc1, A2, lrc2), data = datN, control = list(scaleOffset=1)) fmX <- nls(conc ~ SSbiexp(time, A1, lrc1, A2, lrc2), data = datN, control = list(scaleOffset=1, printEval=TRUE, tol=1e-11, nDcentral=TRUE), trace=TRUE) all.equal(coef(fm1), coef(fmX1), tolerance=0) # ... rel.diff.: 1.57e-6 all.equal(coef(fm1), coef(fmX), tolerance=0) # ... rel.diff.: 1.03e-12 stopifnot(all.equal(coef(fm1), coef(fmX1), tolerance = 6e-6), all.equal(coef(fm1), coef(fmX ), tolerance = 1e-11))
Indo.1 <- Indometh[Indometh$Subject == 1, ] SSbiexp( Indo.1$time, 3, 1, 0.6, -1.3 ) # response only A1 <- 3; lrc1 <- 1; A2 <- 0.6; lrc2 <- -1.3 SSbiexp( Indo.1$time, A1, lrc1, A2, lrc2 ) # response and gradient print(getInitial(conc ~ SSbiexp(time, A1, lrc1, A2, lrc2), data = Indo.1), digits = 5) ## Initial values are in fact the converged values fm1 <- nls(conc ~ SSbiexp(time, A1, lrc1, A2, lrc2), data = Indo.1) summary(fm1) ## Show the model components visually require(graphics) xx <- seq(0, 5, length.out = 101) y1 <- 3.5 * exp(-4*xx) y2 <- 1.5 * exp(-xx) plot(xx, y1 + y2, type = "l", lwd=2, ylim = c(-0.2,6), xlim = c(0, 5), main = "Components of the SSbiexp model") lines(xx, y1, lty = 2, col="tomato"); abline(v=0, h=0, col="gray40") lines(xx, y2, lty = 3, col="blue2" ) legend("topright", c("y1+y2", "y1 = 3.5 * exp(-4*x)", "y2 = 1.5 * exp(-x)"), lty=1:3, col=c("black","tomato","blue2"), bty="n") axis(2, pos=0, at = c(3.5, 1.5), labels = c("A1","A2"), las=2) ## and how you could have got their sum via SSbiexp(): ySS <- SSbiexp(xx, 3.5, log(4), 1.5, log(1)) ## --- --- stopifnot(all.equal(y1+y2, ySS, tolerance = 1e-15)) ## Show a no-noise example datN <- data.frame(time = (0:600)/64) datN$conc <- predict(fm1, newdata=datN) plot(conc ~ time, data=datN) # perfect, no noise ## Fails by default (scaleOffset=0) on most platforms {also after increasing maxiter !} ## Not run: nls(conc ~ SSbiexp(time, A1, lrc1, A2, lrc2), data = datN, trace=TRUE) ## End(Not run) fmX1 <- nls(conc ~ SSbiexp(time, A1, lrc1, A2, lrc2), data = datN, control = list(scaleOffset=1)) fmX <- nls(conc ~ SSbiexp(time, A1, lrc1, A2, lrc2), data = datN, control = list(scaleOffset=1, printEval=TRUE, tol=1e-11, nDcentral=TRUE), trace=TRUE) all.equal(coef(fm1), coef(fmX1), tolerance=0) # ... rel.diff.: 1.57e-6 all.equal(coef(fm1), coef(fmX), tolerance=0) # ... rel.diff.: 1.03e-12 stopifnot(all.equal(coef(fm1), coef(fmX1), tolerance = 6e-6), all.equal(coef(fm1), coef(fmX ), tolerance = 1e-11))
Functions to compute matrix of residual sums of squares and products, or the estimated variance matrix for multivariate linear models.
# S3 method for class 'mlm' SSD(object, ...) # S3 methods for class 'SSD' and 'mlm' estVar(object, ...)
# S3 method for class 'mlm' SSD(object, ...) # S3 methods for class 'SSD' and 'mlm' estVar(object, ...)
object |
|
... |
Unused |
SSD()
returns a list of class "SSD"
containing the
following components
SSD |
The residual sums of squares and products matrix |
df |
Degrees of freedom |
call |
Copied from |
estVar
returns a matrix with the estimated variances and
covariances.
# Lifted from Baron+Li: # "Notes on the use of R for psychology experiments and questionnaires" # Maxwell and Delaney, p. 497 reacttime <- matrix(c( 420, 420, 480, 480, 600, 780, 420, 480, 480, 360, 480, 600, 480, 480, 540, 660, 780, 780, 420, 540, 540, 480, 780, 900, 540, 660, 540, 480, 660, 720, 360, 420, 360, 360, 480, 540, 480, 480, 600, 540, 720, 840, 480, 600, 660, 540, 720, 900, 540, 600, 540, 480, 720, 780, 480, 420, 540, 540, 660, 780), ncol = 6, byrow = TRUE, dimnames = list(subj = 1:10, cond = c("deg0NA", "deg4NA", "deg8NA", "deg0NP", "deg4NP", "deg8NP"))) mlmfit <- lm(reacttime ~ 1) SSD(mlmfit) estVar(mlmfit)
# Lifted from Baron+Li: # "Notes on the use of R for psychology experiments and questionnaires" # Maxwell and Delaney, p. 497 reacttime <- matrix(c( 420, 420, 480, 480, 600, 780, 420, 480, 480, 360, 480, 600, 480, 480, 540, 660, 780, 780, 420, 540, 540, 480, 780, 900, 540, 660, 540, 480, 660, 720, 360, 420, 360, 360, 480, 540, 480, 480, 600, 540, 720, 840, 480, 600, 660, 540, 720, 900, 540, 600, 540, 480, 720, 780, 480, 420, 540, 540, 660, 780), ncol = 6, byrow = TRUE, dimnames = list(subj = 1:10, cond = c("deg0NA", "deg4NA", "deg8NA", "deg0NP", "deg4NP", "deg8NP"))) mlmfit <- lm(reacttime ~ 1) SSD(mlmfit) estVar(mlmfit)
nls
First-order Compartment ModelThis selfStart
model evaluates the first-order compartment
function and its gradient. It has an initial
attribute that
creates initial estimates of the parameters lKe
, lKa
,
and lCl
.
SSfol(Dose, input, lKe, lKa, lCl)
SSfol(Dose, input, lKe, lKa, lCl)
Dose |
a numeric value representing the initial dose. |
input |
a numeric vector at which to evaluate the model. |
lKe |
a numeric parameter representing the natural logarithm of the elimination rate constant. |
lKa |
a numeric parameter representing the natural logarithm of the absorption rate constant. |
lCl |
a numeric parameter representing the natural logarithm of the clearance. |
a numeric vector of the same length as input
, which is the
value of the expression
Dose * exp(lKe+lKa-lCl) * (exp(-exp(lKe)*input) - exp(-exp(lKa)*input)) / (exp(lKa) - exp(lKe))
If all of the arguments lKe
, lKa
, and lCl
are
names of objects, the gradient matrix with respect to these names is
attached as an attribute named gradient
.
José Pinheiro and Douglas Bates
Theoph.1 <- Theoph[ Theoph$Subject == 1, ] with(Theoph.1, SSfol(Dose, Time, -2.5, 0.5, -3)) # response only with(Theoph.1, local({ lKe <- -2.5; lKa <- 0.5; lCl <- -3 SSfol(Dose, Time, lKe, lKa, lCl) # response _and_ gradient })) getInitial(conc ~ SSfol(Dose, Time, lKe, lKa, lCl), data = Theoph.1) ## Initial values are in fact the converged values fm1 <- nls(conc ~ SSfol(Dose, Time, lKe, lKa, lCl), data = Theoph.1) summary(fm1)
Theoph.1 <- Theoph[ Theoph$Subject == 1, ] with(Theoph.1, SSfol(Dose, Time, -2.5, 0.5, -3)) # response only with(Theoph.1, local({ lKe <- -2.5; lKa <- 0.5; lCl <- -3 SSfol(Dose, Time, lKe, lKa, lCl) # response _and_ gradient })) getInitial(conc ~ SSfol(Dose, Time, lKe, lKa, lCl), data = Theoph.1) ## Initial values are in fact the converged values fm1 <- nls(conc ~ SSfol(Dose, Time, lKe, lKa, lCl), data = Theoph.1) summary(fm1)
nls
Four-Parameter Logistic ModelThis selfStart
model evaluates the four-parameter logistic
function and its gradient. It has an initial
attribute computing
initial estimates of the parameters A
, B
,
xmid
, and scal
for a given set of data.
SSfpl(input, A, B, xmid, scal)
SSfpl(input, A, B, xmid, scal)
input |
a numeric vector of values at which to evaluate the model. |
A |
a numeric parameter representing the horizontal asymptote on
the left side (very small values of |
B |
a numeric parameter representing the horizontal asymptote on
the right side (very large values of |
xmid |
a numeric parameter representing the |
scal |
a numeric scale parameter on the |
a numeric vector of the same length as input
. It is the value of
the expression A+(B-A)/(1+exp((xmid-input)/scal))
. If all of
the arguments A
, B
, xmid
, and scal
are
names of objects, the gradient matrix with respect to these names is
attached as an attribute named gradient
.
José Pinheiro and Douglas Bates
Chick.1 <- ChickWeight[ChickWeight$Chick == 1, ] SSfpl(Chick.1$Time, 13, 368, 14, 6) # response only local({ A <- 13; B <- 368; xmid <- 14; scal <- 6 SSfpl(Chick.1$Time, A, B, xmid, scal) # response _and_ gradient }) print(getInitial(weight ~ SSfpl(Time, A, B, xmid, scal), data = Chick.1), digits = 5) ## Initial values are in fact the converged values fm1 <- nls(weight ~ SSfpl(Time, A, B, xmid, scal), data = Chick.1) summary(fm1) ## Visualizing the SSfpl() parametrization xx <- seq(-0.5, 5, length.out = 101) yy <- 1 + 4 / (1 + exp((2-xx))) # == SSfpl(xx, *) : stopifnot( all.equal(yy, SSfpl(xx, A = 1, B = 5, xmid = 2, scal = 1)) ) require(graphics) op <- par(mar = c(0, 0, 3.5, 0)) plot(xx, yy, type = "l", axes = FALSE, ylim = c(0,6), xlim = c(-1, 5), xlab = "", ylab = "", lwd = 2, main = "Parameters in the SSfpl model") mtext(quote(list(phi[1] == "A", phi[2] == "B", phi[3] == "xmid", phi[4] == "scal"))) usr <- par("usr") arrows(usr[1], 0, usr[2], 0, length = 0.1, angle = 25) arrows(0, usr[3], 0, usr[4], length = 0.1, angle = 25) text(usr[2] - 0.2, 0.1, "x", adj = c(1, 0)) text( -0.1, usr[4], "y", adj = c(1, 1)) abline(h = c(1, 5), lty = 3) arrows(-0.8, c(2.1, 2.9), -0.8, c(0, 5 ), length = 0.1, angle = 25) text (-0.8, 2.5, quote(phi[1])) arrows(-0.3, c(1/4, 3/4), -0.3, c(0, 1 ), length = 0.07, angle = 25) text (-0.3, 0.5, quote(phi[2])) text(2, -.1, quote(phi[3])) segments(c(2,3,3), c(0,3,4), # SSfpl(x = xmid = 2) = 3 c(2,3,2), c(3,4,3), lty = 2, lwd = 0.75) arrows(c(2.3, 2.7), 3, c(2.0, 3 ), 3, length = 0.08, angle = 25) text( 2.5, 3, quote(phi[4])); text(3.1, 3.5, "1") par(op)
Chick.1 <- ChickWeight[ChickWeight$Chick == 1, ] SSfpl(Chick.1$Time, 13, 368, 14, 6) # response only local({ A <- 13; B <- 368; xmid <- 14; scal <- 6 SSfpl(Chick.1$Time, A, B, xmid, scal) # response _and_ gradient }) print(getInitial(weight ~ SSfpl(Time, A, B, xmid, scal), data = Chick.1), digits = 5) ## Initial values are in fact the converged values fm1 <- nls(weight ~ SSfpl(Time, A, B, xmid, scal), data = Chick.1) summary(fm1) ## Visualizing the SSfpl() parametrization xx <- seq(-0.5, 5, length.out = 101) yy <- 1 + 4 / (1 + exp((2-xx))) # == SSfpl(xx, *) : stopifnot( all.equal(yy, SSfpl(xx, A = 1, B = 5, xmid = 2, scal = 1)) ) require(graphics) op <- par(mar = c(0, 0, 3.5, 0)) plot(xx, yy, type = "l", axes = FALSE, ylim = c(0,6), xlim = c(-1, 5), xlab = "", ylab = "", lwd = 2, main = "Parameters in the SSfpl model") mtext(quote(list(phi[1] == "A", phi[2] == "B", phi[3] == "xmid", phi[4] == "scal"))) usr <- par("usr") arrows(usr[1], 0, usr[2], 0, length = 0.1, angle = 25) arrows(0, usr[3], 0, usr[4], length = 0.1, angle = 25) text(usr[2] - 0.2, 0.1, "x", adj = c(1, 0)) text( -0.1, usr[4], "y", adj = c(1, 1)) abline(h = c(1, 5), lty = 3) arrows(-0.8, c(2.1, 2.9), -0.8, c(0, 5 ), length = 0.1, angle = 25) text (-0.8, 2.5, quote(phi[1])) arrows(-0.3, c(1/4, 3/4), -0.3, c(0, 1 ), length = 0.07, angle = 25) text (-0.3, 0.5, quote(phi[2])) text(2, -.1, quote(phi[3])) segments(c(2,3,3), c(0,3,4), # SSfpl(x = xmid = 2) = 3 c(2,3,2), c(3,4,3), lty = 2, lwd = 0.75) arrows(c(2.3, 2.7), 3, c(2.0, 3 ), 3, length = 0.08, angle = 25) text( 2.5, 3, quote(phi[4])); text(3.1, 3.5, "1") par(op)
nls
Gompertz Growth ModelThis selfStart
model evaluates the Gompertz growth model
and its gradient. It has an initial
attribute that
creates initial estimates of the parameters Asym
,
b2
, and b3
.
SSgompertz(x, Asym, b2, b3)
SSgompertz(x, Asym, b2, b3)
x |
a numeric vector of values at which to evaluate the model. |
Asym |
a numeric parameter representing the asymptote. |
b2 |
a numeric parameter related to the value of the function at
|
b3 |
a numeric parameter related to the scale the |
a numeric vector of the same length as input
. It is the value of
the expression Asym*exp(-b2*b3^x)
. If all of
the arguments Asym
, b2
, and b3
are
names of objects the gradient matrix with respect to these names is attached as
an attribute named gradient
.
Douglas Bates
DNase.1 <- subset(DNase, Run == 1) SSgompertz(log(DNase.1$conc), 4.5, 2.3, 0.7) # response only local({ Asym <- 4.5; b2 <- 2.3; b3 <- 0.7 SSgompertz(log(DNase.1$conc), Asym, b2, b3) # response _and_ gradient }) print(getInitial(density ~ SSgompertz(log(conc), Asym, b2, b3), data = DNase.1), digits = 5) ## Initial values are in fact the converged values fm1 <- nls(density ~ SSgompertz(log(conc), Asym, b2, b3), data = DNase.1) summary(fm1) plot(density ~ log(conc), DNase.1, # xlim = c(0, 21), main = "SSgompertz() fit to DNase.1") ux <- par("usr")[1:2]; x <- seq(ux[1], ux[2], length.out=250) lines(x, do.call(SSgompertz, c(list(x=x), coef(fm1))), col = "red", lwd=2) As <- coef(fm1)[["Asym"]]; abline(v = 0, h = 0, lty = 3) axis(2, at= exp(-coef(fm1)[["b2"]]), quote(e^{-b[2]}), las=1, pos=0)
DNase.1 <- subset(DNase, Run == 1) SSgompertz(log(DNase.1$conc), 4.5, 2.3, 0.7) # response only local({ Asym <- 4.5; b2 <- 2.3; b3 <- 0.7 SSgompertz(log(DNase.1$conc), Asym, b2, b3) # response _and_ gradient }) print(getInitial(density ~ SSgompertz(log(conc), Asym, b2, b3), data = DNase.1), digits = 5) ## Initial values are in fact the converged values fm1 <- nls(density ~ SSgompertz(log(conc), Asym, b2, b3), data = DNase.1) summary(fm1) plot(density ~ log(conc), DNase.1, # xlim = c(0, 21), main = "SSgompertz() fit to DNase.1") ux <- par("usr")[1:2]; x <- seq(ux[1], ux[2], length.out=250) lines(x, do.call(SSgompertz, c(list(x=x), coef(fm1))), col = "red", lwd=2) As <- coef(fm1)[["Asym"]]; abline(v = 0, h = 0, lty = 3) axis(2, at= exp(-coef(fm1)[["b2"]]), quote(e^{-b[2]}), las=1, pos=0)
nls
Logistic ModelThis selfStart
model evaluates the logistic
function and its gradient. It has an initial
attribute that
creates initial estimates of the parameters Asym
,
xmid
, and scal
. In R 3.4.2 and earlier, that
init function failed when min(input)
was exactly zero.
SSlogis(input, Asym, xmid, scal)
SSlogis(input, Asym, xmid, scal)
input |
a numeric vector of values at which to evaluate the model. |
Asym |
a numeric parameter representing the asymptote. |
xmid |
a numeric parameter representing the |
scal |
a numeric scale parameter on the |
a numeric vector of the same length as input
. It is the value of
the expression Asym/(1+exp((xmid-input)/scal))
. If all of
the arguments Asym
, xmid
, and scal
are
names of objects the gradient matrix with respect to these names is
attached as an attribute named gradient
.
José Pinheiro and Douglas Bates
Chick.1 <- ChickWeight[ChickWeight$Chick == 1, ] SSlogis(Chick.1$Time, 368, 14, 6) # response only local({ Asym <- 368; xmid <- 14; scal <- 6 SSlogis(Chick.1$Time, Asym, xmid, scal) # response _and_ gradient }) getInitial(weight ~ SSlogis(Time, Asym, xmid, scal), data = Chick.1) ## Initial values are in fact the converged one here, "Number of iter...: 0" : fm1 <- nls(weight ~ SSlogis(Time, Asym, xmid, scal), data = Chick.1) summary(fm1) ## but are slightly improved here: fm2 <- update(fm1, control=nls.control(tol = 1e-9, warnOnly=TRUE), trace = TRUE) all.equal(coef(fm1), coef(fm2)) # "Mean relative difference: 9.6e-6" str(fm2$convInfo) # 3 iterations dwlg1 <- data.frame(Prop = c(rep(0,5), 2, 5, rep(9, 9)), end = 1:16) iPar <- getInitial(Prop ~ SSlogis(end, Asym, xmid, scal), data = dwlg1) ## failed in R <= 3.4.2 (because of the '0's in 'Prop') stopifnot(all.equal(tolerance = 1e-6, iPar, c(Asym = 9.0678, xmid = 6.79331, scal = 0.499934))) ## Visualize the SSlogis() model parametrization : xx <- seq(-0.75, 5, by=1/32) yy <- 5 / (1 + exp((2-xx)/0.6)) # == SSlogis(xx, *): stopifnot( all.equal(yy, SSlogis(xx, Asym = 5, xmid = 2, scal = 0.6)) ) require(graphics) op <- par(mar = c(0.5, 0, 3.5, 0)) plot(xx, yy, type = "l", axes = FALSE, ylim = c(0,6), xlim = c(-1, 5), xlab = "", ylab = "", lwd = 2, main = "Parameters in the SSlogis model") mtext(quote(list(phi[1] == "Asym", phi[2] == "xmid", phi[3] == "scal"))) usr <- par("usr") arrows(usr[1], 0, usr[2], 0, length = 0.1, angle = 25) arrows(0, usr[3], 0, usr[4], length = 0.1, angle = 25) text(usr[2] - 0.2, 0.1, "x", adj = c(1, 0)) text( -0.1, usr[4], "y", adj = c(1, 1)) abline(h = 5, lty = 3) arrows(-0.8, c(2.1, 2.9), -0.8, c(0, 5 ), length = 0.1, angle = 25) text (-0.8, 2.5, quote(phi[1])) segments(c(2,2.6,2.6), c(0, 2.5,3.5), # NB. SSlogis(x = xmid = 2) = 2.5 c(2,2.6,2 ), c(2.5,3.5,2.5), lty = 2, lwd = 0.75) text(2, -.1, quote(phi[2])) arrows(c(2.2, 2.4), 2.5, c(2.0, 2.6), 2.5, length = 0.08, angle = 25) text( 2.3, 2.5, quote(phi[3])); text(2.7, 3, "1") par(op)
Chick.1 <- ChickWeight[ChickWeight$Chick == 1, ] SSlogis(Chick.1$Time, 368, 14, 6) # response only local({ Asym <- 368; xmid <- 14; scal <- 6 SSlogis(Chick.1$Time, Asym, xmid, scal) # response _and_ gradient }) getInitial(weight ~ SSlogis(Time, Asym, xmid, scal), data = Chick.1) ## Initial values are in fact the converged one here, "Number of iter...: 0" : fm1 <- nls(weight ~ SSlogis(Time, Asym, xmid, scal), data = Chick.1) summary(fm1) ## but are slightly improved here: fm2 <- update(fm1, control=nls.control(tol = 1e-9, warnOnly=TRUE), trace = TRUE) all.equal(coef(fm1), coef(fm2)) # "Mean relative difference: 9.6e-6" str(fm2$convInfo) # 3 iterations dwlg1 <- data.frame(Prop = c(rep(0,5), 2, 5, rep(9, 9)), end = 1:16) iPar <- getInitial(Prop ~ SSlogis(end, Asym, xmid, scal), data = dwlg1) ## failed in R <= 3.4.2 (because of the '0's in 'Prop') stopifnot(all.equal(tolerance = 1e-6, iPar, c(Asym = 9.0678, xmid = 6.79331, scal = 0.499934))) ## Visualize the SSlogis() model parametrization : xx <- seq(-0.75, 5, by=1/32) yy <- 5 / (1 + exp((2-xx)/0.6)) # == SSlogis(xx, *): stopifnot( all.equal(yy, SSlogis(xx, Asym = 5, xmid = 2, scal = 0.6)) ) require(graphics) op <- par(mar = c(0.5, 0, 3.5, 0)) plot(xx, yy, type = "l", axes = FALSE, ylim = c(0,6), xlim = c(-1, 5), xlab = "", ylab = "", lwd = 2, main = "Parameters in the SSlogis model") mtext(quote(list(phi[1] == "Asym", phi[2] == "xmid", phi[3] == "scal"))) usr <- par("usr") arrows(usr[1], 0, usr[2], 0, length = 0.1, angle = 25) arrows(0, usr[3], 0, usr[4], length = 0.1, angle = 25) text(usr[2] - 0.2, 0.1, "x", adj = c(1, 0)) text( -0.1, usr[4], "y", adj = c(1, 1)) abline(h = 5, lty = 3) arrows(-0.8, c(2.1, 2.9), -0.8, c(0, 5 ), length = 0.1, angle = 25) text (-0.8, 2.5, quote(phi[1])) segments(c(2,2.6,2.6), c(0, 2.5,3.5), # NB. SSlogis(x = xmid = 2) = 2.5 c(2,2.6,2 ), c(2.5,3.5,2.5), lty = 2, lwd = 0.75) text(2, -.1, quote(phi[2])) arrows(c(2.2, 2.4), 2.5, c(2.0, 2.6), 2.5, length = 0.08, angle = 25) text( 2.3, 2.5, quote(phi[3])); text(2.7, 3, "1") par(op)
nls
Michaelis-Menten ModelThis selfStart
model evaluates the Michaelis-Menten model and
its gradient. It has an initial
attribute that
will evaluate initial estimates of the parameters Vm
and K
SSmicmen(input, Vm, K)
SSmicmen(input, Vm, K)
input |
a numeric vector of values at which to evaluate the model. |
Vm |
a numeric parameter representing the maximum value of the response. |
K |
a numeric parameter representing the |
a numeric vector of the same length as input
. It is the value of
the expression Vm*input/(K+input)
. If both
the arguments Vm
and K
are
names of objects, the gradient matrix with respect to these names is
attached as an attribute named gradient
.
José Pinheiro and Douglas Bates
PurTrt <- Puromycin[ Puromycin$state == "treated", ] SSmicmen(PurTrt$conc, 200, 0.05) # response only local({ Vm <- 200; K <- 0.05 SSmicmen(PurTrt$conc, Vm, K) # response _and_ gradient }) print(getInitial(rate ~ SSmicmen(conc, Vm, K), data = PurTrt), digits = 3) ## Initial values are in fact the converged values fm1 <- nls(rate ~ SSmicmen(conc, Vm, K), data = PurTrt) summary(fm1) ## Alternative call using the subset argument fm2 <- nls(rate ~ SSmicmen(conc, Vm, K), data = Puromycin, subset = state == "treated") summary(fm2) # The same indeed: stopifnot(all.equal(coef(summary(fm1)), coef(summary(fm2)))) ## Visualize the SSmicmen() Michaelis-Menton model parametrization : xx <- seq(0, 5, length.out = 101) yy <- 5 * xx/(1+xx) stopifnot(all.equal(yy, SSmicmen(xx, Vm = 5, K = 1))) require(graphics) op <- par(mar = c(0, 0, 3.5, 0)) plot(xx, yy, type = "l", lwd = 2, ylim = c(-1/4,6), xlim = c(-1, 5), ann = FALSE, axes = FALSE, main = "Parameters in the SSmicmen model") mtext(quote(list(phi[1] == "Vm", phi[2] == "K"))) usr <- par("usr") arrows(usr[1], 0, usr[2], 0, length = 0.1, angle = 25) arrows(0, usr[3], 0, usr[4], length = 0.1, angle = 25) text(usr[2] - 0.2, 0.1, "x", adj = c(1, 0)) text( -0.1, usr[4], "y", adj = c(1, 1)) abline(h = 5, lty = 3) arrows(-0.8, c(2.1, 2.9), -0.8, c(0, 5 ), length = 0.1, angle = 25) text( -0.8, 2.5, quote(phi[1])) segments(1, 0, 1, 2.7, lty = 2, lwd = 0.75) text(1, 2.7, quote(phi[2])) par(op)
PurTrt <- Puromycin[ Puromycin$state == "treated", ] SSmicmen(PurTrt$conc, 200, 0.05) # response only local({ Vm <- 200; K <- 0.05 SSmicmen(PurTrt$conc, Vm, K) # response _and_ gradient }) print(getInitial(rate ~ SSmicmen(conc, Vm, K), data = PurTrt), digits = 3) ## Initial values are in fact the converged values fm1 <- nls(rate ~ SSmicmen(conc, Vm, K), data = PurTrt) summary(fm1) ## Alternative call using the subset argument fm2 <- nls(rate ~ SSmicmen(conc, Vm, K), data = Puromycin, subset = state == "treated") summary(fm2) # The same indeed: stopifnot(all.equal(coef(summary(fm1)), coef(summary(fm2)))) ## Visualize the SSmicmen() Michaelis-Menton model parametrization : xx <- seq(0, 5, length.out = 101) yy <- 5 * xx/(1+xx) stopifnot(all.equal(yy, SSmicmen(xx, Vm = 5, K = 1))) require(graphics) op <- par(mar = c(0, 0, 3.5, 0)) plot(xx, yy, type = "l", lwd = 2, ylim = c(-1/4,6), xlim = c(-1, 5), ann = FALSE, axes = FALSE, main = "Parameters in the SSmicmen model") mtext(quote(list(phi[1] == "Vm", phi[2] == "K"))) usr <- par("usr") arrows(usr[1], 0, usr[2], 0, length = 0.1, angle = 25) arrows(0, usr[3], 0, usr[4], length = 0.1, angle = 25) text(usr[2] - 0.2, 0.1, "x", adj = c(1, 0)) text( -0.1, usr[4], "y", adj = c(1, 1)) abline(h = 5, lty = 3) arrows(-0.8, c(2.1, 2.9), -0.8, c(0, 5 ), length = 0.1, angle = 25) text( -0.8, 2.5, quote(phi[1])) segments(1, 0, 1, 2.7, lty = 2, lwd = 0.75) text(1, 2.7, quote(phi[2])) par(op)
nls
Weibull Growth Curve ModelThis selfStart
model evaluates the Weibull model for growth
curve data and its gradient. It has an initial
attribute that
will evaluate initial estimates of the parameters Asym
, Drop
,
lrc
, and pwr
for a given set of data.
SSweibull(x, Asym, Drop, lrc, pwr)
SSweibull(x, Asym, Drop, lrc, pwr)
x |
a numeric vector of values at which to evaluate the model. |
Asym |
a numeric parameter representing the horizontal asymptote on
the right side (very small values of |
Drop |
a numeric parameter representing the change from
|
lrc |
a numeric parameter representing the natural logarithm of the rate constant. |
pwr |
a numeric parameter representing the power to which |
This model is a generalization of the SSasymp
model in
that it reduces to SSasymp
when pwr
is unity.
a numeric vector of the same length as x
. It is the value of
the expression Asym-Drop*exp(-exp(lrc)*x^pwr)
. If all of
the arguments Asym
, Drop
, lrc
, and pwr
are
names of objects, the gradient matrix with respect to these names is
attached as an attribute named gradient
.
Douglas Bates
Ratkowsky, David A. (1983), Nonlinear Regression Modeling, Dekker. (section 4.4.5)
Chick.6 <- subset(ChickWeight, (Chick == 6) & (Time > 0)) SSweibull(Chick.6$Time, 160, 115, -5.5, 2.5) # response only local({ Asym <- 160; Drop <- 115; lrc <- -5.5; pwr <- 2.5 SSweibull(Chick.6$Time, Asym, Drop, lrc, pwr) # response _and_ gradient }) getInitial(weight ~ SSweibull(Time, Asym, Drop, lrc, pwr), data = Chick.6) ## Initial values are in fact the converged values fm1 <- nls(weight ~ SSweibull(Time, Asym, Drop, lrc, pwr), data = Chick.6) summary(fm1) ## Data and Fit: plot(weight ~ Time, Chick.6, xlim = c(0, 21), main = "SSweibull() fit to Chick.6") ux <- par("usr")[1:2]; x <- seq(ux[1], ux[2], length.out=250) lines(x, do.call(SSweibull, c(list(x=x), coef(fm1))), col = "red", lwd=2) As <- coef(fm1)[["Asym"]]; abline(v = 0, h = c(As, As - coef(fm1)[["Drop"]]), lty = 3)
Chick.6 <- subset(ChickWeight, (Chick == 6) & (Time > 0)) SSweibull(Chick.6$Time, 160, 115, -5.5, 2.5) # response only local({ Asym <- 160; Drop <- 115; lrc <- -5.5; pwr <- 2.5 SSweibull(Chick.6$Time, Asym, Drop, lrc, pwr) # response _and_ gradient }) getInitial(weight ~ SSweibull(Time, Asym, Drop, lrc, pwr), data = Chick.6) ## Initial values are in fact the converged values fm1 <- nls(weight ~ SSweibull(Time, Asym, Drop, lrc, pwr), data = Chick.6) summary(fm1) ## Data and Fit: plot(weight ~ Time, Chick.6, xlim = c(0, 21), main = "SSweibull() fit to Chick.6") ux <- par("usr")[1:2]; x <- seq(ux[1], ux[2], length.out=250) lines(x, do.call(SSweibull, c(list(x=x), coef(fm1))), col = "red", lwd=2) As <- coef(fm1)[["Asym"]]; abline(v = 0, h = c(As, As - coef(fm1)[["Drop"]]), lty = 3)
Extract and encode the times the first and last observations were taken. Provided only for compatibility with S version 2.
start(x, ...) end(x, ...)
start(x, ...) end(x, ...)
x |
a univariate or multivariate time-series, or a vector or matrix. |
... |
extra arguments for future methods. |
These are generic functions, which will use the
tsp
attribute of x
if it exists.
Their default methods decode the start time from the original time
units, so that for a monthly series 1995.5
is represented
as c(1995, 7)
. For a series of frequency f
, time
n+i/f
is presented as c(n, i+1)
(even for i = 0
and f = 1
).
The representation used by start
and end
has no
meaning unless the frequency is supplied.
This is a utility function, used in lm
and
glm
methods for anova(..., test != NULL)
and should not be used by the average user.
stat.anova(table, test = c("Rao","LRT", "Chisq", "F", "Cp"), scale, df.scale, n)
stat.anova(table, test = c("Rao","LRT", "Chisq", "F", "Cp"), scale, df.scale, n)
table |
numeric matrix as results from
|
test |
a character string, partially matching one of |
scale |
a residual mean square or other scale estimate to be used as the denominator in an F test. |
df.scale |
degrees of freedom corresponding to |
n |
number of observations. |
A matrix which is the original table
, augmented by a column
of test statistics, depending on the test
argument.
Hastie, T. J. and Pregibon, D. (1992) Generalized linear models. Chapter 6 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
##-- Continued from '?glm': print(ag <- anova(glm.D93)) stat.anova(ag$table, test = "Cp", scale = sum(resid(glm.D93, "pearson")^2)/4, df.scale = 4, n = 9)
##-- Continued from '?glm': print(ag <- anova(glm.D93)) stat.anova(ag$table, test = "Cp", scale = sum(resid(glm.D93, "pearson")^2)/4, df.scale = 4, n = 9)
These functions are provided for compatibility with older versions of R only, and may be defunct as soon as the next release.
There are currently no deprecated functions in this package.
Select a formula-based model by AIC.
step(object, scope, scale = 0, direction = c("both", "backward", "forward"), trace = 1, keep = NULL, steps = 1000, k = 2, ...)
step(object, scope, scale = 0, direction = c("both", "backward", "forward"), trace = 1, keep = NULL, steps = 1000, k = 2, ...)
object |
an object representing a model of an appropriate class (mainly
|
scope |
defines the range of models examined in the stepwise search.
This should be either a single formula, or a list containing
components |
scale |
used in the definition of the AIC statistic for selecting the models,
currently only for |
direction |
the mode of stepwise search, can be one of |
trace |
if positive, information is printed during the running of |
keep |
a filter function whose input is a fitted model object and the
associated |
steps |
the maximum number of steps to be considered. The default is 1000 (essentially as many as required). It is typically used to stop the process early. |
k |
the multiple of the number of degrees of freedom used for the penalty.
Only |
... |
any additional arguments to |
step
uses add1
and drop1
repeatedly; it will work for any method for which they work, and that
is determined by having a valid method for extractAIC
.
When the additive constant can be chosen so that AIC is equal to
Mallows' , this is done and the tables are labelled
appropriately.
The set of models searched is determined by the scope
argument.
The right-hand-side of its lower
component is always included
in the model, and right-hand-side of the model is included in the
upper
component. If scope
is a single formula, it
specifies the upper
component, and the lower
model is
empty. If scope
is missing, the initial model is used as the
upper
model.
Models specified by scope
can be templates to update
object
as used by update.formula
. So using
.
in a scope
formula means ‘what is
already there’, with .^2
indicating all interactions of
existing terms.
There is a potential problem in using glm
fits with a
variable scale
, as in that case the deviance is not simply
related to the maximized log-likelihood. The "glm"
method for
function extractAIC
makes the
appropriate adjustment for a gaussian
family, but may need to be
amended for other cases. (The binomial
and poisson
families have fixed scale
by default and do not correspond
to a particular maximum-likelihood problem for variable scale
.)
the stepwise-selected model is returned, with up to two additional
components. There is an "anova"
component corresponding to the
steps taken in the search, as well as a "keep"
component if the
keep=
argument was supplied in the call. The
"Resid. Dev"
column of the analysis of deviance table refers
to a constant minus twice the maximized log likelihood: it will be a
deviance only in cases where a saturated model is well-defined
(thus excluding lm
, aov
and survreg
fits,
for example).
The model fitting must apply the models to the same dataset. This
may be a problem if there are missing values and R's default of
na.action = na.omit
is used. We suggest you remove the
missing values first.
Calls to the function nobs
are used to check that the
number of observations involved in the fitting process remains unchanged.
This function differs considerably from the function in S, which uses a number of approximations and does not in general compute the correct AIC.
This is a minimal implementation. Use stepAIC
in package MASS for a wider range of object classes.
B. D. Ripley: step
is a slightly simplified version of
stepAIC
in package MASS (Venables &
Ripley, 2002 and earlier editions).
The idea of a step
function follows that described in Hastie &
Pregibon (1992); but the implementation in R is more general.
Hastie, T. J. and Pregibon, D. (1992) Generalized linear models. Chapter 6 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
Venables, W. N. and Ripley, B. D. (2002) Modern Applied Statistics with S. New York: Springer (4th ed).
## following on from example(lm) step(lm.D9) summary(lm1 <- lm(Fertility ~ ., data = swiss)) slm1 <- step(lm1) summary(slm1) slm1$anova
## following on from example(lm) step(lm.D9) summary(lm1 <- lm(Fertility ~ ., data = swiss)) slm1 <- step(lm1) summary(slm1) slm1$anova
Given the vectors and
(one value
more!),
stepfun(x, y, ...)
returns an interpolating
‘step’ function, say fn
. I.e., (constant) for
and at the abscissa values, if (by default)
right = FALSE
, and for
right = TRUE
, , for
.
The value of the constant above depends on the
‘continuity’ parameter
f
.
For the default, right = FALSE, f = 0
,
fn
is a cadlag function, i.e., continuous from the right,
limits from the left, so that the function is piecewise constant on
intervals that include their left endpoint.
In general, is interpolated in between the
neighbouring
values,
.
Therefore, for non-0 values of
f
, fn
may no longer be a proper
step function, since it can be discontinuous from both sides, unless
right = TRUE, f = 1
which is left-continuous (i.e., constant
pieces contain their right endpoint).
stepfun(x, y, f = as.numeric(right), ties = "ordered", right = FALSE) is.stepfun(x) knots(Fn, ...) as.stepfun(x, ...) ## S3 method for class 'stepfun' print(x, digits = getOption("digits") - 2, ...) ## S3 method for class 'stepfun' summary(object, ...)
stepfun(x, y, f = as.numeric(right), ties = "ordered", right = FALSE) is.stepfun(x) knots(Fn, ...) as.stepfun(x, ...) ## S3 method for class 'stepfun' print(x, digits = getOption("digits") - 2, ...) ## S3 method for class 'stepfun' summary(object, ...)
x |
numeric vector giving the knots or jump locations of the step
function for |
y |
numeric vector one longer than |
f |
a number between 0 and 1, indicating how interpolation outside
the given x values should happen. See |
ties |
Handling of tied |
right |
logical, indicating if the intervals should be closed on the right (and open on the left) or vice versa. |
Fn , object
|
an R object inheriting from |
digits |
number of significant digits to use, see |
... |
potentially further arguments (required by the generic). |
A function of class "stepfun"
, say fn
.
There are methods available for summarizing ("summary(.)"
),
representing ("print(.)"
) and plotting ("plot(.)"
, see
plot.stepfun
) "stepfun"
objects.
The environment
of fn
contains all the
information needed;
"x" , "y"
|
the original arguments |
"n" |
number of knots (x values) |
"f" |
continuity parameter |
"yleft" , "yright"
|
the function values outside the knots |
"method" |
(always |
The knots are also available via knots(fn)
.
The objects of class "stepfun"
are not intended to be used for
permanent storage and may change structure between versions of R (and
did at R 3.0.0). They can usually be re-created by
eval(attr(old_obj, "call"), environment(old_obj))
since the data used is stored as part of the object's environment.
Martin Maechler, [email protected] with some basic code from Thomas Lumley.
ecdf
for empirical distribution functions as
special step functions and plot.stepfun
for plotting
step functions.
y0 <- c(1., 2., 4., 3.) sfun0 <- stepfun(1:3, y0, f = 0) sfun.2 <- stepfun(1:3, y0, f = 0.2) sfun1 <- stepfun(1:3, y0, f = 1) sfun1c <- stepfun(1:3, y0, right = TRUE) # hence f=1 sfun0 summary(sfun0) summary(sfun.2) ## look at the internal structure: unclass(sfun0) ls(envir = environment(sfun0)) x0 <- seq(0.5, 3.5, by = 0.25) rbind(x = x0, f.f0 = sfun0(x0), f.f02 = sfun.2(x0), f.f1 = sfun1(x0), f.f1c = sfun1c(x0)) ## Identities : stopifnot(identical(y0[-1], sfun0 (1:3)), # right = FALSE identical(y0[-4], sfun1c(1:3))) # right = TRUE
y0 <- c(1., 2., 4., 3.) sfun0 <- stepfun(1:3, y0, f = 0) sfun.2 <- stepfun(1:3, y0, f = 0.2) sfun1 <- stepfun(1:3, y0, f = 1) sfun1c <- stepfun(1:3, y0, right = TRUE) # hence f=1 sfun0 summary(sfun0) summary(sfun.2) ## look at the internal structure: unclass(sfun0) ls(envir = environment(sfun0)) x0 <- seq(0.5, 3.5, by = 0.25) rbind(x = x0, f.f0 = sfun0(x0), f.f02 = sfun.2(x0), f.f1 = sfun1(x0), f.f1c = sfun1c(x0)) ## Identities : stopifnot(identical(y0[-1], sfun0 (1:3)), # right = FALSE identical(y0[-4], sfun1c(1:3))) # right = TRUE
Decompose a time series into seasonal, trend and irregular components
using loess
, acronym STL.
stl(x, s.window, s.degree = 0, t.window = NULL, t.degree = 1, l.window = nextodd(period), l.degree = t.degree, s.jump = ceiling(s.window/10), t.jump = ceiling(t.window/10), l.jump = ceiling(l.window/10), robust = FALSE, inner = if(robust) 1 else 2, outer = if(robust) 15 else 0, na.action = na.fail)
stl(x, s.window, s.degree = 0, t.window = NULL, t.degree = 1, l.window = nextodd(period), l.degree = t.degree, s.jump = ceiling(s.window/10), t.jump = ceiling(t.window/10), l.jump = ceiling(l.window/10), robust = FALSE, inner = if(robust) 1 else 2, outer = if(robust) 15 else 0, na.action = na.fail)
x |
univariate time series to be decomposed.
This should be an object of class |
s.window |
either the character string |
s.degree |
degree of locally-fitted polynomial in seasonal extraction. Should be zero or one. |
t.window |
the span (in lags) of the loess window for trend
extraction, which should be odd. If |
t.degree |
degree of locally-fitted polynomial in trend extraction. Should be zero or one. |
l.window |
the span (in lags) of the loess window of the low-pass
filter used for each subseries. Defaults to the smallest odd
integer greater than or equal to |
l.degree |
degree of locally-fitted polynomial for the subseries low-pass filter. Must be 0 or 1. |
s.jump , t.jump , l.jump
|
integers at least one to increase speed of
the respective smoother. Linear interpolation happens between every
|
robust |
logical indicating if robust fitting be used in the
|
inner |
integer; the number of ‘inner’ (backfitting) iterations; usually very few (2) iterations suffice. |
outer |
integer; the number of ‘outer’ robustness iterations. |
na.action |
action on missing values. |
The seasonal component is found by loess smoothing the
seasonal sub-series (the series of all January values, ...); if
s.window = "periodic"
smoothing is effectively replaced by
taking the mean. The seasonal values are removed, and the remainder
smoothed to find the trend. The overall level is removed from the
seasonal component and added to the trend component. This process is
iterated a few times. The remainder
component is the
residuals from the seasonal plus trend fit.
Several methods for the resulting class "stl"
objects, see,
plot.stl
.
stl
returns an object of class "stl"
with components
time.series |
a multiple time series with columns
|
weights |
the final robust weights (all one if fitting is not done robustly). |
call |
the matched call. |
win |
integer (length 3 vector) with the spans used for the |
deg |
integer (length 3) vector with the polynomial degrees for these smoothers. |
jump |
integer (length 3) vector with the ‘jumps’ (skips) used for these smoothers. |
ni |
number of inner iterations |
no |
number of outer robustness iterations |
B.D. Ripley; Fortran code by Cleveland et al. (1990) from ‘netlib’.
R. B. Cleveland, W. S. Cleveland, J.E. McRae, and I. Terpenning (1990) STL: A Seasonal-Trend Decomposition Procedure Based on Loess. Journal of Official Statistics, 6, 3–73.
plot.stl
for stl
methods;
loess
in package stats (which is not actually
used in stl
).
StructTS
for different kind of decomposition.
require(graphics) plot(stl(nottem, "per")) plot(stl(nottem, s.window = 7, t.window = 50, t.jump = 1)) plot(stllc <- stl(log(co2), s.window = 21)) summary(stllc) ## linear trend, strict period. plot(stl(log(co2), s.window = "per", t.window = 1000)) ## Two STL plotted side by side : stmd <- stl(mdeaths, s.window = "per") # non-robust summary(stmR <- stl(mdeaths, s.window = "per", robust = TRUE)) op <- par(mar = c(0, 4, 0, 3), oma = c(5, 0, 4, 0), mfcol = c(4, 2)) plot(stmd, set.pars = NULL, labels = NULL, main = "stl(mdeaths, s.w = \"per\", robust = FALSE / TRUE )") plot(stmR, set.pars = NULL) # mark the 'outliers' : (iO <- which(stmR $ weights < 1e-8)) # 10 were considered outliers sts <- stmR$time.series points(time(sts)[iO], 0.8* sts[,"remainder"][iO], pch = 4, col = "red") par(op) # reset
require(graphics) plot(stl(nottem, "per")) plot(stl(nottem, s.window = 7, t.window = 50, t.jump = 1)) plot(stllc <- stl(log(co2), s.window = 21)) summary(stllc) ## linear trend, strict period. plot(stl(log(co2), s.window = "per", t.window = 1000)) ## Two STL plotted side by side : stmd <- stl(mdeaths, s.window = "per") # non-robust summary(stmR <- stl(mdeaths, s.window = "per", robust = TRUE)) op <- par(mar = c(0, 4, 0, 3), oma = c(5, 0, 4, 0), mfcol = c(4, 2)) plot(stmd, set.pars = NULL, labels = NULL, main = "stl(mdeaths, s.w = \"per\", robust = FALSE / TRUE )") plot(stmR, set.pars = NULL) # mark the 'outliers' : (iO <- which(stmR $ weights < 1e-8)) # 10 were considered outliers sts <- stmR$time.series points(time(sts)[iO], 0.8* sts[,"remainder"][iO], pch = 4, col = "red") par(op) # reset
Methods for objects of class stl
, typically the result of
stl
. The plot
method does a multiple figure plot
with some flexibility.
There are also (non-visible) print
and summary
methods.
## S3 method for class 'stl' plot(x, labels = colnames(X), set.pars = list(mar = c(0, 6, 0, 6), oma = c(6, 0, 4, 0), tck = -0.01, mfrow = c(nplot, 1)), main = NULL, range.bars = TRUE, ..., col.range = "light gray")
## S3 method for class 'stl' plot(x, labels = colnames(X), set.pars = list(mar = c(0, 6, 0, 6), oma = c(6, 0, 4, 0), tck = -0.01, mfrow = c(nplot, 1)), main = NULL, range.bars = TRUE, ..., col.range = "light gray")
x |
|
labels |
character of length 4 giving the names of the component time-series. |
set.pars |
settings for |
main |
plot main title. |
range.bars |
logical indicating if each plot should have a bar at its right side which are of equal heights in user coordinates. |
... |
further arguments passed to or from other methods. |
col.range |
colour to be used for the range bars, if plotted.
Note this appears after |
plot.ts
and stl
, particularly for
examples.
Fit a structural model for a time series by maximum likelihood.
StructTS(x, type = c("level", "trend", "BSM"), init = NULL, fixed = NULL, optim.control = NULL)
StructTS(x, type = c("level", "trend", "BSM"), init = NULL, fixed = NULL, optim.control = NULL)
x |
a univariate numeric time series. Missing values are allowed. |
type |
the class of structural model. If omitted, a BSM is used
for a time series with |
init |
initial values of the variance parameters. |
fixed |
optional numeric vector of the same length as the total
number of parameters. If supplied, only |
optim.control |
List of control parameters for
|
Structural time series models are (linear Gaussian) state-space models for (univariate) time series based on a decomposition of the series into a number of components. They are specified by a set of error variances, some of which may be zero.
The simplest model is the local level model specified by
type = "level"
. This has an underlying level which
evolves by
The observations are
There are two parameters,
and
. It is an ARIMA(0,1,1) model,
but with restrictions on the parameter set.
The local linear trend model, type = "trend"
, has the same
measurement equation, but with a time-varying slope in the dynamics for
, given by
with three variance parameters. It is not uncommon to find
(which reduces to the local
level model) or
, which ensures a
smooth trend. This is a restricted ARIMA(0,2,2) model.
The basic structural model, type = "BSM"
, is a local
trend model with an additional seasonal component. Thus the measurement
equation is
where is a seasonal component with dynamics
The boundary case corresponds
to a deterministic (but arbitrary) seasonal pattern. (This is
sometimes known as the ‘dummy variable’ version of the BSM.)
A list of class "StructTS"
with components:
coef |
the estimated variances of the components. |
loglik |
the maximized log-likelihood. Note that as all these
models are non-stationary this includes a diffuse prior for some
observations and hence is not comparable to |
loglik0 |
the maximized log-likelihood with the constant used prior to R 3.0.0, for backwards compatibility. |
data |
the time series |
residuals |
the standardized residuals. |
fitted |
a multiple time series with one component for the level,
slope and seasonal components, estimated contemporaneously (that is
at time |
call |
the matched call. |
series |
the name of the series |
code |
the |
model , model0
|
Lists representing the Kalman filter used in the
fitting. See |
xtsp |
the |
Optimization of structural models is a lot harder than many of the
references admit. For example, the AirPassengers
data
are considered in Brockwell & Davis (1996): their solution appears to
be a local maximum, but nowhere near as good a fit as that produced by
StructTS
. It is quite common to find fits with one or more
variances zero, and this can include .
Brockwell, P. J. & Davis, R. A. (1996). Introduction to Time Series and Forecasting. Springer, New York. Sections 8.2 and 8.5.
Durbin, J. and Koopman, S. J. (2001) Time Series Analysis by State Space Methods. Oxford University Press.
Harvey, A. C. (1989) Forecasting, Structural Time Series Models and the Kalman Filter. Cambridge University Press.
Harvey, A. C. (1993) Time Series Models. 2nd Edition, Harvester Wheatsheaf.
KalmanLike
, tsSmooth
;
stl
for different kind of (seasonal) decomposition.
## see also JohnsonJohnson, Nile and AirPassengers require(graphics) trees <- window(treering, start = 0) (fit <- StructTS(trees, type = "level")) plot(trees) lines(fitted(fit), col = "green") tsdiag(fit) (fit <- StructTS(log10(UKgas), type = "BSM")) par(mfrow = c(4, 1)) # to give appropriate aspect ratio for next plot. plot(log10(UKgas)) plot(cbind(fitted(fit), resids=resid(fit)), main = "UK gas consumption") ## keep some parameters fixed; trace optimizer: StructTS(log10(UKgas), type = "BSM", fixed = c(0.1,0.001,NA,NA), optim.control = list(trace = TRUE))
## see also JohnsonJohnson, Nile and AirPassengers require(graphics) trees <- window(treering, start = 0) (fit <- StructTS(trees, type = "level")) plot(trees) lines(fitted(fit), col = "green") tsdiag(fit) (fit <- StructTS(log10(UKgas), type = "BSM")) par(mfrow = c(4, 1)) # to give appropriate aspect ratio for next plot. plot(log10(UKgas)) plot(cbind(fitted(fit), resids=resid(fit)), main = "UK gas consumption") ## keep some parameters fixed; trace optimizer: StructTS(log10(UKgas), type = "BSM", fixed = c(0.1,0.001,NA,NA), optim.control = list(trace = TRUE))
Summarize an analysis of variance model.
## S3 method for class 'aov' summary(object, intercept = FALSE, split, expand.split = TRUE, keep.zero.df = TRUE, ...) ## S3 method for class 'aovlist' summary(object, ...)
## S3 method for class 'aov' summary(object, intercept = FALSE, split, expand.split = TRUE, keep.zero.df = TRUE, ...) ## S3 method for class 'aovlist' summary(object, ...)
object |
An object of class |
intercept |
logical: should intercept terms be included? |
split |
an optional named list, with names corresponding to terms in the model. Each component is itself a list with integer components giving contrasts whose contributions are to be summed. |
expand.split |
logical: should the split apply also to interactions involving the factor? |
keep.zero.df |
logical: should terms with no degrees of freedom be included? |
... |
Arguments to be passed to or from other methods,
for |
An object of class c("summary.aov", "listof")
or
"summary.aovlist"
respectively.
For fits with a single stratum the result will be a list of
ANOVA tables, one for each response (even if there is only one response):
the tables are of class "anova"
inheriting from class
"data.frame"
. They have columns "Df"
, "Sum Sq"
,
"Mean Sq"
, as well as "F value"
and "Pr(>F)"
if
there are non-zero residual degrees of freedom. There is a row for
each term in the model, plus one for "Residuals"
if there
are any.
For multistratum fits the return value is a list of such summaries, one for each stratum.
The use of expand.split = TRUE
is little tested: it is always
possible to set it to FALSE
and specify exactly all
the splits required.
aov
, summary
, model.tables
,
TukeyHSD
## For a simple example see example(aov) # Cochran and Cox (1957, p.164) # 3x3 factorial with ordered factors, each is average of 12. CC <- data.frame( y = c(449, 413, 326, 409, 358, 291, 341, 278, 312)/12, P = ordered(gl(3, 3)), N = ordered(gl(3, 1, 9)) ) CC.aov <- aov(y ~ N * P, data = CC , weights = rep(12, 9)) summary(CC.aov) # Split both main effects into linear and quadratic parts. summary(CC.aov, split = list(N = list(L = 1, Q = 2), P = list(L = 1, Q = 2))) # Split only the interaction summary(CC.aov, split = list("N:P" = list(L.L = 1, Q = 2:4))) # split on just one var summary(CC.aov, split = list(P = list(lin = 1, quad = 2))) summary(CC.aov, split = list(P = list(lin = 1, quad = 2)), expand.split = FALSE)
## For a simple example see example(aov) # Cochran and Cox (1957, p.164) # 3x3 factorial with ordered factors, each is average of 12. CC <- data.frame( y = c(449, 413, 326, 409, 358, 291, 341, 278, 312)/12, P = ordered(gl(3, 3)), N = ordered(gl(3, 1, 9)) ) CC.aov <- aov(y ~ N * P, data = CC , weights = rep(12, 9)) summary(CC.aov) # Split both main effects into linear and quadratic parts. summary(CC.aov, split = list(N = list(L = 1, Q = 2), P = list(L = 1, Q = 2))) # Split only the interaction summary(CC.aov, split = list("N:P" = list(L.L = 1, Q = 2:4))) # split on just one var summary(CC.aov, split = list(P = list(lin = 1, quad = 2))) summary(CC.aov, split = list(P = list(lin = 1, quad = 2)), expand.split = FALSE)
These functions are all methods
for class glm
or
summary.glm
objects.
## S3 method for class 'glm' summary(object, dispersion = NULL, correlation = FALSE, symbolic.cor = FALSE, ...) ## S3 method for class 'summary.glm' print(x, digits = max(3, getOption("digits") - 3), symbolic.cor = x$symbolic.cor, signif.stars = getOption("show.signif.stars"), show.residuals = FALSE, ...)
## S3 method for class 'glm' summary(object, dispersion = NULL, correlation = FALSE, symbolic.cor = FALSE, ...) ## S3 method for class 'summary.glm' print(x, digits = max(3, getOption("digits") - 3), symbolic.cor = x$symbolic.cor, signif.stars = getOption("show.signif.stars"), show.residuals = FALSE, ...)
object |
an object of class |
x |
an object of class |
dispersion |
the dispersion parameter for the family used.
Either a single numerical value or |
correlation |
logical; if |
digits |
the number of significant digits to use when printing. |
symbolic.cor |
logical. If |
signif.stars |
logical. If |
show.residuals |
logical. If |
... |
further arguments passed to or from other methods. |
print.summary.glm
tries to be smart about formatting the
coefficients, standard errors, etc. and additionally gives
‘significance stars’ if signif.stars
is TRUE
.
The coefficients
component of the result gives the estimated
coefficients and their estimated standard errors, together with their
ratio. This third column is labelled t ratio
if the
dispersion is estimated, and z ratio
if the dispersion is known
(or fixed by the family). A fourth column gives the two-tailed
p-value corresponding to the t or z ratio based on a Student t or
Normal reference distribution. (It is possible that the dispersion is
not known and there are no residual degrees of freedom from which to
estimate it. In that case the estimate is NaN
.)
Aliased coefficients are omitted in the returned object but restored
by the print
method.
Correlations are printed to two decimal places (or symbolically): to
see the actual correlations print summary(object)$correlation
directly.
The dispersion of a GLM is not used in the fitting process, but it is
needed to find standard errors.
If dispersion
is not supplied or NULL
,
the dispersion is taken as 1
for the binomial
and
Poisson
families, and otherwise estimated by the residual
Chi-squared statistic (calculated from cases with non-zero weights)
divided by the residual degrees of freedom.
summary
can be used with Gaussian glm
fits to handle the
case of a linear regression with known error variance, something not
handled by summary.lm
.
summary.glm
returns an object of class "summary.glm"
, a
list with components
call |
the component from |
family |
the component from |
deviance |
the component from |
contrasts |
the component from |
df.residual |
the component from |
null.deviance |
the component from |
df.null |
the component from |
deviance.resid |
the deviance residuals:
see |
coefficients |
the matrix of coefficients, standard errors, z-values and p-values. Aliased coefficients are omitted. |
aliased |
named logical vector showing if the original coefficients are aliased. |
dispersion |
either the supplied argument or the inferred/estimated
dispersion if the former is |
df |
a 3-vector of the rank of the model and the number of residual degrees of freedom, plus number of coefficients (including aliased ones). |
cov.unscaled |
the unscaled ( |
cov.scaled |
ditto, scaled by |
correlation |
(only if |
symbolic.cor |
(only if |
## For examples see example(glm)
## For examples see example(glm)
summary
method for class "lm"
.
## S3 method for class 'lm' summary(object, correlation = FALSE, symbolic.cor = FALSE, ...) ## S3 method for class 'summary.lm' print(x, digits = max(3, getOption("digits") - 3), symbolic.cor = x$symbolic.cor, signif.stars = getOption("show.signif.stars"), ...)
## S3 method for class 'lm' summary(object, correlation = FALSE, symbolic.cor = FALSE, ...) ## S3 method for class 'summary.lm' print(x, digits = max(3, getOption("digits") - 3), symbolic.cor = x$symbolic.cor, signif.stars = getOption("show.signif.stars"), ...)
object |
an object of class |
x |
an object of class |
correlation |
logical; if |
digits |
the number of significant digits to use when printing. |
symbolic.cor |
logical. If |
signif.stars |
logical. If |
... |
further arguments passed to or from other methods. |
print.summary.lm
tries to be smart about formatting the
coefficients, standard errors, etc. and additionally gives
‘significance stars’ if signif.stars
is TRUE
.
Aliased coefficients are omitted in the returned object but restored
by the print
method.
Correlations are printed to two decimal places (or symbolically): to
see the actual correlations print summary(object)$correlation
directly.
The function summary.lm
computes and returns a list of summary
statistics of the fitted linear model given in object
, using
the components (list elements) "call"
and "terms"
from its argument, plus
residuals |
the weighted residuals, the usual residuals
rescaled by the square root of the weights specified in the call to
|
coefficients |
a |
aliased |
named logical vector showing if the original coefficients are aliased. |
sigma |
the square root of the estimated variance of the random error
where |
df |
degrees of freedom, a 3-vector |
fstatistic |
(for models including non-intercept terms) a 3-vector with the value of the F-statistic with its numerator and denominator degrees of freedom. |
r.squared |
where |
adj.r.squared |
the above |
cov.unscaled |
a |
correlation |
the correlation matrix corresponding to the above
|
symbolic.cor |
(only if |
na.action |
from |
The model fitting function lm
, summary
.
Function coef
will extract the matrix of coefficients
with standard errors, t-statistics and p-values.
##-- Continuing the lm(.) example: coef(lm.D90) # the bare coefficients sld90 <- summary(lm.D90 <- lm(weight ~ group -1)) # omitting intercept sld90 coef(sld90) # much more ## model with *aliased* coefficient: lm.D9. <- lm(weight ~ group + I(group != "Ctl")) Sm.D9. <- summary(lm.D9.) Sm.D9. # shows the NA NA NA NA line stopifnot(length(cc <- coef(lm.D9.)) == 3, is.na(cc[3]), dim(coef(Sm.D9.)) == c(2,4), Sm.D9.$df == c(2, 18, 3))
##-- Continuing the lm(.) example: coef(lm.D90) # the bare coefficients sld90 <- summary(lm.D90 <- lm(weight ~ group -1)) # omitting intercept sld90 coef(sld90) # much more ## model with *aliased* coefficient: lm.D9. <- lm(weight ~ group + I(group != "Ctl")) Sm.D9. <- summary(lm.D9.) Sm.D9. # shows the NA NA NA NA line stopifnot(length(cc <- coef(lm.D9.)) == 3, is.na(cc[3]), dim(coef(Sm.D9.)) == c(2,4), Sm.D9.$df == c(2, 18, 3))
A summary
method for class "manova"
.
## S3 method for class 'manova' summary(object, test = c("Pillai", "Wilks", "Hotelling-Lawley", "Roy"), intercept = FALSE, tol = 1e-7, ...)
## S3 method for class 'manova' summary(object, test = c("Pillai", "Wilks", "Hotelling-Lawley", "Roy"), intercept = FALSE, tol = 1e-7, ...)
object |
An object of class |
test |
The name of the test statistic to be used. Partial matching is used so the name can be abbreviated. |
intercept |
logical. If |
tol |
tolerance to be used in deciding if the residuals are
rank-deficient: see |
... |
further arguments passed to or from other methods. |
The summary.manova
method uses a multivariate test statistic
for the summary table. Wilks' statistic is most popular in the
literature, but the default Pillai–Bartlett statistic is recommended
by Hand and Taylor (1987).
The table gives a transformation of the test statistic which has approximately an F distribution. The approximations used follow S-PLUS and SAS (the latter apart from some cases of the Hotelling–Lawley statistic), but many other distributional approximations exist: see Anderson (1984) and Krzanowski and Marriott (1994) for further references. All four approximate F statistics are the same when the term being tested has one degree of freedom, but in other cases that for the Roy statistic is an upper bound.
The tolerance tol
is applied to the QR decomposition of the
residual correlation matrix (unless some response has essentially zero
residuals, when it is unscaled). Thus the default value guards
against very highly correlated responses: it can be reduced but doing
so will allow rather inaccurate results and it will normally be better
to transform the responses to remove the high correlation.
An object of class "summary.manova"
. If there is a positive
residual degrees of freedom, this is a list with components
row.names |
The names of the terms, the row names of the
|
SS |
A named list of sums of squares and product matrices. |
Eigenvalues |
A matrix of eigenvalues. |
stats |
A matrix of the statistics, approximate F value, degrees of freedom and P value. |
otherwise components row.names
, SS
and Df
(degrees of freedom) for the terms (and not the residuals).
Anderson, T. W. (1994) An Introduction to Multivariate Statistical Analysis. Wiley.
Hand, D. J. and Taylor, C. C. (1987) Multivariate Analysis of Variance and Repeated Measures. Chapman and Hall.
Krzanowski, W. J. (1988) Principles of Multivariate Analysis. A User's Perspective. Oxford.
Krzanowski, W. J. and Marriott, F. H. C. (1994) Multivariate Analysis. Part I: Distributions, Ordination and Inference. Edward Arnold.
## Example on producing plastic film from Krzanowski (1998, p. 381) tear <- c(6.5, 6.2, 5.8, 6.5, 6.5, 6.9, 7.2, 6.9, 6.1, 6.3, 6.7, 6.6, 7.2, 7.1, 6.8, 7.1, 7.0, 7.2, 7.5, 7.6) gloss <- c(9.5, 9.9, 9.6, 9.6, 9.2, 9.1, 10.0, 9.9, 9.5, 9.4, 9.1, 9.3, 8.3, 8.4, 8.5, 9.2, 8.8, 9.7, 10.1, 9.2) opacity <- c(4.4, 6.4, 3.0, 4.1, 0.8, 5.7, 2.0, 3.9, 1.9, 5.7, 2.8, 4.1, 3.8, 1.6, 3.4, 8.4, 5.2, 6.9, 2.7, 1.9) Y <- cbind(tear, gloss, opacity) rate <- gl(2,10, labels = c("Low", "High")) additive <- gl(2, 5, length = 20, labels = c("Low", "High")) fit <- manova(Y ~ rate * additive) summary.aov(fit) # univariate ANOVA tables summary(fit, test = "Wilks") # ANOVA table of Wilks' lambda summary(fit) # same F statistics as single-df terms
## Example on producing plastic film from Krzanowski (1998, p. 381) tear <- c(6.5, 6.2, 5.8, 6.5, 6.5, 6.9, 7.2, 6.9, 6.1, 6.3, 6.7, 6.6, 7.2, 7.1, 6.8, 7.1, 7.0, 7.2, 7.5, 7.6) gloss <- c(9.5, 9.9, 9.6, 9.6, 9.2, 9.1, 10.0, 9.9, 9.5, 9.4, 9.1, 9.3, 8.3, 8.4, 8.5, 9.2, 8.8, 9.7, 10.1, 9.2) opacity <- c(4.4, 6.4, 3.0, 4.1, 0.8, 5.7, 2.0, 3.9, 1.9, 5.7, 2.8, 4.1, 3.8, 1.6, 3.4, 8.4, 5.2, 6.9, 2.7, 1.9) Y <- cbind(tear, gloss, opacity) rate <- gl(2,10, labels = c("Low", "High")) additive <- gl(2, 5, length = 20, labels = c("Low", "High")) fit <- manova(Y ~ rate * additive) summary.aov(fit) # univariate ANOVA tables summary(fit, test = "Wilks") # ANOVA table of Wilks' lambda summary(fit) # same F statistics as single-df terms
summary
method for class "nls"
.
## S3 method for class 'nls' summary(object, correlation = FALSE, symbolic.cor = FALSE, ...) ## S3 method for class 'summary.nls' print(x, digits = max(3, getOption("digits") - 3), symbolic.cor = x$symbolic.cor, signif.stars = getOption("show.signif.stars"), ...)
## S3 method for class 'nls' summary(object, correlation = FALSE, symbolic.cor = FALSE, ...) ## S3 method for class 'summary.nls' print(x, digits = max(3, getOption("digits") - 3), symbolic.cor = x$symbolic.cor, signif.stars = getOption("show.signif.stars"), ...)
object |
an object of class |
x |
an object of class |
correlation |
logical; if |
digits |
the number of significant digits to use when printing. |
symbolic.cor |
logical. If |
signif.stars |
logical. If |
... |
further arguments passed to or from other methods. |
The distribution theory used to find the distribution of the standard errors and of the residual standard error (for t ratios) is based on linearization and is approximate, maybe very approximate.
print.summary.nls
tries to be smart about formatting the
coefficients, standard errors, etc. and additionally gives
‘significance stars’ if signif.stars
is TRUE
.
Correlations are printed to two decimal places (or symbolically): to
see the actual correlations print summary(object)$correlation
directly.
The function summary.nls
computes and returns a list of summary
statistics of the fitted model given in object
, using
the component "formula"
from its argument, plus
residuals |
the weighted residuals, the usual residuals
rescaled by the square root of the weights specified in the call to
|
coefficients |
a |
sigma |
the square root of the estimated variance of the random error
where |
df |
degrees of freedom, a 2-vector |
cov.unscaled |
a |
correlation |
the correlation matrix corresponding to the above
|
symbolic.cor |
(only if |
The model fitting function nls
, summary
.
Function coef
will extract the matrix of coefficients
with standard errors, t-statistics and p-values.
The summary
method for class "princomp"
.
## S3 method for class 'princomp' summary(object, loadings = FALSE, cutoff = 0.1, ...) ## S3 method for class 'summary.princomp' print(x, digits = 3, loadings = x$print.loadings, cutoff = x$cutoff, ...)
## S3 method for class 'princomp' summary(object, loadings = FALSE, cutoff = 0.1, ...) ## S3 method for class 'summary.princomp' print(x, digits = 3, loadings = x$print.loadings, cutoff = x$cutoff, ...)
object |
an object of class |
loadings |
logical. Should loadings be included? |
cutoff |
numeric. Loadings below this cutoff in absolute value are shown as blank in the output. |
x |
an object of class |
digits |
the number of significant digits to be used in listing loadings. |
... |
arguments to be passed to or from other methods. |
object
with additional components cutoff
and
print.loadings
.
summary(pc.cr <- princomp(USArrests, cor = TRUE)) ## The signs of the loading columns are arbitrary print(summary(princomp(USArrests, cor = TRUE), loadings = TRUE, cutoff = 0.2), digits = 2)
summary(pc.cr <- princomp(USArrests, cor = TRUE)) ## The signs of the loading columns are arbitrary print(summary(princomp(USArrests, cor = TRUE), loadings = TRUE, cutoff = 0.2), digits = 2)
Smooth the (x, y) values by Friedman's ‘super smoother’.
supsmu(x, y, wt =, span = "cv", periodic = FALSE, bass = 0, trace = FALSE)
supsmu(x, y, wt =, span = "cv", periodic = FALSE, bass = 0, trace = FALSE)
x |
x values for smoothing |
y |
y values for smoothing |
wt |
case weights, by default all equal |
span |
the fraction of the observations in the span of the running
lines smoother, or |
periodic |
if |
bass |
controls the smoothness of the fitted curve. Values of up to 10 indicate increasing smoothness. |
trace |
logical, if true, prints one line of info “per
spar”, notably useful for |
supsmu
is a running lines smoother which chooses between three
spans for the lines. The running lines smoothers are symmetric, with
k/2
data points each side of the predicted point, and values of
k
as 0.5 * n
, 0.2 * n
and 0.05 * n
, where
n
is the number of data points. If span
is specified,
a single smoother with span span * n
is used.
The best of the three smoothers is chosen by cross-validation for each prediction. The best spans are then smoothed by a running lines smoother and the final prediction chosen by linear interpolation.
The FORTRAN code says: “For small samples (n < 40
) or if
there are substantial serial correlations between observations close
in x-value, then a pre-specified fixed span smoother (span >
0
) should be used. Reasonable span values are 0.2 to 0.4.”
Cases with non-finite values of x
, y
or wt
are
dropped, with a warning.
A list with components
x |
the input values in increasing order with duplicates removed. |
y |
the corresponding y values on the fitted curve. |
Friedman, J. H. (1984) SMART User's Guide. Laboratory for Computational Statistics, Stanford University Technical Report No. 1.
Friedman, J. H. (1984) A variable span scatterplot smoother. Laboratory for Computational Statistics, Stanford University Technical Report No. 5.
require(graphics) with(cars, { plot(speed, dist) lines(supsmu(speed, dist)) lines(supsmu(speed, dist, bass = 7), lty = 2) })
require(graphics) with(cars, { plot(speed, dist) lines(supsmu(speed, dist)) lines(supsmu(speed, dist, bass = 7), lty = 2) })
Symbolically encode a given numeric or logical vector or array. Particularly useful for visualization of structured matrices, e.g., correlation, sparse, or logical ones.
symnum(x, cutpoints = c(0.3, 0.6, 0.8, 0.9, 0.95), symbols = if(numeric.x) c(" ", ".", ",", "+", "*", "B") else c(".", "|"), legend = length(symbols) >= 3, na = "?", eps = 1e-5, numeric.x = is.numeric(x), corr = missing(cutpoints) && numeric.x, show.max = if(corr) "1", show.min = NULL, abbr.colnames = has.colnames, lower.triangular = corr && is.numeric(x) && is.matrix(x), diag.lower.tri = corr && !is.null(show.max))
symnum(x, cutpoints = c(0.3, 0.6, 0.8, 0.9, 0.95), symbols = if(numeric.x) c(" ", ".", ",", "+", "*", "B") else c(".", "|"), legend = length(symbols) >= 3, na = "?", eps = 1e-5, numeric.x = is.numeric(x), corr = missing(cutpoints) && numeric.x, show.max = if(corr) "1", show.min = NULL, abbr.colnames = has.colnames, lower.triangular = corr && is.numeric(x) && is.matrix(x), diag.lower.tri = corr && !is.null(show.max))
x |
numeric or logical vector or array. |
cutpoints |
numeric vector whose values |
symbols |
character vector, one shorter than (the
augmented, see When |
legend |
logical indicating if a |
na |
character or logical. How |
eps |
absolute precision to be used at left and right boundary. |
numeric.x |
logical indicating if |
corr |
logical. If |
show.max |
if |
show.min |
if |
abbr.colnames |
logical, integer or |
lower.triangular |
logical. If |
diag.lower.tri |
logical. If |
An atomic character object of class noquote
and the same
dimensions as x
.
If legend
is TRUE
(as by default when there are more
than two classes), the result has an attribute "legend"
containing a legend of the returned character codes, in the form
where = cutpoints[j]
and
= symbols[j]
.
The optional (mostly logical) arguments all try to use smart defaults. Specifying them explicitly may lead to considerably improved output in many cases.
Martin Maechler [email protected]
ii <- setNames(0:8, 0:8) symnum(ii, cutpoints = 2*(0:4), symbols = c(".", "-", "+", "$")) symnum(ii, cutpoints = 2*(0:4), symbols = c(".", "-", "+", "$"), show.max = TRUE) symnum(1:12 %% 3 == 0) # --> "|" = TRUE, "." = FALSE for logical ## Pascal's Triangle modulo 2 -- odd and even numbers: N <- 38 pascal <- t(sapply(0:N, function(n) round(choose(n, 0:N - (N-n)%/%2)))) rownames(pascal) <- rep("", 1+N) # <-- to improve "graphic" symnum(pascal %% 2, symbols = c(" ", "A"), numeric.x = FALSE) ##-- Symbolic correlation matrices: symnum(cor(attitude), diag.lower.tri = FALSE) symnum(cor(attitude), abbr.colnames = NULL) symnum(cor(attitude), abbr.colnames = FALSE) symnum(cor(attitude), abbr.colnames = 2) symnum(cor(rbind(1, rnorm(25), rnorm(25)^2))) symnum(cor(matrix(rexp(30, 1), 5, 18))) # <<-- PATTERN ! -- symnum(cm1 <- cor(matrix(rnorm(90) , 5, 18))) # < White Noise SMALL n symnum(cm1, diag.lower.tri = FALSE) symnum(cm2 <- cor(matrix(rnorm(900), 50, 18))) # < White Noise "BIG" n symnum(cm2, lower.triangular = FALSE) ## NA's: Cm <- cor(matrix(rnorm(60), 10, 6)); Cm[c(3,6), 2] <- NA symnum(Cm, show.max = NULL) ## Graphical P-values (aka "significance stars"): pval <- rev(sort(c(outer(1:6, 10^-(1:3))))) symp <- symnum(pval, corr = FALSE, cutpoints = c(0, .001,.01,.05, .1, 1), symbols = c("***","**","*","."," ")) noquote(cbind(P.val = format(pval), Signif = symp))
ii <- setNames(0:8, 0:8) symnum(ii, cutpoints = 2*(0:4), symbols = c(".", "-", "+", "$")) symnum(ii, cutpoints = 2*(0:4), symbols = c(".", "-", "+", "$"), show.max = TRUE) symnum(1:12 %% 3 == 0) # --> "|" = TRUE, "." = FALSE for logical ## Pascal's Triangle modulo 2 -- odd and even numbers: N <- 38 pascal <- t(sapply(0:N, function(n) round(choose(n, 0:N - (N-n)%/%2)))) rownames(pascal) <- rep("", 1+N) # <-- to improve "graphic" symnum(pascal %% 2, symbols = c(" ", "A"), numeric.x = FALSE) ##-- Symbolic correlation matrices: symnum(cor(attitude), diag.lower.tri = FALSE) symnum(cor(attitude), abbr.colnames = NULL) symnum(cor(attitude), abbr.colnames = FALSE) symnum(cor(attitude), abbr.colnames = 2) symnum(cor(rbind(1, rnorm(25), rnorm(25)^2))) symnum(cor(matrix(rexp(30, 1), 5, 18))) # <<-- PATTERN ! -- symnum(cm1 <- cor(matrix(rnorm(90) , 5, 18))) # < White Noise SMALL n symnum(cm1, diag.lower.tri = FALSE) symnum(cm2 <- cor(matrix(rnorm(900), 50, 18))) # < White Noise "BIG" n symnum(cm2, lower.triangular = FALSE) ## NA's: Cm <- cor(matrix(rnorm(60), 10, 6)); Cm[c(3,6), 2] <- NA symnum(Cm, show.max = NULL) ## Graphical P-values (aka "significance stars"): pval <- rev(sort(c(outer(1:6, 10^-(1:3))))) symp <- symnum(pval, corr = FALSE, cutpoints = c(0, .001,.01,.05, .1, 1), symbols = c("***","**","*","."," ")) noquote(cbind(P.val = format(pval), Signif = symp))
Performs one and two sample t-tests on vectors of data.
t.test(x, ...) ## Default S3 method: t.test(x, y = NULL, alternative = c("two.sided", "less", "greater"), mu = 0, paired = FALSE, var.equal = FALSE, conf.level = 0.95, ...) ## S3 method for class 'formula' t.test(formula, data, subset, na.action = na.pass, ...)
t.test(x, ...) ## Default S3 method: t.test(x, y = NULL, alternative = c("two.sided", "less", "greater"), mu = 0, paired = FALSE, var.equal = FALSE, conf.level = 0.95, ...) ## S3 method for class 'formula' t.test(formula, data, subset, na.action = na.pass, ...)
x |
a (non-empty) numeric vector of data values. |
y |
an optional (non-empty) numeric vector of data values. |
alternative |
a character string specifying the alternative
hypothesis, must be one of |
mu |
a number indicating the true value of the mean (or difference in means if you are performing a two sample test). |
paired |
a logical indicating whether you want a paired t-test. |
var.equal |
a logical variable indicating whether to treat the
two variances as being equal. If |
conf.level |
confidence level of the interval. |
formula |
a formula of the form |
data |
an optional matrix or data frame (or similar: see
|
subset |
an optional vector specifying a subset of observations to be used. |
na.action |
a function which indicates what should happen when
the data contain |
... |
further arguments to be passed to or from methods.
For the |
alternative = "greater"
is the alternative that x
has a
larger mean than y
. For the one-sample case: that the mean is positive.
If paired
is TRUE
then both x
and y
must
be specified and they must be the same length. Missing values are
silently removed (in pairs if paired
is TRUE
). If
var.equal
is TRUE
then the pooled estimate of the
variance is used. By default, if var.equal
is FALSE
then the variance is estimated separately for both groups and the
Welch modification to the degrees of freedom is used.
If the input data are effectively constant (compared to the larger of the two means) an error is generated.
A list with class "htest"
containing the following components:
statistic |
the value of the t-statistic. |
parameter |
the degrees of freedom for the t-statistic. |
p.value |
the p-value for the test. |
conf.int |
a confidence interval for the mean appropriate to the specified alternative hypothesis. |
estimate |
the estimated mean or difference in means depending on whether it was a one-sample test or a two-sample test. |
null.value |
the specified hypothesized value of the mean or mean difference depending on whether it was a one-sample test or a two-sample test. |
stderr |
the standard error of the mean (difference), used as denominator in the t-statistic formula. |
alternative |
a character string describing the alternative hypothesis. |
method |
a character string indicating what type of t-test was performed. |
data.name |
a character string giving the name(s) of the data. |
## Two-sample t-test t.test(1:10, y = c(7:20)) # P = .00001855 t.test(1:10, y = c(7:20, 200)) # P = .1245 -- NOT significant anymore ## Traditional interface with(mtcars, t.test(mpg[am == 0], mpg[am == 1])) ## Formula interface t.test(mpg ~ am, data = mtcars) ## One-sample t-test ## Traditional interface t.test(sleep$extra) ## Formula interface t.test(extra ~ 1, data = sleep) ## Paired t-test ## The sleep data is actually paired, so could have been in wide format: sleep2 <- reshape(sleep, direction = "wide", idvar = "ID", timevar = "group") ## Traditional interface t.test(sleep2$extra.1, sleep2$extra.2, paired = TRUE) ## Formula interface t.test(Pair(extra.1, extra.2) ~ 1, data = sleep2)
## Two-sample t-test t.test(1:10, y = c(7:20)) # P = .00001855 t.test(1:10, y = c(7:20, 200)) # P = .1245 -- NOT significant anymore ## Traditional interface with(mtcars, t.test(mpg[am == 0], mpg[am == 1])) ## Formula interface t.test(mpg ~ am, data = mtcars) ## One-sample t-test ## Traditional interface t.test(sleep$extra) ## Formula interface t.test(extra ~ 1, data = sleep) ## Paired t-test ## The sleep data is actually paired, so could have been in wide format: sleep2 <- reshape(sleep, direction = "wide", idvar = "ID", timevar = "group") ## Traditional interface t.test(sleep2$extra.1, sleep2$extra.2, paired = TRUE) ## Formula interface t.test(Pair(extra.1, extra.2) ~ 1, data = sleep2)
Density, distribution function, quantile function and random
generation for the t distribution with df
degrees of freedom
(and optional non-centrality parameter ncp
).
dt(x, df, ncp, log = FALSE) pt(q, df, ncp, lower.tail = TRUE, log.p = FALSE) qt(p, df, ncp, lower.tail = TRUE, log.p = FALSE) rt(n, df, ncp)
dt(x, df, ncp, log = FALSE) pt(q, df, ncp, lower.tail = TRUE, log.p = FALSE) qt(p, df, ncp, lower.tail = TRUE, log.p = FALSE) rt(n, df, ncp)
x , q
|
vector of quantiles. |
p |
vector of probabilities. |
n |
number of observations. If |
df |
degrees of freedom ( |
ncp |
non-centrality parameter |
log , log.p
|
logical; if TRUE, probabilities p are given as log(p). |
lower.tail |
logical; if TRUE (default), probabilities are
|
The distribution with
df
degrees of
freedom has density
for all real .
It has mean
(for
) and
variance
(for
).
The general non-central
with parameters
= (df, ncp)
is defined as the distribution of
where
and
are independent random
variables,
and
(see Chisquare).
The most used applications are power calculations for -tests:
Let
where
is the
mean
and the sample standard
deviation (
sd
) of which are
i.i.d.
Then
is distributed as non-central
with
df
degrees of freedom and non-centrality parameter
ncp
.
The distribution's cumulative distribution function (cdf),
fulfills
for
, and
for
,
where
, and
is the
incomplete beta function, in R this is
pbeta(x, a,b)
.
dt
gives the density,
pt
gives the distribution function,
qt
gives the quantile function, and
rt
generates random deviates.
Invalid arguments will result in return value NaN
, with a warning.
The length of the result is determined by n
for
rt
, and is the maximum of the lengths of the
numerical arguments for the other functions.
The numerical arguments other than n
are recycled to the
length of the result. Only the first elements of the logical
arguments are used.
Supplying ncp = 0
uses the algorithm for the non-central
distribution, which is not the same algorithm used if ncp
is
omitted. This is to give consistent behaviour in extreme cases with
values of ncp
very near zero.
The code for non-zero ncp
is principally intended to be used
for moderate values of ncp
: it will not be highly accurate,
especially in the tails, for large values.
The central dt
is computed via an accurate formula
provided by Catherine Loader (see the reference in dbinom
).
For the non-central case of dt
, C code contributed by
Claus Ekstrøm based on the relationship (for
) to the cumulative distribution.
For the central case of pt
, a normal approximation in the
tails, otherwise via pbeta
.
For the non-central case of pt
based on a C translation of
Lenth, R. V. (1989). Algorithm AS 243 —
Cumulative distribution function of the non-central distribution,
Applied Statistics 38, 185–189.
This computes the lower tail only, so the upper tail currently suffers from cancellation and a warning will be given when this is likely to be significant.
For central qt
, a C translation of
Hill, G. W. (1970) Algorithm 396: Student's t-quantiles. Communications of the ACM, 13(10), 619–620.
altered to take account of
Hill, G. W. (1981) Remark on Algorithm 396, ACM Transactions on Mathematical Software, 7, 250–1.
The non-central case is done by inversion.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole. (Except non-central versions.)
Johnson, N. L., Kotz, S. and Balakrishnan, N. (1995) Continuous Univariate Distributions, volume 2, chapters 28 and 31. Wiley, New York.
Distributions for other standard distributions, including
df
for the F distribution.
require(graphics) 1 - pt(1:5, df = 1) qt(.975, df = c(1:10,20,50,100,1000)) tt <- seq(0, 10, length.out = 21) ncp <- seq(0, 6, length.out = 31) ptn <- outer(tt, ncp, function(t, d) pt(t, df = 3, ncp = d)) t.tit <- "Non-central t - Probabilities" image(tt, ncp, ptn, zlim = c(0,1), main = t.tit) persp(tt, ncp, ptn, zlim = 0:1, r = 2, phi = 20, theta = 200, main = t.tit, xlab = "t", ylab = "non-centrality parameter", zlab = "Pr(T <= t)") plot(function(x) dt(x, df = 3, ncp = 2), -3, 11, ylim = c(0, 0.32), main = "Non-central t - Density", yaxs = "i") ## Relation between F_t(.) = pt(x, n) and pbeta(): ptBet <- function(t, n) { x <- n/(n + t^2) r <- pb <- pbeta(x, n/2, 1/2) / 2 pos <- t > 0 r[pos] <- 1 - pb[pos] r } x <- seq(-5, 5, by = 1/8) nu <- 3:10 pt. <- outer(x, nu, pt) ptB <- outer(x, nu, ptBet) ## matplot(x, pt., type = "l") stopifnot(all.equal(pt., ptB, tolerance = 1e-15))
require(graphics) 1 - pt(1:5, df = 1) qt(.975, df = c(1:10,20,50,100,1000)) tt <- seq(0, 10, length.out = 21) ncp <- seq(0, 6, length.out = 31) ptn <- outer(tt, ncp, function(t, d) pt(t, df = 3, ncp = d)) t.tit <- "Non-central t - Probabilities" image(tt, ncp, ptn, zlim = c(0,1), main = t.tit) persp(tt, ncp, ptn, zlim = 0:1, r = 2, phi = 20, theta = 200, main = t.tit, xlab = "t", ylab = "non-centrality parameter", zlab = "Pr(T <= t)") plot(function(x) dt(x, df = 3, ncp = 2), -3, 11, ylim = c(0, 0.32), main = "Non-central t - Density", yaxs = "i") ## Relation between F_t(.) = pt(x, n) and pbeta(): ptBet <- function(t, n) { x <- n/(n + t^2) r <- pb <- pbeta(x, n/2, 1/2) / 2 pos <- t > 0 r[pos] <- 1 - pb[pos] r } x <- seq(-5, 5, by = 1/8) nu <- 3:10 pt. <- outer(x, nu, pt) ptB <- outer(x, nu, ptBet) ## matplot(x, pt., type = "l") stopifnot(all.equal(pt., ptB, tolerance = 1e-15))
Plots regression terms against their predictors, optionally with standard errors and partial residuals added.
termplot(model, data = NULL, envir = environment(formula(model)), partial.resid = FALSE, rug = FALSE, terms = NULL, se = FALSE, xlabs = NULL, ylabs = NULL, main = NULL, col.term = 2, lwd.term = 1.5, col.se = "orange", lty.se = 2, lwd.se = 1, col.res = "gray", cex = 1, pch = par("pch"), col.smth = "darkred", lty.smth = 2, span.smth = 2/3, ask = dev.interactive() && nb.fig < n.tms, use.factor.levels = TRUE, smooth = NULL, ylim = "common", plot = TRUE, transform.x = FALSE, ...)
termplot(model, data = NULL, envir = environment(formula(model)), partial.resid = FALSE, rug = FALSE, terms = NULL, se = FALSE, xlabs = NULL, ylabs = NULL, main = NULL, col.term = 2, lwd.term = 1.5, col.se = "orange", lty.se = 2, lwd.se = 1, col.res = "gray", cex = 1, pch = par("pch"), col.smth = "darkred", lty.smth = 2, span.smth = 2/3, ask = dev.interactive() && nb.fig < n.tms, use.factor.levels = TRUE, smooth = NULL, ylim = "common", plot = TRUE, transform.x = FALSE, ...)
model |
fitted model object |
data |
data frame in which variables in |
envir |
environment in which variables in |
partial.resid |
logical; should partial residuals be plotted? |
rug |
add rugplots (jittered 1-d histograms) to the axes? |
terms |
which terms to plot (default |
se |
plot pointwise standard errors? |
xlabs |
vector of labels for the x axes |
ylabs |
vector of labels for the y axes |
main |
logical, or vector of main titles; if |
col.term , lwd.term
|
color and line width for the ‘term curve’,
see |
col.se , lty.se , lwd.se
|
color, line type and line width for the
‘twice-standard-error curve’ when |
col.res , cex , pch
|
color, plotting character expansion and type
for partial residuals, when |
ask |
logical; if |
use.factor.levels |
Should x-axis ticks use factor levels or numbers for factor terms? |
smooth |
|
lty.smth , col.smth , span.smth
|
Passed to |
ylim |
an optional range for the y axis, or |
plot |
if set to |
transform.x |
logical vector; if an element (recycled as necessary)
is |
... |
other graphical parameters. |
The model
object must have a predict
method that accepts
type = "terms"
, e.g., glm
in the stats package,
coxph
and survreg
in
the survival package.
For the partial.resid = TRUE
option model
must have a
residuals
method that accepts type = "partial"
,
which lm
and glm
do.
The data
argument should rarely be needed, but in some cases
termplot
may be unable to reconstruct the original data
frame. Using na.action=na.exclude
makes these problems less likely.
Nothing sensible happens for interaction terms, and they may cause errors.
The plot = FALSE
option is useful when some special action is needed,
e.g. to overlay the results of two different models or to plot
confidence bands.
For plot = FALSE
, a list with one element for each plot which
would have been produced. Each element of the list is a data frame
with variables x
, y
, and optionally the pointwise
standard errors se
. For continuous predictors x
will
contain the ordered unique values and for a factor it will be a factor
containing one instance of each level. The list has attribute
"constant"
copied from the predicted terms object.
Otherwise, the number of terms, invisibly.
For (generalized) linear models, plot.lm
and
predict.glm
.
require(graphics) had.splines <- "package:splines" %in% search() if(!had.splines) rs <- require(splines) x <- 1:100 z <- factor(rep(LETTERS[1:4], 25)) y <- rnorm(100, sin(x/10)+as.numeric(z)) model <- glm(y ~ ns(x, 6) + z) par(mfrow = c(2,2)) ## 2 x 2 plots for same model : termplot(model, main = paste("termplot( ", deparse(model$call)," ...)")) termplot(model, rug = TRUE) termplot(model, partial.resid = TRUE, se = TRUE, main = TRUE) termplot(model, partial.resid = TRUE, smooth = panel.smooth, span.smth = 1/4) if(!had.splines && rs) detach("package:splines") if(requireNamespace("MASS", quietly = TRUE)) { hills.lm <- lm(log(time) ~ log(climb)+log(dist), data = MASS::hills) termplot(hills.lm, partial.resid = TRUE, smooth = panel.smooth, terms = "log(dist)", main = "Original") termplot(hills.lm, transform.x = TRUE, partial.resid = TRUE, smooth = panel.smooth, terms = "log(dist)", main = "Transformed") }
require(graphics) had.splines <- "package:splines" %in% search() if(!had.splines) rs <- require(splines) x <- 1:100 z <- factor(rep(LETTERS[1:4], 25)) y <- rnorm(100, sin(x/10)+as.numeric(z)) model <- glm(y ~ ns(x, 6) + z) par(mfrow = c(2,2)) ## 2 x 2 plots for same model : termplot(model, main = paste("termplot( ", deparse(model$call)," ...)")) termplot(model, rug = TRUE) termplot(model, partial.resid = TRUE, se = TRUE, main = TRUE) termplot(model, partial.resid = TRUE, smooth = panel.smooth, span.smth = 1/4) if(!had.splines && rs) detach("package:splines") if(requireNamespace("MASS", quietly = TRUE)) { hills.lm <- lm(log(time) ~ log(climb)+log(dist), data = MASS::hills) termplot(hills.lm, partial.resid = TRUE, smooth = panel.smooth, terms = "log(dist)", main = "Original") termplot(hills.lm, transform.x = TRUE, partial.resid = TRUE, smooth = panel.smooth, terms = "log(dist)", main = "Transformed") }
The function terms
is a generic function
which can be used to extract terms objects
from various kinds of R data objects.
terms(x, ...)
terms(x, ...)
x |
object used to select a method to dispatch. |
... |
further arguments passed to or from other methods. |
There are methods for classes "aovlist"
, and "terms"
"formula"
(see terms.formula
):
the default method just extracts the terms
component of the
object, or failing that a "terms"
attribute (as used by
model.frame
).
There are print
and labels
methods for
class "terms"
: the latter prints the term labels (see
terms.object
).
An object of class c("terms", "formula")
which contains the
terms representation of a symbolic model. See
terms.object
for its structure.
Chambers, J. M. and Hastie, T. J. (1992) Statistical models. Chapter 2 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
terms.object
, terms.formula
,
lm
, glm
, formula
.
This function takes a formula and some optional arguments and
constructs a terms object. The terms object can then be used to
construct a model.matrix
.
## S3 method for class 'formula' terms(x, specials = NULL, abb = NULL, data = NULL, neg.out = TRUE, keep.order = FALSE, simplify = FALSE, ..., allowDotAsName = FALSE)
## S3 method for class 'formula' terms(x, specials = NULL, abb = NULL, data = NULL, neg.out = TRUE, keep.order = FALSE, simplify = FALSE, ..., allowDotAsName = FALSE)
x |
a |
specials |
which functions in the formula should be marked as
special in the |
abb |
Not implemented in R; deprecated. |
data |
a data frame from which the meaning of the special symbol
|
neg.out |
Not implemented in R; deprecated. |
keep.order |
a logical value indicating whether the terms should
keep their positions. By default, when |
simplify |
should the formula be expanded and simplified, the pre-1.7.0 behaviour? |
... |
further arguments passed to or from other methods. |
allowDotAsName |
normally |
Not all of the options work in the same way that they do in S and not all are implemented.
A terms
object is returned. It is the re-ordered formula (unless
keep.order = TRUE
) with several attributes
, see
terms.object
for details. In all
cases variables within an interaction term in the formula are
re-ordered by the ordering of the "variables"
attribute, which
is the order in which the variables occur in the formula.
terms
, terms.object
, also for examples.
An object of class terms
holds information about a
model. Usually the model was specified in terms of a
formula
and that formula was used to determine the terms
object.
The object itself is simply the result of terms.formula(<formula>)
.
It has a number of attributes
and they are used to construct the model frame:
factors
An integer matrix of variables by terms showing which variables appear in which terms. The entries are
if the variable does not occur in the term,
if it does occur and should be coded by contrasts, and
if it occurs and should be coded via dummy variables for all levels (as when a lower-order term is missing).
Note that variables in main effects always receive 1,
even if the intercept is missing (in which case the first one
should be coded with dummy variables).
If there are no terms other than an intercept and offsets,
this is integer(0)
.
term.labels
A character vector containing the labels for each of the terms in the model, except for offsets. Note that these are after possible re-ordering of terms.
Non-syntactic names will be quoted by backticks: this makes it easier to re-construct the formula from the term labels.
variables
A call to list
of the variables in the model.
intercept
Either 0, indicating no intercept is to be fit, or 1 indicating that an intercept is to be fit.
order
A vector of the same length as term.labels
indicating the order of interaction for each term.
response
The index of the variable (in variables) of the response (the left hand side of the formula). Zero, if there is no response.
offset
If the model contains offset
terms there
is an offset
attribute indicating which variable(s) are offsets
specials
If a specials
argument was given to
terms.formula
there is a specials
attribute, a
pairlist of vectors (one for each specified special function) giving
numeric indices of the arguments of the list returned as the
variables
attribute which contain these special functions.
dataClasses
optional. A named character vector giving the classes
(as given by .MFclass
) of the variables used in a fit.
predvars
optional. An expression to help in computing
predictions at new covariate values; see makepredictcall
.
The object has class c("terms", "formula")
.
These objects are different from those found in S. In particular
there is no formula
attribute: instead the object is itself a
formula. (Thus, the mode of a terms object is different.)
Examples of the specials
argument can be seen in the
aov
and coxph
functions, the
latter from package survival.
## use of specials (as used for gam() in packages mgcv and gam) (tf <- terms(y ~ x + x:z + s(x), specials = "s")) ## Note that the "factors" attribute has variables as row names ## and term labels as column names, both as character vectors. attr(tf, "specials") # index 's' variable(s) rownames(attr(tf, "factors"))[attr(tf, "specials")$s] ## we can keep the order by terms(y ~ x + x:z + s(x), specials = "s", keep.order = TRUE)
## use of specials (as used for gam() in packages mgcv and gam) (tf <- terms(y ~ x + x:z + s(x), specials = "s")) ## Note that the "factors" attribute has variables as row names ## and term labels as column names, both as character vectors. attr(tf, "specials") # index 's' variable(s) rownames(attr(tf, "factors"))[attr(tf, "specials")$s] ## we can keep the order by terms(y ~ x + x:z + s(x), specials = "s", keep.order = TRUE)
time
creates the vector of times at which a time series was sampled.
cycle
gives the positions in the cycle of each observation.
frequency
returns the number of samples per unit time and
deltat
the time interval between observations (see
ts
).
time(x, ...) ## Default S3 method: time(x, offset = 0, ts.eps = getOption("ts.eps"), ...) cycle(x, ...) frequency(x, ...) deltat(x, ...)
time(x, ...) ## Default S3 method: time(x, offset = 0, ts.eps = getOption("ts.eps"), ...) cycle(x, ...) frequency(x, ...) deltat(x, ...)
x |
a univariate or multivariate time-series, or a vector or matrix. |
offset |
can be used to indicate when sampling took place
in the time unit. |
ts.eps |
time series comparison tolerance, used in |
... |
extra arguments for future methods. |
These are all generic functions, which will use the
tsp
attribute of x
if it exists. time
and cycle
have methods for class ts
that coerce
the result to that class.
time()
round()
s values close to an integer, i.e.,
closer than ts.eps
, since R 4.3.0. For previous behaviour,
you can call it with ts.eps = 0
.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
date
for clock time, system.time
for CPU usage.
require(graphics) cycle(presidents) # a simple series plot plot(as.vector(time(presidents)), as.vector(presidents), type = "l")
require(graphics) cycle(presidents) # a simple series plot plot(as.vector(time(presidents)), as.vector(presidents), type = "l")
In its simplest use, toeplitz()
forms a symmetric Toeplitz matrix
given its first column (or row). For the general case, asymmetric and
non-square Toeplitz matrices are formed either by specifying the first
column and row separately,
T1 <- toeplitz(col, row)
or by
T <- toeplitz2(x, nr, nc)
where only one of (nr, nc)
needs to be specified.
In the latter case, the simple equivalence
is fulfilled where
ncol(T)
.
toeplitz (x, r = NULL, symmetric = is.null(r)) toeplitz2(x, nrow = length(x) +1 - ncol, ncol = length(x) +1 - nrow)
toeplitz (x, r = NULL, symmetric = is.null(r)) toeplitz2(x, nrow = length(x) +1 - ncol, ncol = length(x) +1 - nrow)
x |
for |
r |
the first row of the target Toeplitz matrix; only needed in asymmetric cases. |
symmetric |
optional |
nrow , ncol
|
the number of rows and columns; only one needs to be specified. |
The Toeplitz matrix
; for
toeplitz()
: dim(T)
is (n,m)
and m == length(x)
and
n == m
in the symmetric case or n == length(r)
otherwise.
toeplitz2()
: dim(T) == c(nrow, ncol)
.
A. Trapletti and Martin Maechler (speedup and asymmetric extensions)
x <- 1:5 toeplitz (x) T. <- toeplitz (1:5, 11:13) # with a *Warning* x[1] != r[1] T2 <- toeplitz2(c(13:12, 1:5), 5, 3)# this is the same matrix: stopifnot(identical(T., T2)) # Matrix of character (could also have logical, raw, complex ..) {also warning}: noquote(toeplitz(letters[1:4], LETTERS[20:26])) ## A convolution/smoother weight matrix : m <- 17 k <- length(wts <- c(76, 99, 60, 20, 1)) n <- m-k+1 ## Convolution W <- toeplitz2(c(rep(0, m-k), wts, rep(0, m-k)), ncol=n) ## "display" nicely : if(requireNamespace("Matrix")) print(Matrix::Matrix(W)) else { colnames(W) <- paste0(",", if(n <= 9) 1:n else c(1:9, letters[seq_len(n-9)])) print(W) } ## scale W to have column sums 1: W. <- W / sum(wts) all.equal(rep(1, ncol(W.)), colSums(W.), check.attributes = FALSE) ## Visualize "mass-preserving" convolution x <- 1:n; f <- function(x) exp(-((x - .4*n)/3)^2) y <- f(x) + rep_len(3:-2, n)/10 ## Smoothing convolution: y.hat <- W. %*% y # y.hat := smoothed(y) ("mass preserving" -> longer than y) stopifnot(length(y.hat) == m, m == n + (k-1)) plot(x,y, type="b", xlim=c(1,m)); curve(f(x), 1,n, col="gray", lty=2, add=TRUE) lines(1:m, y.hat, col=2, lwd=3) rbind(sum(y), sum(y.hat)) ## mass preserved ## And, yes, convolve(y, *) does the same when called appropriately: all.equal(c(y.hat), convolve(y, rev(wts/sum(wts)), type="open"))
x <- 1:5 toeplitz (x) T. <- toeplitz (1:5, 11:13) # with a *Warning* x[1] != r[1] T2 <- toeplitz2(c(13:12, 1:5), 5, 3)# this is the same matrix: stopifnot(identical(T., T2)) # Matrix of character (could also have logical, raw, complex ..) {also warning}: noquote(toeplitz(letters[1:4], LETTERS[20:26])) ## A convolution/smoother weight matrix : m <- 17 k <- length(wts <- c(76, 99, 60, 20, 1)) n <- m-k+1 ## Convolution W <- toeplitz2(c(rep(0, m-k), wts, rep(0, m-k)), ncol=n) ## "display" nicely : if(requireNamespace("Matrix")) print(Matrix::Matrix(W)) else { colnames(W) <- paste0(",", if(n <= 9) 1:n else c(1:9, letters[seq_len(n-9)])) print(W) } ## scale W to have column sums 1: W. <- W / sum(wts) all.equal(rep(1, ncol(W.)), colSums(W.), check.attributes = FALSE) ## Visualize "mass-preserving" convolution x <- 1:n; f <- function(x) exp(-((x - .4*n)/3)^2) y <- f(x) + rep_len(3:-2, n)/10 ## Smoothing convolution: y.hat <- W. %*% y # y.hat := smoothed(y) ("mass preserving" -> longer than y) stopifnot(length(y.hat) == m, m == n + (k-1)) plot(x,y, type="b", xlim=c(1,m)); curve(f(x), 1,n, col="gray", lty=2, add=TRUE) lines(1:m, y.hat, col=2, lwd=3) rbind(sum(y), sum(y.hat)) ## mass preserved ## And, yes, convolve(y, *) does the same when called appropriately: all.equal(c(y.hat), convolve(y, rev(wts/sum(wts)), type="open"))
The function ts
is used to create time-series objects.
as.ts
and is.ts
coerce an object to a time-series and
test whether an object is a time series.
ts(data = NA, start = 1, end = numeric(), frequency = 1, deltat = 1, ts.eps = getOption("ts.eps"), class = if(nseries > 1) c("mts", "ts", "matrix", "array") else "ts", names = ) as.ts(x, ...) is.ts(x) is.mts(x)
ts(data = NA, start = 1, end = numeric(), frequency = 1, deltat = 1, ts.eps = getOption("ts.eps"), class = if(nseries > 1) c("mts", "ts", "matrix", "array") else "ts", names = ) as.ts(x, ...) is.ts(x) is.mts(x)
data |
a vector or matrix of the observed time-series
values. A data frame will be coerced to a numeric matrix via
|
start |
the time of the first observation. Either a single number or a vector of two numbers (the second of which is an integer), which specify a natural time unit and a (1-based) number of samples into the time unit. See the examples for the use of the second form. |
end |
the time of the last observation, specified in the same way
as |
frequency |
the number of observations per unit of time. |
deltat |
the fraction of the sampling period between successive
observations; e.g., 1/12 for monthly data. Only one of
|
ts.eps |
time series comparison tolerance. Frequencies are
considered equal if their absolute difference is less than
|
class |
class to be given to the result, or none if |
names |
a character vector of names for the series in a multiple
series: defaults to the colnames of |
x |
an arbitrary R object. |
... |
arguments passed to methods (unused for the default method). |
The function ts
is used to create time-series objects. These
are vectors or matrices which inherit from class "ts"
(and have
additional
attributes) which represent data which has been sampled at equispaced
points in time. In the matrix case, each column of the matrix
data
is assumed to contain a single (univariate) time series.
Time series must have at least one observation, and although they need
not be numeric there is very limited support for non-numeric series.
Class "ts"
has a number of methods. In particular arithmetic
will attempt to align time axes, and subsetting to extract subsets of
series can be used (e.g., EuStockMarkets[, "DAX"]
). However,
subsetting the first (or only) dimension will return a matrix or
vector, as will matrix subsetting. Subassignment can be used to
replace values but not to extend a series (see window
).
There is a method for t
that transposes the series as a
matrix (a one-column matrix if a vector) and hence returns a result
that does not inherit from class "ts"
.
Argument frequency
indicates the sampling frequency of the
time series, with the default value 1
indicating one sample in
each unit time interval. For
example, one could use a value of 7
for frequency
when
the data are sampled daily, and the natural time period is a week, or
12
when the data are sampled monthly and the natural time
period is a year. Values of 4
and 12
are assumed in
(e.g.) print
methods to imply a quarterly and monthly series
respectively. frequency
need not be a whole
number: for example, frequency = 0.2
would imply sampling
once every five time units.
as.ts
is generic. Its default method will use the
tsp
attribute of the object if it has one to set the
start and end times and frequency.
is.ts()
tests if an object is a time series, i.e., inherits from
"ts"
and is of positive length.
is.mts(x)
tests if an object x
is a multivariate time series,
i.e., fulfills is.ts(x)
, is.matrix(x)
and inherits from
class "mts"
.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
tsp
,
frequency
,
start
,
end
,
time
,
window
;
print.ts
, the print method for time series objects;
plot.ts
, the plot method for time series objects.
For other definitions of ‘time series’ (e.g., time-ordered observations) see the CRAN task view at https://CRAN.R-project.org/view=TimeSeries.
require(graphics) ts(1:10, frequency = 4, start = c(1959, 2)) # 2nd Quarter of 1959 print( ts(1:10, frequency = 7, start = c(12, 2)), calendar = TRUE) # print.ts(.) ## Using July 1954 as start date: gnp <- ts(cumsum(1 + round(rnorm(100), 2)), start = c(1954, 7), frequency = 12) plot(gnp) # using 'plot.ts' for time-series plot ## Multivariate z <- ts(matrix(rnorm(300), 100, 3), start = c(1961, 1), frequency = 12) class(z) is.mts(z) head(z) # as "matrix" plot(z) plot(z, plot.type = "single", lty = 1:3) ## A phase plot: plot(nhtemp, lag(nhtemp, 1), cex = .8, col = "blue", main = "Lag plot of New Haven temperatures")
require(graphics) ts(1:10, frequency = 4, start = c(1959, 2)) # 2nd Quarter of 1959 print( ts(1:10, frequency = 7, start = c(12, 2)), calendar = TRUE) # print.ts(.) ## Using July 1954 as start date: gnp <- ts(cumsum(1 + round(rnorm(100), 2)), start = c(1954, 7), frequency = 12) plot(gnp) # using 'plot.ts' for time-series plot ## Multivariate z <- ts(matrix(rnorm(300), 100, 3), start = c(1961, 1), frequency = 12) class(z) is.mts(z) head(z) # as "matrix" plot(z) plot(z, plot.type = "single", lty = 1:3) ## A phase plot: plot(nhtemp, lag(nhtemp, 1), cex = .8, col = "blue", main = "Lag plot of New Haven temperatures")
Methods for objects of class "ts"
, typically the result of
ts
.
## S3 method for class 'ts' diff(x, lag = 1, differences = 1, ...) ## S3 method for class 'ts' na.omit(object, ...)
## S3 method for class 'ts' diff(x, lag = 1, differences = 1, ...) ## S3 method for class 'ts' na.omit(object, ...)
x |
an object of class |
lag |
an integer indicating which lag to use. |
differences |
an integer indicating the order of the difference. |
object |
a univariate or multivariate time series. |
... |
further arguments to be passed to or from methods. |
The na.omit
method omits initial and final segments with
missing values in one or more of the series. ‘Internal’
missing values will lead to failure.
For the na.omit
method, a time series without missing values.
The class of object
will be preserved.
diff
;
na.omit
, na.fail
,
na.contiguous
.
Plot several time series on a common plot. Unlike
plot.ts
the series can have a different time bases,
but they should have the same frequency.
ts.plot(..., gpars = list())
ts.plot(..., gpars = list())
... |
one or more univariate or multivariate time series. |
gpars |
list of named graphics parameters to be passed to the
plotting functions. Those commonly used can be supplied directly in
|
None.
Although this can be used for a single time series, plot
is
easier to use and is preferred.
require(graphics) ts.plot(ldeaths, mdeaths, fdeaths, gpars=list(xlab="year", ylab="deaths", lty=c(1:3)))
require(graphics) ts.plot(ldeaths, mdeaths, fdeaths, gpars=list(xlab="year", ylab="deaths", lty=c(1:3)))
Bind time series which have a common frequency. ts.union
pads
with NA
s to the total time coverage, ts.intersect
restricts to the time covered by all the series.
ts.intersect(..., dframe = FALSE) ts.union(..., dframe = FALSE)
ts.intersect(..., dframe = FALSE) ts.union(..., dframe = FALSE)
... |
two or more univariate or multivariate time series, or objects which can coerced to time series. |
dframe |
logical; if |
As a special case, ...
can contain vectors or matrices of the
same length as the combined time series of the time series present, as
well as those of a single row.
A time series object if dframe
is FALSE
, otherwise a
data frame.
ts.union(mdeaths, fdeaths) cbind(mdeaths, fdeaths) # same as the previous line ts.intersect(window(mdeaths, 1976), window(fdeaths, 1974, 1978)) sales1 <- ts.union(BJsales, lead = BJsales.lead) ts.intersect(sales1, lead3 = lag(BJsales.lead, -3))
ts.union(mdeaths, fdeaths) cbind(mdeaths, fdeaths) # same as the previous line ts.intersect(window(mdeaths, 1976), window(fdeaths, 1974, 1978)) sales1 <- ts.union(BJsales, lead = BJsales.lead) ts.intersect(sales1, lead3 = lag(BJsales.lead, -3))
A generic function to plot time-series diagnostics.
tsdiag(object, gof.lag, ...)
tsdiag(object, gof.lag, ...)
object |
a fitted time-series model |
gof.lag |
the maximum number of lags for a Portmanteau goodness-of-fit test |
... |
further arguments to be passed to particular methods |
This is a generic function. It will generally plot the residuals,
often standardized, the autocorrelation function of the residuals, and
the p-values of a Portmanteau test for all lags up to gof.lag
.
The methods for arima
and StructTS
objects
plots residuals scaled by the estimate of their (individual) variance,
and use the Ljung–Box version of the portmanteau test.
None. Diagnostics are plotted.
require(graphics) fit <- arima(lh, c(1,0,0)) tsdiag(fit) ## see also examples(arima) (fit <- StructTS(log10(JohnsonJohnson), type = "BSM")) tsdiag(fit)
require(graphics) fit <- arima(lh, c(1,0,0)) tsdiag(fit) ## see also examples(arima) (fit <- StructTS(log10(JohnsonJohnson), type = "BSM")) tsdiag(fit)
tsp
returns the tsp
attribute (or NULL
).
It is included for compatibility with S version 2. tsp<-
sets the tsp
attribute. hasTsp
ensures x
has a
tsp
attribute, by adding one if needed.
tsp(x) tsp(x) <- value hasTsp(x)
tsp(x) tsp(x) <- value hasTsp(x)
x |
a vector or matrix or univariate or multivariate time-series. |
value |
a numeric vector of length 3 or |
The tsp
attribute gives the start time in time units,
the end time and the frequency (the number of observations per unit of
time, e.g. 12 for a monthly series).
Assignments are checked for consistency.
Assigning NULL
which removes the tsp
attribute
and any "ts"
(or "mts"
) class of x
.
An object which differs from x
only in the tsp
attribute
(unless NULL
is assigned).
hasTsp
adds, if needed, an attribute with a start time and
frequency of 1 and end time NROW(x)
.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
Performs fixed-interval smoothing on a univariate time series via a state-space model. Fixed-interval smoothing gives the best estimate of the state at each time point based on the whole observed series.
tsSmooth(object, ...)
tsSmooth(object, ...)
object |
a time-series fit. Currently only class
|
... |
possible arguments for future methods. |
A time series, with as many dimensions as the state space and results at each time point of the original series. (For seasonal models, only the current seasonal component is returned.)
B. D. Ripley
Durbin, J. and Koopman, S. J. (2001) Time Series Analysis by State Space Methods. Oxford University Press.
For examples consult AirPassengers
,
JohnsonJohnson
and Nile
.
Functions of the distribution of the studentized range, ,
where
is the range of a standard normal sample and
is independently distributed as
chi-squared with
degrees of freedom, see
pchisq
.
ptukey(q, nmeans, df, nranges = 1, lower.tail = TRUE, log.p = FALSE) qtukey(p, nmeans, df, nranges = 1, lower.tail = TRUE, log.p = FALSE)
ptukey(q, nmeans, df, nranges = 1, lower.tail = TRUE, log.p = FALSE) qtukey(p, nmeans, df, nranges = 1, lower.tail = TRUE, log.p = FALSE)
q |
vector of quantiles. |
p |
vector of probabilities. |
nmeans |
sample size for range (same for each group). |
df |
degrees of freedom for |
nranges |
number of groups whose maximum range is considered. |
log.p |
logical; if TRUE, probabilities p are given as log(p). |
lower.tail |
logical; if TRUE (default), probabilities are
|
If nranges
is greater than one, is
the maximum of
groups of
nmeans
observations each.
ptukey
gives the distribution function and qtukey
its
inverse, the quantile function.
The length of the result is the maximum of the lengths of the numerical arguments. The other numerical arguments are recycled to that length. Only the first elements of the logical arguments are used.
A Legendre 16-point formula is used for the integral of ptukey
.
The computations are relatively expensive, especially for
qtukey
which uses a simple secant method for finding the
inverse of ptukey
.
qtukey
will be accurate to the 4th decimal place.
qtukey
is in part adapted from Odeh and Evans (1974).
Copenhaver, Margaret Diponzio and Holland, Burt S. (1988). Computation of the distribution of the maximum studentized range statistic with application to multiple significance testing of simple effects. Journal of Statistical Computation and Simulation, 30, 1–15. doi:10.1080/00949658808811082.
Odeh, R. E. and Evans, J. O. (1974). Algorithm AS 70: Percentage Points of the Normal Distribution. Applied Statistics, 23, 96–97. doi:10.2307/2347061.
Distributions for standard distributions, including
pnorm
and qnorm
for the corresponding
functions for the normal distribution.
if(interactive()) curve(ptukey(x, nm = 6, df = 5), from = -1, to = 8, n = 101) (ptt <- ptukey(0:10, 2, df = 5)) (qtt <- qtukey(.95, 2, df = 2:11)) ## The precision may be not much more than about 8 digits: summary(abs(.95 - ptukey(qtt, 2, df = 2:11)))
if(interactive()) curve(ptukey(x, nm = 6, df = 5), from = -1, to = 8, n = 101) (ptt <- ptukey(0:10, 2, df = 5)) (qtt <- qtukey(.95, 2, df = 2:11)) ## The precision may be not much more than about 8 digits: summary(abs(.95 - ptukey(qtt, 2, df = 2:11)))
Create a set of confidence intervals on the differences between the means of the levels of a factor with the specified family-wise probability of coverage. The intervals are based on the Studentized range statistic, Tukey's ‘Honest Significant Difference’ method.
TukeyHSD(x, which, ordered = FALSE, conf.level = 0.95, ...)
TukeyHSD(x, which, ordered = FALSE, conf.level = 0.95, ...)
x |
A fitted model object, usually an |
which |
A character vector listing terms in the fitted model for which the intervals should be calculated. Defaults to all the terms. |
ordered |
A logical value indicating if the levels of the factor
should be ordered according to increasing average in the sample
before taking differences. If |
conf.level |
A numeric value between zero and one giving the family-wise confidence level to use. |
... |
Optional additional arguments. None are used at present. |
This is a generic function: the description here applies to the method
for fits of class "aov"
.
When comparing the means for the levels of a factor in an analysis of variance, a simple comparison using t-tests will inflate the probability of declaring a significant difference when it is not in fact present. This because the intervals are calculated with a given coverage probability for each interval but the interpretation of the coverage is usually with respect to the entire family of intervals.
John Tukey introduced intervals based on the range of the sample means rather than the individual differences. The intervals returned by this function are based on this Studentized range statistics.
The intervals constructed in this way would only apply exactly to balanced designs where there are the same number of observations made at each level of the factor. This function incorporates an adjustment for sample size that produces sensible intervals for mildly unbalanced designs.
If which
specifies non-factor terms these will be dropped with
a warning: if no terms are left this is an error.
A list of class c("multicomp", "TukeyHSD")
,
with one component for each term requested in which
.
Each component is a matrix with columns diff
giving the
difference in the observed means, lwr
giving the lower
end point of the interval, upr
giving the upper end point
and p adj
giving the p-value after adjustment for the multiple
comparisons.
There are print
and plot
methods for class
"TukeyHSD"
. The plot
method does not accept
xlab
, ylab
or main
arguments and creates its own
values for each plot.
Douglas Bates
Miller, R. G. (1981) Simultaneous Statistical Inference. Springer.
Yandell, B. S. (1997) Practical Data Analysis for Designed Experiments. Chapman & Hall.
aov
, qtukey
, model.tables
,
glht
in package multcomp.
require(graphics) summary(fm1 <- aov(breaks ~ wool + tension, data = warpbreaks)) TukeyHSD(fm1, "tension", ordered = TRUE) plot(TukeyHSD(fm1, "tension"))
require(graphics) summary(fm1 <- aov(breaks ~ wool + tension, data = warpbreaks)) TukeyHSD(fm1, "tension", ordered = TRUE) plot(TukeyHSD(fm1, "tension"))
These functions provide information about the uniform distribution
on the interval from min
to max
. dunif
gives the
density, punif
gives the distribution function qunif
gives the quantile function and runif
generates random
deviates.
dunif(x, min = 0, max = 1, log = FALSE) punif(q, min = 0, max = 1, lower.tail = TRUE, log.p = FALSE) qunif(p, min = 0, max = 1, lower.tail = TRUE, log.p = FALSE) runif(n, min = 0, max = 1)
dunif(x, min = 0, max = 1, log = FALSE) punif(q, min = 0, max = 1, lower.tail = TRUE, log.p = FALSE) qunif(p, min = 0, max = 1, lower.tail = TRUE, log.p = FALSE) runif(n, min = 0, max = 1)
x , q
|
vector of quantiles. |
p |
vector of probabilities. |
n |
number of observations. If |
min , max
|
lower and upper limits of the distribution. Must be finite. |
log , log.p
|
logical; if TRUE, probabilities p are given as log(p). |
lower.tail |
logical; if TRUE (default), probabilities are
|
If min
or max
are not specified they assume the default
values of 0
and 1
respectively.
The uniform distribution has density
for .
For the case of , the limit case of
is assumed, although there is no density in
that case and
dunif
will return NaN
(the error condition).
runif
will not generate either of the extreme values unless
max = min
or max-min
is small compared to min
,
and in particular not for the default arguments.
dunif
gives the density,
punif
gives the distribution function,
qunif
gives the quantile function, and
runif
generates random deviates.
The length of the result is determined by n
for
runif
, and is the maximum of the lengths of the
numerical arguments for the other functions.
The numerical arguments other than n
are recycled to the
length of the result. Only the first elements of the logical
arguments are used.
The characteristics of output from pseudo-random number generators
(such as precision and periodicity) vary widely. See
.Random.seed
for more information on R's random number
generation algorithms.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
RNG
about random number generation in R.
Distributions for other standard distributions.
u <- runif(20) ## The following relations always hold : punif(u) == u dunif(u) == 1 var(runif(10000)) #- ~ = 1/12 = .08333
u <- runif(20) ## The following relations always hold : punif(u) == u dunif(u) == 1 var(runif(10000)) #- ~ = 1/12 = .08333
The function uniroot
searches the interval from lower
to upper
for a root (i.e., zero) of the function f
with
respect to its first argument.
Setting extendInt
to a non-"no"
string, means searching
for the correct interval = c(lower,upper)
if sign(f(x))
does not satisfy the requirements at the interval end points; see the
‘Details’ section.
uniroot(f, interval, ..., lower = min(interval), upper = max(interval), f.lower = f(lower, ...), f.upper = f(upper, ...), extendInt = c("no", "yes", "downX", "upX"), check.conv = FALSE, tol = .Machine$double.eps^0.25, maxiter = 1000, trace = 0)
uniroot(f, interval, ..., lower = min(interval), upper = max(interval), f.lower = f(lower, ...), f.upper = f(upper, ...), extendInt = c("no", "yes", "downX", "upX"), check.conv = FALSE, tol = .Machine$double.eps^0.25, maxiter = 1000, trace = 0)
f |
the function for which the root is sought. |
interval |
a vector containing the end-points of the interval to be searched for the root. |
... |
additional named or unnamed arguments to be passed
to |
lower , upper
|
the lower and upper end points of the interval to be searched. |
f.lower , f.upper
|
the same as |
extendInt |
character string specifying if the interval
|
check.conv |
logical indicating whether a convergence warning of the
underlying |
tol |
the desired accuracy (convergence tolerance). |
maxiter |
the maximum number of iterations. |
trace |
integer number; if positive, tracing information is produced. Higher values giving more details. |
Note that arguments after ...
must be matched exactly.
Either interval
or both lower
and upper
must be
specified: the upper endpoint must be strictly larger than the lower
endpoint.
The function values at the endpoints must be of opposite signs (or
zero), for extendInt="no"
, the default. Otherwise, if
extendInt="yes"
, the interval is extended on both sides, in
search of a sign change, i.e., until the search interval
satisfies
.
If it is known how changes sign at the root
, that is, if the function is increasing or decreasing there,
extendInt
can (and typically should) be specified as
"upX"
(for “upward crossing”) or "downX"
,
respectively. Equivalently, define , to
require
at the solution. In that case, the search interval
possibly is extended to be such that
and
.
uniroot()
uses Fortran subroutine zeroin
(from Netlib)
based on algorithms given in the reference below. They assume a
continuous function (which then is known to have at least one root in
the interval).
Convergence is declared either if f(x) == 0
or the change in
x
for one step of the algorithm is less than tol
(plus an
allowance for representation error in x
).
If the algorithm does not converge in maxiter
steps, a warning
is printed and the current approximation is returned.
f
will be called as f(x, ...)
for a numeric value
of x.
The argument passed to f
has special semantics and used to be
shared between calls. The function should not copy it.
A list with at least five components: root
and f.root
give the location of the root and the value of the function evaluated
at that point. iter
and estim.prec
give the number of
iterations used and an approximate estimated precision for
root
. (If the root occurs at one of the endpoints, the
estimated precision is NA
.)
init.it
contains the number of initial extendInt
iterations if there were any and is NA
otherwise.
In the case of such extendInt
iterations, iter
contains the
sum
of these and the zeroin
iterations.
Further components may be added in the future.
Based on ‘zeroin.c’ in https://netlib.org/c/brent.shar.
Brent, R. (1973) Algorithms for Minimization without Derivatives. Englewood Cliffs, NJ: Prentice-Hall.
polyroot
for all complex roots of a polynomial;
optimize
, nlm
.
require(utils) # for str ## some platforms hit zero exactly on the first step: ## if so the estimated precision is 2/3. f <- function (x, a) x - a str(xmin <- uniroot(f, c(0, 1), tol = 0.0001, a = 1/3)) ## handheld calculator example: fixed point of cos(.): uniroot(function(x) cos(x) - x, lower = -pi, upper = pi, tol = 1e-9)$root str(uniroot(function(x) x*(x^2-1) + .5, lower = -2, upper = 2, tol = 0.0001)) str(uniroot(function(x) x*(x^2-1) + .5, lower = -2, upper = 2, tol = 1e-10)) ## Find the smallest value x for which exp(x) > 0 (numerically): r <- uniroot(function(x) 1e80*exp(x) - 1e-300, c(-1000, 0), tol = 1e-15) str(r, digits.d = 15) # around -745, depending on the platform. exp(r$root) # = 0, but not for r$root * 0.999... minexp <- r$root * (1 - 10*.Machine$double.eps) exp(minexp) # typically denormalized ##--- uniroot() with new interval extension + checking features: -------------- f1 <- function(x) (121 - x^2)/(x^2+1) f2 <- function(x) exp(-x)*(x - 12) try(uniroot(f1, c(0,10))) try(uniroot(f2, c(0, 2))) ##--> error: f() .. end points not of opposite sign ## where as 'extendInt="yes"' simply first enlarges the search interval: u1 <- uniroot(f1, c(0,10),extendInt="yes", trace=1) u2 <- uniroot(f2, c(0,2), extendInt="yes", trace=2) stopifnot(all.equal(u1$root, 11, tolerance = 1e-5), all.equal(u2$root, 12, tolerance = 6e-6)) ## The *danger* of interval extension: ## No way to find a zero of a positive function, but ## numerically, f(-|M|) becomes zero : u3 <- uniroot(exp, c(0,2), extendInt="yes", trace=TRUE) ## Nonsense example (must give an error): tools::assertCondition( uniroot(function(x) 1, 0:1, extendInt="yes"), "error", verbose=TRUE) ## Convergence checking : sinc <- function(x) ifelse(x == 0, 1, sin(x)/x) curve(sinc, -6,18); abline(h=0,v=0, lty=3, col=adjustcolor("gray", 0.8)) uniroot(sinc, c(0,5), extendInt="yes", maxiter=4) #-> "just" a warning ## now with check.conv=TRUE, must signal a convergence error : uniroot(sinc, c(0,5), extendInt="yes", maxiter=4, check.conv=TRUE) ### Weibull cumulative hazard (example origin, Ravi Varadhan): cumhaz <- function(t, a, b) b * (t/b)^a froot <- function(x, u, a, b) cumhaz(x, a, b) - u n <- 1000 u <- -log(runif(n)) a <- 1/2 b <- 1 ## Find failure times ru <- sapply(u, function(x) uniroot(froot, u=x, a=a, b=b, interval= c(1.e-14, 1e04), extendInt="yes")$root) ru2 <- sapply(u, function(x) uniroot(froot, u=x, a=a, b=b, interval= c(0.01, 10), extendInt="yes")$root) stopifnot(all.equal(ru, ru2, tolerance = 6e-6)) r1 <- uniroot(froot, u= 0.99, a=a, b=b, interval= c(0.01, 10), extendInt="up") stopifnot(all.equal(0.99, cumhaz(r1$root, a=a, b=b))) ## An error if 'extendInt' assumes "wrong zero-crossing direction": uniroot(froot, u= 0.99, a=a, b=b, interval= c(0.1, 10), extendInt="down")
require(utils) # for str ## some platforms hit zero exactly on the first step: ## if so the estimated precision is 2/3. f <- function (x, a) x - a str(xmin <- uniroot(f, c(0, 1), tol = 0.0001, a = 1/3)) ## handheld calculator example: fixed point of cos(.): uniroot(function(x) cos(x) - x, lower = -pi, upper = pi, tol = 1e-9)$root str(uniroot(function(x) x*(x^2-1) + .5, lower = -2, upper = 2, tol = 0.0001)) str(uniroot(function(x) x*(x^2-1) + .5, lower = -2, upper = 2, tol = 1e-10)) ## Find the smallest value x for which exp(x) > 0 (numerically): r <- uniroot(function(x) 1e80*exp(x) - 1e-300, c(-1000, 0), tol = 1e-15) str(r, digits.d = 15) # around -745, depending on the platform. exp(r$root) # = 0, but not for r$root * 0.999... minexp <- r$root * (1 - 10*.Machine$double.eps) exp(minexp) # typically denormalized ##--- uniroot() with new interval extension + checking features: -------------- f1 <- function(x) (121 - x^2)/(x^2+1) f2 <- function(x) exp(-x)*(x - 12) try(uniroot(f1, c(0,10))) try(uniroot(f2, c(0, 2))) ##--> error: f() .. end points not of opposite sign ## where as 'extendInt="yes"' simply first enlarges the search interval: u1 <- uniroot(f1, c(0,10),extendInt="yes", trace=1) u2 <- uniroot(f2, c(0,2), extendInt="yes", trace=2) stopifnot(all.equal(u1$root, 11, tolerance = 1e-5), all.equal(u2$root, 12, tolerance = 6e-6)) ## The *danger* of interval extension: ## No way to find a zero of a positive function, but ## numerically, f(-|M|) becomes zero : u3 <- uniroot(exp, c(0,2), extendInt="yes", trace=TRUE) ## Nonsense example (must give an error): tools::assertCondition( uniroot(function(x) 1, 0:1, extendInt="yes"), "error", verbose=TRUE) ## Convergence checking : sinc <- function(x) ifelse(x == 0, 1, sin(x)/x) curve(sinc, -6,18); abline(h=0,v=0, lty=3, col=adjustcolor("gray", 0.8)) uniroot(sinc, c(0,5), extendInt="yes", maxiter=4) #-> "just" a warning ## now with check.conv=TRUE, must signal a convergence error : uniroot(sinc, c(0,5), extendInt="yes", maxiter=4, check.conv=TRUE) ### Weibull cumulative hazard (example origin, Ravi Varadhan): cumhaz <- function(t, a, b) b * (t/b)^a froot <- function(x, u, a, b) cumhaz(x, a, b) - u n <- 1000 u <- -log(runif(n)) a <- 1/2 b <- 1 ## Find failure times ru <- sapply(u, function(x) uniroot(froot, u=x, a=a, b=b, interval= c(1.e-14, 1e04), extendInt="yes")$root) ru2 <- sapply(u, function(x) uniroot(froot, u=x, a=a, b=b, interval= c(0.01, 10), extendInt="yes")$root) stopifnot(all.equal(ru, ru2, tolerance = 6e-6)) r1 <- uniroot(froot, u= 0.99, a=a, b=b, interval= c(0.01, 10), extendInt="up") stopifnot(all.equal(0.99, cumhaz(r1$root, a=a, b=b))) ## An error if 'extendInt' assumes "wrong zero-crossing direction": uniroot(froot, u= 0.99, a=a, b=b, interval= c(0.1, 10), extendInt="down")
update
will update and (by default) re-fit a model. It does this
by extracting the call stored in the object, updating the call and (by
default) evaluating that call. Sometimes it is useful to call
update
with only one argument, for example if the data frame has
been corrected.
“Extracting the call” in update()
and similar functions
uses getCall()
which itself is a (S3) generic function with a
default method that simply gets x$call
.
Because of this, update()
will often work (via its default
method) on new model classes, either automatically, or by providing a
simple getCall()
method for that class.
update(object, ...) ## Default S3 method: update(object, formula., ..., evaluate = TRUE) getCall(x, ...)
update(object, ...) ## Default S3 method: update(object, formula., ..., evaluate = TRUE) getCall(x, ...)
object , x
|
An existing fit from a model function such as |
formula. |
Changes to the formula – see |
... |
Additional arguments to the call, or arguments with
changed values. Use |
evaluate |
If true evaluate the new call else return the call. |
If evaluate = TRUE
the fitted object, otherwise the updated call.
Chambers, J. M. (1992) Linear models. Chapter 4 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
oldcon <- options(contrasts = c("contr.treatment", "contr.poly")) ## Annette Dobson (1990) "An Introduction to Generalized Linear Models". ## Page 9: Plant Weight Data. ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69) group <- gl(2, 10, 20, labels = c("Ctl", "Trt")) weight <- c(ctl, trt) lm.D9 <- lm(weight ~ group) lm.D9 summary(lm.D90 <- update(lm.D9, . ~ . - 1)) options(contrasts = c("contr.helmert", "contr.poly")) update(lm.D9) getCall(lm.D90) # "through the origin" options(oldcon)
oldcon <- options(contrasts = c("contr.treatment", "contr.poly")) ## Annette Dobson (1990) "An Introduction to Generalized Linear Models". ## Page 9: Plant Weight Data. ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69) group <- gl(2, 10, 20, labels = c("Ctl", "Trt")) weight <- c(ctl, trt) lm.D9 <- lm(weight ~ group) lm.D9 summary(lm.D90 <- update(lm.D9, . ~ . - 1)) options(contrasts = c("contr.helmert", "contr.poly")) update(lm.D9) getCall(lm.D90) # "through the origin" options(oldcon)
update.formula
is used to update model formulae.
This typically involves adding or dropping terms,
but updates can be more general.
## S3 method for class 'formula' update(old, new, ...)
## S3 method for class 'formula' update(old, new, ...)
old |
a model formula to be updated. |
new |
a formula giving a template which specifies how to update. |
... |
further arguments passed to or from other methods. |
Either or both of old
and new
can be objects such as
length-one character vectors which can be coerced to a formula via
as.formula
.
The function works by first identifying the left-hand side
and right-hand side of the old
formula.
It then examines the new
formula and substitutes
the lhs of the old
formula for any occurrence
of ‘.’ on the left of new
, and substitutes
the rhs of the old
formula for any occurrence
of ‘.’ on the right of new
. The result is then
simplified via terms.formula(simplify = TRUE)
.
The updated formula is returned. The environment of the result is
that of old
.
update(y ~ x, ~ . + x2) #> y ~ x + x2 update(y ~ x, log(.) ~ . ) #> log(y) ~ x update(. ~ u+v, res ~ . ) #> res ~ u + v
update(y ~ x, ~ . + x2) #> y ~ x + x2 update(y ~ x, log(.) ~ . ) #> log(y) ~ x update(. ~ u+v, res ~ . ) #> res ~ u + v
Performs an F test to compare the variances of two samples from normal populations.
var.test(x, ...) ## Default S3 method: var.test(x, y, ratio = 1, alternative = c("two.sided", "less", "greater"), conf.level = 0.95, ...) ## S3 method for class 'formula' var.test(formula, data, subset, na.action, ...)
var.test(x, ...) ## Default S3 method: var.test(x, y, ratio = 1, alternative = c("two.sided", "less", "greater"), conf.level = 0.95, ...) ## S3 method for class 'formula' var.test(formula, data, subset, na.action, ...)
x , y
|
numeric vectors of data values, or fitted linear model
objects (inheriting from class |
ratio |
the hypothesized ratio of the population variances of
|
alternative |
a character string specifying the alternative
hypothesis, must be one of |
conf.level |
confidence level for the returned confidence interval. |
formula |
a formula of the form |
data |
an optional matrix or data frame (or similar: see
|
subset |
an optional vector specifying a subset of observations to be used. |
na.action |
a function which indicates what should happen when
the data contain |
... |
further arguments to be passed to or from methods. |
The null hypothesis is that the ratio of the variances of the
populations from which x
and y
were drawn, or in the
data to which the linear models x
and y
were fitted, is
equal to ratio
.
A list with class "htest"
containing the following components:
statistic |
the value of the F test statistic. |
parameter |
the degrees of the freedom of the F distribution of the test statistic. |
p.value |
the p-value of the test. |
conf.int |
a confidence interval for the ratio of the population variances. |
estimate |
the ratio of the sample variances of |
null.value |
the ratio of population variances under the null. |
alternative |
a character string describing the alternative hypothesis. |
method |
the character string
|
data.name |
a character string giving the names of the data. |
bartlett.test
for testing homogeneity of variances in
more than two samples from normal distributions;
ansari.test
and mood.test
for two rank
based (nonparametric) two-sample tests for difference in scale.
x <- rnorm(50, mean = 0, sd = 2) y <- rnorm(30, mean = 1, sd = 1) var.test(x, y) # Do x and y have the same variance? var.test(lm(x ~ 1), lm(y ~ 1)) # The same.
x <- rnorm(50, mean = 0, sd = 2) y <- rnorm(30, mean = 1, sd = 1) var.test(x, y) # Do x and y have the same variance? var.test(lm(x ~ 1), lm(y ~ 1)) # The same.
These functions ‘rotate’ loading matrices in factor analysis.
varimax(x, normalize = TRUE, eps = 1e-5) promax(x, m = 4)
varimax(x, normalize = TRUE, eps = 1e-5) promax(x, m = 4)
x |
A loadings matrix, with |
m |
The power used the target for |
normalize |
logical. Should Kaiser normalization be performed?
If so the rows of |
eps |
The tolerance for stopping: the relative change in the sum of singular values. |
These seek a ‘rotation’ of the factors x %*% T
that
aims to clarify the structure of the loadings matrix. The matrix
T
is a rotation (possibly with reflection) for varimax
,
but a general linear transformation for promax
, with the
variance of the factors being preserved.
A list with components
loadings |
The ‘rotated’ loadings matrix,
|
rotmat |
The ‘rotation’ matrix. |
Hendrickson, A. E. and White, P. O. (1964). Promax: a quick method for rotation to orthogonal oblique structure. British Journal of Statistical Psychology, 17, 65–70. doi:10.1111/j.2044-8317.1964.tb00244.x.
Horst, P. (1965). Factor Analysis of Data Matrices. Holt, Rinehart and Winston. Chapter 10.
Kaiser, H. F. (1958). The varimax criterion for analytic rotation in factor analysis. Psychometrika, 23, 187–200. doi:10.1007/BF02289233.
Lawley, D. N. and Maxwell, A. E. (1971). Factor Analysis as a Statistical Method, second edition. Butterworths.
## varimax with normalize = TRUE is the default fa <- factanal( ~., 2, data = swiss) varimax(loadings(fa), normalize = FALSE) promax(loadings(fa))
## varimax with normalize = TRUE is the default fa <- factanal( ~., 2, data = swiss) varimax(loadings(fa), normalize = FALSE) promax(loadings(fa))
Returns the variance-covariance matrix of the main parameters of
a fitted model object. The “main” parameters of model
correspond to those returned by coef
, and typically do
not contain a nuisance scale parameter (sigma
).
vcov(object, ...) ## S3 method for class 'lm' vcov(object, complete = TRUE, ...) ## and also for '[summary.]glm' and 'mlm' ## S3 method for class 'aov' vcov(object, complete = FALSE, ...) .vcov.aliased(aliased, vc, complete = TRUE)
vcov(object, ...) ## S3 method for class 'lm' vcov(object, complete = TRUE, ...) ## and also for '[summary.]glm' and 'mlm' ## S3 method for class 'aov' vcov(object, complete = FALSE, ...) .vcov.aliased(aliased, vc, complete = TRUE)
object |
a fitted model object, typically. Sometimes also a
|
complete |
for the |
... |
additional arguments for method functions. For the
|
aliased |
a |
vc |
a variance-covariance matrix, typically “incomplete”, i.e., with no rows and columns for aliased coefficients. |
vcov()
is a generic function and functions with names beginning
in vcov.
will be methods for this function.
Classes with methods for this function include:
lm
, mlm
, glm
, nls
,
summary.lm
, summary.glm
,
negbin
, polr
, rlm
(in package MASS),
multinom
(in package nnet)
gls
, lme
(in package nlme),
coxph
and survreg
(in package survival).
(vcov()
methods for summary objects allow more
efficient and still encapsulated access when both
summary(mod)
and vcov(mod)
are needed.)
.vcov.aliased()
is an auxiliary function useful for
vcov
method implementations which have to deal with singular
model fits encoded via NA coefficients: It augments a vcov–matrix
vc
by NA
rows and columns where needed, i.e., when
some entries of aliased
are true and vc
is of smaller dimension
than length(aliased)
.
A matrix of the estimated covariances between the parameter estimates
in the linear or non-linear predictor of the model. This should have
row and column names corresponding to the parameter names given by the
coef
method.
When some coefficients of the (linear) model are undetermined and
hence NA
because of linearly dependent terms (or an
“over specified” model), also called
“aliased”, see alias
, then since R version 3.5.0,
vcov()
(iff complete = TRUE
, i.e., by default for
lm
etc, but not for aov
) contains corresponding rows and
columns of NA
s, wherever coef()
has always
contained such NA
s.
Density, distribution function, quantile function and random
generation for the Weibull distribution with parameters shape
and scale
.
dweibull(x, shape, scale = 1, log = FALSE) pweibull(q, shape, scale = 1, lower.tail = TRUE, log.p = FALSE) qweibull(p, shape, scale = 1, lower.tail = TRUE, log.p = FALSE) rweibull(n, shape, scale = 1)
dweibull(x, shape, scale = 1, log = FALSE) pweibull(q, shape, scale = 1, lower.tail = TRUE, log.p = FALSE) qweibull(p, shape, scale = 1, lower.tail = TRUE, log.p = FALSE) rweibull(n, shape, scale = 1)
x , q
|
vector of quantiles. |
p |
vector of probabilities. |
n |
number of observations. If |
shape , scale
|
shape and scale parameters, the latter defaulting to 1. |
log , log.p
|
logical; if TRUE, probabilities p are given as log(p). |
lower.tail |
logical; if TRUE (default), probabilities are
|
The Weibull distribution with shape
parameter and
scale
parameter has density given by
for .
The cumulative distribution function is
on
, the
mean is
, and
the
.
dweibull
gives the density,
pweibull
gives the distribution function,
qweibull
gives the quantile function, and
rweibull
generates random deviates.
Invalid arguments will result in return value NaN
, with a warning.
The length of the result is determined by n
for
rweibull
, and is the maximum of the lengths of the
numerical arguments for the other functions.
The numerical arguments other than n
are recycled to the
length of the result. Only the first elements of the logical
arguments are used.
The cumulative hazard
is
-pweibull(t, a, b, lower = FALSE, log = TRUE)
which is just .
[dpq]weibull
are calculated directly from the definitions.
rweibull
uses inversion.
Johnson, N. L., Kotz, S. and Balakrishnan, N. (1995) Continuous Univariate Distributions, volume 1, chapter 21. Wiley, New York.
Distributions for other standard distributions, including the Exponential which is a special case of the Weibull distribution.
x <- c(0, rlnorm(50)) all.equal(dweibull(x, shape = 1), dexp(x)) all.equal(pweibull(x, shape = 1, scale = pi), pexp(x, rate = 1/pi)) ## Cumulative hazard H(): all.equal(pweibull(x, 2.5, pi, lower.tail = FALSE, log.p = TRUE), -(x/pi)^2.5, tolerance = 1e-15) all.equal(qweibull(x/11, shape = 1, scale = pi), qexp(x/11, rate = 1/pi))
x <- c(0, rlnorm(50)) all.equal(dweibull(x, shape = 1), dexp(x)) all.equal(pweibull(x, shape = 1, scale = pi), pexp(x, rate = 1/pi)) ## Cumulative hazard H(): all.equal(pweibull(x, 2.5, pi, lower.tail = FALSE, log.p = TRUE), -(x/pi)^2.5, tolerance = 1e-15) all.equal(qweibull(x/11, shape = 1, scale = pi), qexp(x/11, rate = 1/pi))
Compute a weighted mean.
weighted.mean(x, w, ...) ## Default S3 method: weighted.mean(x, w, ..., na.rm = FALSE)
weighted.mean(x, w, ...) ## Default S3 method: weighted.mean(x, w, ..., na.rm = FALSE)
x |
an object containing the values whose weighted mean is to be computed. |
w |
a numerical vector of weights the same length as |
... |
arguments to be passed to or from methods. |
na.rm |
a logical value indicating whether |
This is a generic function and methods can be defined for the first
argument x
: apart from the default methods there are methods
for the date-time classes "POSIXct"
, "POSIXlt"
,
"difftime"
and "Date"
. The default method will work for
any numeric-like object for which [
, multiplication, division
and sum
have suitable methods, including complex vectors.
If w
is missing then all elements of x
are given the
same weight, otherwise the weights
are normalized to sum to one (if possible: if
their sum is zero or infinite the value is likely to be NaN
).
Missing values in w
are not handled specially and so give a
missing value as the result. However, zero weights are handled
specially and the corresponding x
values are omitted from the
sum.
For the default method, a length-one numeric vector.
## GPA from Siegel 1994 wt <- c(5, 5, 4, 1)/15 x <- c(3.7,3.3,3.5,2.8) xm <- weighted.mean(x, wt)
## GPA from Siegel 1994 wt <- c(5, 5, 4, 1)/15 x <- c(3.7,3.3,3.5,2.8) xm <- weighted.mean(x, wt)
Computed weighted residuals from a linear model fit.
weighted.residuals(obj, drop0 = TRUE)
weighted.residuals(obj, drop0 = TRUE)
obj |
|
drop0 |
logical. If |
Weighted residuals are based on the deviance residuals, which for
a lm
fit are the raw residuals
multiplied by
, where
are the
weights
as specified in lm
's call.
Dropping cases with weights zero is compatible with
influence
and related functions.
Numeric vector of length , where
is the number
of non-0 weights (
drop0 = TRUE
) or the number of
observations, otherwise.
residuals
, lm.influence
, etc.
## following on from example(lm) all.equal(weighted.residuals(lm.D9), residuals(lm.D9)) x <- 1:10 w <- 0:9 y <- rnorm(x) weighted.residuals(lmxy <- lm(y ~ x, weights = w)) weighted.residuals(lmxy, drop0 = FALSE)
## following on from example(lm) all.equal(weighted.residuals(lm.D9), residuals(lm.D9)) x <- 1:10 w <- 0:9 y <- rnorm(x) weighted.residuals(lmxy <- lm(y ~ x, weights = w)) weighted.residuals(lmxy, drop0 = FALSE)
weights
is a generic function which extracts fitting weights from
objects returned by modeling functions.
Methods can make use of napredict
methods to compensate
for the omission of missing values. The default methods does so.
weights(object, ...)
weights(object, ...)
object |
an object for which the extraction of model weights is meaningful. |
... |
other arguments passed to methods. |
Weights extracted from the object object
: the default method
looks for component "weights"
and if not NULL
calls
napredict
on it.
Chambers, J. M. and Hastie, T. J. (1992) Statistical Models in S. Wadsworth & Brooks/Cole.
Performs one- and two-sample Wilcoxon tests on vectors of data; the latter is also known as ‘Mann-Whitney’ test.
wilcox.test(x, ...) ## Default S3 method: wilcox.test(x, y = NULL, alternative = c("two.sided", "less", "greater"), mu = 0, paired = FALSE, exact = NULL, correct = TRUE, conf.int = FALSE, conf.level = 0.95, tol.root = 1e-4, digits.rank = Inf, ...) ## S3 method for class 'formula' wilcox.test(formula, data, subset, na.action = na.pass, ...)
wilcox.test(x, ...) ## Default S3 method: wilcox.test(x, y = NULL, alternative = c("two.sided", "less", "greater"), mu = 0, paired = FALSE, exact = NULL, correct = TRUE, conf.int = FALSE, conf.level = 0.95, tol.root = 1e-4, digits.rank = Inf, ...) ## S3 method for class 'formula' wilcox.test(formula, data, subset, na.action = na.pass, ...)
x |
numeric vector of data values. Non-finite (e.g., infinite or missing) values will be omitted. |
y |
an optional numeric vector of data values: as with |
alternative |
a character string specifying the alternative
hypothesis, must be one of |
mu |
a number specifying an optional parameter used to form the null hypothesis. See ‘Details’. |
paired |
a logical indicating whether you want a paired test. |
exact |
a logical indicating whether an exact p-value should be computed. |
correct |
a logical indicating whether to apply continuity correction in the normal approximation for the p-value. |
conf.int |
a logical indicating whether a confidence interval should be computed. |
conf.level |
confidence level of the interval. |
tol.root |
(when |
digits.rank |
a number; if finite, |
formula |
a formula of the form |
data |
an optional matrix or data frame (or similar: see
|
subset |
an optional vector specifying a subset of observations to be used. |
na.action |
a function which indicates what should happen when
the data contain |
... |
further arguments to be passed to or from methods.
For the |
The formula interface is only applicable for the 2-sample tests.
If only x
is given, or if both x
and y
are given
and paired
is TRUE
, a Wilcoxon signed rank test of the
null that the distribution of x
(in the one sample case) or of
x - y
(in the paired two sample case) is symmetric about
mu
is performed.
Otherwise, if both x
and y
are given and paired
is FALSE
, a Wilcoxon rank sum test (equivalent to the
Mann-Whitney test: see the Note) is carried out. In this case, the
null hypothesis is that the distributions of x
and y
differ by a location shift of mu
and the alternative is that
they differ by some other location shift (and the one-sided
alternative "greater"
is that x
is shifted to the right
of y
).
By default (if exact
is not specified), an exact p-value
is computed if the samples contain less than 50 finite values and
there are no ties. Otherwise, a normal approximation is used.
For stability reasons, it may be advisable to use rounded data or to set
digits.rank = 7
, say, such that determination of ties does not
depend on very small numeric differences (see the example).
Optionally (if argument conf.int
is true), a nonparametric
confidence interval and an estimator for the pseudomedian (one-sample
case) or for the difference of the location parameters x-y
is
computed. (The pseudomedian of a distribution is the median
of the distribution of
, where
and
are
independent, each with distribution
. If
is symmetric,
then the pseudomedian and median coincide. See Hollander & Wolfe
(1973), page 34.) Note that in the two-sample case the estimator for
the difference in location parameters does not estimate the
difference in medians (a common misconception) but rather the median
of the difference between a sample from
x
and a sample from
y
.
If exact p-values are available, an exact confidence interval is
obtained by the algorithm described in Bauer (1972), and the
Hodges-Lehmann estimator is employed. Otherwise, the returned
confidence interval and point estimate are based on normal
approximations. These are continuity-corrected for the interval but
not the estimate (as the correction depends on the
alternative
).
With small samples it may not be possible to achieve very high confidence interval coverages. If this happens a warning will be given and an interval with lower coverage will be substituted.
When x
(and y
if applicable) are valid, the function now
always returns, also in the conf.int = TRUE
case when a
confidence interval cannot be computed, in which case the interval
boundaries and sometimes the estimate
now contain
NaN
.
A list with class "htest"
containing the following components:
statistic |
the value of the test statistic with a name describing it. |
parameter |
the parameter(s) for the exact distribution of the test statistic. |
p.value |
the p-value for the test. |
null.value |
the location parameter |
alternative |
a character string describing the alternative hypothesis. |
method |
the type of test applied. |
data.name |
a character string giving the names of the data. |
conf.int |
a confidence interval for the location parameter.
(Only present if argument |
estimate |
an estimate of the location parameter.
(Only present if argument |
This function can use large amounts of memory and stack (and even
crash R if the stack limit is exceeded) if exact = TRUE
and
one sample is large (several thousands or more).
The literature is not unanimous about the definitions of the Wilcoxon
rank sum and Mann-Whitney tests. The two most common definitions
correspond to the sum of the ranks of the first sample with the
minimum value ( for a first sample of size
)
subtracted or not: R subtracts. It seems Wilcoxon's original paper
used the unadjusted sum of the ranks but subsequent tables subtracted
the minimum.
R's value can also be computed as the number of all pairs
(x[i], y[j])
for which y[j]
is not greater than
x[i]
, the most common definition of the Mann-Whitney test.
David F. Bauer (1972). Constructing confidence sets using rank statistics. Journal of the American Statistical Association 67, 687–690. doi:10.1080/01621459.1972.10481279.
Myles Hollander and Douglas A. Wolfe (1973).
Nonparametric Statistical Methods.
New York: John Wiley & Sons.
Pages 27–33 (one-sample), 68–75 (two-sample).
Or second edition (1999).
wilcox_test
in package
coin for exact, asymptotic and Monte Carlo
conditional p-values, including in the presence of ties.
kruskal.test
for testing homogeneity in location
parameters in the case of two or more samples;
t.test
for an alternative under normality
assumptions [or large samples]
require(graphics) ## One-sample test. ## Hollander & Wolfe (1973), 29f. ## Hamilton depression scale factor measurements in 9 patients with ## mixed anxiety and depression, taken at the first (x) and second ## (y) visit after initiation of a therapy (administration of a ## tranquilizer). x <- c(1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30) y <- c(0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29) wilcox.test(x, y, paired = TRUE, alternative = "greater") wilcox.test(y - x, alternative = "less") # The same. wilcox.test(y - x, alternative = "less", exact = FALSE, correct = FALSE) # H&W large sample # approximation ## Formula interface to one-sample and paired tests depression <- data.frame(first = x, second = y, change = y - x) wilcox.test(change ~ 1, data = depression) wilcox.test(Pair(first, second) ~ 1, data = depression) ## Two-sample test. ## Hollander & Wolfe (1973), 69f. ## Permeability constants of the human chorioamnion (a placental ## membrane) at term (x) and between 12 to 26 weeks gestational ## age (y). The alternative of interest is greater permeability ## of the human chorioamnion for the term pregnancy. x <- c(0.80, 0.83, 1.89, 1.04, 1.45, 1.38, 1.91, 1.64, 0.73, 1.46) y <- c(1.15, 0.88, 0.90, 0.74, 1.21) wilcox.test(x, y, alternative = "g") # greater wilcox.test(x, y, alternative = "greater", exact = FALSE, correct = FALSE) # H&W large sample # approximation wilcox.test(rnorm(10), rnorm(10, 2), conf.int = TRUE) ## Formula interface. boxplot(Ozone ~ Month, data = airquality) wilcox.test(Ozone ~ Month, data = airquality, subset = Month %in% c(5, 8)) ## accuracy in ties determination via 'digits.rank': wilcox.test( 4:2, 3:1, paired=TRUE) # Warning: cannot compute exact p-value with ties wilcox.test((4:2)/10, (3:1)/10, paired=TRUE) # no ties => *no* warning wilcox.test((4:2)/10, (3:1)/10, paired=TRUE, digits.rank = 9) # same ties as (4:2, 3:1)
require(graphics) ## One-sample test. ## Hollander & Wolfe (1973), 29f. ## Hamilton depression scale factor measurements in 9 patients with ## mixed anxiety and depression, taken at the first (x) and second ## (y) visit after initiation of a therapy (administration of a ## tranquilizer). x <- c(1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30) y <- c(0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29) wilcox.test(x, y, paired = TRUE, alternative = "greater") wilcox.test(y - x, alternative = "less") # The same. wilcox.test(y - x, alternative = "less", exact = FALSE, correct = FALSE) # H&W large sample # approximation ## Formula interface to one-sample and paired tests depression <- data.frame(first = x, second = y, change = y - x) wilcox.test(change ~ 1, data = depression) wilcox.test(Pair(first, second) ~ 1, data = depression) ## Two-sample test. ## Hollander & Wolfe (1973), 69f. ## Permeability constants of the human chorioamnion (a placental ## membrane) at term (x) and between 12 to 26 weeks gestational ## age (y). The alternative of interest is greater permeability ## of the human chorioamnion for the term pregnancy. x <- c(0.80, 0.83, 1.89, 1.04, 1.45, 1.38, 1.91, 1.64, 0.73, 1.46) y <- c(1.15, 0.88, 0.90, 0.74, 1.21) wilcox.test(x, y, alternative = "g") # greater wilcox.test(x, y, alternative = "greater", exact = FALSE, correct = FALSE) # H&W large sample # approximation wilcox.test(rnorm(10), rnorm(10, 2), conf.int = TRUE) ## Formula interface. boxplot(Ozone ~ Month, data = airquality) wilcox.test(Ozone ~ Month, data = airquality, subset = Month %in% c(5, 8)) ## accuracy in ties determination via 'digits.rank': wilcox.test( 4:2, 3:1, paired=TRUE) # Warning: cannot compute exact p-value with ties wilcox.test((4:2)/10, (3:1)/10, paired=TRUE) # no ties => *no* warning wilcox.test((4:2)/10, (3:1)/10, paired=TRUE, digits.rank = 9) # same ties as (4:2, 3:1)
Density, distribution function, quantile function and random
generation for the distribution of the Wilcoxon rank sum statistic
obtained from samples with size m
and n
, respectively.
dwilcox(x, m, n, log = FALSE) pwilcox(q, m, n, lower.tail = TRUE, log.p = FALSE) qwilcox(p, m, n, lower.tail = TRUE, log.p = FALSE) rwilcox(nn, m, n)
dwilcox(x, m, n, log = FALSE) pwilcox(q, m, n, lower.tail = TRUE, log.p = FALSE) qwilcox(p, m, n, lower.tail = TRUE, log.p = FALSE) rwilcox(nn, m, n)
x , q
|
vector of quantiles. |
p |
vector of probabilities. |
nn |
number of observations. If |
m , n
|
numbers of observations in the first and second sample, respectively. Can be vectors of positive integers. |
log , log.p
|
logical; if TRUE, probabilities p are given as log(p). |
lower.tail |
logical; if TRUE (default), probabilities are
|
This distribution is obtained as follows. Let x
and y
be two random, independent samples of size m
and n
.
Then the Wilcoxon rank sum statistic is the number of all pairs
(x[i], y[j])
for which y[j]
is not greater than
x[i]
. This statistic takes values between 0
and
m * n
, and its mean and variance are m * n / 2
and
m * n * (m + n + 1) / 12
, respectively.
If any of the first three arguments are vectors, the recycling rule is used to do the calculations for all combinations of the three up to the length of the longest vector.
dwilcox
gives the density,
pwilcox
gives the distribution function,
qwilcox
gives the quantile function, and
rwilcox
generates random deviates.
The length of the result is determined by nn
for
rwilcox
, and is the maximum of the lengths of the
numerical arguments for the other functions.
The numerical arguments other than nn
are recycled to the
length of the result. Only the first elements of the logical
arguments are used.
These functions can use large amounts of memory and stack (and even crash R if the stack limit is exceeded and stack-checking is not in place) if one sample is large (several thousands or more).
S-PLUS used a different (but equivalent) definition of the Wilcoxon
statistic: see wilcox.test
for details.
Kurt Hornik
These ("d","p","q") are calculated via recursion, based on cwilcox(k, m, n)
,
the number of choices with statistic k
from samples of size
m
and n
, which is itself calculated recursively and the
results cached. Then dwilcox
and pwilcox
sum
appropriate values of cwilcox
, and qwilcox
is based on
inversion.
rwilcox
generates a random permutation of ranks and evaluates
the statistic. Note that it is based on the same C code as sample()
,
and hence is determined by .Random.seed
, notably from
RNGkind(sample.kind = ..)
which changed with R version 3.6.0.
wilcox.test
to calculate the statistic from data, find p
values and so on.
Distributions for standard distributions, including
dsignrank
for the distribution of the
one-sample Wilcoxon signed rank statistic.
require(graphics) x <- -1:(4*6 + 1) fx <- dwilcox(x, 4, 6) Fx <- pwilcox(x, 4, 6) layout(rbind(1,2), widths = 1, heights = c(3,2)) plot(x, fx, type = "h", col = "violet", main = "Probabilities (density) of Wilcoxon-Statist.(n=6, m=4)") plot(x, Fx, type = "s", col = "blue", main = "Distribution of Wilcoxon-Statist.(n=6, m=4)") abline(h = 0:1, col = "gray20", lty = 2) layout(1) # set back N <- 200 hist(U <- rwilcox(N, m = 4,n = 6), breaks = 0:25 - 1/2, border = "red", col = "pink", sub = paste("N =",N)) mtext("N * f(x), f() = true \"density\"", side = 3, col = "blue") lines(x, N*fx, type = "h", col = "blue", lwd = 2) points(x, N*fx, cex = 2) ## Better is a Quantile-Quantile Plot qqplot(U, qw <- qwilcox((1:N - 1/2)/N, m = 4, n = 6), main = paste("Q-Q-Plot of empirical and theoretical quantiles", "Wilcoxon Statistic, (m=4, n=6)", sep = "\n")) n <- as.numeric(names(print(tU <- table(U)))) text(n+.2, n+.5, labels = tU, col = "red")
require(graphics) x <- -1:(4*6 + 1) fx <- dwilcox(x, 4, 6) Fx <- pwilcox(x, 4, 6) layout(rbind(1,2), widths = 1, heights = c(3,2)) plot(x, fx, type = "h", col = "violet", main = "Probabilities (density) of Wilcoxon-Statist.(n=6, m=4)") plot(x, Fx, type = "s", col = "blue", main = "Distribution of Wilcoxon-Statist.(n=6, m=4)") abline(h = 0:1, col = "gray20", lty = 2) layout(1) # set back N <- 200 hist(U <- rwilcox(N, m = 4,n = 6), breaks = 0:25 - 1/2, border = "red", col = "pink", sub = paste("N =",N)) mtext("N * f(x), f() = true \"density\"", side = 3, col = "blue") lines(x, N*fx, type = "h", col = "blue", lwd = 2) points(x, N*fx, cex = 2) ## Better is a Quantile-Quantile Plot qqplot(U, qw <- qwilcox((1:N - 1/2)/N, m = 4, n = 6), main = paste("Q-Q-Plot of empirical and theoretical quantiles", "Wilcoxon Statistic, (m=4, n=6)", sep = "\n")) n <- as.numeric(names(print(tU <- table(U)))) text(n+.2, n+.5, labels = tU, col = "red")
window
is a generic function which
extracts the subset of the object x
observed between the times start
and end
. If a
frequency is specified, the series is then re-sampled at the new
frequency.
window(x, ...) ## S3 method for class 'ts' window(x, ...) ## Default S3 method: window(x, start = NULL, end = NULL, frequency = NULL, deltat = NULL, extend = FALSE, ts.eps = getOption("ts.eps"), ...) window(x, ...) <- value ## S3 replacement method for class 'ts' window(x, start, end, frequency, deltat, ...) <- value
window(x, ...) ## S3 method for class 'ts' window(x, ...) ## Default S3 method: window(x, start = NULL, end = NULL, frequency = NULL, deltat = NULL, extend = FALSE, ts.eps = getOption("ts.eps"), ...) window(x, ...) <- value ## S3 replacement method for class 'ts' window(x, start, end, frequency, deltat, ...) <- value
x |
a time-series (or other object if not replacing values). |
start |
the start time of the period of interest. |
end |
the end time of the period of interest. |
frequency , deltat
|
the new frequency can be specified by either (or both if they are consistent). |
extend |
logical. If true, the |
ts.eps |
time series comparison tolerance. Frequencies are
considered equal if their absolute difference is less than
|
... |
further arguments passed to or from other methods. |
value |
replacement values. |
The start and end times can be specified as for ts
. If
there is no observation at the new start
or end
,
the immediately following (start
) or preceding (end
)
observation time is used.
The replacement function has a method for ts
objects, and
is allowed to extend the series (with a warning). There is no default
method.
The value depends on the method. window.default
will return a
vector or matrix with an appropriate tsp
attribute.
window.ts
differs from window.default
only in
ensuring the result is a ts
object.
If extend = TRUE
the series will be padded with NA
s if
needed.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
window(presidents, 1960, c(1969,4)) # values in the 1960's window(presidents, deltat = 1) # All Qtr1s window(presidents, start = c(1945,3), deltat = 1) # All Qtr3s window(presidents, 1944, c(1979,2), extend = TRUE) pres <- window(presidents, 1945, c(1949,4)) # values in the 1940's window(pres, 1945.25, 1945.50) <- c(60, 70) window(pres, 1944, 1944.75) <- 0 # will generate a warning window(pres, c(1945,4), c(1949,4), frequency = 1) <- 85:89 pres
window(presidents, 1960, c(1969,4)) # values in the 1960's window(presidents, deltat = 1) # All Qtr1s window(presidents, start = c(1945,3), deltat = 1) # All Qtr3s window(presidents, 1944, c(1979,2), extend = TRUE) pres <- window(presidents, 1945, c(1949,4)) # values in the 1940's window(pres, 1945.25, 1945.50) <- c(60, 70) window(pres, 1944, 1944.75) <- 0 # will generate a warning window(pres, c(1945,4), c(1949,4), frequency = 1) <- 85:89 pres
Create a contingency table (optionally a sparse matrix) from cross-classifying factors, usually contained in a data frame, using a formula interface.
xtabs(formula = ~., data = parent.frame(), subset, sparse = FALSE, na.action, na.rm = FALSE, addNA = FALSE, exclude = if(!addNA) c(NA, NaN), drop.unused.levels = FALSE) ## S3 method for class 'xtabs' print(x, na.print = "", ...)
xtabs(formula = ~., data = parent.frame(), subset, sparse = FALSE, na.action, na.rm = FALSE, addNA = FALSE, exclude = if(!addNA) c(NA, NaN), drop.unused.levels = FALSE) ## S3 method for class 'xtabs' print(x, na.print = "", ...)
formula |
a formula object with the cross-classifying variables
(separated by |
data |
an optional matrix or data frame (or similar: see
|
subset |
an optional vector specifying a subset of observations to be used. |
sparse |
logical specifying if the result should be a
sparse matrix, i.e., inheriting from
|
na.action |
a |
na.rm |
logical: should missing values on the left-hand side of the
|
addNA |
logical indicating if |
exclude |
a vector of values to be excluded when forming the set of levels of the classifying factors. |
drop.unused.levels |
a logical indicating whether to drop unused
levels in the classifying factors. If this is |
x |
an object of class |
na.print |
character string (or |
... |
further arguments passed to or from other methods. |
There is a summary
method for contingency table objects created
by table
or xtabs(*, sparse = FALSE)
, which gives basic
information and performs a chi-squared test for independence of
factors (note that the function chisq.test
currently
only handles 2-d tables).
If a left-hand side is given in formula
, its entries are simply
summed over the cells corresponding to the right-hand side; this also
works if the LHS does not give counts.
For variables in formula
which are factors, exclude
must be specified explicitly; the default exclusions will not be used.
In R versions before 3.4.0, e.g., when na.action = na.pass
,
sometimes zeroes (0
) were returned instead of NA
s.
In R versions before 4.4.0, when !addNA
as by default,
the default na.action
was na.omit
, effectively
treating missing counts as zero.
By default, when sparse = FALSE
,
a contingency table in array representation of S3 class c("xtabs",
"table")
, with a "call"
attribute storing the matched call.
When sparse = TRUE
, a sparse numeric matrix, specifically an
object of S4 class
dgTMatrix
from package
Matrix.
table
for traditional cross-tabulation, and
as.data.frame.table
which is the inverse operation of
xtabs
(see the DF
example below).
sparseMatrix
on sparse
matrices in package Matrix.
## 'esoph' has the frequencies of cases and controls for all levels of ## the variables 'agegp', 'alcgp', and 'tobgp'. xtabs(cbind(ncases, ncontrols) ~ ., data = esoph) ## Output is not really helpful ... flat tables are better: ftable(xtabs(cbind(ncases, ncontrols) ~ ., data = esoph)) ## In particular if we have fewer factors ... ftable(xtabs(cbind(ncases, ncontrols) ~ agegp, data = esoph)) ## This is already a contingency table in array form. DF <- as.data.frame(UCBAdmissions) ## Now 'DF' is a data frame with a grid of the factors and the counts ## in variable 'Freq'. DF ## Nice for taking margins ... xtabs(Freq ~ Gender + Admit, DF) ## And for testing independence ... summary(xtabs(Freq ~ ., DF)) ## with NA's DN <- DF; DN[cbind(6:9, c(1:2,4,1))] <- NA DN # 'Freq' is missing only for (Rejected, Female, B) (xtNA <- xtabs(Freq ~ Gender + Admit, DN)) # NA prints 'invisibly' print(xtNA, na.print = "NA") # show NA's better xtabs(Freq ~ Gender + Admit, DN, na.rm = TRUE) # ignore missing Freq ## Use addNA = TRUE to tabulate missing factor levels: xtabs(Freq ~ Gender + Admit, DN, addNA = TRUE) xtabs(Freq ~ Gender + Admit, DN, addNA = TRUE, na.rm = TRUE) ## na.action = na.omit removes all rows with NAs right from the start: xtabs(Freq ~ Gender + Admit, DN, na.action = na.omit) ## Create a nice display for the warp break data. warpbreaks$replicate <- rep_len(1:9, 54) ftable(xtabs(breaks ~ wool + tension + replicate, data = warpbreaks)) ### ---- Sparse Examples ---- if(require("Matrix")) withAutoprint({ ## similar to "nlme"s 'ergoStool' : d.ergo <- data.frame(Type = paste0("T", rep(1:4, 9*4)), Subj = gl(9, 4, 36*4)) xtabs(~ Type + Subj, data = d.ergo) # 4 replicates each set.seed(15) # a subset of cases: xtabs(~ Type + Subj, data = d.ergo[sample(36, 10), ], sparse = TRUE) ## Hypothetical two-level setup: inner <- factor(sample(letters[1:25], 100, replace = TRUE)) inout <- factor(sample(LETTERS[1:5], 25, replace = TRUE)) fr <- data.frame(inner = inner, outer = inout[as.integer(inner)]) xtabs(~ inner + outer, fr, sparse = TRUE) })
## 'esoph' has the frequencies of cases and controls for all levels of ## the variables 'agegp', 'alcgp', and 'tobgp'. xtabs(cbind(ncases, ncontrols) ~ ., data = esoph) ## Output is not really helpful ... flat tables are better: ftable(xtabs(cbind(ncases, ncontrols) ~ ., data = esoph)) ## In particular if we have fewer factors ... ftable(xtabs(cbind(ncases, ncontrols) ~ agegp, data = esoph)) ## This is already a contingency table in array form. DF <- as.data.frame(UCBAdmissions) ## Now 'DF' is a data frame with a grid of the factors and the counts ## in variable 'Freq'. DF ## Nice for taking margins ... xtabs(Freq ~ Gender + Admit, DF) ## And for testing independence ... summary(xtabs(Freq ~ ., DF)) ## with NA's DN <- DF; DN[cbind(6:9, c(1:2,4,1))] <- NA DN # 'Freq' is missing only for (Rejected, Female, B) (xtNA <- xtabs(Freq ~ Gender + Admit, DN)) # NA prints 'invisibly' print(xtNA, na.print = "NA") # show NA's better xtabs(Freq ~ Gender + Admit, DN, na.rm = TRUE) # ignore missing Freq ## Use addNA = TRUE to tabulate missing factor levels: xtabs(Freq ~ Gender + Admit, DN, addNA = TRUE) xtabs(Freq ~ Gender + Admit, DN, addNA = TRUE, na.rm = TRUE) ## na.action = na.omit removes all rows with NAs right from the start: xtabs(Freq ~ Gender + Admit, DN, na.action = na.omit) ## Create a nice display for the warp break data. warpbreaks$replicate <- rep_len(1:9, 54) ftable(xtabs(breaks ~ wool + tension + replicate, data = warpbreaks)) ### ---- Sparse Examples ---- if(require("Matrix")) withAutoprint({ ## similar to "nlme"s 'ergoStool' : d.ergo <- data.frame(Type = paste0("T", rep(1:4, 9*4)), Subj = gl(9, 4, 36*4)) xtabs(~ Type + Subj, data = d.ergo) # 4 replicates each set.seed(15) # a subset of cases: xtabs(~ Type + Subj, data = d.ergo[sample(36, 10), ], sparse = TRUE) ## Hypothetical two-level setup: inner <- factor(sample(letters[1:25], 100, replace = TRUE)) inout <- factor(sample(LETTERS[1:5], 25, replace = TRUE)) fr <- data.frame(inner = inner, outer = inout[as.integer(inner)]) xtabs(~ inner + outer, fr, sparse = TRUE) })