## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----install, eval = FALSE----------------------------------------------------
# install.packages("smartcor")

## ----library------------------------------------------------------------------
library(smartcor)

## ----pairwise-----------------------------------------------------------------
# continuous + continuous: Pearson unless there are outliers or nonlinearity
smart_cor(mtcars$mpg, mtcars$wt)

## ----pairwise-binary----------------------------------------------------------
# continuous + binary: biserial when the diagnostics support latent normality
smart_cor(mtcars$mpg, mtcars$vs)

## ----outliers-----------------------------------------------------------------
detect_outliers(mtcars$mpg)
detect_outliers(c(mtcars$mpg, 120))

## ----normality----------------------------------------------------------------
test_bivariate_normality(mtcars$mpg, mtcars$wt)
test_bivariate_normality(mtcars$mpg, mtcars$vs)
test_bivariate_normality(mtcars$gear, mtcars$carb)

## ----linearity----------------------------------------------------------------
diagnostics = test_linearity(mtcars$mpg, mtcars$wt)
diagnostics

## ----diagnostics-element------------------------------------------------------
res = smart_cor(mtcars$mpg, mtcars$vs, alpha = 0.01, verbose = FALSE)
names(res$diagnostics)
res$diagnostics$normality

## ----ordinal------------------------------------------------------------------
# numeric variables with 10 or fewer unique values are treated as ordinal;
# assume_latent_normal controls whether polychoric or Kendall is used
smart_cor(mtcars$gear, mtcars$carb, assume_latent_normal = FALSE)

## ----categorical--------------------------------------------------------------
# some categorical data
set.seed(1)
colour = factor(sample(c("red", "blue", "green"), 100, replace = TRUE))
shape  = factor(sample(c("circle", "square", "triangle"), 100, replace = TRUE))
smart_cor(colour, shape)

## ----continuous-categorical---------------------------------------------------
smart_cor(mtcars$mpg, factor(mtcars$cyl))

## ----override-----------------------------------------------------------------
# force cyl to be treated as continuous
smart_cor(mtcars$mpg, mtcars$cyl, y_type = "continuous")

## ----force-method-------------------------------------------------------------
smart_cor(mtcars$mpg, mtcars$wt, method = "spearman")

## ----cormat-------------------------------------------------------------------
mat = smart_cormat(
  mtcars[, c("mpg", "cyl", "vs", "gear")],
  assume_latent_normal = FALSE
)
mat

## ----tidy---------------------------------------------------------------------
res = smart_cor(mtcars$mpg, mtcars$wt, verbose = FALSE)
tidy(res)

## ----tidy-mat-----------------------------------------------------------------
tidy(mat)

## ----available----------------------------------------------------------------
available_methods("ordinal", "ordinal")
available_methods("continuous", "binary")
available_methods("categorical", "categorical")

## ----auto---------------------------------------------------------------------
# runs the LR test and picks polychoric or Kendall accordingly
smart_cor(mtcars$gear, mtcars$carb, verbose = FALSE)$rationale

## ----nearpd-------------------------------------------------------------------
adjusted = smart_cormat(mtcars[, c("mpg", "wt", "disp")], nearPD = TRUE, verbose = FALSE)
adjusted$nearPD

