Apply a fitted nested model to generate different types of predictions.
stats::predict()
/ parsnip::predict_raw()
methods for nested model fits.
Usage
# S3 method for nested_model_fit
predict(object, new_data, type = NULL, opts = list(), ...)
# S3 method for nested_model_fit
predict_raw(object, new_data, opts = list(), ...)
Arguments
- object
A
nested_model_fit
object produced byfit.nested_model()
.- new_data
A data frame to make predictions on. Can be nested or non-nested.
- type
A singular character vector or
NULL
. Passed on toparsnip::predict.model_fit()
.- opts
A list of optional arguments. Passed on to
parsnip::predict.model_fit()
.- ...
Arguments for the underlying model's predict function. Passed on to
parsnip::predict.model_fit()
.
Value
A data frame of model predictions. For predict_raw()
, a
matrix, data frame, vector or list.
Examples
library(dplyr)
library(tidyr)
library(parsnip)
data <- filter(example_nested_data, id %in% 5:15)
nested_data <- nest(data, data = -id)
model <- linear_reg() %>%
set_engine("lm") %>%
nested()
fitted <- fit(model, z ~ x + y + a + b, nested_data)
predict(fitted, example_nested_data)
#> Warning: Some predictions failed.
#> # A tibble: 1,000 × 1
#> .pred
#> <dbl>
#> 1 NA
#> 2 NA
#> 3 NA
#> 4 NA
#> 5 NA
#> 6 NA
#> 7 NA
#> 8 NA
#> 9 NA
#> 10 NA
#> # ℹ 990 more rows
predict_raw(fitted, example_nested_data)
#> Warning: Some predictions failed.
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#> 1 2 3 4
#> NA NA 37.6319619 34.6675302 48.9534648 32.9415712
#> 5 6 7 8 9 10
#> 45.8397678 47.1841754 43.1352860 42.3881863 35.6477312 47.2081061
#> 11 12 13 14 15 16
#> 40.6555079 45.4181232 41.2040202 46.7792115 44.4373731 48.3988571
#> 17 18 19 20 21 22
#> 40.5440131 32.1688648 48.5258193 43.5221122 32.0952033 41.6197029
#> 23 24 25 26 27 28
#> 40.7089757 32.7541173 32.0640006 45.1652044 30.3157805 45.0106798
#> 29 30 31 32 33 34
#> 46.3454584 36.8788189 34.0243024 44.8696121 31.7896132 36.6176057
#> 35 36 37 38 39 40
#> 37.5103511 44.0099607 41.5118849 47.1318431 35.7775797 53.3682921
#> 41 42 43 44 45 46
#> 43.7354649 41.5104474 48.3590848 32.0641994 45.1734049 44.2209702
#> 47 48 49 50 1 2
#> 41.9862098 41.6871112 37.6096485 41.4406573 60.0262234 57.9752386
#> 3 4 5 6 7 8
#> 59.7343479 62.7541347 60.0519092 58.1765191 58.0936291 57.1523762
#> 9 10 11 12 13 14
#> 57.0379805 55.2115190 59.8094270 58.3606515 52.8335588 55.0243082
#> 15 16 17 18 19 20
#> 53.9338442 50.1080697 53.1660877 52.9348141 49.4996065 48.5304871
#> 21 22 23 24 25 26
#> 51.2501840 49.8909167 47.3404559 47.5148040 50.5442324 50.3227284
#> 27 28 29 30 31 32
#> 46.8712255 43.2607977 45.1797767 44.3283019 42.8747889 47.7669559
#> 33 34 35 36 37 38
#> 43.0519355 39.8008969 39.7971714 43.9129156 39.0025160 42.9337298
#> 39 40 41 42 43 44
#> 40.9821696 43.2342899 37.7671806 36.7960719 36.8436979 35.5445246
#> 45 46 47 48 49 50
#> 37.1840650 39.4344557 33.5125533 36.3283083 34.9105095 36.0586859
#> 1 2 3 4 5 6
#> 47.6270504 44.9779840 38.9374635 45.6040120 39.9995875 42.6771916
#> 7 8 9 10 11 12
#> 39.0842412 38.1573825 41.6624586 40.1410279 37.2942556 35.3571091
#> 13 14 15 16 17 18
#> 39.9791497 33.7271553 36.9598664 31.3087216 37.1522938 32.5407975
#> 19 20 21 22 23 24
#> 34.3787560 31.3314482 28.0155530 30.1316277 29.0110942 27.3069825
#> 25 26 27 28 29 30
#> 29.2992724 26.9808910 28.3713655 26.4926729 24.6028192 22.7136345
#> 31 32 33 34 35 36
#> 22.0220717 25.0605769 19.5532483 24.0512586 20.8009876 22.2872590
#> 37 38 39 40 41 42
#> 17.2784601 22.7430536 21.9177355 16.8666721 16.3499239 14.7423782
#> 43 44 45 46 47 48
#> 15.7407314 18.4255277 16.8997643 16.3302286 11.8593669 14.8871917
#> 49 50 1 2 3 4
#> 10.2563614 10.3298892 -4.6597627 -0.7729351 -1.9298875 -6.3903353
#> 5 6 7 8 9 10
#> 6.3429502 6.7001328 -0.8295463 8.0850894 -0.6711028 1.0427615
#> 11 12 13 14 15 16
#> 16.0514342 10.2374064 20.0777287 15.3062785 13.8410911 16.7587516
#> 17 18 19 20 21 22
#> 22.9417777 20.1170145 28.5519583 23.5208137 18.4518123 26.6187735
#> 23 24 25 26 27 28
#> 33.2655085 28.5345982 39.8935201 33.4665089 38.3795749 39.4056647
#> 29 30 31 32 33 34
#> 32.4826788 38.8494729 40.2738227 39.9392240 42.7028124 46.0474268
#> 35 36 37 38 39 40
#> 46.5327054 46.2398823 42.4489682 50.2040629 52.8044990 46.8118580
#> 41 42 43 44 45 46
#> 44.0987255 55.3271360 58.4921417 57.7546026 60.6917431 52.6197356
#> 47 48 49 50 1 2
#> 71.8127670 64.3434586 61.9503640 64.3456740 12.6178982 15.9579781
#> 3 4 5 6 7 8
#> 10.6516537 7.9941765 20.2339026 4.1767053 14.1355939 13.6570942
#> 9 10 11 12 13 14
#> 6.8265423 11.1436191 16.1651110 20.9646700 12.6646558 18.0317407
#> 15 16 17 18 19 20
#> 16.2314779 13.9287035 14.4495542 22.8200182 24.4623801 14.1764728
#> 21 22 23 24 25 26
#> 12.1373103 22.0332494 22.4213244 27.9869683 23.4815512 14.5084430
#> 27 28 29 30 31 32
#> 20.2110573 22.1601193 22.4900335 32.1739308 17.9158363 26.4835242
#> 33 34 35 36 37 38
#> 29.3073776 19.6526993 20.4506191 23.5028700 26.9377671 20.0697628
#> 39 40 41 42 43 44
#> 29.4252753 29.7907190 29.4226469 27.9681131 36.4067197 30.4968134
#> 45 46 47 48 49 50
#> 27.9950469 39.2499585 40.0480645 38.7637573 32.6845166 34.3517123
#> 1 2 3 4 5 6
#> 58.6554884 52.7758770 59.1136089 56.0251534 53.7841345 56.2272744
#> 7 8 9 10 11 12
#> 52.0875110 53.5655861 54.8031787 49.6824029 46.4993234 50.0825963
#> 13 14 15 16 17 18
#> 48.9426382 44.1945380 45.5915874 49.2596970 56.3597240 40.9450382
#> 19 20 21 22 23 24
#> 44.9299528 47.6287002 43.2544530 45.8374365 44.9412198 45.5603300
#> 25 26 27 28 29 30
#> 44.9251559 45.0189507 44.4893216 35.0739765 45.9922116 42.5517345
#> 31 32 33 34 35 36
#> 31.5419386 37.9684869 31.8247371 41.0757635 38.4770884 36.6007458
#> 37 38 39 40 41 42
#> 32.8898397 36.1740439 35.9159420 32.8090816 29.5765640 40.7226928
#> 43 44 45 46 47 48
#> 36.0163276 32.7715529 27.5472294 29.8313247 23.8872894 28.9691656
#> 49 50 1 2 3 4
#> 30.4639153 22.3694375 67.4301750 56.3641727 63.3770162 63.3282906
#> 5 6 7 8 9 10
#> 60.8231323 59.9416137 55.9034984 62.8529149 62.5708989 63.2669514
#> 11 12 13 14 15 16
#> 68.3810391 68.1360765 66.8051949 60.4203894 64.4124677 69.1867473
#> 17 18 19 20 21 22
#> 66.4676480 60.7817937 64.4086397 62.9247264 70.5821959 65.1999972
#> 23 24 25 26 27 28
#> 68.1761496 62.7962592 61.2844191 63.5496921 72.4839440 66.3976033
#> 29 30 31 32 33 34
#> 72.5093924 70.1426021 66.7839671 66.6470649 65.4266072 65.6433722
#> 35 36 37 38 39 40
#> 64.9991391 69.5088061 72.6192642 66.7288387 70.5814474 70.7230023
#> 41 42 43 44 45 46
#> 72.9205104 67.6599723 75.3569122 66.3664665 66.9391441 71.3213974
#> 47 48 49 50 1 2
#> 64.8486959 64.8050604 73.9185089 72.3672801 35.2869863 48.0989095
#> 3 4 5 6 7 8
#> 36.2594384 47.3496358 34.5153320 42.8911928 49.4947234 46.8839578
#> 9 10 11 12 13 14
#> 39.1606905 45.7852838 35.4563535 48.5450551 41.5238941 43.0631028
#> 15 16 17 18 19 20
#> 45.8011953 34.6886035 38.8863423 43.8014706 36.4075476 51.3896708
#> 21 22 23 24 25 26
#> 41.6585191 38.9908428 45.3642732 39.4249230 40.6142592 36.4266625
#> 27 28 29 30 31 32
#> 55.3555374 40.9995792 45.9642066 47.6047924 40.6866584 51.9413918
#> 33 34 35 36 37 38
#> 53.8551732 42.4826266 54.5580218 56.7909426 44.0116956 43.6185562
#> 39 40 41 42 43 44
#> 51.2759960 50.6942094 47.4186889 46.4033258 48.8416828 53.8120641
#> 45 46 47 48 49 50
#> 42.0583968 54.5283986 45.0899179 51.7082706 60.1344676 48.6807409
#> 1 2 3 4 5 6
#> 12.4539686 7.3277666 8.8241755 9.4342860 18.7285734 25.3102923
#> 7 8 9 10 11 12
#> 5.3873881 20.3161579 31.1943961 24.2975665 13.0124614 9.0670309
#> 13 14 15 16 17 18
#> 23.8014856 32.2031088 20.4280224 26.5132095 21.7482036 17.6894767
#> 19 20 21 22 23 24
#> 36.3228073 35.8395574 13.0662829 37.1729722 33.7166645 29.1201793
#> 25 26 27 28 29 30
#> 25.0176581 22.7464219 15.1689437 11.4293465 24.3833775 19.1135294
#> 31 32 33 34 35 36
#> 33.3759354 38.0382440 21.3448039 37.3692517 17.6142252 34.3958260
#> 37 38 39 40 41 42
#> 28.2629230 24.5774928 38.1750095 21.9652849 26.3396000 37.7524645
#> 43 44 45 46 47 48
#> 14.8950216 26.5671625 15.5942890 19.3566025 20.4875612 23.3167278
#> 49 50 1 2 3 4
#> 24.4140344 26.5813349 31.8417641 40.2994324 34.6409752 39.0592006
#> 5 6 7 8 9 10
#> 36.8345046 41.0420969 32.1350578 26.9688458 49.8119991 46.3006879
#> 11 12 13 14 15 16
#> 47.1489074 39.7488633 58.6362942 59.3719706 41.2767033 58.9487117
#> 17 18 19 20 21 22
#> 56.5415605 61.9480501 66.2836627 57.3516566 68.4344143 54.7828809
#> 23 24 25 26 27 28
#> 60.4023060 71.3612328 73.3034496 75.3584465 62.0194758 63.5701553
#> 29 30 31 32 33 34
#> 65.6625583 71.7073614 78.7254917 79.1349128 80.4311489 82.1185507
#> 35 36 37 38 39 40
#> 82.7057521 92.1475406 84.6981243 83.4236188 78.0558329 88.5457438
#> 41 42 43 44 45 46
#> 88.6545834 98.7425898 90.7964204 88.4693719 91.6455289 104.5759794
#> 47 48 49 50 1 2
#> 97.2528679 105.9453477 100.6166761 104.6145182 43.6946109 47.7436782
#> 3 4 5 6 7 8
#> 48.5066010 48.6921365 51.3078385 45.2649564 53.5026143 45.8205012
#> 9 10 11 12 13 14
#> 49.2426855 47.0611171 53.7941337 52.2044752 49.4328923 53.4458424
#> 15 16 17 18 19 20
#> 49.1537807 57.3432681 52.7335145 55.8099069 55.7429048 57.6857122
#> 21 22 23 24 25 26
#> 57.0456806 61.0721955 62.1630024 58.8017271 60.7163645 58.0715075
#> 27 28 29 30 31 32
#> 61.5298623 61.7265050 60.5305004 61.4287467 55.1953300 65.3877370
#> 33 34 35 36 37 38
#> 61.5237645 68.4515213 63.0780219 59.7022799 62.1074827 63.7355654
#> 39 40 41 42 43 44
#> 67.6034835 70.8445246 69.2889159 65.1792696 68.8095712 65.9851224
#> 45 46 47 48 49 50
#> 65.1439401 73.7479839 65.3615468 71.8035759 65.8045864 74.7195222
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA NA NA
#>
#> NA NA NA NA