An R package for correlation analysis with support for both standard and partial correlations using Spearman's rank method.
ImmiR provides easy-to-use functions for conducting correlation analyses in R. The package includes:
- Normal Correlation Test: Bivariate correlation analysis between two variables
- Partial Correlation Test: Correlation analysis while controlling for confounding variables
Both functions use Spearman's rank correlation method, making them suitable for non-parametric data and robust to outliers.
ImmiR requires the following packages:
ppcor- For partial correlation analysisdplyr- For data manipulationdata.table- For data.table support
Performs bivariate correlation analysis between two variables using Spearman's rank correlation.
Usage:
normal_cor_test(data, var_names)Arguments:
data: A data.frame or data.table containing your variablesvar_names: A character vector of length 2 with the names of variables to correlate
Returns: A data.frame with two columns:
spearman_cor: Spearman's correlation coefficient (rho)spearman_pv: P-value of the correlation test
Example:
library(ImmiR)
# Create sample data
data <- data.frame(
gene_expression = rnorm(100),
mir_expression = rnorm(100)
)
# Perform correlation test
result <- normal_cor_test(data, c("gene_expression", "mir_expression"))
print(result)Performs partial correlation analysis between two variables while controlling for a third variable.
Usage:
partial_cor_test(data, var_names)Arguments:
data: A data.frame or data.table containing your variablesvar_names: A character vector of length 3:- First element: primary variable
- Second element: secondary variable
- Third element: control variable
Returns: A data.frame with two columns:
spearman_cor: Partial correlation coefficientspearman_pv: P-value of the partial correlation test
Example:
library(ImmiR)
# Create sample data
data <- data.frame(
gene_expression = rnorm(100),
mir_expression = rnorm(100),
age = rnorm(100)
)
# Perform partial correlation test controlling for age
result <- partial_cor_test(
data,
c("gene_expression", "mir_expression", "age")
)
print(result)Use normal_cor_test() when you want to:
- Examine the direct relationship between two variables
- Identify monotonic relationships in your data
- Work with non-parametric data or data with outliers
Use partial_cor_test() when you want to:
- Control for confounding variables
- Examine the relationship between two variables while removing the effect of a third variable
- Conduct more sophisticated correlation analyses in multivariate datasets
- Robust error handling: Returns NA values when computation fails
- Missing data handling: Automatically handles missing values
- Non-parametric method: Uses Spearman's rank correlation, suitable for non-normal distributions
- Flexible input: Works with both data.frame and data.table objects
- Input validation: Checks for valid inputs before computation
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.