freqweights (version 1.0.1)

statsfreq: Descriptive statistics of a frequency table.

Description

Computes the descriptive statistics of a frequency table.

Usage

meanfreq(data, freq = NULL)

.meanfreq(tfq)

quantilefreq(data, probs = c(0, 0.25, 0.5, 0.75, 1), freq = NULL)

.quantilefreq(tfq, probs = c(0, 0.25, 0.5, 0.75, 1))

covfreq(data, freq = NULL)

.covfreq(tfq)

sdfreq(data, freq = NULL)

.sdfreq(tfq)

scalefreq(data, freq = NULL)

.scalefreq(tfq)

corfreq(data, freq = NULL)

.corfreq(tfq)

Arguments

data
any object that can be processed by link{tablefreq}.
freq
a single name of the variable specifying frequency weights.
tfq
a tablefreq object, or a matrix, data frame with the last column being the frequency wweights
probs
A vector of quantiles to compute. Default is 0 (min), .25, .5, .75, 1 (max).

Value

  • meanfreq and sdfreq return vectors. quantilefreq returns a vector or matrix. covfreq and corfreq the estimated covariance matrix and correlation matrix, respectively. scalefreq return a data frame or matrix

Details

These functions compute various weighted versions of standard estimators.

meanfreq, sdfreq, quantilefreq, covfreq, corfreq estimate the mean, standard desviation, quantiles, covariances and correlation matrix, respectively. In this last two cases, resulst are equals to the pairwise.complete.obs option of cov and cor of the desaggregated data, respectively.

Missing values or cases with non-positive frequency weights are automatically removed.

If freq is not null, the data set must contain a column with that name. These variable are removed from the data set in order to calculate the descriptive statistics.

The dot versions are intented to be used when programing. The tfq may be a tablefreq object or a matrix or a data frame with the last column being the frequency weights.

The algorithm of quantilefreq are based on wtd.quantile.

The intern functions are for programming purpose. It does not check the data.

References

Andrews, Chris, https://stat.ethz.ch/pipermail/r-help/2014-March/368350.html

See Also

tablefreq, wtd.quantile

Examples

Run this code
if(require(hflights)) {
  meanfreq(hflights[,c("ArrDelay","DepDelay")])
  sdfreq(hflights[,c("ArrDelay","DepDelay")])
  corfreq(hflights[,c("ArrDelay","DepDelay")])
}

tfq <- tablefreq(iris$Sepal.Length)
tfq

meanfreq(iris$Sepal.Length)
meanfreq(tfq,freq="freq")
.meanfreq(tfq)

dat <- iris[,1:4]
quantilefreq(dat)
corfreq(dat)

tfq <- tablefreq(dat)
.meanfreq(tfq)
.quantilefreq(tfq)
.corfreq(tfq)

## dplyr integration
library(dplyr)
tfq  %>%
  summarise( mean = .meanfreq(cbind(Sepal.Length,freq)),
            sd = .sdfreq(cbind(Sepal.Length,freq)))

tfq <- tablefreq(iris)
tfq %>% group_by(Species) %>%
  summarise( mean = .meanfreq(cbind(Sepal.Length,freq)),
            sd = .sdfreq(cbind(Sepal.Length,freq)))

Run the code above in your browser using DataCamp Workspace