library(misc)

Example: Conformal Prediction with Out-of-Sample Coverage

In this example, we demonstrate how to use the conformalize function to perform conformal prediction and calculate the out-of-sample coverage rate.

Simulated Data

We will generate a simple dataset for demonstration purposes.

set.seed(123)
n <- 200
x <- matrix(runif(n * 2), ncol = 2)
y <- 3 * x[, 1] + 2 * x[, 2] + rnorm(n, sd = 0.5)
data <- data.frame(x1 = x[, 1], x2 = x[, 2], y = y)

Fit Conformal Model

We will use a linear model (lm) as the fit_func and its corresponding predict function as the predict_func.

library(stats)

# Define fit and predict functions
fit_func <- function(formula, data, ...) lm(formula, data = data, ...)
predict_func <- function(fit, newdata, ...) predict(fit, newdata = newdata, ...)

# Apply conformalize
conformal_model <- misc::conformalize(
  formula = y ~ x1 + x2,
  data = data,
  fit_func = fit_func,
  predict_func = predict_func,
  split_ratio = 0.8,
  seed = 123
)

Generate Predictions and Prediction Intervals

We will use the predict.conformalize method to generate predictions and calculate prediction intervals.

# New data for prediction
new_data <- data.frame(x1 = runif(50), x2 = runif(50))

# Predict with split conformal method
predictions <- predict(
  conformal_model,
  newdata = new_data,
  level = 0.95,
  method = "split"
)
## 
## [1] "object's value:"
## $fit
## 
## Call:
## lm(formula = formula, data = data)
## 
## Coefficients:
## (Intercept)           x1           x2  
##   -0.000171     3.115230     1.954085  
## 
## 
## $residuals
##          10          15          18          19          28          33 
## -0.07155337 -0.50575404 -0.34743168 -0.61805759 -0.40117678  0.05274032 
##          45          47          49          58          59          61 
## -0.09260136 -0.05885147 -0.31844333  0.24731261 -0.13813234 -0.29452465 
##          65          66          73          75          82          95 
##  1.07655235  0.74875010  0.26380004  0.40559198  0.29773432  0.99912730 
##         102         104         106         107         113         114 
## -0.41409168 -0.60257935  0.08524682 -1.10511279 -0.83744059 -0.40724958 
##         115         119         120         133         136         142 
## -0.24259646  0.79117708  0.44859913 -0.40430463 -1.07935716  0.43797248 
##         146         148         150         151         152         160 
## -0.88608587 -0.27676132 -0.14552706  0.41625998 -1.00911160  1.29913204 
##         161         174         175         199 
## -0.15375511  0.34773043  0.20273890 -0.64231123 
## 
## $sd_residuals
## [1] 0.5868316
## 
## $scaled_residuals
## [1] -0.1249228
## 
## attr(,"class")
## [1] "conformalize"
head(predictions)
##         fit        lwr      upr
## 1 1.6023773  0.5217324 2.683022
## 2 2.4634938  1.3828489 3.544139
## 3 0.6216433 -0.4590017 1.702288
## 4 0.9257140 -0.1549310 2.006359
## 5 2.0106565  0.9300115 3.091301
## 6 0.7427247 -0.3379203 1.823370

Calculate Out-of-Sample Coverage Rate

The coverage rate is the proportion of true values that fall within the prediction intervals.

# Simulate true values for the new data
true_y <- 3 * new_data$x1 + 2 * new_data$x2 + rnorm(50, sd = 0.5)

# Check if true values fall within the prediction intervals
coverage <- mean(true_y >= predictions[, "lwr"] & true_y <= predictions[, "upr"])

cat("Out-of-sample coverage rate:", coverage)
## Out-of-sample coverage rate: 0.98

Results

  • The prediction intervals are calculated using the split conformal method.
  • The out-of-sample coverage rate is displayed, which should be close to the specified confidence level (e.g., 0.95).

Example: Conformal Prediction with the MASS::Boston Dataset

In this example, we use the MASS::Boston dataset to demonstrate conformal prediction.

Load the Data

We will use the MASS package to access the Boston dataset.

library(MASS)

# Load the Boston dataset
data(Boston)

# Inspect the dataset
head(Boston)
##      crim zn indus chas   nox    rm  age    dis rad tax ptratio  black lstat
## 1 0.00632 18  2.31    0 0.538 6.575 65.2 4.0900   1 296    15.3 396.90  4.98
## 2 0.02731  0  7.07    0 0.469 6.421 78.9 4.9671   2 242    17.8 396.90  9.14
## 3 0.02729  0  7.07    0 0.469 7.185 61.1 4.9671   2 242    17.8 392.83  4.03
## 4 0.03237  0  2.18    0 0.458 6.998 45.8 6.0622   3 222    18.7 394.63  2.94
## 5 0.06905  0  2.18    0 0.458 7.147 54.2 6.0622   3 222    18.7 396.90  5.33
## 6 0.02985  0  2.18    0 0.458 6.430 58.7 6.0622   3 222    18.7 394.12  5.21
##   medv
## 1 24.0
## 2 21.6
## 3 34.7
## 4 33.4
## 5 36.2
## 6 28.7

Split the Data

We will split the data into training and test sets to ensure they are disjoint.

set.seed(123)
n <- nrow(Boston)
train_indices <- sample(seq_len(n), size = floor(0.8 * n))
train_data <- Boston[train_indices, ]
test_data <- Boston[-train_indices, ]

Fit Conformal Model 1

# Define fit and predict functions
fit_func <- function(formula, data, ...) MASS::rlm(formula, data = data, ...)
predict_func <- function(fit, newdata, ...) predict(fit, newdata, ...)

# Apply conformalize using the training data
conformal_model_boston <- misc::conformalize(
  formula = medv ~ .,
  data = train_data,
  fit_func = fit_func,
  predict_func = predict_func,
  seed = 123
)

