Author: Jiacheng Miao jiacheng.miao@wisc.edu
Date: 2024-02-19

# Test POP-Inf in IPD R package

#### Jiacheng Miao

#### 2024-02-19

## Mean Estimation

    rm(list = ls())
    #- Generate Example Data
    set.seed(42)
    dat <- IPD::simdat()

    form <- Y - Yhat ~ X1

    Y_l <- dat[dat$set == "labeled", all.vars(form)[1]] |> matrix(ncol = 1)

    f_l <- dat[dat$set == "labeled", all.vars(form)[2]] |> matrix(ncol = 1)

    f_u <- dat[dat$set == "unlabeled", all.vars(form)[2]] |> matrix(ncol = 1)

    IPD::popinf_mean(Y_l, f_l, f_u)

    ## $est
    ## [1] 12.4554
    ##
    ## $se
    ## [1] 0.1654749

## Linear regression

    rm(list = ls())
    #- Generate Example Data
    set.seed(42)
    dat <- IPD::simdat()

    form <- Y - Yhat ~ X1

    X_l <- model.matrix(form, data = dat[dat$set == "labeled",])

    Y_l <- dat[dat$set == "labeled", all.vars(form)[1]] |> matrix(ncol = 1)

    f_l <- dat[dat$set == "labeled", all.vars(form)[2]] |> matrix(ncol = 1)

    X_u <- model.matrix(form, data = dat[dat$set == "unlabeled",])

    f_u <- dat[dat$set == "unlabeled", all.vars(form)[2]] |> matrix(ncol = 1)

    IPD::popinf_ols(X_l, Y_l, f_l, X_u, f_u)

    ## $est
    ## [1] 11.6936575  0.8064982
    ##
    ## $se
    ## [1] 0.2190285 0.1519738

## Logistic regression

    rm(list = ls())
    #- Generate Example Data
    set.seed(42)
    dat <- IPD::simdat()

    form <- Y - Yhat ~ X1

    X_l <- model.matrix(form, data = dat[dat$set == "labeled",])

    Y_l <- dat[dat$set == "labeled", all.vars(form)[1]] |> matrix(ncol = 1)
    Y_l <- ifelse(Y_l > quantile(Y_l, 0.75), 1, 0)

    f_l <- dat[dat$set == "labeled", all.vars(form)[2]] |> matrix(ncol = 1)
    f_l <- ifelse(f_l > quantile(f_l, 0.75), 1, 0)

    X_u <- model.matrix(form, data = dat[dat$set == "unlabeled",])

    f_u <- dat[dat$set == "unlabeled", all.vars(form)[2]] |> matrix(ncol = 1)
    f_u <- ifelse(f_u > quantile(f_u, 0.75), 1, 0)

    IPD::popinf_logistic(X_l, Y_l, f_l, X_u, f_u)

    ## $est
    ## [1] -1.7615489  0.5975317
    ##
    ## $se
    ## [1] 0.2187251 0.1412411

## Poisson regression

    rm(list = ls())
    #- Generate Example Data
    set.seed(42)
    dat <- IPD::simdat()

    form <- Y - Yhat ~ X1

    X_l <- model.matrix(form, data = dat[dat$set == "labeled",])

    Y_l <- dat[dat$set == "labeled", all.vars(form)[1]] |> matrix(ncol = 1)
    Y_l <- round(Y_l - min(Y_l))

    f_l <- dat[dat$set == "labeled", all.vars(form)[2]] |> matrix(ncol = 1)
    f_l <- round(f_l - min(f_l))

    X_u <- model.matrix(form, data = dat[dat$set == "unlabeled",])

    f_u <- dat[dat$set == "unlabeled", all.vars(form)[2]] |> matrix(ncol = 1)
    f_u <- round(f_u - min(f_u))

    IPD::popinf_poisson(X_l, Y_l, f_l, X_u, f_u)

    ## $est
    ## [1] 2.14598868 0.09371846
    ##
    ## $se
    ## [1] 0.02586625 0.01665662

## Quantile Estimation

    rm(list = ls())
    #- Generate Example Data
    set.seed(42)
    dat <- IPD::simdat()

    form <- Y - Yhat ~ X1

    Y_l <- dat[dat$set == "labeled", all.vars(form)[1]] |> matrix(ncol = 1)

    f_l <- dat[dat$set == "labeled", all.vars(form)[2]] |> matrix(ncol = 1)

    f_u <- dat[dat$set == "unlabeled", all.vars(form)[2]] |> matrix(ncol = 1)

    IPD::popinf_quantile(Y_l, f_l, f_u, q = 0.5)

    ## $est
    ## [1] 12.37386
    ##
    ## $se
    ## [1] 0.2353643