{"data":{"slug":"github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-xgboost-analysis-0c193a488234","title":"XGBoost Modeling And Feature Importance Ranking","summary":"Train XGBoost models on tabular data and export feature importance rankings and plots, including tables and PNG figures.","url":"https:\/\/skillshunter.net\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-xgboost-analysis-0c193a488234","json_url":"https:\/\/skillshunter.net\/api\/public\/v1\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-xgboost-analysis-0c193a488234","markdown_url":"https:\/\/skillshunter.net\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-xgboost-analysis-0c193a488234.md","download_url":"https:\/\/skillshunter.net\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-xgboost-analysis-0c193a488234\/download","category":{"slug":"data-research","name":"Data & Research"},"editor_note":"Ensure the input file contains the target column and is in CSV, TXT, or TSV format. Install required R packages such as `xgboost`, `data.table`, and `optparse` if they are missing. Exclude any first unnamed identifier column like `V1` if it contains unique sample IDs. For classification, the target must have exactly 2 classes.","instructions":"---\nname: xgboost-analysis\ndescription: 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.\nlicense: MIT\nauthor: AIPOCH\n---\n> **Source**: [https:\/\/github.com\/aipoch\/medical-research-skills](https:\/\/github.com\/aipoch\/medical-research-skills)\n\n# XGBoost Modeling And Feature Importance Ranking\n\nUse this skill to train an XGBoost model from a tabular dataset and export both feature importance ranking tables and feature importance plots.\n\n## Use This Skill When\n\n- You need a command-line XGBoost workflow in R for tabular data.\n- You need a reproducible train-test split, model training, and evaluation.\n- You need feature importance ranking outputs as both a table and a figure.\n- You need automatic one-hot encoding for categorical predictors.\n- Your data may contain a first unnamed sample ID column such as `V1` that should not enter the model.\n\n## Do Not Use This Skill When\n\n- Your classification target has more than 2 classes.\n- Your input is not tabular CSV, TXT, or TSV data.\n- You need causal interpretation, mechanism claims, or policy, business, or clinical conclusions.\n- You only need narrative interpretation or triage of an existing result rather than model training.\n\n## Primary Command\n\n```bash\nRscript scripts\/main.R \\\n  --data_file <input_file> \\\n  --target_var <target_column> \\\n  --task_type <auto|classification|regression> \\\n  --output_dir <output_dir>\n```\n\n## Prerequisites\n\n- `Rscript` is available in the shell.\n- Required R packages: `optparse`, `data.table`, `Matrix`, `xgboost`.\n- Install missing packages with `Rscript -e 'install.packages(c(\"optparse\", \"data.table\", \"Matrix\", \"xgboost\"), repos=\"https:\/\/cloud.r-project.org\")'`.\n\n## Core Arguments\n\n| Argument | Required | Description |\n|----------|----------|-------------|\n| `--data_file` | Yes | Input CSV, TXT, or TSV file |\n| `--target_var` | Yes | Target column used for modeling |\n| `--task_type` | No | `auto`, `classification`, or `regression`. Default `auto` |\n| `--output_dir` | No | Output directory, default `.\/XGBoost_Results` |\n| `--ignore_vars` | No | Comma-separated columns to exclude from predictors |\n| `--positive_class` | No | Positive class label for binary classification |\n| `--test_size` | No | Test set proportion between 0 and 1, default `0.2` |\n| `--seed` | No | Random seed, default `123` |\n| `--nrounds` | No | Maximum boosting rounds, default `300` |\n| `--max_depth` | No | Tree depth, default `6` |\n| `--eta` | No | Learning rate, default `0.1` |\n| `--subsample` | No | Row sampling ratio, default `0.8` |\n| `--colsample_bytree` | No | Column sampling ratio, default `0.8` |\n| `--min_child_weight` | No | Minimum child weight, default `1` |\n| `--gamma` | No | Minimum split loss reduction, default `0` |\n| `--lambda` | No | L2 regularization, default `1` |\n| `--alpha` | No | L1 regularization, default `0` |\n| `--early_stopping_rounds` | No | Early stopping rounds, default `20` |\n| `--importance_metric` | No | `gain`, `cover`, or `frequency`. Default `gain` |\n| `--top_n` | No | Number of features to plot, default `20` |\n| `--output_format` | No | Table format: `csv` or `txt`, default `csv` |\n| `--output_prefix` | No | Output filename prefix, default `xgboost` |\n\n## Input Requirements\n\n- The input file must contain the target column.\n- Predictor columns can be numeric, integer, logical, character, or factor-like text.\n- Character and factor predictors are one-hot encoded automatically.\n- A first unnamed identifier column such as `V1` is automatically excluded when it contains unique sample IDs.\n- Rows with missing target values are removed before training.\n- For classification, exactly 2 classes are required.\n- For regression, the target column must be numeric.\n- Each class should have at least 2 rows so both training and test sets can be created.\n\nExample input:\n\n```csv\n,fustat,CAMK2N2,GGT6,GPR161,RAB26,RIBC2\nTCGA-C5-A1M5,1,2.248291938,5.274690305,2.825215762,3.121114894,5.35318565\nTCGA-EA-A5O9,0,3.346176843,5.404368414,2.604616977,0.629473197,4.429314674\nTCGA-C5-A3HL,0,3.363100974,5.363314779,4.124799581,4.127228806,4.916596068\n```\n\n## Minimal Workflow\n\n1. Confirm the input file exists and the target column name is correct.\n2. Run `scripts\/main.R` with `--data_file` and `--target_var`.\n3. Check the output directory for `table\/feature_importance_*` and `figure\/feature_importance_*`.\n\nIf you omit `--data_file` or `--target_var`, 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 outputs:\n\n- `table\/<output_prefix>_feature_importance.csv`\n- `table\/<output_prefix>_model_performance.csv`\n- `figure\/<output_prefix>_feature_importance_<importance_metric>.png`\n\nAdditional outputs:\n\n- `session_info.txt`\n\nFeature importance table fields include:\n\n- `Rank`\n- `Feature`\n- `Gain`\n- `Cover`\n- `Frequency`\n- `SelectedMetric`\n- `SelectedValue`\n\n## Feature Importance Metrics\n\n- `gain`: Average contribution to loss reduction. Recommended for most ranking use cases.\n- `cover`: Relative sample coverage contributed by a feature.\n- `frequency`: How often the feature is used in splits.\n\n## Read These Files When Needed\n\n| Need | File |\n|------|------|\n| XGBoost method details and importance interpretation | `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| Bundled test data | `tests\/data\/` |\n\n## Quick Examples\n\nAuto-detected binary classification on `dt_sample1.csv`:\n\n```bash\nRscript scripts\/main.R \\\n  --data_file tests\/data\/dt_sample1.csv \\\n  --target_var fustat \\\n  --task_type auto \\\n  --output_dir tests\/output_binary\n```\n\nBinary classification on `dt_sample2.csv`:\n\n```bash\nRscript scripts\/main.R \\\n  --data_file tests\/data\/dt_sample2.csv \\\n  --target_var fustat \\\n  --task_type classification \\\n  --importance_metric gain \\\n  --output_dir tests\/output_gain\n```\n\nCharacter-label classification on `dt_sample3.txt`:\n\n```bash\nRscript scripts\/main.R \\\n  --data_file tests\/data\/dt_sample3.txt \\\n  --target_var Group \\\n  --task_type classification \\\n  --positive_class high \\\n  --top_n 15 \\\n  --output_dir tests\/output_group\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\/dt_sample1.csv \\\n  --target_var fustat \\\n  --task_type classification \\\n  --output_dir tests\/validation_output\n```\n\nAfter running analysis, verify that these files exist:\n\n- `tests\/validation_output\/table\/xgboost_feature_importance.csv`\n- `tests\/validation_output\/table\/xgboost_model_performance.csv`\n- `tests\/validation_output\/figure\/xgboost_feature_importance_gain.png`\n\n## Common Errors\n\n- `SKILL_FILE_NOT_FOUND`: Input file path is wrong or inaccessible.\n- `SKILL_MISSING_COLUMNS`: The target column is missing.\n- `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.\n- `SKILL_INVALID_PARAMETER`: An argument value is invalid.\n- `SKILL_DEPENDENCY_MISSING`: A required R package such as `xgboost` is unavailable.\n\nIf the issue is not obvious, read `references\/troubleshooting.md`.\n","use_cases":["Train an XGBoost model on medical research data and export feature importance rankings.","Perform binary classification with automatic train-test split and feature importance analysis.","Generate feature importance plots for regression tasks with numeric target columns."],"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\/XGBoost-analysis\/SKILL.md","source_commit":"686e09dfdb45d9fa1a08664f06bb93769b3c9d91","license":"MIT","version":null,"listing_approved_at":"2026-11-24","created_at":"2026-11-23T06:52:19.000000Z","updated_at":"2026-11-24T07:13:04.000000Z","seo_title":"XGBoost Feature Importance Ranking Tool","seo_description":"Train XGBoost models and export feature importance rankings and plots for tabular data.","seo_keywords":["XGBoost feature importance","tabular data modeling","feature ranking analysis","classification model training","regression feature importance","automated train-test split"]}}