Generate Predictions and Prediction Intervals 1

We will use the predict.conformalize method to generate predictions and calculate prediction intervals for the test set.

# Predict with split conformal method on the test data
predictions_boston <- predict(
  conformal_model_boston,
  newdata = test_data,
  level = 0.95,
  method = "split"
)
## 
## [1] "object's value:"
## $fit
## Call:
## rlm(formula = formula, data = data)
## Converged in 12 iterations
## 
## Coefficients:
##  (Intercept)         crim           zn        indus         chas          nox 
## 10.053413833 -0.163015431  0.009957524 -0.013355275  1.557351564 -5.289064676 
##           rm          age          dis          rad          tax      ptratio 
##  6.050082402 -0.014130776 -1.046959248  0.204156664 -0.010752932 -0.815953461 
##        black        lstat 
##  0.014196664 -0.436463429 
## 
## Degrees of freedom: 202 total; 188 residual
## Scale estimate: 2.78 
## 
## $residuals
##          415          463          179          426          118          503 
##  17.75101224  -0.42301639  -1.61859953   1.62139385  -3.34677969  -1.60897767 
##           90          256          197          491          348          355 
##  -3.19085491   1.54228923  -1.78273122   5.81481859  -0.79919511   5.50240802 
##            7          501          485          254          211           43 
##   0.53762811  -3.44980333   2.53375234   8.03603880   1.44113352   0.08310029 
##          373          332          425          330           23          411 
##  28.09729049  -1.37881939   2.08155331  -2.10520186  -1.24294958   7.86360572 
##          309          135          290           72           76           63 
##  -5.85410610   3.32023507  -0.94636403   0.80984792  -1.85752392  -1.35877320 
##          210          493          294           41          492          316 
##   6.10840592   4.26900808  -1.23403353   2.06751492  -0.11235268  -3.23281956 
##           16           94          342           39          240            4 
##   0.81505531  -1.98229092   1.20380307   2.23545905  -4.28334192   3.17274197 
##           13          409          308           89           25          291 
##   1.08193007   6.47568699  -3.13637115  -7.61851115  -0.14593182  -2.55986770 
##          286          396          110          158          398           67 
##  -3.95756249  -6.92122367  -0.08380848   8.39453871  -6.20029505  -2.16451929 
##          335           85          136          178          236           98 
##  -2.13636104  -0.97950834  -0.01953636  -3.55277580  -0.09793921   0.42318320 
##          214          127          212          273          310          232 
##   2.77058176   2.35196874   5.06669808  -3.18238722  -2.29399575  -2.62510928 
##          366          416          350          407          280          154 
##  20.43126404  -0.62828007   2.65823308  10.23724986   0.74559441   1.59669803 
##          102          255          326          272          470          288 
##   0.19552077   0.18581742  -1.05991028  -1.16045291   4.23230707  -1.66642094 
##          440           55          331          478          184          459 
##   0.71422248   5.50936970  -2.00530538   4.02428952   2.53299654  -1.93859807 
##          196          432          352           20          502          177 
##   9.48644109  -2.62067731   3.24264261   0.51984714  -2.24793764  -1.58613996 
##           42          405          380          395          194          249 
##  -2.75990583   4.56727197  -5.27523660  -3.43589886   0.10886146   3.11941696 
##          200          377          250          292          434           33 
##   6.02359042  -3.63739240   1.09081050   4.60846568  -1.70144111   4.07698311 
##          152           54          430          289          185          205 
##   1.29737169   0.24062338  -1.99405324  -2.80371431   6.00421913   7.74226900 
##          334          215          318           57          105          279 
##  -1.14753812  12.85549644   1.83703448   1.44486879  -0.60194612   0.46171405 
##          129          106          480          369          406          287 
##  -1.75885183   2.26221951   1.79691136  31.89913520   1.75716435   1.61365538 
##          356          225          117          452          400          341 
##   5.24072632   3.87927876  -1.67191387  -4.92493185  -3.94064347  -2.50204595 
##          176          220          191           53          104          447 
##  -1.26482459  -4.90616974   6.42986225  -2.64155010  -0.47874865  -2.81083340 
##          320          354          275          261            2          336 
##  -0.53303073   5.21706683  -1.12106092  -2.06641580  -3.33379070  -0.19269560 
##          497          394          448          327          419          112 
##   6.50546425  -5.49102250  -5.42896409  -1.29979669   9.33706323  -4.11746097 
##           36           87          399          111          461          351 
##  -3.60153400   0.90077494   1.54781531   0.97506146  -3.11921704   1.75920907 
##           31           73          424          333          122           92 
##   1.35292531  -0.96867871   3.25415805  -2.67640505  -1.09730455  -4.87346776 
##          234          338          472          300          323          314 
##   8.19783868  -0.82543739  -1.15016922  -2.15925065  -2.00661608  -3.39475797 
##          443          387          376          208          499          206 
##  -0.32010786   8.62390298 -10.57350717   5.50712569   0.08881949   0.54973502 
##          297          219           82          169          413          228 
##  -0.11392277  -1.13639371  -2.23070320  -1.15670018  22.68901628  -1.11958292 
##          423          258          385          482          498          192 
##   6.06871179   2.90343078  10.38887008  -2.11698974  -0.34891269   0.99064382 
##          386          414           50          260           97          183 
##   1.34099245  10.71923362   3.13590006  -4.57405529  -2.25277923   3.81266117 
##          100          274          484          296          357          227 
##  -0.46119518  -0.96786208   2.97493526  -0.20959741  -0.95380842  -1.96415748 
##          190          329          156          131            8          468 
##   1.00965741  -1.42530635  -4.17827374  -1.58936371   7.51853676   4.12047455 
##           70          213          382          182 
##   1.16853787   1.78909033  -7.07189205   9.67559663 
## 
## $sd_residuals
## [1] 5.384367
## 
## $scaled_residuals
## [1] 0.1888384
## 
## attr(,"class")
## [1] "conformalize"
head(predictions_boston)
##         fit       lwr      upr
## 1  29.92942 20.263283 39.59556
## 15 19.30837  9.642229 28.97451
## 17 20.71124 11.045100 30.37738
## 19 14.86650  5.200365 24.53264
## 28 14.79883  5.132688 24.46497
## 37 20.98752 11.321382 30.65366

