| Title: | Calculate Descriptive Statistics of Censored Vectors |
|---|---|
| Description: | Calculate descriptive statistics of left- and right-censored numeric vectors: the arithmetic mean, geometric mean, standard deviation, and percent coefficient of variation. Censoring is accounted for by maximum likelihood estimation with the Tobit model (Tobin (1958) <doi:10.2307/1907382>), using the convex reparameterization of the likelihood described by Olsen (1978) <doi:10.2307/1911445>, rather than by ignoring or substituting censored values. |
| Authors: | Bill Denney [aut, cre] (ORCID: <https://orcid.org/0000-0002-5759-428X>) |
| Maintainer: | Bill Denney <[email protected]> |
| License: | AGPL (>= 3) |
| Version: | 0.1.0.9000 |
| Built: | 2026-12-06 14:55:42 UTC |
| Source: | https://github.com/humanpred/tobit.mean |
Calculate the percent coefficient of variation (CV)
as_cv_tobit(x) ## S3 method for class 'mean_tobit' as_cv_tobit(x) ## S3 method for class 'geomean_tobit' as_cv_tobit(x) ## S3 method for class 'sd_tobit' as_cv_tobit(x)as_cv_tobit(x) ## S3 method for class 'mean_tobit' as_cv_tobit(x) ## S3 method for class 'geomean_tobit' as_cv_tobit(x) ## S3 method for class 'sd_tobit' as_cv_tobit(x)
x |
The object to use for CV calculation |
The percent coefficient of variation as a numeric scalar
as_cv_tobit(mean_tobit): CV for the arithmetic mean: 100*sd/mu
as_cv_tobit(geomean_tobit): CV for the geometric mean:
100*sqrt(exp(sd^2)-1)
as_cv_tobit(sd_tobit): CV from a 'sd_tobit' object, using the geometric
formula when it came from a 'geomean_tobit' object and the arithmetic
formula otherwise
Other Class creation and coercion:
as_geomean_tobit(),
as_mean_tobit(),
as_sd_tobit()
as_cv_tobit(as_mean_tobit(mu = 10, sd = 2)) as_cv_tobit(as_geomean_tobit(mu = 10, sd = 0.2))as_cv_tobit(as_mean_tobit(mu = 10, sd = 2)) as_cv_tobit(as_geomean_tobit(mu = 10, sd = 0.2))
Create or cast to a geomean_tobit object
as_geomean_tobit(mu, sd, message = NULL, status = NULL) ## Default S3 method: as_geomean_tobit(mu, sd, message = NULL, status = NULL) ## S3 method for class 'mean_tobit' as_geomean_tobit(mu, sd, message = NULL, status = NULL)as_geomean_tobit(mu, sd, message = NULL, status = NULL) ## Default S3 method: as_geomean_tobit(mu, sd, message = NULL, status = NULL) ## S3 method for class 'mean_tobit' as_geomean_tobit(mu, sd, message = NULL, status = NULL)
mu |
The geometric mean |
sd |
The geometric standard deviation |
message |
Additional information about the mean |
status |
|
A 'geomean_tobit' class object which is a number with attributes for 'sd', 'message', and (when given) 'status'.
Other Class creation and coercion:
as_cv_tobit(),
as_mean_tobit(),
as_sd_tobit()
as_geomean_tobit(mu=10, sd=2, message="This is just an example")as_geomean_tobit(mu=10, sd=2, message="This is just an example")
Create or cast to a mean_tobit object
as_mean_tobit(mu, sd, message = NULL, status = NULL) ## Default S3 method: as_mean_tobit(mu, sd, message = NULL, status = NULL)as_mean_tobit(mu, sd, message = NULL, status = NULL) ## Default S3 method: as_mean_tobit(mu, sd, message = NULL, status = NULL)
mu |
The arithmetic mean |
sd |
The arithmetic standard deviation ( |
message |
Additional information about the mean |
status |
|
A 'mean_tobit' class object which is a number with attributes for 'sd', 'message', and (when given) 'status'.
Other Class creation and coercion:
as_cv_tobit(),
as_geomean_tobit(),
as_sd_tobit()
as_mean_tobit(mu=10, sd=2, message="This is just an example")as_mean_tobit(mu=10, sd=2, message="This is just an example")
Create or extract the standard deviation from a mean_tobit object
as_sd_tobit(mu, sd, message = NULL, status = NULL) ## Default S3 method: as_sd_tobit(mu, sd, message = NULL, status = NULL) ## S3 method for class 'mean_tobit' as_sd_tobit(mu, sd, message = NULL, status = NULL) ## S3 method for class 'geomean_tobit' as_sd_tobit(mu, sd, message = NULL, status = NULL) ## S3 method for class 'sd_tobit' as_sd_tobit(mu, sd, message = NULL, status = NULL)as_sd_tobit(mu, sd, message = NULL, status = NULL) ## Default S3 method: as_sd_tobit(mu, sd, message = NULL, status = NULL) ## S3 method for class 'mean_tobit' as_sd_tobit(mu, sd, message = NULL, status = NULL) ## S3 method for class 'geomean_tobit' as_sd_tobit(mu, sd, message = NULL, status = NULL) ## S3 method for class 'sd_tobit' as_sd_tobit(mu, sd, message = NULL, status = NULL)
mu |
The arithmetic mean |
sd |
The arithmetic standard deviation ( |
message |
Additional information about the mean |
status |
|
A 'sd_tobit' class object which is a number with attributes of 'mu' (the mean), 'message', and (when given) 'status'.
as_sd_tobit(mean_tobit): Convert a 'mean_tobit' object to a 'sd_tobit' object
as_sd_tobit(geomean_tobit): Convert a 'geomean_tobit' object to a 'sd_tobit'
object
as_sd_tobit(sd_tobit): Return a 'sd_tobit' object unchanged
Other Class creation and coercion:
as_cv_tobit(),
as_geomean_tobit(),
as_mean_tobit()
as_sd_tobit(mu = 10, sd = 2) as_sd_tobit(as_mean_tobit(mu = 10, sd = 2))as_sd_tobit(mu = 10, sd = 2) as_sd_tobit(as_mean_tobit(mu = 10, sd = 2))
The geometric mean is calculated as the exponential of the censored mean of
log(x).
geomean_tobit(x, lower = 0, upper = Inf, na.rm = TRUE, ...)geomean_tobit(x, lower = 0, upper = Inf, na.rm = TRUE, ...)
x |
A numeric vector |
lower, upper
|
The lower and upper bounds for censoring |
na.rm |
A logical value indicating whether |
... |
Arguments passed on to
|
A "geomean_tobit" class object (which also has the "mean_tobit"
class) which is a number with attributes for the standard deviation of
log(x) (sd), convergence information in the "message" attribute,
and how the result was reached in the "status" attribute, one of
tobit_status_codes$status.
Other Censored Descriptive Statistics:
mean_tobit(),
sd_tobit(),
tobit_condition_classes,
tobit_status_codes
geomean_tobit(c(2, 2, 3, 5, 6, 8), lower = 2, distribution = "norm")geomean_tobit(c(2, 2, 3, 5, 6, 8), lower = 2, distribution = "norm")
Calculate a censored mean
mean_tobit( x, lower = -Inf, upper = Inf, include_equal = TRUE, include_equal_lower = include_equal, include_equal_upper = include_equal, distribution = c("t", "norm"), na.rm = TRUE, ... )mean_tobit( x, lower = -Inf, upper = Inf, include_equal = TRUE, include_equal_lower = include_equal, include_equal_upper = include_equal, distribution = c("t", "norm"), na.rm = TRUE, ... )
x |
A numeric vector |
lower, upper
|
The lower and upper bounds for censoring |
include_equal, include_equal_lower, include_equal_upper
|
Should the
bounds consider |
distribution |
What distribution (Student |
na.rm |
A logical value indicating whether |
... |
Arguments passed on to
|
When no values are censored, the ordinary mean() and
sd() are returned. When there are no uncensored values, the mean
cannot be estimated and NA is returned. When all values are equal
(and some, but not all, are censored), the mean is that value and the
standard deviation is NA.
Values of x must be finite unless they are censored at a finite
limit (for example, -Inf with lower = 0).
A 'mean_tobit' object which is a number with attributes for the
standard deviation (sd), convergence information in the "message"
attribute, and how the result was reached in the "status" attribute, one
of tobit_status_codes$status.
Tobin J. Estimation of Relationships for Limited Dependent Variables. Econometrica. 1958;26(1):24-36. doi:10.2307/1907382
Olsen RJ. Note on the Uniqueness of the Maximum Likelihood Estimator for the Tobit Model. Econometrica. 1978;46(5):1211-1215. doi:10.2307/1911445
Other Censored Descriptive Statistics:
geomean_tobit(),
sd_tobit(),
tobit_condition_classes,
tobit_status_codes
# Values at or below 2 are censored (for example, below a limit of # quantification) mean_tobit(c(2, 2, 3, 5, 6, 8), lower = 2, distribution = "norm")# Values at or below 2 are censored (for example, below a limit of # quantification) mean_tobit(c(2, 2, 3, 5, 6, 8), lower = 2, distribution = "norm")
Since the standard deviation is almost always desired with the mean, it is
more efficient to use as_sd_tobit() on the mean object instead of
calculating the standard deviation separately.
sd_tobit(...)sd_tobit(...)
... |
Arguments passed on to
|
A "sd_tobit" class object
Other Censored Descriptive Statistics:
geomean_tobit(),
mean_tobit(),
tobit_condition_classes,
tobit_status_codes
sd_tobit(c(2, 2, 3, 5, 6, 8), lower = 2, distribution = "norm")sd_tobit(c(2, 2, 3, 5, 6, 8), lower = 2, distribution = "norm")
Every error and warning the package raises has a class from this table,
so a caller can handle one without matching its message. The classes
follow one pattern: tobit_mean_error_<what> for an error and
tobit_mean_warning_<what> for a warning, and every error also has
the class "tobit_mean_error" (every warning
"tobit_mean_warning") before the classes stop() and
warning() gave in 0.1.0: "simpleError", "error",
and "condition" ("simpleWarning", "warning", and
"condition"). The classes are added in front, and the messages
and calls are unchanged from 0.1.0, so tryCatch(..., error = ),
inherits(e, "simpleError"), and code that matches the message keep
working. A printed condition shows its first class, so it begins with the
new class name.
tobit_condition_classestobit_condition_classes
A data.frame with one row per class: class, type
("error" or "warning"), message (the message, or
NA for "tobit_mean_error_internal", whose message names the
check that failed), and meaning
tobit_mean_error_mu_not_scalarerror: The mean given to a constructor such as as_mean_tobit() is not one value. ("'mu' must be a scalar")
tobit_mean_error_sd_not_scalarerror: The standard deviation given to a constructor such as as_mean_tobit() is not one value. ("'sd' must be a scalar")
tobit_mean_error_mu_not_numericerror: The mean given to a constructor such as as_mean_tobit() is not a number. ("'mu' must be numeric")
tobit_mean_error_sd_not_numericerror: The standard deviation given to a constructor such as as_mean_tobit() is not a number. ("'sd' must be numeric")
tobit_mean_error_sd_without_muerror: A standard deviation was given with a missing mean. ("'sd' must be NA when 'mu' is NA")
tobit_mean_error_status_invaliderror: The status given to a constructor such as as_mean_tobit() is not one of tobit_status_codes. ("'status' must be NULL or one of tobit_status_codes$status")
tobit_mean_error_convert_not_naerror: A mean_tobit with a value was given to as_geomean_tobit(). ("mean_tobit can only be converted to a geomean_tobit if it is NA")
tobit_mean_error_x_is_mean_tobiterror: A result of the package was given as the values. ("'x' may not have class 'mean_tobit'")
tobit_mean_error_lower_not_numericerror: The lower limit is not numeric. ("lower must be a numeric")
tobit_mean_error_lower_missingerror: The lower limit has a missing value. ("lower may not have missing values")
tobit_mean_error_lower_lengtherror: The lower limit is neither one value nor one per value. ("lower must be a scalar or the same length as x")
tobit_mean_error_upper_not_numericerror: The upper limit is not numeric. ("upper must be a numeric")
tobit_mean_error_upper_missingerror: The upper limit has a missing value. ("upper may not have missing values")
tobit_mean_error_upper_lengtherror: The upper limit is neither one value nor one per value. ("upper must be a scalar or the same length as x")
tobit_mean_error_limits_not_orderederror: A lower limit is not below its upper limit. ("each lower must be less than upper")
tobit_mean_error_x_not_finiteerror: A value that is not censored, or a limit a censored value takes, is not finite. ("'x' must be finite unless it is censored at a finite limit")
tobit_mean_error_geomean_nonpositiveerror: geomean_tobit() has a value and its lower limit both at or below zero. ("'x' and 'lower' cannot both be <= 0 at the same time")
tobit_mean_error_geomean_upper_nonpositiveerror: geomean_tobit() has an upper limit at or below zero. ("some 'upper' values were at or below zero")
tobit_mean_error_internalerror: A check inside the package failed; its message names the check. Please report it as a bug.
tobit_mean_warning_sd_ignoredwarning: A standard deviation was given to the conversion of a result, which keeps its own. ("'sd' is ignored for object conversion (sd is taken from the object)")
tobit_mean_warning_message_ignoredwarning: A message was given to the conversion of a result, which keeps its own. ("'message' is ignored for object conversion (message is taken from the object)")
tobit_mean_warning_status_ignoredwarning: A status was given to the conversion of a result, which keeps its own. ("'status' is ignored for object conversion (status is taken from the object)")
tobit_mean_warning_x_negativewarning: geomean_tobit() set values below zero to zero. ("some 'x' values were below zero, setting to zero")
tobit_mean_warning_lower_negativewarning: geomean_tobit() set lower limits below zero to zero. ("some 'lower' values were below zero, setting to zero")
Other Censored Descriptive Statistics:
geomean_tobit(),
mean_tobit(),
sd_tobit(),
tobit_status_codes
tobit_condition_classes tryCatch( mean_tobit(1:3, lower = 2, upper = 2), tobit_mean_error_limits_not_ordered = function(e) conditionMessage(e) )tobit_condition_classes tryCatch( mean_tobit(1:3, lower = 2, upper = 2), tobit_mean_error_limits_not_ordered = function(e) conditionMessage(e) )
The "status" attribute of the results of mean_tobit(),
geomean_tobit(), and sd_tobit() is one of these codes. It
says how the result was reached, so a caller can act on it without reading
the "message" attribute, whose text may change.
tobit_status_codestobit_status_codes
A data.frame with one row per code: status (the code) and
meaning
Arithmetic on a result keeps its attributes, as it kept "sd" and
"message" in 0.1.0: after m + 1, the "status" still
describes the fit that gave m, not the new value.
"no_values"No values were given; the mean is NA.
"all_na"All values were NA; the mean is NA.
"na_not_removed"Some values were NA and na.rm is FALSE; the mean is NA.
"uncensored"No value was censored; the mean and standard deviation are the ordinary ones.
"all_above_upper"Every value was censored at the upper limit; the mean is NA.
"all_below_lower"Every value was censored at the lower limit; the mean is NA.
"none_between"Every value was censored, some at the lower limit and some at the upper; the mean is NA.
"single_value"Some values were censored and all values were equal; the mean is that value and the standard deviation is NA.
"converged"The censored model was fit and stats::optim() converged.
"not_converged"stats::optim() did not converge; the mean is NA.
"optim_error"stats::optim() stopped with an error; the mean is NA.
Other Censored Descriptive Statistics:
geomean_tobit(),
mean_tobit(),
sd_tobit(),
tobit_condition_classes
tobit_status_codes attr(mean_tobit(c(2, 2, 3, 5, 6, 8), lower = 2, distribution = "norm"), "status")tobit_status_codes attr(mean_tobit(c(2, 2, 3, 5, 6, 8), lower = 2, distribution = "norm"), "status")