# NOT RUN {
library(piRF)
#functions to get average length and average coverage of output
getPILength <- function(x){
#average PI length across each set of predictions
l <- x[,2] - x[,1]
avg_l <- mean(l)
return(avg_l)
}
getCoverage <- function(x, response){
#output coverage for test data
coverage <- sum((response >= x[,1]) * (response <= x[,2]))/length(response)
return(coverage)
}
#import airfoil self noise dataset
data(airfoil)
method_vec <- c("quantile", "Zhang", "Tung", "Romano", "Roy", "HDI", "Ghosal")
#generate train and test data
ratio <- .975
nrow <- nrow(airfoil)
n <- floor(nrow*ratio)
samp <- sample(1:nrow, size = n)
train <- airfoil[samp,]
test <- airfoil[-samp,]
#generate prediction intervals
res <- rfint(pressure ~ . , train_data = train, test_data = test,
method = method_vec,
concise= FALSE,
num_threads = 1)
#empirical coverage, and average prediction interval length for each method
coverage <- sapply(res$int, FUN = getCoverage, response = test$pressure)
coverage
length <- sapply(res$int, FUN = getPILength)
length
#get current mfrow setting
opar <- par(mfrow = c(2,2))
#plotting intervals and predictions
for(i in 1:7){
col <- ((test$pressure >= res$int[[i]][,1]) *
(test$pressure <= res$int[[i]][,2])-1)*(-1)+1
plot(x = res$preds[[i]], y = test$pressure, pch = 20,
col = "black", ylab = "true", xlab = "predicted", main = method_vec[i])
abline(a = 0, b = 1)
segments(x0 = res$int[[i]][,1], x1 = res$int[[i]][,2],
y1 = test$pressure, y0 = test$pressure, lwd = 1, col = col)
}
par(opar)
# }
Run the code above in your browser using DataLab