Calculate Out-of-Sample Coverage Rate 1

The coverage rate is the proportion of true values in the test set that fall within the prediction intervals.

# True values for the test set
true_y_boston <- test_data$medv

# Check if true values fall within the prediction intervals
coverage_boston <- mean(true_y_boston >= predictions_boston[, "lwr"] & true_y_boston <= predictions_boston[, "upr"])

cat("Out-of-sample coverage rate for Boston dataset:", coverage_boston)
## Out-of-sample coverage rate for Boston dataset: 0.9509804

Fit Conformal Model 2

# Define fit and predict functions
fit_func <- function(formula, data, ...) stats::glm(formula, data = data, ...)
predict_func <- function(fit, newdata, ...) predict(fit, newdata, ...)

# Apply conformalize using the training data
conformal_model_boston <- misc::conformalize(
  formula = medv ~ .,
  data = train_data,
  fit_func = fit_func,
  predict_func = predict_func,
  seed = 123
)

Generate Predictions and Prediction Intervals 2

We will use the predict.conformalize method to generate predictions and calculate prediction intervals for the test set.

# Predict with split conformal method on the test data
predictions_boston <- predict(
  conformal_model_boston,
  newdata = test_data,
  level = 0.95,
  method = "split"
)
## 
## [1] "object's value:"
## $fit
## 
## Call:  stats::glm(formula = formula, data = data)
## 
## Coefficients:
## (Intercept)         crim           zn        indus         chas          nox  
##    29.78825     -0.14238      0.03251      0.03061      2.17196    -16.17824  
##          rm          age          dis          rad          tax      ptratio  
##     4.70344      0.01813     -1.50717      0.35519     -0.01344     -1.04058  
##       black        lstat  
##     0.01303     -0.58559  
## 
## Degrees of Freedom: 201 Total (i.e. Null);  188 Residual
## Null Deviance:       18580 
## Residual Deviance: 3991  AIC: 1206
## 
## $residuals
##           415           463           179           426           118 
##  15.764477831  -0.988770013  -2.543179984   0.246263589  -4.612173369 
##           503            90           256           197           491 
##  -1.693827822  -2.950069022   1.869965859  -2.430610387   6.641430762 
##           348           355             7           501           485 
##  -0.387896963   7.048338684   0.194748907  -3.493814316   1.693920234 
##           254           211            43           373           332 
##  12.292956642   0.138159179   1.154004128  24.027816881  -0.803632969 
##           425           330            23           411           309 
##  -0.839920969  -0.722612132  -0.167767308   2.167940990  -6.825169566 
##           135           290            72            76            63 
##   3.275100495  -0.759414026   1.430389678  -2.020087653  -1.674552399 
##           210           493           294            41           492 
##   4.586854531   4.552309079  -1.126062979   1.696969057   0.262178313 
##           316            16            94           342            39 
##  -3.970341530   1.333549618  -3.241784129   2.197383166   2.775323621 
##           240             4            13           409           308 
##  -4.694843972   4.306893821   1.896411213   4.373885971  -4.894902111 
##            89            25           291           286           396 
##  -8.248709022   0.398861978  -3.821840049  -3.993434219  -8.052811215 
##           110           158           398            67           335 
##  -0.174707897   6.170637038  -7.912137723  -3.779100281  -0.420307060 
##            85           136           178           236            98 
##  -0.505685158   0.722000550  -5.082576649  -1.112554638   1.000305006 
##           214           127           212           273           310 
##   3.417047314   2.068581362   4.029083985  -3.973489628  -3.257778331 
##           232           366           416           350           407 
##  -3.217537805  14.504200597  -0.789956450   5.421465622   5.957527419 
##           280           154           102           255           326 
##  -0.388687016   1.987991575   0.466096118   0.007181811   0.558481278 
##           272           470           288           440            55 
##  -0.918384377   2.276754357  -2.553856315   0.008825628   6.349577246 
##           331           478           184           459           196 
##  -0.804163641   1.663888937   0.439328371  -2.359964178   8.911889804 
##           432           352            20           502           177 
##  -4.146198759   5.045979801   0.563337346  -1.280802168  -1.992182379 
##            42           405           380           395           194 
##  -0.902292374   3.327617862  -6.667225863  -5.400633140   0.231787928 
##           249           200           377           250           292 
##   3.977422436   6.478740182  -4.023381192   2.932225794   3.772826861 
##           434            33           152            54           430 
##  -2.448673653   6.358540758   0.946984605   0.628531680  -2.546717499 
##           289           185           205           334           215 
##  -3.941265332   4.102284871   6.607098719   0.430018524  15.728088344 
##           318            57           105           279           129 
##   2.099510664   1.644595746  -1.270661910  -0.426073914  -1.271359683 
##           106           480           369           406           287 
##   1.426383844  -0.792647956  25.844187265  -0.968727677   2.656329159 
##           356           225           117           452           400 
##   6.852693030   3.881995799  -2.152249451  -5.166074745  -3.503250830 
##           341           176           220           191            53 
##  -2.262305509  -1.417549048  -7.035616819   6.863606354  -2.022958762 
##           104           447           320           354           275 
##  -0.802709122  -3.305050943   0.093391012   5.989158592  -2.851763168 
##           261             2           336           497           394 
##  -2.597612328  -3.713695585   1.263629192   6.975450133  -7.050402445 
##           448           327           419           112            36 
##  -6.187067749  -0.138726762   6.104096769  -4.466775330  -4.616862136 
##            87           399           111           461           351 
##   1.284500325   0.300778763   1.758188903  -3.221342364   3.619381597 
##            31            73           424           333           122 
##   2.428740626  -0.527694477   1.845675138  -2.221711574  -2.189798218 
##            92           234           338           472           300 
##  -5.666921491   8.775318305  -0.162437018  -4.000684029  -1.784296138 
##           323           314           443           387           376 
##  -1.977525789  -4.395997397  -1.201281672   6.307724361 -11.738018736 
##           208           499           206           297           219 
##   5.537880093   0.246994887   1.102222072  -0.460269305  -2.987158004 
##            82           169           413           228           423 
##  -3.021002286  -3.205200388  19.853308439  -2.192024195   2.797529204 
##           258           385           482           498           192 
##   3.414178564   8.271338091  -4.451362820  -0.266787057   0.792207439 
##           386           414            50           260            97 
##   0.442623562   6.487624676   3.388454435  -6.629745005  -2.978426708 
##           183           100           274           484           296 
##   2.427214778  -0.031584384  -0.570256827   1.238172323   0.095873701 
##           357           227           190           329           156 
##  -1.933201226  -2.531726289   0.382566988  -0.545690834  -3.581922258 
##           131             8           468            70           213 
##  -1.346444916   7.621427858   2.329583294   1.528233254   1.223808670 
##           382           182 
##  -7.999559935   8.543358664 
## 
## $sd_residuals
## [1] 5.091575
## 
## $scaled_residuals
## [1] 0.1125522
## 
## attr(,"class")
## [1] "conformalize"
head(predictions_boston)
##         fit       lwr      upr
## 1  30.39434 21.864586 38.92410
## 15 19.10867 10.578908 27.63842
## 17 19.48015 10.950390 28.00990
## 19 14.18587  5.656113 22.71563
## 28 13.85324  5.323482 22.38300
## 37 21.66451 13.134753 30.19427
# Predict with split conformal method on the test data
predictions_boston2 <- predict(
  conformal_model_boston,
  newdata = test_data,
  level = 0.95,
  method = "kde"
)
## 
## [1] "object's value:"
## $fit
## 
## Call:  stats::glm(formula = formula, data = data)
## 
## Coefficients:
## (Intercept)         crim           zn        indus         chas          nox  
##    29.78825     -0.14238      0.03251      0.03061      2.17196    -16.17824  
##          rm          age          dis          rad          tax      ptratio  
##     4.70344      0.01813     -1.50717      0.35519     -0.01344     -1.04058  
##       black        lstat  
##     0.01303     -0.58559  
## 
## Degrees of Freedom: 201 Total (i.e. Null);  188 Residual
## Null Deviance:       18580 
## Residual Deviance: 3991  AIC: 1206
## 
## $residuals
##           415           463           179           426           118 
##  15.764477831  -0.988770013  -2.543179984   0.246263589  -4.612173369 
##           503            90           256           197           491 
##  -1.693827822  -2.950069022   1.869965859  -2.430610387   6.641430762 
##           348           355             7           501           485 
##  -0.387896963   7.048338684   0.194748907  -3.493814316   1.693920234 
##           254           211            43           373           332 
##  12.292956642   0.138159179   1.154004128  24.027816881  -0.803632969 
##           425           330            23           411           309 
##  -0.839920969  -0.722612132  -0.167767308   2.167940990  -6.825169566 
##           135           290            72            76            63 
##   3.275100495  -0.759414026   1.430389678  -2.020087653  -1.674552399 
##           210           493           294            41           492 
##   4.586854531   4.552309079  -1.126062979   1.696969057   0.262178313 
##           316            16            94           342            39 
##  -3.970341530   1.333549618  -3.241784129   2.197383166   2.775323621 
##           240             4            13           409           308 
##  -4.694843972   4.306893821   1.896411213   4.373885971  -4.894902111 
##            89            25           291           286           396 
##  -8.248709022   0.398861978  -3.821840049  -3.993434219  -8.052811215 
##           110           158           398            67           335 
##  -0.174707897   6.170637038  -7.912137723  -3.779100281  -0.420307060 
##            85           136           178           236            98 
##  -0.505685158   0.722000550  -5.082576649  -1.112554638   1.000305006 
##           214           127           212           273           310 
##   3.417047314   2.068581362   4.029083985  -3.973489628  -3.257778331 
##           232           366           416           350           407 
##  -3.217537805  14.504200597  -0.789956450   5.421465622   5.957527419 
##           280           154           102           255           326 
##  -0.388687016   1.987991575   0.466096118   0.007181811   0.558481278 
##           272           470           288           440            55 
##  -0.918384377   2.276754357  -2.553856315   0.008825628   6.349577246 
##           331           478           184           459           196 
##  -0.804163641   1.663888937   0.439328371  -2.359964178   8.911889804 
##           432           352            20           502           177 
##  -4.146198759   5.045979801   0.563337346  -1.280802168  -1.992182379 
##            42           405           380           395           194 
##  -0.902292374   3.327617862  -6.667225863  -5.400633140   0.231787928 
##           249           200           377           250           292 
##   3.977422436   6.478740182  -4.023381192   2.932225794   3.772826861 
##           434            33           152            54           430 
##  -2.448673653   6.358540758   0.946984605   0.628531680  -2.546717499 
##           289           185           205           334           215 
##  -3.941265332   4.102284871   6.607098719   0.430018524  15.728088344 
##           318            57           105           279           129 
##   2.099510664   1.644595746  -1.270661910  -0.426073914  -1.271359683 
##           106           480           369           406           287 
##   1.426383844  -0.792647956  25.844187265  -0.968727677   2.656329159 
##           356           225           117           452           400 
##   6.852693030   3.881995799  -2.152249451  -5.166074745  -3.503250830 
##           341           176           220           191            53 
##  -2.262305509  -1.417549048  -7.035616819   6.863606354  -2.022958762 
##           104           447           320           354           275 
##  -0.802709122  -3.305050943   0.093391012   5.989158592  -2.851763168 
##           261             2           336           497           394 
##  -2.597612328  -3.713695585   1.263629192   6.975450133  -7.050402445 
##           448           327           419           112            36 
##  -6.187067749  -0.138726762   6.104096769  -4.466775330  -4.616862136 
##            87           399           111           461           351 
##   1.284500325   0.300778763   1.758188903  -3.221342364   3.619381597 
##            31            73           424           333           122 
##   2.428740626  -0.527694477   1.845675138  -2.221711574  -2.189798218 
##            92           234           338           472           300 
##  -5.666921491   8.775318305  -0.162437018  -4.000684029  -1.784296138 
##           323           314           443           387           376 
##  -1.977525789  -4.395997397  -1.201281672   6.307724361 -11.738018736 
##           208           499           206           297           219 
##   5.537880093   0.246994887   1.102222072  -0.460269305  -2.987158004 
##            82           169           413           228           423 
##  -3.021002286  -3.205200388  19.853308439  -2.192024195   2.797529204 
##           258           385           482           498           192 
##   3.414178564   8.271338091  -4.451362820  -0.266787057   0.792207439 
##           386           414            50           260            97 
##   0.442623562   6.487624676   3.388454435  -6.629745005  -2.978426708 
##           183           100           274           484           296 
##   2.427214778  -0.031584384  -0.570256827   1.238172323   0.095873701 
##           357           227           190           329           156 
##  -1.933201226  -2.531726289   0.382566988  -0.545690834  -3.581922258 
##           131             8           468            70           213 
##  -1.346444916   7.621427858   2.329583294   1.528233254   1.223808670 
##           382           182 
##  -7.999559935   8.543358664 
## 
## $sd_residuals
## [1] 5.091575
## 
## $scaled_residuals
## [1] 0.1125522
## 
## attr(,"class")
## [1] "conformalize"
head(predictions_boston2)
##         fit      lwr      upr
## 1  30.39434 22.37589 49.43768
## 15 19.10867 11.96002 28.22319
## 17 19.48015 11.91617 34.06139
## 19 14.18587  6.77628 23.56133
## 28 13.85324  6.61761 27.71907
## 37 21.66451 13.49167 37.83532
# Predict with split conformal method on the test data
predictions_boston3 <- predict(
  conformal_model_boston,
  newdata = test_data,
  level = 0.95,
  method = "surrogate"
)
## 
## [1] "object's value:"
## $fit
## 
## Call:  stats::glm(formula = formula, data = data)
## 
## Coefficients:
## (Intercept)         crim           zn        indus         chas          nox  
##    29.78825     -0.14238      0.03251      0.03061      2.17196    -16.17824  
##          rm          age          dis          rad          tax      ptratio  
##     4.70344      0.01813     -1.50717      0.35519     -0.01344     -1.04058  
##       black        lstat  
##     0.01303     -0.58559  
## 
## Degrees of Freedom: 201 Total (i.e. Null);  188 Residual
## Null Deviance:       18580 
## Residual Deviance: 3991  AIC: 1206
## 
## $residuals
##           415           463           179           426           118 
##  15.764477831  -0.988770013  -2.543179984   0.246263589  -4.612173369 
##           503            90           256           197           491 
##  -1.693827822  -2.950069022   1.869965859  -2.430610387   6.641430762 
##           348           355             7           501           485 
##  -0.387896963   7.048338684   0.194748907  -3.493814316   1.693920234 
##           254           211            43           373           332 
##  12.292956642   0.138159179   1.154004128  24.027816881  -0.803632969 
##           425           330            23           411           309 
##  -0.839920969  -0.722612132  -0.167767308   2.167940990  -6.825169566 
##           135           290            72            76            63 
##   3.275100495  -0.759414026   1.430389678  -2.020087653  -1.674552399 
##           210           493           294            41           492 
##   4.586854531   4.552309079  -1.126062979   1.696969057   0.262178313 
##           316            16            94           342            39 
##  -3.970341530   1.333549618  -3.241784129   2.197383166   2.775323621 
##           240             4            13           409           308 
##  -4.694843972   4.306893821   1.896411213   4.373885971  -4.894902111 
##            89            25           291           286           396 
##  -8.248709022   0.398861978  -3.821840049  -3.993434219  -8.052811215 
##           110           158           398            67           335 
##  -0.174707897   6.170637038  -7.912137723  -3.779100281  -0.420307060 
##            85           136           178           236            98 
##  -0.505685158   0.722000550  -5.082576649  -1.112554638   1.000305006 
##           214           127           212           273           310 
##   3.417047314   2.068581362   4.029083985  -3.973489628  -3.257778331 
##           232           366           416           350           407 
##  -3.217537805  14.504200597  -0.789956450   5.421465622   5.957527419 
##           280           154           102           255           326 
##  -0.388687016   1.987991575   0.466096118   0.007181811   0.558481278 
##           272           470           288           440            55 
##  -0.918384377   2.276754357  -2.553856315   0.008825628   6.349577246 
##           331           478           184           459           196 
##  -0.804163641   1.663888937   0.439328371  -2.359964178   8.911889804 
##           432           352            20           502           177 
##  -4.146198759   5.045979801   0.563337346  -1.280802168  -1.992182379 
##            42           405           380           395           194 
##  -0.902292374   3.327617862  -6.667225863  -5.400633140   0.231787928 
##           249           200           377           250           292 
##   3.977422436   6.478740182  -4.023381192   2.932225794   3.772826861 
##           434            33           152            54           430 
##  -2.448673653   6.358540758   0.946984605   0.628531680  -2.546717499 
##           289           185           205           334           215 
##  -3.941265332   4.102284871   6.607098719   0.430018524  15.728088344 
##           318            57           105           279           129 
##   2.099510664   1.644595746  -1.270661910  -0.426073914  -1.271359683 
##           106           480           369           406           287 
##   1.426383844  -0.792647956  25.844187265  -0.968727677   2.656329159 
##           356           225           117           452           400 
##   6.852693030   3.881995799  -2.152249451  -5.166074745  -3.503250830 
##           341           176           220           191            53 
##  -2.262305509  -1.417549048  -7.035616819   6.863606354  -2.022958762 
##           104           447           320           354           275 
##  -0.802709122  -3.305050943   0.093391012   5.989158592  -2.851763168 
##           261             2           336           497           394 
##  -2.597612328  -3.713695585   1.263629192   6.975450133  -7.050402445 
##           448           327           419           112            36 
##  -6.187067749  -0.138726762   6.104096769  -4.466775330  -4.616862136 
##            87           399           111           461           351 
##   1.284500325   0.300778763   1.758188903  -3.221342364   3.619381597 
##            31            73           424           333           122 
##   2.428740626  -0.527694477   1.845675138  -2.221711574  -2.189798218 
##            92           234           338           472           300 
##  -5.666921491   8.775318305  -0.162437018  -4.000684029  -1.784296138 
##           323           314           443           387           376 
##  -1.977525789  -4.395997397  -1.201281672   6.307724361 -11.738018736 
##           208           499           206           297           219 
##   5.537880093   0.246994887   1.102222072  -0.460269305  -2.987158004 
##            82           169           413           228           423 
##  -3.021002286  -3.205200388  19.853308439  -2.192024195   2.797529204 
##           258           385           482           498           192 
##   3.414178564   8.271338091  -4.451362820  -0.266787057   0.792207439 
##           386           414            50           260            97 
##   0.442623562   6.487624676   3.388454435  -6.629745005  -2.978426708 
##           183           100           274           484           296 
##   2.427214778  -0.031584384  -0.570256827   1.238172323   0.095873701 
##           357           227           190           329           156 
##  -1.933201226  -2.531726289   0.382566988  -0.545690834  -3.581922258 
##           131             8           468            70           213 
##  -1.346444916   7.621427858   2.329583294   1.528233254   1.223808670 
##           382           182 
##  -7.999559935   8.543358664 
## 
## $sd_residuals
## [1] 5.091575
## 
## $scaled_residuals
## [1] 0.1125522
## 
## attr(,"class")
## [1] "conformalize"
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
head(predictions_boston3)
##         fit       lwr      upr
## 1  30.39434 22.482206 39.30623
## 15 19.10867 12.073048 31.40162
## 17 19.48015 12.698948 34.93286
## 19 14.18587  7.396239 29.63858
## 28 13.85324  6.134993 22.68221
## 37 21.66451 14.614108 33.95747
# Predict with split conformal method on the test data
predictions_boston4 <- predict(
  conformal_model_boston,
  newdata = test_data,
  level = 0.95,
  method = "bootstrap"
)
## 
## [1] "object's value:"
## $fit
## 
## Call:  stats::glm(formula = formula, data = data)
## 
## Coefficients:
## (Intercept)         crim           zn        indus         chas          nox  
##    29.78825     -0.14238      0.03251      0.03061      2.17196    -16.17824  
##          rm          age          dis          rad          tax      ptratio  
##     4.70344      0.01813     -1.50717      0.35519     -0.01344     -1.04058  
##       black        lstat  
##     0.01303     -0.58559  
## 
## Degrees of Freedom: 201 Total (i.e. Null);  188 Residual
## Null Deviance:       18580 
## Residual Deviance: 3991  AIC: 1206
## 
## $residuals
##           415           463           179           426           118 
##  15.764477831  -0.988770013  -2.543179984   0.246263589  -4.612173369 
##           503            90           256           197           491 
##  -1.693827822  -2.950069022   1.869965859  -2.430610387   6.641430762 
##           348           355             7           501           485 
##  -0.387896963   7.048338684   0.194748907  -3.493814316   1.693920234 
##           254           211            43           373           332 
##  12.292956642   0.138159179   1.154004128  24.027816881  -0.803632969 
##           425           330            23           411           309 
##  -0.839920969  -0.722612132  -0.167767308   2.167940990  -6.825169566 
##           135           290            72            76            63 
##   3.275100495  -0.759414026   1.430389678  -2.020087653  -1.674552399 
##           210           493           294            41           492 
##   4.586854531   4.552309079  -1.126062979   1.696969057   0.262178313 
##           316            16            94           342            39 
##  -3.970341530   1.333549618  -3.241784129   2.197383166   2.775323621 
##           240             4            13           409           308 
##  -4.694843972   4.306893821   1.896411213   4.373885971  -4.894902111 
##            89            25           291           286           396 
##  -8.248709022   0.398861978  -3.821840049  -3.993434219  -8.052811215 
##           110           158           398            67           335 
##  -0.174707897   6.170637038  -7.912137723  -3.779100281  -0.420307060 
##            85           136           178           236            98 
##  -0.505685158   0.722000550  -5.082576649  -1.112554638   1.000305006 
##           214           127           212           273           310 
##   3.417047314   2.068581362   4.029083985  -3.973489628  -3.257778331 
##           232           366           416           350           407 
##  -3.217537805  14.504200597  -0.789956450   5.421465622   5.957527419 
##           280           154           102           255           326 
##  -0.388687016   1.987991575   0.466096118   0.007181811   0.558481278 
##           272           470           288           440            55 
##  -0.918384377   2.276754357  -2.553856315   0.008825628   6.349577246 
##           331           478           184           459           196 
##  -0.804163641   1.663888937   0.439328371  -2.359964178   8.911889804 
##           432           352            20           502           177 
##  -4.146198759   5.045979801   0.563337346  -1.280802168  -1.992182379 
##            42           405           380           395           194 
##  -0.902292374   3.327617862  -6.667225863  -5.400633140   0.231787928 
##           249           200           377           250           292 
##   3.977422436   6.478740182  -4.023381192   2.932225794   3.772826861 
##           434            33           152            54           430 
##  -2.448673653   6.358540758   0.946984605   0.628531680  -2.546717499 
##           289           185           205           334           215 
##  -3.941265332   4.102284871   6.607098719   0.430018524  15.728088344 
##           318            57           105           279           129 
##   2.099510664   1.644595746  -1.270661910  -0.426073914  -1.271359683 
##           106           480           369           406           287 
##   1.426383844  -0.792647956  25.844187265  -0.968727677   2.656329159 
##           356           225           117           452           400 
##   6.852693030   3.881995799  -2.152249451  -5.166074745  -3.503250830 
##           341           176           220           191            53 
##  -2.262305509  -1.417549048  -7.035616819   6.863606354  -2.022958762 
##           104           447           320           354           275 
##  -0.802709122  -3.305050943   0.093391012   5.989158592  -2.851763168 
##           261             2           336           497           394 
##  -2.597612328  -3.713695585   1.263629192   6.975450133  -7.050402445 
##           448           327           419           112            36 
##  -6.187067749  -0.138726762   6.104096769  -4.466775330  -4.616862136 
##            87           399           111           461           351 
##   1.284500325   0.300778763   1.758188903  -3.221342364   3.619381597 
##            31            73           424           333           122 
##   2.428740626  -0.527694477   1.845675138  -2.221711574  -2.189798218 
##            92           234           338           472           300 
##  -5.666921491   8.775318305  -0.162437018  -4.000684029  -1.784296138 
##           323           314           443           387           376 
##  -1.977525789  -4.395997397  -1.201281672   6.307724361 -11.738018736 
##           208           499           206           297           219 
##   5.537880093   0.246994887   1.102222072  -0.460269305  -2.987158004 
##            82           169           413           228           423 
##  -3.021002286  -3.205200388  19.853308439  -2.192024195   2.797529204 
##           258           385           482           498           192 
##   3.414178564   8.271338091  -4.451362820  -0.266787057   0.792207439 
##           386           414            50           260            97 
##   0.442623562   6.487624676   3.388454435  -6.629745005  -2.978426708 
##           183           100           274           484           296 
##   2.427214778  -0.031584384  -0.570256827   1.238172323   0.095873701 
##           357           227           190           329           156 
##  -1.933201226  -2.531726289   0.382566988  -0.545690834  -3.581922258 
##           131             8           468            70           213 
##  -1.346444916   7.621427858   2.329583294   1.528233254   1.223808670 
##           382           182 
##  -7.999559935   8.543358664 
## 
## $sd_residuals
## [1] 5.091575
## 
## $scaled_residuals
## [1] 0.1125522
## 
## attr(,"class")
## [1] "conformalize"
head(predictions_boston4)
##         fit       lwr      upr
## 1  30.39434 23.343942 45.84706
## 15 19.10867 11.055854 34.56138
## 17 19.48015 11.427336 33.98435
## 19 14.18587  6.133060 25.25477
## 28 13.85324  5.800429 21.80538
## 37 21.66451 13.611700 36.16871

