The Saqrmisc package provides a comprehensive suite of functions for data processing, statistical analysis, and visualization. This vignette demonstrates the main features and capabilities of the package through practical examples.
The Saqrmisc package includes ten main functional areas:
compare_groups)correlation_matrix)correlations)descriptive_table)categorical_table)center,
scale_vars, standardize,
reverse_code)missing_analysis, replace_missing)clustering)mosaic_analysis)estimate_single_network)All functions use a consistent quoted-string API for variable names:
# Variable names as quoted strings
compare_groups(data, category = "gender", Vars = c("score1", "score2"))
correlations(data, Vars = c("x", "y", "z"), group_by = "group")
center(data, Vars = "score", group_by = "cluster")
# NULL for auto-selection of all numeric variables
correlations(data) # Correlates all numeric variables
correlation_matrix(data) # Matrix of all numeric variablesThe compare_groups() function creates comprehensive
comparison plots using ggbetweenstats to compare groups
across multiple variables.
# Generate sample treatment data
set.seed(456)
treatment_data <- data.frame(
patient_id = 1:200,
group = sample(c("Control", "Treatment"), 200, replace = TRUE),
anxiety = rnorm(200, mean = 5, sd = 1.5),
depression = rnorm(200, mean = 4, sd = 1.2),
stress = rnorm(200, mean = 6, sd = 1.8),
study_site = sample(c("Site_A", "Site_B", "Site_C"), 200, replace = TRUE),
gender = sample(c("Male", "Female"), 200, replace = TRUE)
)
# Basic comparison analysis
comparison_results <- compare_groups(
data = treatment_data,
category = "group",
Vars = c("anxiety", "depression", "stress"),
plots = TRUE,
table = TRUE
)
# View results
comparison_results$plots$group_vs_anxiety
comparison_results$summary_table
# Stratified analysis by study site
stratified_results <- compare_groups(
data = treatment_data,
category = "group",
Vars = c("anxiety", "depression"),
repeat_category = "study_site",
plots = TRUE,
table = TRUE
)Stratify by multiple variables simultaneously using
c():
Use repeat_levels to analyze only specific
subgroups:
Generate a formatted table of all pairwise comparisons:
# Generate sample data with 3+ groups
set.seed(123)
multi_group_data <- data.frame(
condition = sample(c("A", "B", "C"), 150, replace = TRUE),
score = rnorm(150, mean = 50, sd = 10),
performance = rnorm(150, mean = 75, sd = 15)
)
# Get post-hoc comparison table
posthoc_results <- compare_groups(
data = multi_group_data,
category = "condition",
Vars = c("score", "performance"),
posthoc = TRUE,
posthoc_table = TRUE,
table = TRUE
)
# Access the formatted post-hoc table
posthoc_results$posthoc_tableUse type shortcuts for different test types:
# Mann-Whitney U test (2 groups)
mw_results <- compare_groups(
data = treatment_data,
category = "group",
Vars = c("anxiety", "depression"),
type = "np", # nonparametric
table = TRUE
)
# Kruskal-Wallis test (3+ groups)
kw_results <- compare_groups(
data = multi_group_data,
category = "condition",
Vars = c("score", "performance"),
type = "np",
table = TRUE
)repeat_category = c("var1", "var2") for combined
groupingrepeat_levels to
select specific subgroupsposthoc_table = TRUE for formatted pairwise
comparisonstype = "np" for
nonparametric (Mann-Whitney/Kruskal-Wallis)The correlation_matrix() function creates
publication-ready correlation tables with significance stars and
optional heatmap visualization. Supports bivariate, partial, and
semi-partial correlations.
# Basic correlation matrix (all numeric variables)
correlation_matrix(mtcars)
# Specific variables with title
correlation_matrix(
data = mtcars,
Vars = c("mpg", "cyl", "disp", "hp", "wt"),
title = "Motor Trend Car Correlations"
)
# With confidence intervals
correlation_matrix(
data = mtcars,
Vars = c("mpg", "hp", "wt"),
show_ci = TRUE
)
# Partial correlations (controlling for other variables)
correlation_matrix(
data = mtcars,
Vars = c("mpg", "cyl", "disp", "hp"),
type = "partial",
title = "Partial Correlations"
)
# Spearman correlations with Bonferroni correction
correlation_matrix(
data = mtcars,
Vars = c("mpg", "cyl", "disp"),
method = "spearman",
p_adjust = "bonferroni"
)
# Full matrix with heatmap
result <- correlation_matrix(
data = mtcars,
Vars = c("mpg", "cyl", "disp", "hp"),
triangle = "full",
heatmap = TRUE
)
# Access the heatmap plot
result$heatmapThe correlations() function creates comprehensive
long-format correlation tables with full statistics for each variable
pair. This is ideal for detailed reporting and when you need complete
statistics (r, CI, t, df, p, n) for each correlation.
# Full correlation table - all numeric variables
correlations(mtcars)
# Specify variables
correlations(
data = mtcars,
Vars = c("mpg", "cyl", "disp", "hp", "wt")
)
# Only show strong correlations (|r| >= 0.7)
correlations(
data = mtcars,
Vars = c("mpg", "cyl", "disp", "hp", "wt"),
min_r = 0.7
)
# Only show significant correlations
correlations(
data = mtcars,
Vars = c("mpg", "cyl", "disp", "hp", "wt", "qsec"),
sig_only = TRUE
)
# With p-value adjustment
correlations(
data = mtcars,
Vars = c("mpg", "cyl", "disp", "hp"),
p_adjust = "bonferroni"
)Compute correlations separately for each group level, with results combined into one table:
For repeated measures or hierarchically nested data, use multilevel correlations to estimate within-cluster associations while removing between-cluster variance:
# Create sample longitudinal data
set.seed(123)
n_subjects <- 30
n_timepoints <- 4
longitudinal_data <- data.frame(
participant_id = rep(1:n_subjects, each = n_timepoints),
time = rep(1:n_timepoints, n_subjects),
anxiety = rnorm(n_subjects * n_timepoints, mean = 5, sd = 1.5),
depression = rnorm(n_subjects * n_timepoints, mean = 4, sd = 1.2),
stress = rnorm(n_subjects * n_timepoints, mean = 6, sd = 1.8)
)
# Within-person correlations (multilevel)
correlations(
data = longitudinal_data,
Vars = c("anxiety", "depression", "stress"),
multilevel = TRUE,
id = "participant_id"
)
# Include between-person correlations
correlations(
data = longitudinal_data,
Vars = c("anxiety", "depression"),
multilevel = TRUE,
id = "participant_id",
between = TRUE
)The package provides consistent transformation functions with group-wise support.
# Grand-mean centering
mtcars_c <- center(mtcars, Vars = c("mpg", "hp", "wt"))
# Group-mean centering
mtcars_c <- center(mtcars, Vars = c("mpg", "hp"), group_by = "cyl")
# With dplyr (vectorized version)
library(dplyr)
mtcars %>% mutate(mpg_c = center_vec(mpg))
mtcars %>% group_by(cyl) %>% mutate(mpg_c = center_vec(mpg))For Likert scales and similar measures:
# Create sample data
df <- data.frame(
item1 = c(1, 2, 3, 4, 5),
item2 = c(5, 4, 3, 2, 1), # reverse-worded
item3 = c(2, 3, 4, 3, 2)
)
# Reverse code with auto-detected scale
df <- reverse_code(df, Vars = "item2")
# Explicit 1-5 Likert scale
df <- reverse_code(df, Vars = c("item2", "item3"), min = 1, max = 5)
# With dplyr
df %>% mutate(item2_r = reverse_code_vec(item2, min = 1, max = 5))The descriptive_table() function creates
publication-ready descriptive statistics tables.
# Generate sample data
data <- data.frame(
age = rnorm(100, mean = 35, sd = 10),
score = rnorm(100, mean = 75, sd = 15),
income = rnorm(100, mean = 50000, sd = 15000),
gender = sample(c("Male", "Female"), 100, replace = TRUE)
)
# Basic descriptive table
descriptive_table(
data = data,
Vars = c("age", "score", "income")
)
# With grouping and extended statistics
descriptive_table(
data = data,
Vars = c("age", "score"),
group_by = "gender",
stats = c("n", "mean", "sd", "median", "iqr", "skewness"),
title = "Sample Characteristics",
theme = "colorful"
)The categorical_table() function creates frequency
tables with chi-square tests.
# Generate sample data
data <- data.frame(
gender = sample(c("Male", "Female", "Other"), 200, replace = TRUE),
education = sample(c("High School", "Bachelor", "Master", "PhD"), 200, replace = TRUE)
)
# Single variable frequency table
categorical_table(data, var = "gender")
# Cross-tabulation with chi-square test
categorical_table(
data = data,
var = "gender",
by = "education",
chi_square = TRUE,
cramers_v = TRUE
)# Add some missing values
df <- mtcars
df$mpg[c(1, 5, 10)] <- NA
df$hp[c(2, 5, 15)] <- NA
# Run missing data analysis
results <- missing_analysis(df)
# Components
results$summary # gt table with per-variable missing counts
results$patterns # Missing data patterns
results$mcar # Little's MCAR test results
results$plot # Missing pattern visualizationclustering() fits latent profile models with the latents
package, one for every combination of profile count and covariance
structure, and keeps the one with the lowest BIC.
set.seed(123)
vars <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")
fit <- clustering(iris, vars, n_profiles = 2:4, models = c("EEE", "VVI"))
fitsummary() compares every candidate;
as.data.frame() gives the profile means and
fitted() the input data with each row’s profile.
plot() draws the profiles; type selects
other views, listed by clustering_plot_types().
The mosaic_analysis() function performs comprehensive
analysis of categorical variables.
# Generate sample demographic data
set.seed(789)
demographic_data <- data.frame(
gender = sample(c("Male", "Female", "Other"), 500, replace = TRUE, prob = c(0.45, 0.45, 0.1)),
education = sample(c("High School", "Bachelor", "Master", "PhD"), 500, replace = TRUE),
region = sample(c("North", "South", "East", "West"), 500, replace = TRUE)
)
# Perform mosaic analysis
mosaic_results <- mosaic_analysis(
data = demographic_data,
var1 = "gender",
var2 = "education",
min_count = 10,
title = "Gender Distribution by Education Level",
show_percentages = TRUE
)
# Access results
print(mosaic_results$chi_test)
print(mosaic_results$cramers_v)
print(mosaic_results$consolidated_table)The estimate_single_network() function estimates
psychological networks.
# Generate sample data
set.seed(123)
network_data <- data.frame(
var1 = rnorm(200),
var2 = rnorm(200),
var3 = rnorm(200),
var4 = rnorm(200),
var5 = rnorm(200)
)
# Estimate network
network_results <- estimate_single_network(
data = network_data,
vars = c("var1", "var2", "var3", "var4", "var5"),
method = "EBICglasso"
)
# View results
print(network_results)
summary(network_results)?function_name for detailed documentationThe Saqrmisc package provides a comprehensive toolkit for data analysis and visualization. The functions are designed to work together seamlessly while also being useful independently.
For more information, visit the package documentation or GitHub repository.