knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(caret)
library(dplyr)
library(conformr)

Process of formatting raw data for analysis. No need to run this step again. Processed data stored as tables in ./data directory. Steps included without evaluation below only for sake of reproducibility. Full datasets must be downloaded from UCI website to run this code. These raw data files are large and therefore not included in raw format in this repository.

#Preprocess crop data from raw
data_raw_1 <- read.csv(file = "WinnipegDataset.txt", sep = ",", header = T, na.strings = "", strip.white = T)
pop <- data_raw_1 %>% 
    select(f100,f101,f103,label) %>%
    filter(label == 4 | label == 3) %>%
    mutate(label = ifelse(label == 4, 1, 0))
#write.table(pop, "data/crop")

#Preprocess forest data from raw
data_raw_2 <- read.csv(file = "covtype.data", sep = ",", header = F, na.strings = "", strip.white = T)
Filter only classes 1 and 2, switch to binary labels
pop <- data_raw_2 %>% 
  select(V1,V2,V3,V55) %>%
  filter(V55 == 1 | V55 == 2) %>%
  mutate(V55 = ifelse(V55 == 1, 1, 0))
#write.table(pop, "data/forest")

Setup for Experiment A

set.seed(3) #originally experiment 3 of 3

pop <- read.table("../data/forest.txt")

#Data frame to hold results
expA_results_d <- data.frame()
expA_results_e <- data.frame()
expA_results_z <- data.frame()
expA_results_c <- data.frame()