Fit Conformal Model 2

# Define fit and predict functions
fit_func <- function(formula, data, ...) ranger::ranger(formula, data = data)
predict_func <- function(fit, newdata, ...) predict(fit, newdata)$predictions

# Apply conformalize using the training data
conformal_model_boston_rf <- misc::conformalize(
  formula = medv ~ .,
  data = train_data,
  fit_func = fit_func,
  predict_func = predict_func,
  seed = 123
)

# Predict with split conformal method on the test data
predictions_boston_rf <- predict(
  conformal_model_boston_rf,
  newdata = test_data,
  predict_func = predict_func,
  level = 0.95,
  method = "kde"
)
## 
## [1] "object's value:"
## $fit
## Ranger result
## 
## Call:
##  ranger::ranger(formula, data = data) 
## 
## Type:                             Regression 
## Number of trees:                  500 
## Sample size:                      202 
## Number of independent variables:  13 
## Mtry:                             3 
## Target node size:                 5 
## Variable importance mode:         none 
## Splitrule:                        variance 
## OOB prediction error (MSE):       13.55259 
## R squared (OOB):                  0.8533691 
## 
## $residuals
##   [1]  -3.419252485  -0.948371904   2.675323333  -2.598149629  -0.923586667
##   [6]   2.806848571  -2.826340000  -1.491453333  -2.489190000  -3.565245897
##  [11]  -2.136576667  -3.257890000   2.622440000  -3.145675238  -0.244042279
##  [16]   4.792316667   1.998528333   0.404793333  22.685658431  -3.982500000
##  [21]  -2.671341579  -1.622233333  -1.460822619  -6.905260537  -5.999110000
##  [26]   0.155501429   0.392040000   0.149533333  -0.930151111  -1.906894762
##  [31]   2.173411667   1.539468254   0.940606667   0.309360000  -1.265117857
##  [36]  -4.139176667   0.080682063   0.790000000  -0.670880000   3.063760000
##  [41]  -1.711786190  -1.024086667   2.283516667   3.230715208  -0.860076667
##  [46]  -7.209290000  -0.960430079  -2.272120000  -3.188883333  -0.830568072
##  [51]   1.028754762   9.078797302  -3.833185873  -2.743470000  -3.063023333
##  [56]   0.233986667   1.097631190  -0.455676667   1.006970000  -0.354476667
##  [61]   4.763956667  -0.214310000   0.975033333  -0.950043333  -1.107186667
##  [66]  -4.651613333   5.937836459  -3.254903344  -1.278533333  -6.213082086
##  [71]   4.446340000   3.104358571   3.332153333  -2.110590000  -0.577501429
##  [76]   0.138066667   1.508937921  -0.760330000   0.262449105  -1.368658889
##  [81]  -3.598423333  -1.123331889   3.749873333  -0.731447167   6.017003333
##  [86]  -1.556023540  -1.682773333  -0.802122381   1.794991905   0.792356667
##  [91]  -1.384863333  -1.873168579  -1.470001346   0.871751192   2.918430000
##  [96]   1.689620000   3.567063333   1.616160292   0.607816667   2.928053333
## [101]  -1.604249246  -1.887962857   1.271774740   1.465173333  -3.736407921
## [106]  -1.379770000   2.114656667   4.379490000  -2.429900000   4.534193333
## [111]   0.818860000  -0.974893333   0.386531429   2.707783333   0.954815952
## [116]   1.397428095   1.750509706  19.166842727  -5.368585249  -4.832006667
## [121]  -2.089930000   0.794706667   0.424038571  -0.912724448  -5.966195684
## [126]  -1.858970000   1.488720000   0.234165000   8.115750000  -1.359113333
## [131]  -0.284088571   0.008984731  -0.644770000  -1.141963333   0.697113333
## [136]  -1.385102684  -1.537040000  -0.528966667   2.202580000  -0.482767638
## [141]  -1.977078277  -1.385644762  -1.348723163   0.422290000  -2.897343333
## [146]   1.137146667  -4.545621403   1.054850000  -0.258624034  -2.568693333
## [151]  -1.473339524  -0.854933333  -0.319572550  -3.069270000   0.061366667
## [156]  -2.431423333   4.908740000  -1.892460000  -1.433907712  -1.354686667
## [161]  -1.865293333  -1.118776667   2.543779556   0.463239708 -11.145920805
## [166]   3.396180000   0.547756667   0.718356667   2.520637619   1.517316667
## [171]  -1.419243333   1.834314921   4.713897526  -2.458633333   1.398365831
## [176]   7.671565411  -2.675716524  -1.082686528  -2.071096667   2.584770000
## [181]  -3.645966658   2.998171877   0.397487619  -2.018283636  -3.104970000
## [186]   2.172127619  -0.680980000  -0.271110000   0.765309626   3.178264286
## [191]   1.714637993  -6.863493333   1.529373333  -3.405393333  -2.415632886
## [196]   0.214736190   9.585452619   3.925567955   0.351836667   1.739140000
## [201]  -0.202268616  10.164143333
## 
## $sd_residuals
## [1] 3.624106
## 
## $scaled_residuals
## [1] 0.01260864
## 
## attr(,"class")
## [1] "conformalize"
head(predictions_boston_rf)
##           fit       lwr      upr
## [1,] 27.03134 21.991838 32.43038
## [2,] 19.20299 13.542260 25.05314
## [3,] 21.34472 17.000993 30.77696
## [4,] 18.77455 12.341589 25.88818
## [5,] 15.60764  9.157478 21.48264
## [6,] 21.31355 14.591954 29.75374

