cpp_mean: Calculate mean by factors

cpp_meanR Documentation

Calculate mean by factors

Description

These functions calculate the arithmetic and geometric mean of the weight for each class. geometric_mean and arithmetic_mean return a numeric vector of the same length as w which stores the averaged weight for each observation. geometric_mean_reference returns the same value by reference, i.e. the input value w gets overwritten by the updated weights. See examples.

Usage

geometric_mean_reference(w, classes)

Arguments

w

An numeric vector. All entries should be positive.

classes

A factor variable. Must have the same length as w.

Examples


## Not run: 

## create random data
nobs <- 10
classLabels <- letters[1:3]
dat = data.frame(
  weight = exp(rnorm(nobs)),
  household = factor(sample(classLabels, nobs, replace = TRUE))
)
dat

## calculate weights with geometric_mean
geom_weight <- geometric_mean(dat$weight, dat$household)
cbind(dat, geom_weight)

## calculate weights with arithmetic_mean
arith_weight <- arithmetic_mean(dat$weight, dat$household)
cbind(dat, arith_weight)

## calculate weights "by reference"
geometric_mean_reference(dat$weight, dat$household)
dat

## End(Not run)

surveysd documentation built on Dec. 28, 2022, 2:15 a.m.