cor_matrix_mi: Create a (weighted) correlation matrix from multiply imputed...

cor_matrix_miR Documentation

Create a (weighted) correlation matrix from multiply imputed data

Description

This function takes an imputationList with a vector of weights and returns a correlation matrix for all numeric variables as well as a list of descriptives that pools the results across all imputations.

Usage

cor_matrix_mi(data, weights = NULL, var_names = NULL)

Arguments

data

A dataframe with multiple imputations distinguished by a .imp variable. Typically the output from 'mice::complete(mids, "long").

weights

A variable within data that gives the survey weights

var_names

A named character vector with new variable names or a tibble as provided by get_rename_tribbles() for variables. If NULL, then the variables are not renamed. If names are provided, only the variables included here are retained. This is most helpful when the results are passed to some print function, such as report_cor_table() To facilitate post-processing, correlations with original variable names are returned in the tests element.

Details

Variables starting with . are dropped, as these are likely to be .imp and .id from mice. If you want correlations for such variables, rename them.

Value

A correlation matrix list similar to the format provided by jtools::svycor() with the addition of a desc-element with means and standard deviations of the variables.

Source

Takes some code from the miceadds::micombine.cor function, but adapted to use weights and return in the format accepted by report_cor_table

Examples


library(dplyr)
library(mice)

# Create Dataset with missing data
ess_health <- ess_health %>% sample_n(500) %>% 
    select(etfruit, eatveg , dosprt, health, wt = pspwght)
add_missing <- function(x) {x[!rbinom(length(x), 1, .9)] <- NA; x}
ess_health <- ess_health %>% mutate(across(c(everything(), -wt), add_missing))

# Impute data
ess_health_mi <- mice(ess_health, printFlag = FALSE) 
ess_health_mi <- complete(ess_health_mi, "long")

cor_matrix <- cor_matrix_mi(ess_health_mi, weights = wt)

LukasWallrich/timesaveR documentation built on Nov. 29, 2024, 4:47 a.m.