R Under development (unstable) (2025-07-07 r88391 ucrt) -- "Unsuffered Consequences" Copyright (C) 2025 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > ### actuar: Actuarial Functions and Heavy Tailed Distributions > ### > ### Tests for the 'var' and 'sd' methods for individual and grouped > ### data. > ### > ### AUTHOR: Vincent Goulet > > ## Load the package > library(actuar) Attaching package: 'actuar' The following objects are masked from 'package:stats': sd, var The following object is masked from 'package:grDevices': cm > > ### > ### Individual data > ### > > ## Check that results are identical to stats::var and stats::sd, as it > ## should be. > stopifnot(exprs = { + identical(var(dental), stats::var(dental)) + identical(sd(dental), stats::sd(dental)) + }) > > ## Check correct handling of missing data (issue #5 fixed with > ## 62b92eff). > x <- c(dental, NA) > stopifnot(exprs = { + identical(var(x, na.rm = TRUE), stats::var(x, na.rm = TRUE)) + identical(sd(x, na.rm = TRUE), stats::sd(x, na.rm = TRUE)) + }) > > ### > ### Grouped data > ### > > ## Extract group boundaries and frequencies from a grouped data > ## object. > cj <- gdental[, 1] > nj <- gdental[, 2] > > ## Compute variance and standard deviation by hand. > midpoints <- cj[-length(cj)] + diff(cj)/2 > means <- drop(crossprod(nj, midpoints)/sum(nj)) > v <- drop(crossprod(nj, (midpoints - means)^2)/(sum(nj) - 1)) > s <- sqrt(v) > stopifnot(exprs = { + all.equal(v, unname(var(gdental))) + all.equal(s, unname(sd(gdental))) + }) > > proc.time() user system elapsed 0.12 0.04 0.17