calibrated prediction across diverse contexts
Project description
CalPred (Calibrated Prediction Intervals for Polygenic Scores Across Diverse Contexts)
See companion manuscript github repository for analysis scripts used in the manuscript.
Installation
# calpred calls R packages statmod and Rchoice in fitting the model
Rscript -e "install.packages(c('statmod', 'Rchoice'), repos='https://cran.rstudio.com')"
pip install calpred
Quick example
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import calpred
np.random.seed(42)
n = 1000
pgs = np.random.normal(size=n)
age = np.random.normal(loc=40, scale=10, size=n)
sex = np.random.binomial(n=1, p=0.5, size=n)
y_mean = 8 + pgs * 0.5 + age * -0.2 + sex * 0.5
y_sd = np.sqrt(np.exp(2 + age * -0.03 + sex * 1))
y = np.random.normal(loc=y_mean, scale=y_sd)
df = pd.DataFrame({"intercept": 1, "pgs": pgs, "age": age, "sex": sex, "y": y})
# x and z are the columns for fitting the mean and standard deviation
x = z = df[["intercept", "pgs", "age", "sex"]]
model = calpred.fit(y=df["y"], x=x, z=z)
# prediction mean and [low, high] for 90% prediction interval
df["pred_mean"], df["pred_sd"] = calpred.predict(x=x, z=z, model_fit=model)
df["pred_low"] = df["pred_mean"] - df["pred_sd"] * 1.645
df["pred_high"] = df["pred_mean"] + df["pred_sd"] * 1.645
# show prediction intervals at 5% / 95% quantile of prediction mean
fig, ax = plt.subplots(figsize=(4, 4), dpi=150)
ax.scatter(df["pred_mean"], df["y"], s=4)
ax.axline((0, 0), slope=1, ls="--", color="red")
idx1 = df.sort_values("pred_mean").index[int(n * 0.05)]
idx2 = df.sort_values("pred_mean").index[int(n * 0.95)]
for idx in [idx1, idx2]:
ax.errorbar(
x=df.loc[idx, "pred_mean"],
y=df.loc[idx, "pred_mean"],
yerr=df.loc[idx, "pred_sd"] * 1.645,
color="red",
capsize=3,
lw=1,
)
fig.show()
Upload to PyPI (for developers)
python setup.py sdist
twine upload dist/*
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
calpred-0.1.1.tar.gz
(16.8 kB
view details)
File details
Details for the file calpred-0.1.1.tar.gz
.
File metadata
- Download URL: calpred-0.1.1.tar.gz
- Upload date:
- Size: 16.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce5357c615d6d62669055df74b88b3aedbba7c90184e07883fe573b96f36cda1 |
|
MD5 | e614e8fd8c8257fe76b01b582a51c70d |
|
BLAKE2b-256 | f87b1d7b11038ae178c382c547af111cfec326e5f13785e575a0adff773e22a8 |