#Perform 100 iterations of experiment
for(i in 1:100){

  #Sample dataset 2/3 for training/calibration and 1/3 for testing
  split_id <- sample(1:nrow(pop), size = ceiling(2/3*nrow(pop)))
  split_A <- pop[split_id,]
  split_B <- pop[!(1:nrow(pop) %in% split_id),]

  #Sample 1/2 of split_A for training, 1/2 for calibration
  train_id <- sample(1:nrow(split_A), size = ceiling(1/2*nrow(split_A)))
  cal_id <- (1:nrow(split_A))[!(1:nrow(split_A) %in% train_id)]

  #Clean up split_A dataset
  split_A <- data.frame(split_A %>% select(V1,V2,V3,V55))
  colnames(split_A) <- c('x1','x2','x3','y')

  #Subset 1000 covariate shifted observations from split_B to form test dataset
  split_B_eligible <- split_B[(split_B$V1 >= 2500 & split_B$V1 <= 2700),]
  test_id <- sample(1:nrow(split_B_eligible), 1000, replace = TRUE)
  split_B_eligible <- data.frame(split_B_eligible %>% select(V1,V2,V3,V55))
  colnames(split_B_eligible) <- c('x1','x2','x3','y')
  test <- split_B_eligible[test_id,]

  #Train random forest model on proper training set
  trained_model <- caret::train(as.matrix(split_A[train_id,1:3]), as.factor(split_A[train_id,4]), method="rf", tuneGrid=expand.grid(.mtry = c(1)), trControl=caret::trainControl(method="none"))

  print(paste("Calibrating round:", i, sep=" "))

  print("standard")
  standard_conformal <- conform.classification(x = split_A[,1:3], y = split_A$y, algorithm = "standard", type = "diff", train_id = train_id, cal_id = cal_id, pretrained = trained_model)
  standard_p <- predict(standard_conformal, test[,1:3])
  standard_analysis <- validate.conform.classification(standard_p$p0, standard_p$p1, test$y, c(0.01,0.025,0.05,0.1,0.25,0.5))

  print("mondrian")
  mondrian_conformal <- conform.classification(x = split_A[,1:3], y = split_A$y, algorithm = "mondrian", type = "diff", train_id = train_id, cal_id = cal_id, pretrained = trained_model)
  mondrian_p <- predict(mondrian_conformal, test[,1:3])
  mondrian_analysis <- validate.conform.classification(mondrian_p$p0, mondrian_p$p1, test$y, c(0.01,0.025,0.05,0.1,0.25,0.5))

  print("glm-weighted")
  weighted_conformal <- conform.classification(x = split_A[,1:3], y = split_A$y, algorithm = "weighted", type = "diff", train_id = train_id, cal_id = cal_id, pretrained = trained_model)
  weighted_p <- predict(weighted_conformal, test[,1:3], weight.method = "glm", trControl=caret::trainControl(method="none"))
  glm_weighted_analysis <- validate.conform.classification(weighted_p$p0, weighted_p$p1, test$y, c(0.01,0.025,0.05,0.1,0.25,0.5))

  print("rf-weighted")
  weighted_conformal <- conform.classification(x = split_A[,1:3], y = split_A$y, algorithm = "weighted", type = "diff", train_id = train_id, cal_id = cal_id, pretrained = trained_model)
  weighted_p <- predict(weighted_conformal, test[,1:3], weight.method = "rf", tuneGrid=expand.grid(.mtry = c(1)), trControl=caret::trainControl(method="none"))
  rf_weighted_analysis <- validate.conform.classification(weighted_p$p0, weighted_p$p1, test$y, c(0.01,0.025,0.05,0.1,0.25,0.5))

  print("knn")
  knn_conformal <- conform.classification(x = split_A[,1:3], y = split_A$y, algorithm = "knn", type = "diff", train_id = train_id, cal_id = cal_id, pretrained = trained_model)
  knn_p <- predict(knn_conformal, test[,1:3])
  knn_analysis <- validate.conform.classification(knn_p$p0, knn_p$p1, test$y, c(0.01,0.025,0.05,0.1,0.25,0.5))

  expA_results_d <- rbind(expA_results_d, c(i, "GCP", 1-standard_analysis$validity))
  expA_results_d <- rbind(expA_results_d, c(i, "MCP", 1-mondrian_analysis$validity))
  expA_results_d <- rbind(expA_results_d, c(i,  "lrWCP", 1-glm_weighted_analysis$validity))
  expA_results_d <- rbind(expA_results_d, c(i,  "rfWCP", 1-rf_weighted_analysis$validity))
  expA_results_d <- rbind(expA_results_d, c(i, "SCP", 1-knn_analysis$validity))

  expA_results_e <- rbind(expA_results_e, c(i, "GCP", standard_analysis$efficiency))
  expA_results_e <- rbind(expA_results_e, c(i, "MCP", mondrian_analysis$efficiency))
  expA_results_e <- rbind(expA_results_e, c(i,  "lrWCP", glm_weighted_analysis$efficiency))
  expA_results_e <- rbind(expA_results_e, c(i,  "rfWCP", rf_weighted_analysis$efficiency))
  expA_results_e <- rbind(expA_results_e, c(i, "SCP", knn_analysis$efficiency))

  expA_results_z <- rbind(expA_results_z, c(i, "GCP", standard_analysis$zero))
  expA_results_z <- rbind(expA_results_z, c(i, "MCP", mondrian_analysis$zero))
  expA_results_z <- rbind(expA_results_z, c(i,  "lrWCP", glm_weighted_analysis$zero))
  expA_results_z <- rbind(expA_results_z, c(i,  "rfWCP", rf_weighted_analysis$zero))
  expA_results_z <- rbind(expA_results_z, c(i, "SCP", knn_analysis$zero))

  expA_results_c <- rbind(expA_results_c, c(i, "GCP", standard_analysis$correct))
  expA_results_c <- rbind(expA_results_c, c(i, "MCP", mondrian_analysis$correct))
  expA_results_c <- rbind(expA_results_c, c(i,  "lrWCP", glm_weighted_analysis$correct))
  expA_results_c <- rbind(expA_results_c, c(i,  "rfWCP", rf_weighted_analysis$correct))
  expA_results_c <- rbind(expA_results_c, c(i, "SCP", knn_analysis$correct))

}

colnames(expA_results_d) <- c("Round", "Method", "0.01","0.025","0.05","0.1","0.25","0.5")