Calculate Out-of-Sample Coverage Rate 2

The coverage rate is the proportion of true values in the test set that fall within the prediction intervals.

# True values for the test set
true_y_boston <- test_data$medv

# Check if true values fall within the prediction intervals
coverage_boston <- mean(true_y_boston >= predictions_boston[, "lwr"] & true_y_boston <= predictions_boston[, "upr"])

cat("Out-of-sample coverage rate for Boston dataset:", coverage_boston)
## Out-of-sample coverage rate for Boston dataset: 0.9411765
# True values for the test set
true_y_boston <- test_data$medv

# Check if true values fall within the prediction intervals
coverage_boston <- mean(true_y_boston >= predictions_boston2[, "lwr"] & true_y_boston <= predictions_boston2[, "upr"])

cat("Out-of-sample coverage rate for Boston dataset:", coverage_boston)
## Out-of-sample coverage rate for Boston dataset: 0.9607843
# True values for the test set
true_y_boston <- test_data$medv

# Check if true values fall within the prediction intervals
coverage_boston <- mean(true_y_boston >= predictions_boston3[, "lwr"] & true_y_boston <= predictions_boston3[, "upr"])

cat("Out-of-sample coverage rate for Boston dataset:", coverage_boston)
## Out-of-sample coverage rate for Boston dataset: 0.9705882
# True values for the test set
true_y_boston <- test_data$medv

# Check if true values fall within the prediction intervals
coverage_boston <- mean(true_y_boston >= predictions_boston4[, "lwr"] & true_y_boston <= predictions_boston4[, "upr"])

cat("Out-of-sample coverage rate for Boston dataset:", coverage_boston)
## Out-of-sample coverage rate for Boston dataset: 0.9607843
# True values for the test set
true_y_boston <- test_data$medv

# Check if true values fall within the prediction intervals
coverage_boston <- mean(true_y_boston >= predictions_boston_rf[, "lwr"] & true_y_boston <= predictions_boston_rf[, "upr"])

cat("Out-of-sample coverage rate for Boston dataset:", coverage_boston)
## Out-of-sample coverage rate for Boston dataset: 0.9215686

Results

  • The prediction intervals are calculated using the split conformal method.
  • The out-of-sample coverage rate is displayed, which should be close to the specified confidence level (e.g., 0.95).