Knn Imputation logo

Knn Imputation

OrganizationPopular
aipoch
knn-imputation

Use when filtering genes with high missingness and then imputing missing values in a bulk expression matrix with group-aware KNN through DMwR2, where donor samples are restricted by one annotation column before imputation. For strata with 10 or fewer samples, the script falls back to row-wise direct filling with mean or median. NOT for: single-cell data, multi-column stratification, non-tabular inputs, network access, or interactive workflows.

Overview

Publisheraipoch
Repositorymedical-research-skills
Skill nameknn-imputation
Stars
1.9K
Forks
175
Bundled files
13
LicenseMIT
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 13 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by aipoch on GitHub. Read the source before you install it.

Installation

Install the Knn Imputation AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.

Use it in TypingMind

Enable Knn Imputation in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Knn Imputation on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Knn Imputation is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

KNN Imputation

When to Use

Use this skill when you need to remove genes with more than 50% missing values from a bulk expression matrix and then run group-aware KNN imputation, with the donor pool restricted by one grouping column.

Do not use this skill for:

  • single-cell data
  • multi-column stratification
  • non-tabular inputs
  • network-dependent workflows
  • interactive analysis sessions

When to Read External Files

SituationFile to ReadPurpose
Need algorithm detailsreferences/algorithm.mdGroup-stratified KNN method, fallback rules, and assumptions
Need to run analysisscripts/main.RExecute: Rscript scripts/main.R --input_file ... --group_file ...
Encounter errorsreferences/troubleshooting.mdCommon errors and solutions
Need CLI examplesreferences/cli-guide.mdDetailed CLI usage examples
Need sample input fixturestests/data/Repository fixtures for local validation and examples

Input Validation

This skill accepts: a bulk expression matrix CSV (features × samples) and a sample annotation CSV file with a single grouping column for KNN stratification.

If the user's request does not involve imputing missing values in a bulk expression matrix — for example, asking to impute single-cell data, use multi-column stratification, or run network-dependent workflows — do not proceed with the workflow. Instead respond:

"knn-imputation is designed to filter and impute missing values in bulk expression matrices using group-aware KNN with DMwR2. Your request appears to be outside this scope. Please provide a bulk expression matrix with a single grouping column, or use a more appropriate tool for your task."

Prerequisites

DMwR2 is not available on CRAN. Install it from GitHub before running:

r
install.packages("remotes")
remotes::install_github("cran/DMwR2")

If SKILL_DEPENDENCY_MISSING is raised, use the command above to install DMwR2 before retrying. Standard install.packages("DMwR2") will not work.


Usage

bash
Rscript scripts/main.R \
  --input_file tests/data/sample_expression_matrix.csv \
  --group_file tests/data/sample_groups.csv \
  --output_dir tests/output/basic_run \
  --sample_column sample \
  --group_column group \
  --k 10 \
  --small_strata_fill_method mean \
  --overwrite \
  --timeout_seconds 0 \
  --seed 42

If re-running into an existing output_dir, pass --overwrite. Otherwise use a fresh output directory.


Arguments

ShortLongTypeDefaultDescription
-i--input_filecharacterrequiredExpression matrix CSV file with features in rows and samples in columns
-g--group_filecharacterrequiredSample annotation CSV file
-o--output_dircharacter./output/Output directory
-c--sample_columncharactersampleSample ID column in the group file
-l--group_columncharactergroupSingle grouping column used to define imputation strata
-k--kinteger10Number of nearest neighbors used inside each stratum
-m--small_strata_fill_methodcharactermeanFill method for strata with 10 or fewer samples: mean or median
--overwriteflagFALSEOverwrite existing output files in output_dir
-t--timeout_secondsinteger0Optional elapsed timeout in seconds, 0 disables timeout
-s--seedinteger42Random seed for reproducibility

Input Format

Expression Matrix (input_file)

Features as rows, samples as columns, CSV format with feature ID in the first column.

csv
,Sample01,Sample02,Sample03
TSPAN6,1.84,1.83,3.82
SEMA3F,4.83,4.04,5.28

Requirements:

  • The first column stores feature IDs.
  • All remaining columns must be numeric or empty.
  • Missing values must be encoded as empty cells or NA.

Group File (group_file)

CSV with one sample ID column and one grouping column.

csv
sample,group
Sample01,case
Sample02,control
Sample03,case

Requirements:

  • sample_column must match the expression matrix sample names exactly.
  • The column named in group_column must exist in the group file.
  • sample_column and the selected grouping column must be non-missing.
  • KNN only runs for strata with at least 11 samples.
  • Strata with 10 or fewer samples use row-wise direct filling with --small_strata_fill_method.

Output Files

FileFormatDescription
imputed_expression_matrix.csvCSVComplete imputed expression matrix
session_info.txtTXTR session and package version information

Workflow

Step 1: Validate Input

  • Check file existence.
  • Validate sample matching between expression matrix and group file.
  • Verify that the requested grouping column exists.