expA_results_d_longer <- expA_results_d %>% 
  tidyr::pivot_longer(cols = 3:8, names_to = "Nominal", values_to = "Empirical", names_transform = list(Nominal = as.factor)) %>%
  mutate(Empirical = as.double(Empirical)) %>%
  mutate(Method = factor(Method, levels = c("GCP", "MCP",  "lrWCP",  "rfWCP", "SCP"))) %>%
  mutate(Empirical = round(Empirical, 5))

colnames(expA_results_e) <- c("Round", "Method", "0.01","0.025","0.05","0.1","0.25","0.5")

expA_results_e_longer <- expA_results_e %>% 
  tidyr::pivot_longer(cols = 3:8, names_to = "Nominal", values_to = "both", names_transform = list(Nominal = as.factor)) %>%
  mutate(both = as.double(both)) %>%
  mutate(Method = factor(Method, levels = c("GCP", "MCP",  "lrWCP",  "rfWCP", "SCP"))) %>%
  group_by(Method, Nominal) %>%
  summarize(mean(both))

colnames(expA_results_z) <- c("Round", "Method", "0.01","0.025","0.05","0.1","0.25","0.5")

expA_results_z_longer <- expA_results_z %>% 
  tidyr::pivot_longer(cols = 3:8, names_to = "Nominal", values_to = "zero", names_transform = list(Nominal = as.factor)) %>%
  mutate(zero = as.double(zero)) %>%
  mutate(Method = factor(Method, levels = c("GCP", "MCP",  "lrWCP",  "rfWCP", "SCP"))) %>%
  group_by(Method, Nominal) %>%
  summarize(mean(zero))

colnames(expA_results_c) <- c("Round", "Method", "0.01","0.025","0.05","0.1","0.25","0.5")

expA_results_c_longer <- expA_results_c %>% 
  tidyr::pivot_longer(cols = 3:8, names_to = "Nominal", values_to = "correct", names_transform = list(Nominal = as.factor)) %>%
  mutate(correct = as.double(correct)) %>%
  mutate(Method = factor(Method, levels = c("GCP", "MCP",  "lrWCP",  "rfWCP", "SCP"))) %>%
  group_by(Method, Nominal) %>%
  summarize(mean(correct))

combo <- cbind(expA_results_e_longer, Zero=expA_results_z_longer$`mean(zero)`, Correct=expA_results_c_longer$`mean(correct)`)
colnames(combo) <- c("Method", "Nominal", "Both", "Neither", "Correct")
combo <- combo %>% mutate(Incorrect = 1000 - (Both + Neither + Correct)) %>%
          tidyr::pivot_longer(cols = 3:6, names_to = "Condition", values_to = "Proportion", names_transform = list(Condition = as.factor))

#write.table(expA_results_d_longer, file = "experiment_a_coverage")
#write.table(combo, file = "experiment_a_efficiency")

Covariate Shift figure for Experiment A

set.seed(3) #originally experiment 3 of 3
pop <- read.table("../data/forest.txt")
i <- 1

#Sample dataset 2/3 for training/calibration and 1/3 for testing
split_id <- sample(1:nrow(pop), size = ceiling(2/3*nrow(pop)))
split_A <- pop[split_id,]
split_B <- pop[!(1:nrow(pop) %in% split_id),]

#Sample 1/2 of split_A for training, 1/2 for calibration
train_id <- sample(1:nrow(split_A), size = ceiling(1/2*nrow(split_A)))
cal_id <- (1:nrow(split_A))[!(1:nrow(split_A) %in% train_id)]

#Clean up split_A dataset
split_A <- data.frame(split_A %>% select(V1,V2,V3,V55))
colnames(split_A) <- c('x1','x2','x3','y')

