Package 'tobit.mean'

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

Help Index


Calculate the percent coefficient of variation (CV)

Description

Calculate the percent coefficient of variation (CV)

Usage

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)

Arguments

x

The object to use for CV calculation

Value

The percent coefficient of variation as a numeric scalar

Methods (by class)

  • 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

See Also

Other Class creation and coercion: as_geomean_tobit(), as_mean_tobit(), as_sd_tobit()

Examples

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

Description

Create or cast to a geomean_tobit object

Usage

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)

Arguments

mu

The geometric mean

sd

The geometric standard deviation

message

Additional information about the mean

status

NULL, or how the mean was reached: one of tobit_status_codes$status

Value

A 'geomean_tobit' class object which is a number with attributes for 'sd', 'message', and (when given) 'status'.

See Also

Other Class creation and coercion: as_cv_tobit(), as_mean_tobit(), as_sd_tobit()

Examples

as_geomean_tobit(mu=10, sd=2, message="This is just an example")

Create or cast to a mean_tobit object

Description

Create or cast to a mean_tobit object

Usage

as_mean_tobit(mu, sd, message = NULL, status = NULL)

## Default S3 method:
as_mean_tobit(mu, sd, message = NULL, status = NULL)

Arguments

mu

The arithmetic mean

sd

The arithmetic standard deviation (NA when it cannot be estimated; it must be NA when mu is NA)

message

Additional information about the mean

status

NULL, or how the mean was reached: one of tobit_status_codes$status

Value

A 'mean_tobit' class object which is a number with attributes for 'sd', 'message', and (when given) 'status'.

See Also

Other Class creation and coercion: as_cv_tobit(), as_geomean_tobit(), as_sd_tobit()

Examples

as_mean_tobit(mu=10, sd=2, message="This is just an example")

Create or extract the standard deviation from a mean_tobit object

Description

Create or extract the standard deviation from a mean_tobit object

Usage

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)

Arguments

mu

The arithmetic mean

sd

The arithmetic standard deviation (NA when it cannot be estimated; it must be NA when mu is NA)

message

Additional information about the mean

status

NULL, or how the mean was reached: one of tobit_status_codes$status

Value

A 'sd_tobit' class object which is a number with attributes of 'mu' (the mean), 'message', and (when given) 'status'.

Methods (by class)

  • 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

See Also

Other Class creation and coercion: as_cv_tobit(), as_geomean_tobit(), as_mean_tobit()

Examples

as_sd_tobit(mu = 10, sd = 2)
as_sd_tobit(as_mean_tobit(mu = 10, sd = 2))

Calculate the censored geometric mean

Description

The geometric mean is calculated as the exponential of the censored mean of log(x).

Usage

geomean_tobit(x, lower = 0, upper = Inf, na.rm = TRUE, ...)

Arguments

x

A numeric vector

lower, upper

The lower and upper bounds for censoring x

na.rm

A logical value indicating whether NA values should be stripped from x before the computation proceeds.

...

Arguments passed on to mean_tobit

lower,upper

The lower and upper bounds for censoring x

include_equal,include_equal_lower,include_equal_upper

Should the bounds consider x <= lower and x >= upper (if TRUE) or x < lower and x > upper (if FALSE) to be censored? (include_equal is overridden by setting include_equal_lower or include_equal_upper.)

distribution

What distribution (Student "t", the default, or normal, "norm") should be used. If "t", the degrees of freedom are the number of non-NA values in x, and the reported standard deviation is that of the fitted t distribution (scale*sqrt(df/(df - 2))), which is NA when there are two or fewer values.

Value

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.

See Also

Other Censored Descriptive Statistics: mean_tobit(), sd_tobit(), tobit_condition_classes, tobit_status_codes

Examples

geomean_tobit(c(2, 2, 3, 5, 6, 8), lower = 2, distribution = "norm")

Calculate a censored mean

Description

Calculate a censored mean

Usage

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,
  ...
)

Arguments

x

A numeric vector

lower, upper

The lower and upper bounds for censoring x

include_equal, include_equal_lower, include_equal_upper

Should the bounds consider x <= lower and x >= upper (if TRUE) or x < lower and x > upper (if FALSE) to be censored? (include_equal is overridden by setting include_equal_lower or include_equal_upper.)

distribution

What distribution (Student "t", the default, or normal, "norm") should be used. If "t", the degrees of freedom are the number of non-NA values in x, and the reported standard deviation is that of the fitted t distribution (scale*sqrt(df/(df - 2))), which is NA when there are two or fewer values.

na.rm

