{"data":{"slug":"github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-lasso-logistics-analysis-f2a483f27746","title":"LASSO Logistic Regression Analysis for Binary Classification","summary":"Perform binary classification using LASSO logistic regression with cross-validation and coefficient path visualization. Analyze omics data for feature selection, producing output files such as coefficient tables, selected features, and visualizations.","url":"https:\/\/skillshunter.net\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-lasso-logistics-analysis-f2a483f27746","json_url":"https:\/\/skillshunter.net\/api\/public\/v1\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-lasso-logistics-analysis-f2a483f27746","markdown_url":"https:\/\/skillshunter.net\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-lasso-logistics-analysis-f2a483f27746.md","download_url":"https:\/\/skillshunter.net\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-lasso-logistics-analysis-f2a483f27746\/download","category":{"slug":"data-research","name":"Data & Research"},"editor_note":"Ensure the input files are in CSV or TSV format with features as rows and samples as columns. The group file must contain sample IDs and binary group labels (case\/control). The case group must be encoded as `1` and the control group as `0`. The output directory will be created if it does not exist.","instructions":"---\nname: lasso-logistics-analysis\ndescription: \"Use when building a binary classification model from an expression matrix or other omics feature matrix with LASSO logistic regression, cross-validation, and coefficient path visualization. NOT for: multiclass classification, survival\/Cox models, or ordinary linear regression.\"\n---\n\n# LASSO Logistic Regression Analysis\n\n## When to Read External Files\n\n| Situation | File to Read | Purpose |\n|-----------|--------------|---------|\n| **Need algorithm details** | `references\/algorithm.md` | LASSO objective function, cross-validation, and interpretation |\n| **Need to run analysis** | `scripts\/main.R` | Execute: `Rscript scripts\/main.R --input_file ... --group_file ...` |\n| **Encounter errors** | `references\/troubleshooting.md` | Common errors and solutions |\n| **Need CLI examples** | `references\/cli-guide.md` | Detailed CLI usage examples |\n| **Need test data** | `tests\/data\/` | Sample input files for testing |\n| **Need workflow implementation details** | `scripts\/run_analysis.R` | Inspect orchestration, outputs, and file-writing behavior |\n| **Need input-validation or error-handling details** | `scripts\/utils.R`, `scripts\/io.R` | Inspect validation, parsing, logging, and standardized safeguards |\n\n---\n\n## Usage\n\n```bash\nRscript scripts\/main.R \\\n  --input_file .\/expression_matrix.csv \\\n  --group_file .\/groups.csv \\\n  --case_group case \\\n  --control_group control \\\n  --output_dir .\/output\/ \\\n  --nfolds 10 \\\n  --timeout_seconds 1800 \\\n  --seed 42\n```\n\n---\n\n## Arguments\n\n| Short | Long | Type | Default | Description |\n|-------|------|------|---------|-------------|\n| `-i` | `--input_file` | character | **required** | Expression matrix file (features as rows, samples as columns) |\n| `-g` | `--group_file` | character | **required** | Group file with sample and group columns |\n| `-c` | `--case_group` | character | **required** | Case class label encoded as `1` |\n| `-t` | `--control_group` | character | **required** | Control class label encoded as `0` |\n| `-f` | `--feature` | character | `NULL` | Optional feature list file or comma-separated feature names |\n| `-n` | `--nfolds` | integer | `10` | Cross-validation folds: `3`, `5`, `7`, `10` |\n|  | `--cv_title` | character | `\"\"` | Optional title for the cross-validation plot |\n|  | `--path_title` | character | `\"\"` | Optional title for the coefficient path plot |\n|  | `--timeout_seconds` | integer | `1800` | Maximum elapsed runtime in seconds |\n| `-o` | `--output_dir` | character | `.\/output\/` | Output directory |\n| `-s` | `--seed` | integer | `42` | Random seed for reproducibility |\n\n---\n\n## Input Format\n\n### Expression Matrix (`input_file`)\n\nFeatures as rows, samples as columns, CSV or TSV format with feature IDs in the first column.\n\n```csv\n,Sample01,Sample02,Sample03\nTSPAN6,1.8479,1.8318,3.8276\nTNMD,0.0349,0.0533,1.3889\n```\n\n### Group File (`group_file`)\n\nCSV or TSV with sample IDs and binary-group labels.\n\n```csv\nsample,group\nSample01,case\nSample02,control\nSample03,case\n```\n\n### Optional Feature File (`feature`)\n\nOne feature per line, or pass a comma-separated feature list directly on the CLI.\n\n```text\nTNMD\nDPM1\nSCYL3\n```\n\n---\n\n## Output Files\n\n| File | Description |\n|------|-------------|\n| `coefficient.csv` | All coefficients at `lambda.min` |\n| `feature_matrix.csv` | Sample-level matrix with original group labels and binary event column |\n| `selected_features.txt` | Non-zero features at `lambda.min` excluding the intercept, when available |\n| `missing_features.txt` | Requested features not found in the matrix, when applicable |\n| `lasso_lambda_binary_plot.pdf` | Cross-validation curve |\n| `lasso_var_binary_plot.pdf` | Coefficient path plot |\n| `session_info.txt` | R session and package version info |\n\n---\n\n## Workflow\n\n### Step 1: Validate Input\n**WHEN checking validation rules or parsing behavior**, READ: `scripts\/utils.R` and `scripts\/io.R`\n\n- Check file existence\n- Read expression matrix and group file\n- Verify samples match between files\n- Ensure both classes are present with at least 2 samples per class\n\n### Step 2: Prepare Modeling Matrix\n**WHEN checking class encoding or feature filtering behavior**, READ: `scripts\/modeling.R`\n\n- Encode `case_group` as `1` and `control_group` as `0`\n- Optionally restrict to a user-supplied feature panel\n- Transpose expression data to sample-by-feature format\n\n### Step 3: Fit LASSO Logistic Regression\n**WHEN understanding the statistical method or lambda selection**, READ: `references\/algorithm.md`\n\n- Train a binomial `glmnet` model with `alpha = 1`\n- Run `cv.glmnet` to select the optimal lambda\n- Extract coefficients at `lambda.min`\n\n### Step 4: Save Results and Visualizations\n**WHEN checking output generation or plot behavior**, READ: `scripts\/run_analysis.R` and `scripts\/plotting.R`\n\n- Save flat output files directly into `output_dir`\n- Generate cross-validation and coefficient path PDF plots\n- Leave plot titles empty by default unless the user provides custom titles\n\n---\n\n## Methods\n\n### LASSO Logistic Regression\nThe model minimizes binomial deviance with an L1 penalty, shrinking weak coefficients to zero and performing embedded feature selection.\n\n### Cross-Validation\n`cv.glmnet` evaluates candidate lambda values across `nfolds` folds and reports `lambda.min` and `lambda.1se`.\n\n---\n\n## Examples\n\n### Basic Usage\n```bash\nRscript scripts\/main.R \\\n  -i .\/expression_matrix.csv \\\n  -g .\/groups.csv \\\n  -c case \\\n  -t control \\\n  -o .\/output\n```\n\n### Use a Feature Panel\n```bash\nRscript scripts\/main.R \\\n  -i .\/expression_matrix.csv \\\n  -g .\/groups.csv \\\n  -c case \\\n  -t control \\\n  -f .\/genes.txt \\\n  -o .\/output\n```\n\n### Custom Folds and Seed\n```bash\nRscript scripts\/main.R \\\n  -i .\/expression_matrix.csv \\\n  -g .\/groups.csv \\\n  -c case \\\n  -t control \\\n  -n 5 \\\n  --timeout_seconds 900 \\\n  -s 123 \\\n  -o .\/output\n```\n\n### Custom Plot Titles\n```bash\nRscript scripts\/main.R \\\n  -i .\/expression_matrix.csv \\\n  -g .\/groups.csv \\\n  -c case \\\n  -t control \\\n  --cv_title \"LASSO Cross-Validation\" \\\n  --path_title \"LASSO Coefficient Paths\" \\\n  --timeout_seconds 1200 \\\n  -o .\/output\n```\n\n---\n\n## Error Handling\n\n### Common Errors\n\n| Error | Cause | Solution |\n|-------|-------|----------|\n| `SKILL_FILE_NOT_FOUND` | Input file does not exist | Check file path |\n| `SKILL_EMPTY_FILE` | An input file exists but contains no data | Verify the file is not empty |\n| `SKILL_PARSE_ERROR` | The input file cannot be parsed as CSV or TSV | Check delimiters, headers, and encoding |\n| `SKILL_FILE_WRITE_ERROR` | The output directory cannot be created or written | Check output path and permissions |\n| `SKILL_EMPTY_DATA` | The loaded table has no usable rows or columns | Verify that the input file contains valid data |\n| `SKILL_MISSING_COLUMNS` | The group file does not provide the required columns | Provide sample and group columns |\n| `SKILL_INVALID_TYPE` | A parameter or data field has the wrong type | Ensure numeric fields are numeric and strings are valid |\n| `SKILL_SAMPLE_MISMATCH` | Sample IDs differ between matrix and group file | Make names match exactly |\n| `SKILL_INVALID_GROUP` | Case\/control labels not found in group file | Check `--case_group` and `--control_group` |\n| `SKILL_INVALID_DATA` | Too few classes, samples, or valid features | Review input structure and feature list |\n| `SKILL_INVALID_PARAMETER` | Unsupported `nfolds` or empty parameter | Use documented argument values |\n| `SKILL_DEPENDENCY_MISSING` | Required R package not installed | Install missing CRAN package |\n| `SKILL_TIMEOUT` | Analysis exceeded the configured time limit | Reduce feature count or increase `--timeout_seconds` |\n| `SKILL_MEMORY_ERROR` | The runtime environment cannot allocate enough memory | Reduce matrix size or available workload |\n| `SKILL_RUNTIME_ERROR` | An unexpected runtime error occurred | Review the exact console error and retry |\n\n**IF error persists**, READ: `references\/troubleshooting.md`\n\n---\n\n## Testing\n\n### Test with Sample Data\n\n```bash\n# Check help\nRscript scripts\/main.R --help\n\n# Run with sample data\nRscript scripts\/main.R \\\n  -i tests\/data\/expression_matrix.csv \\\n  -g tests\/data\/groups.csv \\\n  -c case \\\n  -t control \\\n  -f tests\/data\/genes.csv \\\n  --timeout_seconds 1800 \\\n  -o tests\/output\n```\n\n### Validation Commands\n\n```bash\n# Check coefficient output\nls -la tests\/output\/coefficient.csv\n\n# Check plots exist\nls -la tests\/output\/lasso_lambda_binary_plot.pdf\nls -la tests\/output\/lasso_var_binary_plot.pdf\n```\n\n---\n\n## Implementation Checklist\n\n- [x] CLI parsing with `optparse`\n- [x] `set.seed()` for reproducibility\n- [x] `requireNamespace()` dependency checks\n- [x] Session info recording\n- [x] Timeout control with `--timeout_seconds`\n- [x] Temp file cleanup\n- [x] File reading instructions in SKILL.md\n- [x] Modular script structure (<150 lines per file)\n- [x] Test data provided\n- [x] Error handling with SKILL_* codes\n- [x] Scripts in `scripts\/` directory\n- [x] References in `references\/` directory\n\n---\n\n*Last updated: 2026-04-17 | Version: 1.0.0*\n","use_cases":["Classify cancer subtypes using gene expression data","Identify biomarkers from RNA-seq data","Select features for binary disease classification"],"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\/lasso-logistics-analysis\/SKILL.md","source_commit":"686e09dfdb45d9fa1a08664f06bb93769b3c9d91","license":null,"version":null,"listing_approved_at":"2026-11-24","created_at":"2026-11-23T07:19:31.000000Z","updated_at":"2026-11-24T07:13:04.000000Z","seo_title":"LASSO Logistic Regression Binary Classification","seo_description":"Perform binary classification using LASSO logistic regression with cross-validation and coefficient path visualization. Analyze omics data for feature selection.","seo_keywords":["LASSO logistic regression binary classification","omics data feature selection","cross-validation coefficient path","binary classification analysis","gene expression classification"]}}