#Subset 1000 covariate shifted observations from split_B to form test dataset
split_B_eligible <- split_B[(split_B$V1 >= 2500 & split_B$V1 <= 2700),]
test_id <- sample(1:nrow(split_B_eligible), 1000, replace = TRUE)
split_B_eligible <- data.frame(split_B_eligible %>% select(V1,V2,V3,V55))
colnames(split_B_eligible) <- c('x1','x2','x3','y')
test <- split_B_eligible[test_id,]

cal_obs <- cbind(split_A[cal_id,], Set = rep("Calibration", length(cal_id)))
cal_obs <- cal_obs[sample(1:nrow(cal_obs), 1000),]
test_obs <- cbind(test, Set = rep("Test", nrow(test)))
shift <- rbind(cal_obs, test_obs)

covshift_a <- ggplot(shift %>% mutate(y = as.factor(y)), aes(x=x1, y=x2, group=y)) + 
  geom_point(alpha = 0.3, aes(shape=y, color=y)) +
  geom_rug(alpha = 0.3, aes(color=y)) +
  facet_wrap(~Set, ncol = 2, scale = "fixed") +
  theme_labuzzetta() + 
  scale_shape_manual(name = bquote(Y[i]), values = c(17,19)) +
  scale_color_manual(name = bquote(Y[i]), values = c("#005CAB","#E31B23")) +
  xlab(bquote(X[i1])) +
  ylab(bquote(X[i2]))

covshift_a
#ggsave("experiment_a_covshift.pdf", plot = covshift_a, dev = "pdf", width = 5.5, height = 2.75, units = c("in"), dpi = 1200)

print(paste("Calibration Class 0 N:", sum(cal_obs$y==0)))
print(paste("Calibration Class 1 N:", sum(cal_obs$y==1)))
print(paste("Test Class 0 N:", sum(test_obs$y==0)))
print(paste("Test Class 1 N:", sum(test_obs$y==1)))

Coverage figure for Experiment A

expA_results_d_table <- read.table("experiment_a_coverage")

lines <- data.frame(Nominal = factor(c("0.01","0.05","0.1")), value = c(0.01,0.05,0.1))

library(ggplot2)

dummy2 <- data.frame(Nominal = c("0.01", "0.05", "0.1"), Z = c(0.01,0.05,0.1))
expA_results_d_table$Method <- factor(expA_results_d_table$Method, levels = c("GCP", "MCP", "lrWCP", "rfWCP", "SCP"))
expA_coverage <- ggplot(expA_results_d_table %>% filter(Nominal %in% c("0.01", "0.05", "0.1")), aes(x=Empirical, y=Method, fill=Method)) +
  geom_boxplot() + 
  stat_summary(fun=mean, geom="point", shape=23, size=2) +
  facet_wrap(~Nominal, ncol=3, scales = "free_x") +
  theme_labuzzetta() +
  theme(legend.position = "none") + 
  scale_fill_manual(values = c("#005CAB","#E31B23","#a6cee3","#fb9a99","#fdb462")) +
  geom_vline(data = dummy2, aes(xintercept=Z)) +
  xlab("Miscoverage rate")

expA_coverage
#ggsave("experiment_a_coverage.pdf", plot = expA_coverage, dev = "pdf", width = 5.5, height = 2, units = c("in"), dpi = 1200)

Setup for Experiment B

set.seed(1) #Originally experiment 1 of 3

pop <- read.table("../data/crop.txt")

#Calculate mean and sd of f100 given class 1 for covariate shift
f100_1_mean <- mean(pop[pop$label==1,]$f100)
f100_1_sd <- sd(pop[pop$label==1,]$f100)

#Data frame to hold results
expB_results_d <- data.frame()
expB_results_e <- data.frame()
expB_results_z <- data.frame()
expB_results_c <- data.frame()

