Source: https://github.com/aipoch/medical-research-skills
XGBoost Modeling And Feature Importance Ranking
Use this skill to train an XGBoost model from a tabular dataset and export both feature importance ranking tables and feature importance plots.
Use This Skill When
- You need a command-line XGBoost workflow in R for tabular data.
- You need a reproducible train-test split, model training, and evaluation.
- You need feature importance ranking outputs as both a table and a figure.
- You need automatic one-hot encoding for categorical predictors.
- Your data may contain a first unnamed sample ID column such as
V1 that should not enter the model.
Do Not Use This Skill When
- Your classification target has more than 2 classes.
- Your input is not tabular CSV, TXT, or TSV data.
- You need causal interpretation, mechanism claims, or policy, business, or clinical conclusions.
- You only need narrative interpretation or triage of an existing result rather than model training.
Primary Command
Rscript scripts/main.R \
--data_file <input_file> \
--target_var <target_column> \
--task_type <auto|classification|regression> \
--output_dir <output_dir>
Prerequisites
Rscript is available in the shell.
- Required R packages:
optparse, data.table, Matrix, xgboost.
- Install missing packages with
Rscript -e 'install.packages(c("optparse", "data.table", "Matrix", "xgboost"), repos="https://cloud.r-project.org")'.
Core Arguments
| Argument |
Required |
Description |
--data_file |
Yes |
Input CSV, TXT, or TSV file |
--target_var |
Yes |
Target column used for modeling |
--task_type |
No |
auto, classification, or regression. Default auto |
--output_dir |
No |
Output directory, default ./XGBoost_Results |
--ignore_vars |
No |
Comma-separated columns to exclude from predictors |
--positive_class |
No |
Positive class label for binary classification |
--test_size |
No |
Test set proportion between 0 and 1, default 0.2 |
--seed |
No |
Random seed, default 123 |
--nrounds |
No |
Maximum boosting rounds, default 300 |
--max_depth |
No |
Tree depth, default 6 |
--eta |
No |
Learning rate, default 0.1 |
--subsample |
No |
Row sampling ratio, default 0.8 |
--colsample_bytree |
No |
Column sampling ratio, default 0.8 |
--min_child_weight |
No |
Minimum child weight, default 1 |
--gamma |
No |
Minimum split loss reduction, default 0 |
--lambda |
No |
L2 regularization, default 1 |
--alpha |
No |
L1 regularization, default 0 |
--early_stopping_rounds |
No |
Early stopping rounds, default 20 |
--importance_metric |
No |
gain, cover, or frequency. Default gain |
--top_n |
No |
Number of features to plot, default 20 |
--output_format |
No |
Table format: csv or txt, default csv |
--output_prefix |
No |
Output filename prefix, default xgboost |
Input Requirements
- The input file must contain the target column.
- Predictor columns can be numeric, integer, logical, character, or factor-like text.
- Character and factor predictors are one-hot encoded automatically.
- A first unnamed identifier column such as
V1 is automatically excluded when it contains unique sample IDs.
- Rows with missing target values are removed before training.
- For classification, exactly 2 classes are required.
- For regression, the target column must be numeric.
- Each class should have at least 2 rows so both training and test sets can be created.
Example input:
,fustat,CAMK2N2,GGT6,GPR161,RAB26,RIBC2
TCGA-C5-A1M5,1,2.248291938,5.274690305,2.825215762,3.121114894,5.35318565
TCGA-EA-A5O9,0,3.346176843,5.404368414,2.604616977,0.629473197,4.429314674
TCGA-C5-A3HL,0,3.363100974,5.363314779,4.124799581,4.127228806,4.916596068
Minimal Workflow
- Confirm the input file exists and the target column name is correct.
- Run
scripts/main.R with --data_file and --target_var.
- Check the output directory for
table/feature_importance_* and figure/feature_importance_*.
If you omit --data_file or --target_var, the script exits with SKILL_MISSING_INPUT.
Outputs
Expected output structure:
<output_dir>/
├── table/
├── figure/
└── data/
Primary outputs:
table/<output_prefix>_feature_importance.csv
table/<output_prefix>_model_performance.csv
figure/<output_prefix>_feature_importance_<importance_metric>.png
Additional outputs:
Feature importance table fields include:
Rank
Feature
Gain
Cover
Frequency
SelectedMetric
SelectedValue
Feature Importance Metrics
gain: Average contribution to loss reduction. Recommended for most ranking use cases.
cover: Relative sample coverage contributed by a feature.
frequency: How often the feature is used in splits.
Read These Files When Needed
| Need |
File |
| XGBoost method details and importance interpretation |
references/algorithm.md |
| More CLI examples |
references/cli-guide.md |
| Error diagnosis |
references/troubleshooting.md |
| Main execution entry point |
scripts/main.R |
| Bundled test data |
tests/data/ |
Quick Examples
Auto-detected binary classification on dt_sample1.csv:
Rscript scripts/main.R \
--data_file tests/data/dt_sample1.csv \
--target_var fustat \
--task_type auto \
--output_dir tests/output_binary
Binary classification on dt_sample2.csv:
Rscript scripts/main.R \
--data_file tests/data/dt_sample2.csv \
--target_var fustat \
--task_type classification \
--importance_metric gain \
--output_dir tests/output_gain
Character-label classification on dt_sample3.txt:
Rscript scripts/main.R \
--data_file tests/data/dt_sample3.txt \
--target_var Group \
--task_type classification \
--positive_class high \
--top_n 15 \
--output_dir tests/output_group
Validation
Rscript scripts/main.R --help
Rscript scripts/main.R \
--data_file tests/data/dt_sample1.csv \
--target_var fustat \
--task_type classification \
--output_dir tests/validation_output
After running analysis, verify that these files exist:
tests/validation_output/table/xgboost_feature_importance.csv
tests/validation_output/table/xgboost_model_performance.csv
tests/validation_output/figure/xgboost_feature_importance_gain.png
Common Errors
SKILL_FILE_NOT_FOUND: Input file path is wrong or inaccessible.
SKILL_MISSING_COLUMNS: The target column is missing.
SKILL_INVALID_DATA: Data is malformed, the target type is unsuitable, classification has more or fewer than 2 classes, or too few usable rows remain.
SKILL_INVALID_PARAMETER: An argument value is invalid.
SKILL_DEPENDENCY_MISSING: A required R package such as xgboost is unavailable.
If the issue is not obvious, read references/troubleshooting.md.
1---
2name: xgboost-analysis
3description: Use when building XGBoost models on tabular data and returning feature importance ranking outputs. Supports binary classification and regression with automatic task detection, train-test split, performance tables, feature importance ranking tables, and PNG importance plots.
4license: MIT
5author: AIPOCH
6metadata:
7 source: https://skillmd.com/skills/aibot88/xgboost-analysis
8---
9
10> **Source**: [https://github.com/aipoch/medical-research-skills](https://github.com/aipoch/medical-research-skills)
11
12# XGBoost Modeling And Feature Importance Ranking
13
14Use this skill to train an XGBoost model from a tabular dataset and export both feature importance ranking tables and feature importance plots.
15
16## Use This Skill When
17
18- You need a command-line XGBoost workflow in R for tabular data.
19- You need a reproducible train-test split, model training, and evaluation.
20- You need feature importance ranking outputs as both a table and a figure.
21- You need automatic one-hot encoding for categorical predictors.
22- Your data may contain a first unnamed sample ID column such as `V1` that should not enter the model.
23
24## Do Not Use This Skill When
25
26- Your classification target has more than 2 classes.
27- Your input is not tabular CSV, TXT, or TSV data.
28- You need causal interpretation, mechanism claims, or policy, business, or clinical conclusions.
29- You only need narrative interpretation or triage of an existing result rather than model training.
30
31## Primary Command
32
33```bash
34Rscript scripts/main.R \
35 --data_file <input_file> \
36 --target_var <target_column> \
37 --task_type <auto|classification|regression> \
38 --output_dir <output_dir>
39```
40
41## Prerequisites
42
43- `Rscript` is available in the shell.
44- Required R packages: `optparse`, `data.table`, `Matrix`, `xgboost`.
45- Install missing packages with `Rscript -e 'install.packages(c("optparse", "data.table", "Matrix", "xgboost"), repos="https://cloud.r-project.org")'`.
46
47## Core Arguments
48
49| Argument | Required | Description |
50|----------|----------|-------------|
51| `--data_file` | Yes | Input CSV, TXT, or TSV file |
52| `--target_var` | Yes | Target column used for modeling |
53| `--task_type` | No | `auto`, `classification`, or `regression`. Default `auto` |
54| `--output_dir` | No | Output directory, default `./XGBoost_Results` |
55| `--ignore_vars` | No | Comma-separated columns to exclude from predictors |
56| `--positive_class` | No | Positive class label for binary classification |
57| `--test_size` | No | Test set proportion between 0 and 1, default `0.2` |
58| `--seed` | No | Random seed, default `123` |
59| `--nrounds` | No | Maximum boosting rounds, default `300` |
60| `--max_depth` | No | Tree depth, default `6` |
61| `--eta` | No | Learning rate, default `0.1` |
62| `--subsample` | No | Row sampling ratio, default `0.8` |
63| `--colsample_bytree` | No | Column sampling ratio, default `0.8` |
64| `--min_child_weight` | No | Minimum child weight, default `1` |
65| `--gamma` | No | Minimum split loss reduction, default `0` |
66| `--lambda` | No | L2 regularization, default `1` |
67| `--alpha` | No | L1 regularization, default `0` |
68| `--early_stopping_rounds` | No | Early stopping rounds, default `20` |
69| `--importance_metric` | No | `gain`, `cover`, or `frequency`. Default `gain` |
70| `--top_n` | No | Number of features to plot, default `20` |
71| `--output_format` | No | Table format: `csv` or `txt`, default `csv` |
72| `--output_prefix` | No | Output filename prefix, default `xgboost` |
73
74## Input Requirements
75
76- The input file must contain the target column.
77- Predictor columns can be numeric, integer, logical, character, or factor-like text.
78- Character and factor predictors are one-hot encoded automatically.
79- A first unnamed identifier column such as `V1` is automatically excluded when it contains unique sample IDs.
80- Rows with missing target values are removed before training.
81- For classification, exactly 2 classes are required.
82- For regression, the target column must be numeric.
83- Each class should have at least 2 rows so both training and test sets can be created.
84
85Example input:
86
87```csv
88,fustat,CAMK2N2,GGT6,GPR161,RAB26,RIBC2
89TCGA-C5-A1M5,1,2.248291938,5.274690305,2.825215762,3.121114894,5.35318565
90TCGA-EA-A5O9,0,3.346176843,5.404368414,2.604616977,0.629473197,4.429314674
91TCGA-C5-A3HL,0,3.363100974,5.363314779,4.124799581,4.127228806,4.916596068
92```
93
94## Minimal Workflow
95
961. Confirm the input file exists and the target column name is correct.
972. Run `scripts/main.R` with `--data_file` and `--target_var`.
983. Check the output directory for `table/feature_importance_*` and `figure/feature_importance_*`.
99
100If you omit `--data_file` or `--target_var`, the script exits with `SKILL_MISSING_INPUT`.
101
102## Outputs
103
104Expected output structure:
105
106```text
107<output_dir>/
108├── table/
109├── figure/
110└── data/
111```
112
113Primary outputs:
114
115- `table/<output_prefix>_feature_importance.csv`
116- `table/<output_prefix>_model_performance.csv`
117- `figure/<output_prefix>_feature_importance_<importance_metric>.png`
118
119Additional outputs:
120
121- `session_info.txt`
122
123Feature importance table fields include:
124
125- `Rank`
126- `Feature`
127- `Gain`
128- `Cover`
129- `Frequency`
130- `SelectedMetric`
131- `SelectedValue`
132
133## Feature Importance Metrics
134
135- `gain`: Average contribution to loss reduction. Recommended for most ranking use cases.
136- `cover`: Relative sample coverage contributed by a feature.
137- `frequency`: How often the feature is used in splits.
138
139## Read These Files When Needed
140
141| Need | File |
142|------|------|
143| XGBoost method details and importance interpretation | `references/algorithm.md` |
144| More CLI examples | `references/cli-guide.md` |
145| Error diagnosis | `references/troubleshooting.md` |
146| Main execution entry point | `scripts/main.R` |
147| Bundled test data | `tests/data/` |
148
149## Quick Examples
150
151Auto-detected binary classification on `dt_sample1.csv`:
152
153```bash
154Rscript scripts/main.R \
155 --data_file tests/data/dt_sample1.csv \
156 --target_var fustat \
157 --task_type auto \
158 --output_dir tests/output_binary
159```
160
161Binary classification on `dt_sample2.csv`:
162
163```bash
164Rscript scripts/main.R \
165 --data_file tests/data/dt_sample2.csv \
166 --target_var fustat \
167 --task_type classification \
168 --importance_metric gain \
169 --output_dir tests/output_gain
170```
171
172Character-label classification on `dt_sample3.txt`:
173
174```bash
175Rscript scripts/main.R \
176 --data_file tests/data/dt_sample3.txt \
177 --target_var Group \
178 --task_type classification \
179 --positive_class high \
180 --top_n 15 \
181 --output_dir tests/output_group
182```
183
184## Validation
185
186```bash
187Rscript scripts/main.R --help
188```
189
190```bash
191Rscript scripts/main.R \
192 --data_file tests/data/dt_sample1.csv \
193 --target_var fustat \
194 --task_type classification \
195 --output_dir tests/validation_output
196```
197
198After running analysis, verify that these files exist:
199
200- `tests/validation_output/table/xgboost_feature_importance.csv`
201- `tests/validation_output/table/xgboost_model_performance.csv`
202- `tests/validation_output/figure/xgboost_feature_importance_gain.png`
203
204## Common Errors
205
206- `SKILL_FILE_NOT_FOUND`: Input file path is wrong or inaccessible.
207- `SKILL_MISSING_COLUMNS`: The target column is missing.
208- `SKILL_INVALID_DATA`: Data is malformed, the target type is unsuitable, classification has more or fewer than 2 classes, or too few usable rows remain.
209- `SKILL_INVALID_PARAMETER`: An argument value is invalid.
210- `SKILL_DEPENDENCY_MISSING`: A required R package such as `xgboost` is unavailable.
211
212If the issue is not obvious, read `references/troubleshooting.md`.