{"data":{"slug":"github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-sample-correlation-analysis-e3cf1fd365c9","title":"Run Correlation Analysis on Two Variables","summary":"Perform Pearson or Spearman correlation analysis on two numeric variables from a tabular data file, with support for command-line execution and output in CSV or TXT format.","url":"https:\/\/skillshunter.net\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-sample-correlation-analysis-e3cf1fd365c9","json_url":"https:\/\/skillshunter.net\/api\/public\/v1\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-sample-correlation-analysis-e3cf1fd365c9","markdown_url":"https:\/\/skillshunter.net\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-sample-correlation-analysis-e3cf1fd365c9.md","download_url":"https:\/\/skillshunter.net\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-sample-correlation-analysis-e3cf1fd365c9\/download","category":{"slug":"data-research","name":"Data & Research"},"editor_note":"Ensure Rscript is available in the shell and install required R packages `optparse` and `data.table` if missing. The input file must contain both target variables with numeric values, and at least 3 complete observation pairs are required.","instructions":"---\nname: sample-correlation-analysis\ndescription: Use when performing correlation analysis between two variables including Pearson and Spearman correlation methods. Supports command-line parameter input, automatic data format detection, parameter validation, result directory creation, and CSV or TXT format result export.\nlicense: MIT\nauthor: AIPOCH\n---\n> **Source**: [https:\/\/github.com\/aipoch\/medical-research-skills](https:\/\/github.com\/aipoch\/medical-research-skills)\n\n# Correlation Analysis\n\nUse this skill to run correlation analysis on two variables from a tabular data file.\n\n## Use This Skill When\n\n- You need Pearson or Spearman correlation between two variables stored either as columns or as first-column row labels.\n- You need a command-line workflow with parameter validation.\n- You need standardized output files in CSV or TXT format.\n\n## Primary Command\n\n```bash\nRscript scripts\/main.R \\\n  --data_file <input_file> \\\n  --method <pearson|spearman> \\\n  --x_var <variable_name> \\\n  --y_var <variable_name> \\\n  --output_dir <output_dir>\n```\n\n## Prerequisites\n\n- Rscript is available in the shell.\n- Required R packages: `optparse`, `data.table`.\n- Install missing packages with `Rscript -e 'install.packages(c(\"optparse\", \"data.table\"), repos=\"https:\/\/cloud.r-project.org\")'`.\n\n## Core Arguments\n\n| Argument | Required | Description |\n|----------|----------|-------------|\n| `--data_file` | Yes | Input data file in CSV, TXT, or TSV format |\n| `--method` | No | Correlation method: `pearson` or `spearman`. Default `pearson` |\n| `--x_var` | No | First variable name. It can match a column name or a first-column row label. Default `variable1` |\n| `--y_var` | No | Second variable name. It can match a column name or a first-column row label. Default `variable2` |\n| `--output_dir` | No | Output directory, default `.\/Correlation_Results` |\n| `--alternative` | No | `two.sided`, `less`, or `greater`. Default `two.sided` |\n| `--conf_level` | No | Confidence level between 0 and 1, default `0.95` |\n| `--output_format` | No | `csv` or `txt`, default `csv` |\n| `--output_prefix` | No | Output filename prefix, default `correlation` |\n\n## Input Requirements\n\n- The input file must contain both target variables.\n- Both variables must contain numeric values.\n- If the first column stores variable names and the remaining columns are samples, the script automatically reads variables by row label.\n- Rows with missing values in either variable are excluded.\n- At least 3 complete observation pairs are needed.\n\nExample input:\n\n```csv\nvariable1,variable2\n10.2,8.5\n11.5,9.2\n9.8,7.9\n```\n\n## Minimal Workflow\n\n1. Confirm the input file exists and variable names are correct.\n2. Run `scripts\/main.R` with the requested method and variable names.\n3. Check the output directory for result files under `table\/`.\n\nIf you omit `--data_file`, the script exits with `SKILL_MISSING_INPUT`.\n\n## Outputs\n\nExpected output structure:\n\n```text\n<output_dir>\/\n\u251c\u2500\u2500 table\/\n\u251c\u2500\u2500 figure\/\n\u2514\u2500\u2500 data\/\n```\n\nPrimary result file:\n\n- `table\/<output_prefix>_<method>.csv`\n- `table\/<output_prefix>_<method>.txt`\n\nResult fields include:\n\n- `method`\n- `correlation`\n- `statistic`\n- `p_value`\n- `conf_low`\n- `conf_high`\n- `sample_size`\n- `x_variable`\n- `y_variable`\n- `variable_orientation`\n\n## Choose the Method\n\n- Use `pearson` for linear relationships between continuous variables.\n- Use `spearman` for monotonic relationships, non-normal data, or outlier-prone data.\n\n## Read These Files When Needed\n\n| Need | File |\n|------|------|\n| Statistical details and assumptions | `references\/algorithm.md` |\n| More CLI examples | `references\/cli-guide.md` |\n| Error diagnosis | `references\/troubleshooting.md` |\n| Main execution entry point | `scripts\/main.R` |\n| Sample test data | `tests\/data\/` |\n\n## Quick Examples\n\nPearson:\n\n```bash\nRscript scripts\/main.R \\\n  --data_file tests\/data\/sample_correlation_1.csv \\\n  --method pearson \\\n  --x_var variable1 \\\n  --y_var variable2 \\\n  --output_dir tests\/output_pearson\n```\n\nSpearman:\n\n```bash\nRscript scripts\/main.R \\\n  --data_file tests\/data\/sample_correlation_3.csv \\\n  --method spearman \\\n  --x_var \"Activated CD8 T cell\" \\\n  --y_var \"Central memory CD8 T cell\" \\\n  --output_dir tests\/output_spearman\n```\n\n## Validation\n\n```bash\nRscript scripts\/main.R --help\n```\n\n```bash\nRscript scripts\/main.R \\\n  --data_file tests\/data\/sample_correlation_1.csv \\\n  --method pearson \\\n  --x_var variable1 \\\n  --y_var variable2 \\\n  --output_dir tests\/validation_output\n```\n\nAfter running analysis, verify that `tests\/validation_output\/table\/correlation_pearson.csv` exists.\n\n## Common Errors\n\n- `SKILL_FILE_NOT_FOUND`: Input file path is wrong or inaccessible.\n- `SKILL_MISSING_COLUMNS`: One or both requested variables are missing.\n- `SKILL_INVALID_DATA`: Input data is malformed or unsuitable for analysis.\n- `SKILL_INVALID_PARAMETER`: An argument value is invalid.\n- `SKILL_INSUFFICIENT_DATA`: Too few complete observations remain after filtering.\n- `SKILL_DEPENDENCY_MISSING`: A required R package such as `optparse` or `data.table` is unavailable.\n\nIf the issue is not obvious, read `references\/troubleshooting.md`.\n","use_cases":["Calculate Pearson correlation between two continuous variables in a CSV file.","Compute Spearman correlation for non-normal data with outlier-prone observations.","Generate standardized output files for statistical analysis in medical research."],"limitations":[],"compatibility":[],"setup_tip":null,"sample_input":null,"sample_output":null,"source_url":"https:\/\/github.com\/aipoch\/medical-research-skills\/blob\/686e09dfdb45d9fa1a08664f06bb93769b3c9d91\/awesome-med-research-skills\/Data%20Analysis\/sample-correlation-analysis\/SKILL.md","source_commit":"686e09dfdb45d9fa1a08664f06bb93769b3c9d91","license":"MIT","version":null,"listing_approved_at":"2026-11-24","created_at":"2026-11-23T07:29:51.000000Z","updated_at":"2026-11-24T07:13:04.000000Z","seo_title":"Run Correlation Analysis on Two Variables","seo_description":"Perform Pearson or Spearman correlation analysis on two numeric variables from a tabular data file with command-line support, output in CSV or TXT format.","seo_keywords":["correlation analysis","pearson correlation","spearman correlation","statistical analysis","medical research","command-line analysis"]}}