#Perform 100 iterations of experiment
for(i in 1:100){

  #Sample dataset 1/3 for training/calibration and 1/3 for testing
  split_id <- sample(1:nrow(pop), size = ceiling(2/3*nrow(pop)))
  split_A <- pop[split_id,]
  split_B <- pop[!(1:nrow(pop) %in% split_id),]

  #Sample 0.5 of split_A for training, 0.5 for calibration
  train_id <- sample(1:nrow(split_A), size = ceiling(1/2*nrow(split_A)))
  cal_id <- (1:nrow(split_A))[!(1:nrow(split_A) %in% train_id)]

  #Clean up split_A dataset
  split_A <- data.frame(split_A %>% select(f100,f101,f103,label))
  colnames(split_A) <- c('x1','x2','x3','y')

  #Subset 1000 covariate shifted observations from split_B to form test dataset
  test_id <- sample(1:nrow(split_B), 1000, replace = TRUE, prob = (dnorm(split_B$f100, f100_1_mean, f100_1_sd)))
  split_B <- data.frame(split_B %>% select(f100,f101,f103,label))
  colnames(split_B) <- c('x1','x2','x3','y')
  test <- split_B[test_id,]

  #Train random forest model on proper training set
  trained_model <- caret::train(as.matrix(split_A[train_id,1:3]), as.factor(split_A[train_id,4]), method="rf", tuneGrid=expand.grid(.mtry = c(1)), trControl=caret::trainControl(method="none"))

  print(paste("Calibrating round:", i, sep=" "))

  print("standard")
  standard_conformal <- conform.classification(x = split_A[,1:3], y = split_A$y, algorithm = "standard", type = "diff", train_id = train_id, cal_id = cal_id, pretrained = trained_model)
  standard_p <- predict(standard_conformal, test[,1:3])
  standard_analysis <- validate.conform.classification(standard_p$p0, standard_p$p1, test$y, c(0.01,0.025,0.05,0.1,0.25,0.5))

  print("mondrian")
  mondrian_conformal <- conform.classification(x = split_A[,1:3], y = split_A$y, algorithm = "mondrian", type = "diff", train_id = train_id, cal_id = cal_id, pretrained = trained_model)
  mondrian_p <- predict(mondrian_conformal, test[,1:3])
  mondrian_analysis <- validate.conform.classification(mondrian_p$p0, mondrian_p$p1, test$y, c(0.01,0.025,0.05,0.1,0.25,0.5))

  print("glm-weighted")
  weighted_conformal <- conform.classification(x = split_A[,1:3], y = split_A$y, algorithm = "weighted", type = "diff", train_id = train_id, cal_id = cal_id, pretrained = trained_model)
  weighted_p <- predict(weighted_conformal, test[,1:3], weight.method = "glm", trControl=caret::trainControl(method="none"))
  glm_weighted_analysis <- validate.conform.classification(weighted_p$p0, weighted_p$p1, test$y, c(0.01,0.025,0.05,0.1,0.25,0.5))

  print("rf-weighted")
  weighted_conformal <- conform.classification(x = split_A[,1:3], y = split_A$y, algorithm = "weighted", type = "diff", train_id = train_id, cal_id = cal_id, pretrained = trained_model)
  weighted_p <- predict(weighted_conformal, test[,1:3], weight.method = "rf", tuneGrid=expand.grid(.mtry = c(1)), trControl=caret::trainControl(method="none"))
  rf_weighted_analysis <- validate.conform.classification(weighted_p$p0, weighted_p$p1, test$y, c(0.01,0.025,0.05,0.1,0.25,0.5))

  print("knn")
  knn_conformal <- conform.classification(x = split_A[,1:3], y = split_A$y, algorithm = "knn", type = "diff", train_id = train_id, cal_id = cal_id, pretrained = trained_model)
  knn_p <- predict(knn_conformal, test[,1:3])
  knn_analysis <- validate.conform.classification(knn_p$p0, knn_p$p1, test$y, c(0.01,0.025,0.05,0.1,0.25,0.5))

  expB_results_d <- rbind(expB_results_d, c(i, "GCP", 1-standard_analysis$validity))
  expB_results_d <- rbind(expB_results_d, c(i, "MCP", 1-mondrian_analysis$validity))
  expB_results_d <- rbind(expB_results_d, c(i,  "lrWCP", 1-glm_weighted_analysis$validity))
  expB_results_d <- rbind(expB_results_d, c(i,  "rfWCP", 1-rf_weighted_analysis$validity))
  expB_results_d <- rbind(expB_results_d, c(i, "SCP", 1-knn_analysis$validity))

  expB_results_e <- rbind(expB_results_e, c(i, "GCP", standard_analysis$efficiency))
  expB_results_e <- rbind(expB_results_e, c(i, "MCP", mondrian_analysis$efficiency))
  expB_results_e <- rbind(expB_results_e, c(i,  "lrWCP", glm_weighted_analysis$efficiency))
  expB_results_e <- rbind(expB_results_e, c(i,  "rfWCP", rf_weighted_analysis$efficiency))
  expB_results_e <- rbind(expB_results_e, c(i, "SCP", knn_analysis$efficiency))

  expB_results_z <- rbind(expB_results_z, c(i, "GCP", standard_analysis$zero))
  expB_results_z <- rbind(expB_results_z, c(i, "MCP", mondrian_analysis$zero))
  expB_results_z <- rbind(expB_results_z, c(i,  "lrWCP", glm_weighted_analysis$zero))
  expB_results_z <- rbind(expB_results_z, c(i,  "rfWCP", rf_weighted_analysis$zero))
  expB_results_z <- rbind(expB_results_z, c(i, "SCP", knn_analysis$zero))

  expB_results_c <- rbind(expB_results_c, c(i, "GCP", standard_analysis$correct))
  expB_results_c <- rbind(expB_results_c, c(i, "MCP", mondrian_analysis$correct))
  expB_results_c <- rbind(expB_results_c, c(i,  "lrWCP", glm_weighted_analysis$correct))
  expB_results_c <- rbind(expB_results_c, c(i,  "rfWCP", rf_weighted_analysis$correct))
  expB_results_c <- rbind(expB_results_c, c(i, "SCP", knn_analysis$correct))

}

