Giter Club home page Giter Club logo

psps's Introduction

PSPS

Repository for R and Python packages PSPS that implements Task-Agnostic Machine Learning-Assisted Inference.

PSPS is a simple and task-agnotsic protocol for valid and efficient machine learning (ML)-assited infernece. It can be easily adapted to a variety of statistical tasks.

R Package

Install R Package PSPS

# install.packages("devtools")
devtools::install_github("qlu-lab/PSPS", subdir = "psps-r")

TL;DR

fit_PSPS <- PSPS(est_lab_y, est_lab_yhat, est_unlab_yhat, Sigma)

Suppose we are interested in estimating a K-dimensional parameter, then

  • est_lab_y: a K-dimensional vector of Point estimates using Y in labeled data.
  • est_lab_yhat: a K-dimensional vector of Point estimates using Yhat in labeled data.
  • est_unlab_yhat: a K-dimensional vector of Point estimates using Yhat in unlabeled data.
  • Sigma: a 3K x 3K Variance-covariance matrix for the above three estimators (Note: not the asymptotic variance).

Example

Here is an example of PSPS for logistic regression. For other tasks, simply replace logistic regression with other algorithms to produce summary statistics.

# Load the package
library(PSPS)

# Load the Labeled and unlabelled data
lab <- read.csv("./PSPS/test_data/lab.csv")
unlab <- read.csv("./PSPS/test_data/unlab.csv")

Logistic regression

# Point Estimates Preparation

## Fit logistic regression models and extract the second coefficient (typically the slope)
est_lab_y <- coef(glm(Y ~ X, data = lab, family = binomial("logit")))[2]
est_lab_yhat <- coef(glm(Yhat ~ X, data = lab, family = binomial("logit")))[2]
est_unlab_yhat <- coef(glm(Yhat ~ X, data = unlab, family = binomial("logit")))[2]

# Variance-Covariance Matrix Preparation

## Bootstrap for Covariance Calculation between est_lab_y and est_lab_yhat
B <- 1000  # Number of bootstrap iterations
n <- nrow(lab)  # Total observations in labeled data

### Initialize matrices to store bootstrap estimates
est_lab_y_boot <- vector("numeric", B)
est_lab_yhat_boot <- vector("numeric", B)

### Perform bootstrap to estimate the variance-covariance of est_lab_y and est_lab_yhat
set.seed(123)  # Set seed for reproducibility
for (i in 1:B) {
  boot_indices <- sample(1:n, n, replace = TRUE)
  boot_lab <- lab[boot_indices, ]
  est_lab_y_boot[i] <- coef(glm(Y ~ X, data = boot_lab, family = binomial("logit")))[2]
  est_lab_yhat_boot[i] <- coef(glm(Yhat ~ X, data = boot_lab, family = binomial("logit")))[2]
}

### Compute the covariance matrix
Sigma <- matrix(0, nrow = 3, ncol = 3)
Sigma[1:2, 1:2] <- cov(cbind(est_lab_y_boot, est_lab_yhat_boot))  # Covariance of bootstrap estimates
Sigma[3, 3] <- summary(glm(Yhat ~ X, data = unlab, family = binomial("logit")))$coefficients[2, "Std. Error"]^2

## One-step Debiasing with the PSPS Method
fit_PSPS <- PSPS(est_lab_y, est_lab_yhat, est_unlab_yhat, Sigma)
print(fit_PSPS)

### Example output from PSPS function:
# Estimate  Std.Error  Lower.CI  Upper.CI  P.value
# 0.7537844 0.09366147 0.5702113 0.9373575 8.418062e-16

Python package

Install Python Package PSPS

pip install psps_py

TL;DR

PSPS inputs summary statistics from three separate analyses and returns the ML-assisted estimator.

fit_PSPS = PSPS(est_lab_y, est_lab_yhat, est_unlab_yhat, Sigma)

Suppose we are interested in estimating a K-dimensional parameter, then

  • est_lab_y: a K-dimensional vector of Point estimates using Y in labeled data.
  • est_lab_yhat: a K-dimensional vector of Point estimates using Yhat in labeled data.
  • est_unlab_yhat: a K-dimensional vector of Point estimates using Yhat in unlabeled data.
  • Sigma: a 3K x 3K Variance-covariance matrix for the above three estimators (Note: not the asymptotic variance).

Example

Here is an example of PSPS for logistic regression. For other tasks, simply replace logistic regression with other algorithms to produce summary statistics.

import pandas as pd
import numpy as np
import statsmodels.api as sm
from statsmodels.formula.api import glm
# Load the package
from psps_py import PSPS

# Load data
lab = pd.read_csv("./PSPS/test_data/lab.csv")
unlab = pd.read_csv("./PSPS/test_data/unlab.csv")

Logistic regression

# Fit logistic regression models and extract the second coefficient
est_lab_y = glm('Y ~ X', data=lab, family=sm.families.Binomial()).fit().params['X']
est_lab_yhat = glm('Yhat ~ X', data=lab, family=sm.families.Binomial()).fit().params['X']
est_unlab_yhat = glm('Yhat ~ X', data=unlab, family=sm.families.Binomial()).fit().params['X']

# Bootstrap for covariance calculation
B = 1000  # Number of bootstrap iterations
n = len(lab)  # Total observations in labeled data
np.random.seed(123)  # For reproducibility

est_lab_y_boot = np.zeros(B)
est_lab_yhat_boot = np.zeros(B)

for i in range(B):
    boot_indices = np.random.choice(lab.index, size=n, replace=True)
    boot_lab = lab.loc[boot_indices]
    est_lab_y_boot[i] = glm('Y ~ X', data=boot_lab, family=sm.families.Binomial()).fit().params['X']
    est_lab_yhat_boot[i] = glm('Yhat ~ X', data=boot_lab, family=sm.families.Binomial()).fit().params['X']

# Compute the covariance matrix
Sigma = np.zeros((3, 3))
Sigma[0:2, 0:2] = np.cov(np.vstack([est_lab_y_boot, est_lab_yhat_boot]))
Sigma[2, 2] = glm('Yhat ~ X', data=unlab, family=sm.families.Binomial()).fit().bse['X']**2

# Run PSPS
fit_PSPS = PSPS(est_lab_y, est_lab_yhat, est_unlab_yhat, Sigma)
print(fit_PSPS)

### Example output from PSPS function:
# Estimate  Std.Error  Lower.CI  Upper.CI  P.value
# 0.755438   0.090828  0.577417  0.933458  9.007197e-17

Analysis script

We provide the script for analysis in the PSPS paper here.

Contact

Please submit an issue or contact Jiacheng ([email protected]) or Qiongshi ([email protected]) for questions.

Reference

Task-Agnostic Machine Learning-Assisted Inference

Assumption-Lean and Data-Adaptive Post-Prediction Inference

Valid Inference for Machine Learning-Assisted GWAS

Familial links

  • POP-TOOLS (POst-Prediction TOOLS) is a toolkit for conducting valid and powerful machine learning (ML)-assisted genetic association studies. It currently implements POP-GWAS, where statistical and computational methods are optimized for GWAS applications.

psps's People

Contributors

jmiao24 avatar qlu-lab avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.