wtdHetcor: Weighted heterogeneous correlation matrix.

wtdHetcorR Documentation

Weighted heterogeneous correlation matrix.

Description

Computes a weighted heterogeneous correlation matrix, consisting of Pearson product-moment correlations between numeric variables, polyserial correlations between numeric and ordinal variables, and polychoric correlations between ordinal variables.

Usage

wtdHetcor(
  dataFrame,
  vars = NULL,
  weights = NULL,
  out = c("wide", "long", "both"),
  triangular = FALSE
)

Arguments

dataFrame

a data.frame containing all variables

vars

character or numeric vector indicating the variables for which a correlation table should be computed. If NULL, all variables in the data.frame will be used.

weights

character or numeric vector indicating the column in dataFrame which contains numeric non-negative weights. If NULL, equally weighted cases are assumed, i.e. all weights are defaulted to 1.

out

Specifies the output format. "wide" gives a classical correlation matrix, "long" gives a long format table which includes the type of correlation.

triangular

Logical: should the wide-format matrix be arranged in triangular shape?

Details

Variables in the data.frame should be accordingly classified as numeric or factor variables. Function resembles the hetcor function from the polycor package, but allows for incorporating weights. For this purpose, the function makes use of the weightedCorr function from the wCorr package.

Value

a correlation table or a list

Examples

data(mtcars)
# create arbitrary weights
mtcars[,"weight"] <- abs(rnorm(nrow(mtcars), 10,5))
# choose variables
vars <- c("mpg", "cyl", "hp")
# inappropriate classes: variables which are inherently ordinal, have the 'wrong'
# class 'numeric'.
sapply(mtcars[,vars], class)
mtcars[,"cyl"] <- as.factor(mtcars[,"cyl"])
wtdHetcor(mtcars, vars = vars, out = "long")
wtdHetcor(mtcars, vars = vars, weights = "weight", out = "long")


beckerbenj/eatAnalysis documentation built on July 7, 2023, 5:51 p.m.