colnames(expB_results_d) <- c("Round", "Method", "0.01","0.025","0.05","0.1","0.25","0.5")

expB_results_d_longer <- expB_results_d %>% 
  tidyr::pivot_longer(cols = 3:8, names_to = "Nominal", values_to = "Empirical", names_transform = list(Nominal = as.factor)) %>%
  mutate(Empirical = as.double(Empirical)) %>%
  mutate(Method = factor(Method, levels = c("GCP", "MCP",  "lrWCP",  "rfWCP", "SCP"))) %>%
  mutate(Empirical = round(Empirical, 5))

colnames(expB_results_e) <- c("Round", "Method", "0.01","0.025","0.05","0.1","0.25","0.5")

expB_results_e_longer <- expB_results_e %>% 
  tidyr::pivot_longer(cols = 3:8, names_to = "Nominal", values_to = "both", names_transform = list(Nominal = as.factor)) %>%
  mutate(both = as.double(both)) %>%
  mutate(Method = factor(Method, levels = c("GCP", "MCP", "lrWCP", "rfWCP", "SCP"))) %>%
  group_by(Method, Nominal) %>%
  summarize(mean(both))

colnames(expB_results_z) <- c("Round", "Method", "0.01","0.025","0.05","0.1","0.25","0.5")

expB_results_z_longer <- expB_results_z %>% 
  tidyr::pivot_longer(cols = 3:8, names_to = "Nominal", values_to = "zero", names_transform = list(Nominal = as.factor)) %>%
  mutate(zero = as.double(zero)) %>%
  mutate(Method = factor(Method, levels = c("GCP", "MCP", "lrWCP", "rfWCP", "SCP"))) %>%
  group_by(Method, Nominal) %>%
  summarize(mean(zero))

colnames(expB_results_c) <- c("Round", "Method", "0.01","0.025","0.05","0.1","0.25","0.5")

