latentcor"

options(tinytex.verbose = TRUE)
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(latentcor)

Introduction

R package latentcor utilizes the powerful semi-parametric latent Gaussian copula models to estimate latent correlations between mixed data types. The package allows to estimate correlations between any of continuous/binary/ternary/zero-inflated (truncated) variable types. The underlying implementation takes advantage of fast multi-linear interpolation scheme with a clever choice of grid points that give the package a small memory footprint, and allows to use the latent correlations with sub-sampling and bootstrapping.

Statement of need

No R software package is currently available that allows accurate and fast correlation estimation from mixed variable data in a unifying manner. The R package latentcor, introduced here, thus represents the first stand-alone R package for computation of latent correlation that takes into account all variable types (continuous/binary/ordinal/zero-inflated), comes with an optimized memory footprint, and is computationally efficient, essentially making latent correlation estimation almost as fast as rank-based correlation estimation.

Getting started

A simple example with two variables

First, we will generate a pair of variables with different types using a sample size $n=100$ which will serve as example data. Here first variable will be ternary, and second variable will be continuous.

simdata = gen_data(n = 100, types = c("ter", "con"))

The output of gen_data is a list with 2 elements:

names(simdata)
X = simdata$X
head(X, n = 6L)
simdata$plotX

Then we can estimate the latent correlation matrix based on these 2 variables using latentcor function.

estimate = latentcor(X, types = c("ter", "con"))

The output of latentcor is a list with several elements:

names(estimate)
estimate$zratios
estimate$K
estimate$Rpointwise
estimate$R
estimate$plotR

Example with mtcars dataset

We use the build-in dataset mtcars:

head(mtcars, n = 6L)

Let's take a look at the unique values for each variable to determine the corresponding data type.

apply(mtcars, 2, table)

Then we can estimate the latent correlation matrix for all variables of mtcars by using latentcor function.

estimate_mtcars = latentcor(mtcars, types = c("con", "ter", "con", "con", "con", "con", "con", "bin", "bin", "ter", "con"))

Note that the determination of variable types can also be done automatically by latentcor package using get_types function:

get_types(mtcars)

This function is run automatically inside latentcor if the types are not supplied by the user, however the automatic determination of types takes extra time, so we recommend to specify types explicitly if they are known in advance.

The output of latentcor for mtcars:

names(estimate_mtcars)
estimate_mtcars$zratios
estimate_mtcars$K
estimate_mtcars$Rpointwise
estimate_mtcars$R
estimate_mtcars$plotR

Example using latentcor with subsampling

While latentcor can determine the types of each variable automatically, it is recommended to call get_types first and then supply types explicitly to save the computation time, especially when using latentcor with sub-sampling (which we illustrate below).

First, we will generate variables with different types using a sample size $n=100$ which will serve as an example data for subsampling.

simdata2 = gen_data(n = 100, types = c(rep("ter", 3), "con", rep("bin", 3)))

To use the data with subsampling, we recommend to first run get_types on the full data

types = get_types(simdata2$X)
types

Then, when doing subsampling, we recommend to explicitly supply identified types to latentcor. We illustrate using 10 subsamples, each of size 80.

start_time = proc.time()
for (s in 1:10){
  # Select a random subsample of size 80
  subsample = sample(1:100, 80)
  # Estimate latent correlation on subsample specifying the types
  Rs = latentcor(simdata2$X[subsample, ], types = types)
}
proc.time() - start_time

Compared with

start_time = proc.time()
for (s in 1:10){
  # Select a random subsample of size 80
  subsample = sample(1:100, 80)
  # Estimate latent correlation on subsample specifying the types
  Rs = latentcor(simdata2$X[subsample, ], types = NULL)
}
proc.time() - start_time

References



Try the latentcor package in your browser

Any scripts or data that you put into this service are public.

latentcor documentation built on Oct. 31, 2021, 9:07 a.m.