mixtureSPRT

knitr::opts_chunk$set(echo = TRUE)

mixtureSPRT is a package for performing mixture Sequential Probability Ratio tests. It includes functions for calculating mixing variance and test statistic, as well as methods for plotting and printing. It also contains an option carry out the calculations in C++ as it reduced runtime substantially. This is particularly useful when many tests are performed to see for example sampling distributions or compare the mSPRT to other tests.

library(mixtureSPRT)
library(tidyverse)

Installation

devtools::install_github("shitoushan/mixtureSPRT")

Usage

    set.seed(1337)
     n <- 10000
     m <- mSPRT(x = rnorm(n),
           y = rnorm(n, mean = 0.05),
           sigma = 1,
           tau =  calcTau(alpha = 0.05, sigma = 1, truncation = n),
           theta = 0,
           distribution = "normal",
           alpha = 0.05)

       plot(m)

C++

library(mixtureSPRT)
library(microbenchmark)

y <- rnorm(100)
x <- rnorm(100)
sigma = 1
tau = calcTau(0.05,1,100)
theta = 0
distribution="normal"
alpha=0.05

microbenchmark(
  m <- mSPRT(x,y,sigma=sigma,tau=tau,
                     useCpp = F),
  mcpp <-  mSPRT(x,y,sigma=sigma,tau=tau,
                         useCpp = T)
)

In case pre-experiment data is available, those can be included too as control variates in order to reduce the variance in the variable tested.

library(MASS)
set.seed(1337)
rho=0.6 # Correlation between pre-experiment data and post-treatment data
sigma = 1
Sigma <- matrix(c(sigma^2,rho*sqrt(sigma^2*sigma^2),rho*sqrt(sigma^2*sigma^2),sigma^2),2,2) # covar.matrix to make sure correlation = rho
n <- 1000 # Truncation point

x <- mvrnorm(n = n, c(0,0.1), Sigma, empirical = T) %>% as.data.frame() # Treatment group
y <- mvrnorm(n = n, c(0,0), Sigma, empirical = T) %>% as.data.frame() # Control group
cor(x[,1],x[,2])
cor(y[,1],y[,2])


m1 <- mSPRT(x = x[,2],
      y = y[,2],
      xpre = x[,1],
      ypre = y[,1],
      sigma = sigma,
      tau = calcTau(alpha = 0.05, truncation = n, sigma = 1),
      theta = 0,
      distribution = "normal",
      alpha = 0.05,
      useCpp = T)

m2 <-  mSPRT(x = x[,2],
      y = y[,2],
      sigma = sigma,
      tau = calcTau(alpha = 0.05, truncation = n, sigma = 1),
      theta = 0,
      distribution = "normal",
      alpha = 0.05,
      useCpp = T)

gridExtra::grid.arrange(plot(m1),plot(m2))


shitoushan/mixtureSPRT documentation built on Sept. 29, 2021, 7:46 a.m.