expB_results_c_longer <- expB_results_c %>% 
  tidyr::pivot_longer(cols = 3:8, names_to = "Nominal", values_to = "correct", names_transform = list(Nominal = as.factor)) %>%
  mutate(correct = as.double(correct)) %>%
  mutate(Method = factor(Method, levels = c("GCP", "MCP",  "lrWCP",  "rfWCP", "SCP"))) %>%
  group_by(Method, Nominal) %>%
  summarize(mean(correct))

combo <- cbind(expB_results_e_longer, Zero=expB_results_z_longer$`mean(zero)`, Correct=expB_results_c_longer$`mean(correct)`)
colnames(combo) <- c("Method", "Nominal", "Both", "Neither", "Correct")
combo <- combo %>% mutate(Incorrect = 1000 - (Both + Neither + Correct)) %>%
          tidyr::pivot_longer(cols = 3:6, names_to = "Condition", values_to = "Proportion", names_transform = list(Condition = as.factor))

#write.table(expB_results_d_longer, file = "experiment_b_coverage")
#write.table(combo, file = "experiment_b_combo")

Covariate Shift figure for Experiment B

set.seed(1) #Originally experiment 1 of 3
pop <- read.table("../data/crop.txt")
i <- 1

#Calculate mean and sd of f100 given class 1 for covariate shift
f100_1_mean <- mean(pop[pop$label==1,]$f100)
f100_1_sd <- sd(pop[pop$label==1,]$f100)

#Sample dataset 1/3 for training/calibration and 1/3 for testing
split_id <- sample(1:nrow(pop), size = ceiling(2/3*nrow(pop)))
split_A <- pop[split_id,]
split_B <- pop[!(1:nrow(pop) %in% split_id),]

#Sample 0.5 of split_A for training, 0.5 for calibration
train_id <- sample(1:nrow(split_A), size = ceiling(1/2*nrow(split_A)))
cal_id <- (1:nrow(split_A))[!(1:nrow(split_A) %in% train_id)]

#Clean up split_A dataset
split_A <- data.frame(split_A %>% select(f100,f101,f103,label))
colnames(split_A) <- c('x1','x2','x3','y')

#Subset 1000 covariate shifted observations from split_B to form test dataset
test_id <- sample(1:nrow(split_B), 1000, replace = TRUE, prob = (dnorm(split_B$f100, f100_1_mean, f100_1_sd)))
split_B <- data.frame(split_B %>% select(f100,f101,f103,label))
colnames(split_B) <- c('x1','x2','x3','y')
test <- split_B[test_id,]

cal_obs <- cbind(split_A[cal_id,], Set = rep("Calibration", length(cal_id)))
cal_obs <- cal_obs[sample(1:nrow(cal_obs), 1000),]
test_obs <- cbind(test, Set = rep("Test", nrow(test)))
shift <- rbind(cal_obs, test_obs)

covshift_b <- ggplot(shift %>% mutate(y = as.factor(y)), aes(x=x1, y=x2, col=y)) + 
  geom_point(alpha = 0.3, aes(shape=y, color=y)) +
  geom_rug(alpha = 0.3, aes(color=y)) +
  facet_wrap(~Set, ncol = 2, scale = "fixed") +
  theme_labuzzetta() + 
  scale_shape_manual(name = bquote(Y[i]), values = c(17,19)) +
  scale_color_manual(name = bquote(Y[i]), values = c("#005CAB","#E31B23")) +
  xlab(bquote(X[i1])) +
  ylab(bquote(X[i2]))

covshift_b
#ggsave("experiment_b_covshift.pdf", plot = covshift_b, dev = "pdf", width = 5.5, height = 2.75, units = c("in"), dpi = 1200)

print(paste("Calibration Class 0 N:", sum(cal_obs$y==0)))
print(paste("Calibration Class 1 N:", sum(cal_obs$y==1)))
print(paste("Test Class 0 N:", sum(test_obs$y==0)))
print(paste("Test Class 1 N:", sum(test_obs$y==1)))

