{"data":{"slug":"github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-rf-model-importance-analysis-80d24df9043e","title":"RF Model Importance Analysis for Binary Classification","summary":"Train a two-class random forest model using an expression-like feature matrix, rank variable importance, and generate reproducible error and importance plots.","url":"https:\/\/skillshunter.net\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-rf-model-importance-analysis-80d24df9043e","json_url":"https:\/\/skillshunter.net\/api\/public\/v1\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-rf-model-importance-analysis-80d24df9043e","markdown_url":"https:\/\/skillshunter.net\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-rf-model-importance-analysis-80d24df9043e.md","download_url":"https:\/\/skillshunter.net\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-rf-model-importance-analysis-80d24df9043e\/download","category":{"slug":"data-research","name":"Data & Research"},"editor_note":"Before running the CLI, ensure the data is already cleaned for binary classification: samples in rows, numeric feature columns only, and no missing values. Imputation, normalization, and batch correction are outside this skill's scope.","instructions":"---\nname: rf-model-importance-analysis\ndescription: Use when you need a standardized R CLI workflow to train a two-class random forest model from an expression-like feature matrix, rank variable importance, and generate reproducible error and importance plots. NOT for regression tasks, multi-class classification, missing-value imputation, preprocessing, or remote data fetching.\nlicense: MIT\nauthor: AIPOCH\n---\n> **Source**: [https:\/\/github.com\/aipoch\/medical-research-skills](https:\/\/github.com\/aipoch\/medical-research-skills)\n\n# RF Model Importance Analysis\n\n## Quick Start\n\nUse one of these three commands first, then consult the full argument table only if you need extra tuning.\n\n### 1. Standard Run\n\n```bash\nRscript scripts\/main.R \\\n  --input_file tests\/data\/expression_matrix.csv \\\n  --group_file tests\/data\/group_info.csv \\\n  --case_group AR \\\n  --control_group Control \\\n  --output_dir tests\/output\/manual-test \\\n  --seed 42 \\\n  --timeout_seconds 300\n```\n\n### 2. Tuned Importance Run\n\n```bash\nRscript scripts\/main.R \\\n  --input_file tests\/data\/expression_matrix.csv \\\n  --group_file tests\/data\/group_info.csv \\\n  --case_group AR \\\n  --control_group Control \\\n  --output_dir tests\/output\/custom-importance \\\n  --seed 42 \\\n  --rf_ntree 800 \\\n  --rf_mtry 4 \\\n  --rf_imp_type 2 \\\n  --rf_imp_threshold 1 \\\n  --rf_top_n 8 \\\n  --rf_importance_top_n 8 \\\n  --timeout_seconds 300\n```\n\n### 3. Plot-Only Rerender\n\nRun this only after a full analysis has already created `output_dir\/data\/rf_result.rds`.\n\n```bash\nRscript scripts\/main.R \\\n  --plot_only TRUE \\\n  --output_dir tests\/output\/manual-test \\\n  --seed 42 \\\n  --timeout_seconds 300\n```\n\n## When to Read External Files\n\n| Situation | File to Read | Purpose |\n|-----------|--------------|---------|\n| Need algorithm details | `references\/algorithm.md` | Explain random forest modeling, importance metrics, assumptions, and result interpretation |\n| Need to execute the analysis | `scripts\/main.R` | Run the CLI entry point with a complete `Rscript` command |\n| Encounter an error | `references\/troubleshooting.md` | Map error codes to causes and fixes |\n| Need CLI examples | `references\/cli-guide.md` | See installation steps and runnable command examples |\n| Need a runnable smoke test | `tests\/data\/` | Use the bundled small dataset for verification |\n\n## Stop Conditions\n\nDo not use this skill when any of the following is true:\n\n- The task is regression, multiclass classification, time-series modeling, or remote data fetching.\n- The input still requires imputation, normalization, batch correction, or other preprocessing.\n- The feature matrix contains missing values, non-numeric feature columns, or mismatched sample IDs.\n\nIf one of those conditions applies, stop and hand off to a preprocessing or alternative modeling workflow before running this skill.\n\n## Usage\n\nBefore running the CLI, ensure the data is already cleaned for binary classification: samples in rows, numeric feature columns only, and no missing values. Imputation, normalization, and batch correction are outside this skill's scope.\n\n```bash\nRscript scripts\/main.R \\\n  --input_file .\/input\/expression_matrix.csv \\\n  --group_file .\/input\/group_info.csv \\\n  --case_group Case \\\n  --control_group Control \\\n  --output_dir output\/basic-run \\\n  --seed 42 \\\n  --timeout_seconds 600\n```\n\n## Arguments\n\n| Short | Long | Type | Default | Required | Description |\n|-------|------|------|---------|----------|-------------|\n| `-i` | `--input_file` | character | none | yes, unless `--plot_only TRUE` | Expression matrix file with samples in rows and features in columns |\n| `-g` | `--group_file` | character | none | yes, unless `--plot_only TRUE` | Group file with sample IDs in the first column |\n| `-c` | `--case_group` | character | none | yes, unless `--plot_only TRUE` | Case group label |\n| `-r` | `--control_group` | character | none | yes, unless `--plot_only TRUE` | Control group label |\n| `-o` | `--output_dir` | character | `output` | yes | Output directory inside the skill root |\n| `-p` | `--plot_only` | logical | `FALSE` | no | Reuse `output_dir\/data\/rf_result.rds` and regenerate plots without retraining |\n| `-s` | `--seed` | integer | `42` | no | Random seed for reproducibility |\n| `-t` | `--timeout_seconds` | integer | `600` | no | Elapsed time limit for the run |\n|  | `--rf_ntree` | integer | `500` | no | Number of trees in the random forest |\n|  | `--rf_mtry` | integer | `NA` | no | Variables sampled at each split; `NA` uses the package default |\n|  | `--rf_nodesize` | integer | `NA` | no | Minimum terminal node size; `NA` uses the package default |\n|  | `--rf_imp_type` | integer | `1` | no | Importance metric type passed to `randomForest::importance`; allowed values are `1` or `2` |\n|  | `--rf_imp_threshold` | numeric | `0` | no | Minimum importance score retained in `rf_top_features.csv` |\n|  | `--rf_top_n` | integer | `30` | no | Maximum number of rows written to `rf_top_features.csv` |\n|  | `--rf_error_xlab` | character | `Number of Trees` | no | X-axis label for the RF error plot |\n|  | `--rf_error_ylab` | character | `Error` | no | Y-axis label for the RF error plot |\n|  | `--rf_error_line_size` | numeric | `0.6` | no | Line width for the RF error plot |\n|  | `--rf_error_line_alpha` | numeric | `1` | no | Line alpha for the RF error plot |\n|  | `--rf_error_line_color` | character | `#6C85F9,#D9503D,#939DE4,#DEA441,#A2C6D6,#E9B9E1,#BDD69F,#EBC98A` | no | Comma-separated line colors for non-OOB curves |\n|  | `--rf_error_line_type` | character | `dashed` | no | Line type for class-specific error curves |\n|  | `--rf_error_line_oob_type` | character | `solid` | no | Line type for the OOB curve |\n|  | `--rf_error_legend_position` | character | `none` | no | Legend position for the RF error plot |\n|  | `--rf_error_border_color` | character | `black` | no | Panel border color for the RF error plot |\n|  | `--rf_error_border_fill` | character | `NA` | no | Panel fill for the RF error plot; use `NA` or `NULL` as text |\n|  | `--rf_error_border_size` | numeric | `0.8` | no | Panel border width for the RF error plot |\n|  | `--rf_error_base_size` | numeric | `14` | no | Base font size for the RF error plot |\n|  | `--rf_error_width` | numeric | `6` | no | RF error plot width in inches |\n|  | `--rf_error_height` | numeric | `5` | no | RF error plot height in inches |\n|  | `--rf_importance_sort` | logical | `TRUE` | no | Sort variables in the importance plot |\n|  | `--rf_importance_top_n` | integer | `10` | no | Maximum number of variables shown in the importance plot |\n|  | `--rf_importance_label_x_ann` | logical | `TRUE` | no | Show x-axis tick labels in the importance plot |\n|  | `--rf_importance_label_color` | character | `black` | no | Text and point outline color in the importance plot |\n|  | `--rf_importance_label_cex` | numeric | `0.9` | no | Label size in the importance plot |\n|  | `--rf_importance_point_cex` | numeric | `0.9` | no | Point size in the importance plot |\n|  | `--rf_importance_point_shape` | integer | `23` | no | Point shape in the importance plot |\n|  | `--rf_importance_point_fill` | character | `red` | no | Point fill color in the importance plot |\n|  | `--rf_importance_line_color` | character | `gray` | no | Segment color in the importance plot |\n|  | `--rf_importance_theme_border` | logical | `TRUE` | no | Draw panel borders in the importance plot |\n|  | `--rf_importance_theme_offset` | numeric | `0.2` | no | Axis expansion factor in the importance plot |\n|  | `--rf_importance_title` | character | `Variable Importance` | no | Main title for the importance plot |\n|  | `--rf_importance_title_x_ann` | logical | `TRUE` | no | Show title and axis annotations in the importance plot |\n|  | `--rf_importance_width` | numeric | `6` | no | RF importance plot width in inches |\n|  | `--rf_importance_height` | numeric | `5` | no | RF importance plot height in inches |\n\n## Input Format\n\n### Expression Matrix\n\n- CSV or TSV.\n- First column: sample IDs.\n- Remaining columns: numeric features.\n- Samples must be rows.\n- Missing or non-numeric feature values are not allowed.\n\nExample:\n\n```csv\nsample,HIF1A,NR4A1,SOCS1\nS1,6.21,-1.34,2.01\nS2,6.57,0.37,3.62\nS3,7.05,2.12,5.01\n```\n\n### Group File\n\n- CSV or TSV.\n- First column: sample IDs.\n- One additional column must contain both the case and control labels.\n- Exactly two groups are supported.\n\nExample:\n\n```csv\nsample,group\nS1,Case\nS2,Case\nS3,Control\n```\n\n## Output Files\n\n| File | Format | Description |\n|------|--------|-------------|\n| `data\/rf_result.rds` | RDS | Serialized model bundle with the trained random forest and metadata |\n| `table\/rf_feature_importance.csv` | CSV | Full ranked feature-importance table using the selected importance metric |\n| `table\/rf_top_features.csv` | CSV | Filtered top feature table after applying `--rf_imp_threshold` and `--rf_top_n` |\n| `plot\/rf_error_plot.pdf` | PDF | Error curves across trees for OOB and class-specific classification error |\n| `plot\/rf_importance_plot.pdf` | PDF | Variable-importance plot generated by `randomForest::varImpPlot()` |\n| `session_info.txt` | TXT | R version, platform, and package version information |\n\n## Error Handling\n\n- Successful runs exit with status code `0`.\n- Failed runs exit with status code `1`.\n- Error messages use standardized names such as `SKILL_FILE_NOT_FOUND` and `SKILL_INVALID_PARAMETER`.\n- Output paths are validated so that `--output_dir` cannot write outside the skill root.\n- The analysis never performs network requests and never executes user input through `eval()`, `exec()`, or `system()`.\n\nCommon codes:\n\n| Error Code | Meaning |\n|------------|---------|\n| `SKILL_FILE_NOT_FOUND` | An input file or required plot-only artifact does not exist |\n| `SKILL_MISSING_COLUMNS` | The input file does not contain the required columns |\n| `SKILL_EMPTY_DATA` | An input file is empty or a required model table is unavailable |\n| `SKILL_INVALID_PARAMETER` | A CLI argument, group setting, numeric constraint, or path is invalid |\n| `SKILL_SAMPLE_MISMATCH` | Sample IDs do not match between the expression matrix and group file |\n| `SKILL_PACKAGE_NOT_FOUND` | One or more required CRAN packages are missing |\n\nFor detailed fixes, READ: `references\/troubleshooting.md`\n\n## Testing\n\n### Help Check\n\n```bash\nRscript scripts\/main.R --help\n```\n\n### Full Test Run\n\n```bash\nRscript tests\/run_tests.R\n```\n\n### Direct Test Command\n\n```bash\nRscript scripts\/main.R \\\n  --input_file tests\/data\/expression_matrix.csv \\\n  --group_file tests\/data\/group_info.csv \\\n  --case_group AR \\\n  --control_group Control \\\n  --output_dir tests\/output\/manual-test \\\n  --seed 42 \\\n  --rf_ntree 200 \\\n  --rf_top_n 5 \\\n  --rf_importance_top_n 5 \\\n  --timeout_seconds 300\n```\n","use_cases":["Train a binary classification model using an expression matrix and group file.","Rank feature importance using a custom importance metric.","Generate reproducible error and importance plots for model evaluation."],"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\/rf-model-importance-analysis\/SKILL.md","source_commit":"686e09dfdb45d9fa1a08664f06bb93769b3c9d91","license":"MIT","version":null,"listing_approved_at":"2026-11-24","created_at":"2026-11-23T07:27:19.000000Z","updated_at":"2026-11-24T07:13:04.000000Z","seo_title":"RF Model Importance Analysis for Binary Classification","seo_description":"Train a two-class random forest model using an expression-like feature matrix, rank variable importance, and generate reproducible error and importance plots.","seo_keywords":["binary classification model","feature importance ranking","random forest analysis","reproducible model plots","expression matrix analysis"]}}