zscore: calculate (robust) zscore

Description Usage Arguments Details Value Methods (by class) Deviations Reference Grouped data frames (dplyr package)

View source: R/fun-zscore.R

Description

Given a numeric vector, convert the numbers to z scores or robust z scores. The function accepts raw values as well as normalized ones.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
zscore(x, ...)

## Default S3 method:
zscore(x, robust = TRUE, deviations = FALSE, reference, ...)

## S3 method for class 'data.frame'
zscore(x, robust = TRUE, deviations = FALSE, reference, variables, ...)

## S3 method for class 'grouped_df'
zscore(x, robust = TRUE, deviations = FALSE, reference, variables, ...)

Arguments

x

a numeric vector, a data.frame, or a grouped data frame

...

arguments passed to methods

robust

logical flag; if TRUE (the default), calculate robust z scores

deviations

logical flag; set to FALSE (the default) if the supplied data is raw and to TRUE (the default) if it has been normalized

reference

optional specification of reference observations: for the default method, a logical vector, for the data frame method, either a logical vector or any predicate (bare or as string) that refers to x's variables

variables

for data frame method, character vector of variables to standardize

Details

The z score is a data point standardized to the distribution it comes from. It is a measure of the deviation of the point from the distribution's location parameter expressed in terms of the dispersion parameter of that distribution.

For a normal z score the difference between a point and the distribution mean is divided by the distribution's standard deviation. For a robust z score the mean and standard deviation are replaced by the median and median absolute deviation, respectively.

Value

A numeric vector of z scores or a data frame with added columns of z scores.

Methods (by class)

Deviations

Data can be raw or normalized and this is specified with a logical flag. For deviations the location parameter is assumed to be 0. If normalization is done by simply subtracting the mean/median of the whole sample from each data point, this is redundant but other normalization methods may behave differently.

Reference

Data points in the sample can be standardized against part of the distribution rather than the whole. Should this be the case, supply a logical vector (or predicate that will be evaluated within the data frame) to determine the reference subpopulation.

Grouped data frames (dplyr package)

The method for class grouped_df is home made because the native behavior of grouped data frames does not readily support functions that return data frames.


olobiolo/siscreenr documentation built on Nov. 26, 2021, 3:08 p.m.