Coverage figure for Experiment B

expB_results_d_table <- read.table("experiment_b_coverage")

lines <- data.frame(Nominal = factor(c("0.01","0.05","0.1")), value = c(0.01,0.05,0.1))

library(ggplot2)

dummy2 <- data.frame(Nominal = c("0.01", "0.05", "0.1"), Z = c(0.01,0.05,0.1))
expB_results_d_table$Method <- factor(expB_results_d_table$Method, levels = c("GCP", "MCP", "lrWCP", "rfWCP", "SCP"))
expB_coverage <- ggplot(expB_results_d_table %>% filter(Nominal %in% c("0.01", "0.05", "0.1")), aes(x=Empirical, y=Method, fill=Method)) +
  geom_boxplot() + 
  stat_summary(fun=mean, geom="point", shape=23, size=2) +
  facet_wrap(~Nominal, ncol=3, scales = "free_x") +
  theme_labuzzetta() +
  theme(legend.position = "none") + 
  scale_fill_manual(values = c("#005CAB","#E31B23","#a6cee3","#fb9a99","#fdb462")) +
  geom_vline(data = dummy2, aes(xintercept=Z)) +
  xlab("Miscoverage rate")

expB_coverage
#ggsave("experiment_b_coverage.pdf", plot = expB_coverage, dev = "pdf", width = 5.5, height = 2, units = c("in"), dpi = 1200)

Combined plot of coverage for Experiments A and B

library(ggpubr)
coverage_both <- ggarrange(expA_coverage + rremove("xlab"), expB_coverage, 
                           labels = c("A", "B"),
                           ncol = 1, nrow = 2, align = "v", heights = c(1,1.05))

coverage_both
#ggsave("experiment_both_coverage.pdf", plot = coverage_both, dev = "pdf", width = 5.5, height = 4, units = c("in"), dpi = 1200)

Combined plot of covariate shift for Experiments A and B

library(ggpubr)
covshift_both <- ggarrange(covshift_a + rremove("xlab"), covshift_b, 
                           labels = c("A", "B"),
                           ncol = 1, nrow = 2, align = "v", heights = c(1,1.1))

covshift_both
#ggsave("experiment_both_covshift.pdf", plot = covshift_both, dev = "pdf", width = 5.5, height = 4, units = c("in"), dpi = 1200)

Plot of efficiency for Experiments A and B

expA_combo_table <- read.table("experiment_a_efficiency")
efficiency_A <- cbind(Experiment = rep("Experiment A", nrow(expA_combo_table)), expA_combo_table)
expB_combo_table <- read.table("experiment_b_efficiency")
efficiency_B <- cbind(Experiment = rep("Experiment B", nrow(expB_combo_table)), expB_combo_table)
efficiency_all <- rbind(efficiency_A, efficiency_B)

efficiency_plot <- efficiency_all %>% filter(Nominal %in% c("0.1"))

efficiency_plot$Method <- factor(efficiency_plot$Method, levels = c("SCP", "rfWCP", "lrWCP", "MCP", "GCP"))

effplot <- ggplot(efficiency_plot, aes(fill=Condition, y=Proportion, x=Method)) +
  geom_bar(position="fill", stat="identity") +
  facet_wrap(~Experiment, ncol=2) +
  theme_labuzzetta() +
  scale_fill_manual(values = c("#005CAB","#fdb462","#E31B23","#404040"),
                    labels = c(expression(paste(widehat(Y), "={0,1}")), 
                               expression(paste(widehat(Y), " = Y")), 
                               expression(widehat(Y) != Y),
                               expression(paste(widehat(Y), " = ", symbol("\306"))))) +
  theme(legend.text.align = 0) + 
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1))

effplot
#ggsave("efficiency_plot.pdf", plot = effplot, dev = "pdf", width = 4, height = 2, units = c("in"), dpi = 1200)


labuzzetta/conformr documentation built on May 13, 2022, 12:50 a.m.