cpp_mean: Calculate mean by factors

Description Usage Arguments Examples

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

1

Arguments

w

An numeric vector. All entries should be positive.

classes

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

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
## 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 Jan. 13, 2021, 4:11 p.m.