A logical value indicating whether NA values should be stripped from x before the computation proceeds.

...

Arguments passed on to stats::optim

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 "BFGS", "CG" and "L-BFGS-B" methods. If it is NULL, a finite-difference approximation will be used.

For the "SANN" method it specifies a function to generate a new candidate point. If it is NULL a default Gaussian Markov kernel is used.

method

The method to be used. See ‘Details’. Can be abbreviated.

lower,upper

Bounds on the variables for the "L-BFGS-B" method, or bounds in which to search for method "Brent".

control

a list of control parameters. See ‘Details’.

hessian

Logical. Should a numerically differentiated Hessian matrix be returned?

Details

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).

Value

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.

References

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

See Also

Other Censored Descriptive Statistics: geomean_tobit(), sd_tobit(), tobit_condition_classes, tobit_status_codes

Examples

# 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")

Calculate the censored standard deviation

Description

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.

Usage

sd_tobit(...)

Arguments

...

Arguments passed on to mean_tobit

x

A numeric vector

lower,upper

The lower and upper bounds for censoring x

include_equal,include_equal_lower,include_equal_upper

Should the bounds consider x <= lower and x >= upper (if TRUE) or x < lower and x > upper (if FALSE) to be censored? (include_equal is overridden by setting include_equal_lower or include_equal_upper.)

distribution

What distribution (Student "t", the default, or normal, "norm") should be used. If "t", the degrees of freedom are the number of non-NA values in x, and the reported standard deviation is that of the fitted t distribution (scale*sqrt(df/(df - 2))), which is NA when there are two or fewer values.

na.rm

A logical value indicating whether NA values should be stripped from x before the computation proceeds.

Value

A "sd_tobit" class object

See Also

Other Censored Descriptive Statistics: geomean_tobit(), mean_tobit(), tobit_condition_classes, tobit_status_codes

Examples

sd_tobit(c(2, 2, 3, 5, 6, 8), lower = 2, distribution = "norm")

Classes of the errors and warnings of the censored statistics

Description

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.

Usage

tobit_condition_classes

Format

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

Classes

tobit_mean_error_mu_not_scalar

error: 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_scalar

error: 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_numeric

error: The mean given to a constructor such as as_mean_tobit() is not a number. ("'mu' must be numeric")

tobit_mean_error_sd_not_numeric

error: 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_mu

error: A standard deviation was given with a missing mean. ("'sd' must be NA when 'mu' is NA")

tobit_mean_error_status_invalid

error: 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_na

error: 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_tobit

error: A result of the package was given as the values. ("'x' may not have class 'mean_tobit'")

tobit_mean_error_lower_not_numeric

error: The lower limit is not numeric. ("lower must be a numeric")

tobit_mean_error_lower_missing

error: The lower limit has a missing value. ("lower may not have missing values")

tobit_mean_error_lower_length

error: 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_numeric

error: The upper limit is not numeric. ("upper must be a numeric")

tobit_mean_error_upper_missing

error: The upper limit has a missing value. ("upper may not have missing values")

tobit_mean_error_upper_length

error: 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_ordered

error: A lower limit is not below its upper limit. ("each lower must be less than upper")

tobit_mean_error_x_not_finite

error: 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_nonpositive

error: 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_nonpositive

error: geomean_tobit() has an upper limit at or below zero. ("some 'upper' values were at or below zero")

tobit_mean_error_internal

error: A check inside the package failed; its message names the check. Please report it as a bug.

tobit_mean_warning_sd_ignored

warning: 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_ignored

warning: 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_ignored

warning: 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_negative

warning: geomean_tobit() set values below zero to zero. ("some 'x' values were below zero, setting to zero")

tobit_mean_warning_lower_negative

warning: geomean_tobit() set lower limits below zero to zero. ("some 'lower' values were below zero, setting to zero")

See Also

Other Censored Descriptive Statistics: geomean_tobit(), mean_tobit(), sd_tobit(), tobit_status_codes

Examples

tobit_condition_classes
tryCatch(
  mean_tobit(1:3, lower = 2, upper = 2),
  tobit_mean_error_limits_not_ordered = function(e) conditionMessage(e)
)

Status codes of a censored mean

Description

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.

Usage

tobit_status_codes

Format

A data.frame with one row per code: status (the code) and meaning

Details

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.

Codes

"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.

See Also

Other Censored Descriptive Statistics: geomean_tobit(), mean_tobit(), sd_tobit(), tobit_condition_classes

Examples

tobit_status_codes
attr(mean_tobit(c(2, 2, 3, 5, 6, 8), lower = 2, distribution = "norm"), "status")