Step 2: Filter Genes

  • Remove genes whose missing-value fraction across all samples is at least 50%.
  • Stop if all genes are removed by this filter.

Step 3: Build Strata

  • Construct one stratum per unique value in group_column.
  • Keep strata even when they are small; only strata with at least 11 samples run KNN.

Step 4: Run Imputation

  • Run group-stratified KNN imputation only within strata that contain at least 11 samples.
  • Within each stratum, skip imputation for any gene whose missing-value fraction in that stratum is at least 50%; leave those values as NA.
  • For strata with 10 or fewer samples, fill missing values by the row-wise mean or median within that stratum.
  • If a small stratum has an all-missing gene row that is still imputable, fall back to the global row-wise mean or median.

Step 5: Save Results

  • Write the imputed matrix.
  • Save session_info.txt to the output directory.

Methods

Missingness Filter + Group-Stratified DMwR2 KNN

Genes with at least 50% missing values are removed first. KNN imputation is then applied within user-defined strata built from one grouping column when the stratum contains at least 11 samples.

If the chosen grouping scheme splits the data into strata of 10 samples or fewer, the command falls back to row-wise direct filling by mean or median inside that stratum. Genes that reach at least 50% missingness within a stratum are skipped in that stratum and remain NA. If another small-stratum row is fully missing but still below that threshold, the script falls back to the corresponding global row summary.

For implementation details, assumptions, and skip behavior for small strata, read references/algorithm.md.


Examples

Basic Usage

bash
Rscript scripts/main.R \
  -i tests/data/sample_expression_matrix.csv \
  -g tests/data/sample_groups.csv \
  -o tests/output/basic_run

Smaller Neighborhood

bash
Rscript scripts/main.R \
  -i tests/data/sample_expression_matrix.csv \
  -g tests/data/sample_groups.csv \
  -o tests/output/k5_run \
  -k 5

Small Strata Fallback

bash
Rscript scripts/main.R \
  -i tests/data/sample_expression_matrix.csv \
  -g tests/data/sample_groups.csv \
  -o tests/output/small_strata_run \
  -l sample \
  -m median \
  --overwrite

Error Handling

Common Errors

ErrorCauseSolution
SKILL_FILE_NOT_FOUNDInput file does not existCheck the file path
SKILL_EMPTY_FILEInput file exists but is emptyReplace it with a valid non-empty CSV file
SKILL_OUTPUT_EXISTSOutput files already existRe-run with --overwrite or change --output_dir
SKILL_SAMPLE_MISMATCHSample names do not match between filesVerify exact sample name matching
SKILL_MISSING_COLUMNSRequested grouping column is absentAdd that column to the group file or change --group_column
SKILL_INVALID_PARAMETERMultiple grouping columns were suppliedPass exactly one grouping column in --group_column
SKILL_INVALID_DATAMatrix or group file structure is invalidCheck input format, duplicated IDs, and group completeness
SKILL_DEPENDENCY_MISSINGDMwR2 not installedInstall with: Rscript -e "install.packages('remotes'); remotes::install_github('cran/DMwR2')" — note: DMwR2 is not on CRAN
SKILL_TIMEOUTTimeout limit was exceededIncrease --timeout_seconds or reduce data size

IF error persists, READ: references/troubleshooting.md


Local Validation

Validate the CLI Entrypoint

bash
# Check help
Rscript scripts/main.R --help

# Run with sample data
Rscript scripts/main.R \
  -i tests/data/sample_expression_matrix.csv \
  -g tests/data/sample_groups.csv \
  -o tests/output/basic_run \
  --overwrite

# Run forced small-strata fallback
Rscript scripts/main.R \
  -i tests/data/sample_expression_matrix.csv \
  -g tests/data/sample_groups.csv \
  -o tests/output/small_strata_run \
  -l sample \
  -m median \
  --overwrite

Output Checks

bash
# Count lines in output
wc -l tests/output/basic_run/imputed_expression_matrix.csv

# Check output files exist
ls -la tests/output/basic_run

Reference Files

FilePurpose
references/algorithm.mdGroup-stratified KNN method, fallback rules, and assumptions
references/troubleshooting.mdCommon errors and solutions
references/cli-guide.mdCLI usage examples

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the Knn Imputation AI skill do?

Use when filtering genes with high missingness and then imputing missing values in a bulk expression matrix with group-aware KNN through DMwR2, where donor samples are restricted by one annotation column before imputation. For strata with 10 or fewer samples, the script falls back to row-wise direct filling with mean or median. NOT for: single-cell data, multi-column stratification, non-tabular inputs, network access, or interactive workflows.

Why use Knn Imputation on TypingMind?

Because you install it once and use it with any model. Knn Imputation is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Knn Imputation in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/aipoch/medical-research-skills/tree/main/awesome-med-research-skills/Data%20Analysis/knn-imputation. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use Knn Imputation?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Knn Imputation?

As many as you like. As long as a model supports skills, you can use Knn Imputation with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Knn Imputation AI skill free?

Yes. It is published on GitHub by aipoch under the MIT license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