# Working with Order Statistics in the mos Package

## Introduction

The `mos` package provides a suite of tools to work with order statistics, including:

- Generating random values of order statistics and censored/record samples,
- Analytical computation of moments (mean, variance, skewness, kurtosis),
- Simulation-based estimation of moments for some of continuous distributions.

These tools are useful for researchers in statistics, reliability, and simulation studies.

## Generating Order Statistics

You can simulate order statistics from continuous distributions using the `ros()` function. This function accepts either:

- A named distribution available in base R (e.g., “norm”, “exp”), or
- A user-defined quantile function using the `qf` argument.

### Example 1: Using a built-in distribution

    ros(5, r = 2, n = 10, dist = "norm", mean = 0, sd = 1)
    #> [1] -0.3770411 -0.6028261 -0.5736009 -0.9602713 -1.1751271

### Example 2: Using a custom quantile function (e.g., Gumbel)

    qgumbel <- function(p, mu = 0, beta = 1) mu - beta * log(-log(p))
    ros(5, r = 3, n = 15, qf = "qgumbel", mu = 0, beta = 1)
    #> [1] -0.7557084 -0.7889203 -0.3763066 -0.8089723 -0.5909530

### Example 3: Generate multiple order statistics from the uniform distribution

    ros(3, 1:5, 5, dist = "unif", min = 1, max = 5)
    #>            r1       r2       r3       r4       r5
    #> [1,] 1.825971 2.980277 3.337345 3.766707 4.377318
    #> [2,] 1.079086 2.337453 2.883835 3.916738 4.619425
    #> [3,] 2.049516 3.774932 3.847909 4.110901 4.256370

## Moments of Order Statistics

The package provides two categories of moment computation:

- Analytical formulas (closed-form), where available
- Simulation-based estimation otherwise

### Distributions with analytical support

The following distributions have closed-form expressions for moments:

- Uniform
- Exponential
- Weibull
- Symmetric Triangular
- Topp-Leon
- Complementary Beta (for integer `b` and `k <= 2`)

### Distributions handled via simulation

- Normal
- Student’s t
- Gamma
- Beta
- Pareto
- Kumaraswamy
- Complementary Beta (for non-integer `b` or `k > 2`)

In all simulation-based cases, the `ros()` function is used internally to generate the order statistics.

### Example: Uniform Distribution (Analytical)

    mo_unif(r = 2, n = 5, k = 1)  # Mean of 2nd order statistic
    #> [1] 0.3333333

### Example: Exponential Distribution (Analytical)

    mo_exp(r = 3, n = 6, k = 2, mu = 0, sigma = 1)
    #> [1] 0.5105556

### Example: Normal Distribution (Simulated)

    mo_norm(r = 2, n = 10, k = 1)
    #> [1] -1.003343

### Example: Beta Distribution (Simulated)

    mo_beta(r = 2, n = 5, k = 2, a = 2, b = 3)
    #> [1] 0.09487993

In addition to `mo_*()` functions, the package includes:

- `varOS()` to compute variance
- `skewOS()` to compute skewness
- `kurtOS()` to compute kurtosis

## Censored Samples

The `mos` package supports simulation of censored data using several standard censoring schemes:

### Type I Censoring (Time-based)

Observations are censored beyond a fixed time limit.

    rcens(n = 10, dist = "exp", type = "I", cens.time = 2, rate = 1)
    #>  [1] 0.04965609 1.67364631 1.25681359 0.07160058 0.42816672 0.19903825
    #>  [7] 0.02879249 1.12775815 0.20484918 0.32955892

### Type II Censoring (Failure-based)

The smallest `r` values are observed (i.e., censoring after a fixed number of failures).

    rcens(n = 10, r = 5, dist = "norm", type = "II", mean = 0, sd = 1)
    #> [1] -0.47197938 -0.37010037 -0.08041435 -0.05771255  0.02607693

### Progressive Type-II Censoring

Implements progressive removal of remaining items based on a censoring scheme.

    rpcens2(n = 10, R = c(2, 1, 2, 0, 0), dist = "exp", rate = 1)
    #> [1] 0.02910825 0.15546476 0.31029202 0.47766020 0.86184076

This is useful in reliability experiments where censoring occurs at each failure stage.

## Record Values

The `rkrec()` function generates upper and lower k-records from continuous distributions.

### Upper k-Records

    rkrec(size = 5, k = 2, record = "upper", dist = "norm", mean = 0, sd = 1)
    #> [1] -0.1252028 -1.2616253 -0.5911243 -1.6933552  0.2620640

### Lower k-Records

    rkrec(size = 5, k = 3, record = "lower", dist = "exp", rate = 1)
    #> [1] 0.3086123 1.9005258 0.8497093 2.1867893 0.8350877

## An Application of the Package in Graphical Goodness-of-Fit

This section compares analytical and simulated values of the second moment, variance, skewness, and kurtosis of order statistics from the Uniform(0,1) distribution:

    if (requireNamespace("moments", quietly = TRUE)) {
      library(moments)
      set.seed(123)
      x <- ros(1e4, r = 1:25, n = 25, dist = "unif")
      observed <- colMeans(x^2)
      expected <- mo_unif(r = 1:25, n = 25, k = 2, a = 0, b = 1)
      oldpar <- par(mfrow = c(2, 2))
      on.exit(par(oldpar))
      plot(observed, expected, main = "2nd Moment of U(0,1)", xlab = "Observed", ylab = "Expected")
      abline(0, 1, col = 2)

      var_obs <- apply(x, 2, var)
      var_expc <- varOS(1:25, 25, dist = "unif", a = 0, b = 1)
      plot(var_obs, var_expc, main = "Variance of Order Statistics from U(0,1)",
           xlab = "Observed", ylab = "Expected")
      abline(0, 1, col = 2)

      skew_obs <- apply(x, 2, moments::skewness)
      skew_expc <- skewOS(1:25, 25, dist = "unif", a = 0, b = 1)
      plot(skew_obs, skew_expc, main = "Skewness of Order Statistics from U(0,1)",
           xlab = "Observed", ylab = "Expected")
      abline(0, 1, col = 2)

      kurt_obs <- apply(x, 2, moments::kurtosis)
      kurt_expc <- kurtOS(1:25, 25, dist = "unif", a = 0, b = 1)
      plot(kurt_obs, kurt_expc, main = "Kurtosis of Order Statistics from U(0,1)",
           xlab = "Observed", ylab = "Expected")
      abline(0, 1, col = 2)
    }

The plots show a strong alignment between the observed and expected values along the identity line. This suggests that the order statistics simulated from the Uniform(0,1) distribution closely follow their theoretical moments. Such alignment in graphical comparisons is often taken as visual evidence of a good fit between observed data and the theoretical model.

## Conclusion

The `mos` package integrates analytical and simulation-based methods for working with order statistics, censored data, and record values. It serves as a powerful and flexible toolkit for statisticians, particularly those involved in reliability analysis and the study of ordered data.