Skip to main content

A package for misclassification error correction in regression using validation data

Project description

validation_correction

A Python package for measurement error correction in regression analysis using validation data.

Installation

pip install validation_correction

Usage

The package provides a simple interface for correcting measurement error in both linear and logistic regression using validation data. The correction is implemented using a bootstrap procedure that:

  1. Resamples the validation data to estimate misclassification probabilities
  2. Applies these probabilities to a bootstrap sample of the research data
  3. Repeats this process to obtain valid confidence intervals

Linear Regression with Mismeasured Predictor

import pandas as pd
from validation_correction import validation_correction

# Load your data
research_data = pd.read_csv("research_data.csv")
validation_data = pd.read_csv("validation_data.csv")

# Run corrected regression with bootstrap
# Format: y ~ w || x + z
# where x is the true variable and w is its mismeasured version
result = validation_correction.ols(
    formula="y ~ w || x + z",
    data=research_data,
    val_data=validation_data,
    bootstrap=True,  # Bootstrap is required for correction
    n_boots=1000    # Number of bootstrap iterations
)

# Run naive regression (no correction)
naive_result = validation_correction.ols(
    formula="y ~ w + z",
    data=research_data,
    val_data=None
)

# Print results with bootstrap confidence intervals
print(result)

# Plot coefficient comparison
validation_correction.plot_coefficients(naive_result, result)

# Plot bootstrap distributions
validation_correction.plot_bootstrap_distributions()

Logistic Regression with Mismeasured Predictor

# Format: u||y ~ x + z
# where y is the true variable and u is its mismeasured version
result = validation_correction.logit(
    formula="u||y ~ x + z",
    data=research_data,
    val_data=validation_data,
    bootstrap=True
)

print(result)

Formula Specification

The package uses a special formula syntax to specify the relationship between true and mismeasured variables:

  1. For mismeasured predictors:

    • Format: y ~ w || x + z
    • Where x is the true variable and w is its mismeasured version
    • Additional covariates (z) are measured without error
  2. For mismeasured outcomes (not yet implemented):

    • Format: u || y ~ x + z
    • Where y is the true outcome and u is its mismeasured version
    • Predictors go on the right side of the ~
  3. For naive regression (no correction):

    • Standard formula format: y ~ w + z
    • No || operator needed
    • Set val_data=None

Visualization

The package provides two types of visualizations:

  1. Coefficient Comparison Plot:

    validation_correction.plot_coefficients(naive_result, corrected_result)
    
    • Shows point estimates and confidence intervals for both naive and corrected models
    • Useful for comparing the magnitude and direction of bias
  2. Bootstrap Distribution Plot:

    validation_correction.plot_bootstrap_distributions()
    
    • Shows the distribution of coefficient estimates from bootstrap samples
    • Includes 95% confidence interval markers
    • Must run regression with bootstrap=True first

Bootstrap confidence intervals:

  • Uses percentile method (2.5th and 97.5th percentiles)
  • Accessible via result['[0.025]'] and result['[0.975]']
  • Number of bootstrap iterations controlled by n_boots parameter
  • Bootstrap is required for measurement error correction

Data Requirements

  • Main dataset (data): Must contain all variables in the formula
  • Validation dataset (val_data): Must contain both the true and mismeasured versions of the relevant variable
  • Both datasets should be pandas DataFrames

References

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

validation_correction-0.1.0.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

validation_correction-0.1.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file validation_correction-0.1.0.tar.gz.

File metadata

  • Download URL: validation_correction-0.1.0.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.12

File hashes

Hashes for validation_correction-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b3002a4391ce1bbdc76b992c82cc1b78e89efc08124e28136c73397beb0e0583
MD5 4ca14b5d15648919e839b7fabf2dc301
BLAKE2b-256 b254ecb664a934dae60bd8f5f3dbb77d673b2739be8567483c37f46987106834

See more details on using hashes here.

File details

Details for the file validation_correction-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for validation_correction-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 38a06b1904448bdf9e3b152fb85ce605089ec19e4150c11b04ced75c288bdacf
MD5 d431fdad5c51fb1e752c10d37d7c51f8
BLAKE2b-256 4c4154d87dd36c5ede45e7019d55ad845460f0175b61d2da23bd0ec53ed03f7c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page