conformalize.Rd
This function implements conformal prediction for regression models using a split conformal approach.
conformalize(
formula = NULL,
x = NULL,
y = NULL,
data = NULL,
fit_func,
predict_func = predict,
split_ratio = 0.5,
seed = NULL,
...
)
A formula specifying the model to be fitted.
A numeric matrix of predictors.
A numeric vector of responses.
A data frame containing the variables in the formula.
A function to fit the model. It should take a formula and data as arguments.
A function to make predictions. It should take a fitted model and new data as arguments.
The proportion of data to use for training. Default is 0.5.
An optional seed for reproducibility.
A list containing the fitted model, residuals, and other relevant information.
# Example usage with a linear model
library(MASS)
data(Boston)
set.seed(123)
fit_func <- function(formula, data) {
lm(formula, data = data)
}
predict_func <- function(fit, newdata) {
predict(fit, newdata)
}
conformalize(formula = medv ~ ., data = Boston, fit_func = fit_func,
predict_func = predict_func, split_ratio = 0.5, seed = 123)
#> $fit
#>
#> Call:
#> lm(formula = formula, data = data)
#>
#> Coefficients:
#> (Intercept) crim zn indus chas nox
#> 32.345703 -0.090792 0.051840 0.027244 3.978434 -12.332055
#> rm age dis rad tax ptratio
#> 3.388799 0.001790 -1.414487 0.281171 -0.011314 -0.718374
#> black lstat
#> 0.009914 -0.655356
#>
#>
#> $residuals
#> 1 2 3 6 8 9
#> -5.93189785 -3.00935726 4.22494386 3.23421500 9.34324443 7.92835920
#> 12 15 17 18 19 21
#> -1.74602250 -1.78222860 1.37242919 0.43011329 3.38575997 1.57827926
#> 24 28 29 30 31 35
#> 1.12275023 0.32364441 -1.36392273 -0.10303276 2.00335673 0.50277448
#> 36 37 38 44 46 47
#> -4.97101469 -2.19364956 -2.32510207 0.35442952 -2.38721172 0.54488750
#> 49 50 53 56 58 59
#> 8.15813653 3.25990784 -2.41381427 4.25988450 -1.25992887 1.03454021
#> 60 62 65 68 70 71
#> -1.72730334 -2.08895036 10.04666043 0.85965644 0.16690569 -0.89967625
#> 73 75 79 80 82 87
#> -1.96627172 -1.39546520 0.56298611 -2.03336132 -3.02105546 1.11439774
#> 88 92 93 95 96 97
#> -3.41360439 -5.11694542 -5.88646719 -6.07298982 0.16399099 -2.55412274
#> 99 100 101 103 104 108
#> 9.17719004 1.58285735 2.54919223 -1.34661782 -1.11097500 -0.32505252
#> 111 112 114 119 120 122
#> 0.95274396 -3.45428418 -1.11130179 0.71661351 -1.16927281 -2.06863831
#> 123 124 126 128 130 131
#> 0.50140276 2.43272774 -0.91473705 0.46884781 -0.20538562 -1.69149626
#> 132 133 138 139 140 142
#> -0.79014435 1.84199231 -2.84232908 -0.45370321 1.15492584 11.81429793
#> 144 146 147 149 150 156
#> 4.21522637 3.15127996 -0.50112182 9.02907198 0.81404382 -6.03766746
#> 157 162 167 169 170 171
#> -0.98132841 13.09633745 13.09324559 -1.98219176 -3.70095576 -4.28320989
#> 172 173 175 176 180 181
#> -4.68361512 1.13743984 -3.42817552 -1.08745868 4.54886314 5.98512598
#> 182 183 187 188 189 190
#> 8.93933375 4.30010288 14.65274272 -0.58438710 -2.17215513 1.17947708
#> 191 192 193 201 202 203
#> 6.81078951 0.62264470 3.84412795 2.20571744 -4.63834603 5.84705568
#> 204 206 207 208 213 216
#> 7.43113330 0.14558243 1.09075989 5.69715052 -0.77068021 0.64397826
#> 219 220 221 227 228 230
#> -3.35063851 -7.43882560 -6.97063444 0.41748260 -0.32749353 0.19009727
#> 231 233 234 237 239 241
#> 0.45989459 4.05200968 11.71518384 -5.70972121 -4.25147663 -4.04319913
#> 242 246 247 248 251 258
#> -2.61031353 6.04501106 4.25419343 0.75001177 -0.03787734 7.85113795
#> 259 260 261 264 266 268
#> 0.42419748 -4.31722945 0.07912086 -2.10329230 -4.90904963 10.82449060
#> 269 271 274 275 276 281
#> 4.92442332 -0.67069443 -1.10876056 -5.07674065 -1.92987430 7.78249180
#> 283 284 285 293 295 296
#> 5.36748370 5.23264428 1.48595341 -4.34301754 -1.76427420 0.83375579
#> 297 298 300 301 304 305
#> 0.50257791 2.27632527 -2.20500867 -5.21642142 0.88430990 3.30411506
#> 311 313 314 317 320 321
#> -2.58205478 -3.76246891 -4.23972121 1.05966711 -0.07690617 -1.39470863
#> 322 323 324 327 329 333
#> -2.14384581 -2.88578557 -1.00225134 -1.20390481 -1.38795675 -3.76956876
#> 336 337 338 340 341 343
#> -0.13235556 -1.07836293 -1.12881613 -2.59982299 -3.15997827 -5.19414013
#> 344 345 346 349 351 354
#> -3.91922353 2.27019961 1.07820257 -3.11206850 1.75114118 4.43926014
#> 357 360 361 364 365 367
#> -3.84252368 2.06229578 0.48173358 -5.87641082 -17.70568186 5.08727909
#> 370 372 375 376 381 382
#> 14.59869952 24.10412405 14.53165030 -10.70842703 -5.21813791 -7.25314247
#> 384 385 386 387 388 390
#> -0.43892271 6.02541908 0.03330461 4.53637500 2.61653252 -2.93749494
#> 391 392 394 397 399 400
#> -2.68258921 5.72818733 -7.02215173 -6.70772692 -0.95154379 -3.35431296
#> 402 403 404 410 412 413
#> -10.56806161 -6.00473330 -5.31157186 8.49076723 1.27853768 18.00394179
#> 414 418 419 421 422 423
#> 4.39086699 3.98040719 1.84287210 -3.57182021 -4.60434214 1.73802777
#> 424 428 429 433 435 437
#> 1.33495669 -4.04204600 -2.74310512 -5.53561611 -4.90532812 -5.17868149
#> 438 442 443 445 446 447
#> 0.58522991 -0.42790262 -0.96957856 -0.57619059 0.39613138 -3.16849803
#> 448 449 451 453 454 456
#> -6.23923636 -3.79196000 -3.06209235 -2.83974904 -4.71698730 -1.66128477
#> 457 461 466 467 468 469
#> -0.02845920 -2.87608278 1.40376674 4.64038658 2.93126454 2.25637829
#> 471 472 473 474 476 482
#> -0.11113848 -3.38589175 0.79568555 3.98752747 -1.60192710 -3.88241389
#> 484 487 488 489 490 495
#> 0.20746680 -0.65684094 -1.30462369 2.82691957 -0.95807072 3.84119515
#> 496 497 498 499 504 505
#> 6.57776371 6.34190063 -0.90558448 -0.21859935 -4.78168690 -5.15957869
#> 506
#> -11.61345162
#>
#> $sd_residuals
#> [1] 4.949644
#>
#> $scaled_residuals
#> [1] 0.03378207
#>
#> attr(,"class")
#> [1] "conformalize"