Predicted values and intervals based on a fitted model object.
Usage
# S3 method for splm
predict(
object,
newdata,
se.fit = FALSE,
interval = c("none", "confidence", "prediction"),
level = 0.95,
local,
...
)
# S3 method for spautor
predict(
object,
newdata,
se.fit = FALSE,
interval = c("none", "confidence", "prediction"),
level = 0.95,
local,
...
)
# S3 method for splm_list
predict(
object,
newdata,
se.fit = FALSE,
interval = c("none", "confidence", "prediction"),
level = 0.95,
local,
...
)
# S3 method for spautor_list
predict(
object,
newdata,
se.fit = FALSE,
interval = c("none", "confidence", "prediction"),
level = 0.95,
local,
...
)
# S3 method for splmRF
predict(object, newdata, local, ...)
# S3 method for spautorRF
predict(object, newdata, local, ...)
# S3 method for splmRF_list
predict(object, newdata, local, ...)
# S3 method for spautorRF_list
predict(object, newdata, local, ...)
# S3 method for spglm
predict(
object,
newdata,
type = c("link", "response"),
se.fit = FALSE,
interval = c("none", "confidence", "prediction"),
newdata_size,
level = 0.95,
local,
var_correct = TRUE,
...
)
# S3 method for spgautor
predict(
object,
newdata,
type = c("link", "response"),
se.fit = FALSE,
interval = c("none", "confidence", "prediction"),
newdata_size,
level = 0.95,
local,
var_correct = TRUE,
...
)
# S3 method for spglm_list
predict(
object,
newdata,
type = c("link", "response"),
se.fit = FALSE,
interval = c("none", "confidence", "prediction"),
newdata_size,
level = 0.95,
local,
var_correct = TRUE,
...
)
# S3 method for spgautor_list
predict(
object,
newdata,
type = c("link", "response"),
se.fit = FALSE,
interval = c("none", "confidence", "prediction"),
newdata_size,
level = 0.95,
local,
var_correct = TRUE,
...
)
Arguments
- object
A fitted model object.
- newdata
A data frame or
sf
object in which to look for variables with which to predict. If a data frame,newdata
must contain all variables used byformula(object)
and all variables representing coordinates. If ansf
object,newdata
must contain all variables used byformula(object)
and coordinates are obtained from the geometry ofnewdata
. If omitted, missing data from the fitted model object are used.- se.fit
A logical indicating if standard errors are returned. The default is
FALSE
.- interval
Type of interval calculation. The default is
"none"
. Other options are"confidence"
(for confidence intervals) and"prediction"
(for prediction intervals).- level
Tolerance/confidence level. The default is
0.95
.- local
A optional logical or list controlling the big data approximation. If omitted,
local
is set toTRUE
orFALSE
based on the sample size of the fitted model object and/or the prediction size ofnewdata
-- if the sample size or prediction size exceeds 5000,local
is set toTRUE
, otherwise it is set toFALSE
. IfFALSE
, no big data approximation is implemented. If a list is provided, the following arguments detail the big data approximation:method
: The big data approximation method. Ifmethod = "all"
, all observations are used andsize
is ignored. Ifmethod = "distance"
, thesize
data observations closest (in terms of Euclidean distance) to the observation requiring prediction are used. Ifmethod = "covariance"
, thesize
data observations with the highest covariance with the observation requiring prediction are used. If random effects and partition factors are not used in estimation and the spatial covariance function is monotone decreasing,"distance"
and"covariance"
are equivalent. The default is"covariance"
. Only used with models fit usingsplm()
orspglm()
.size
: The number of data observations to use whenmethod
is"distance"
or"covariance"
. The default is 50. Only used with models fit usingsplm()
orspglm()
.parallel
: IfTRUE
, parallel processing via the parallel package is automatically used. The default isFALSE
.ncores
: Ifparallel = TRUE
, the number of cores to parallelize over. The default is the number of available cores on your machine.
When
local
is a list, at least one list element must be provided to initialize default arguments for the other list elements. Iflocal
isTRUE
, defaults forlocal
are chosen such thatlocal
is transformed intolist(size = 50, method = "covariance", parallel = FALSE)
.- ...
Other arguments. Only used for models fit using
splmRF()
orspautorRF()
where...
indicates other arguments toranger::predict.ranger()
.- type
The scale (
response
orlink
) of predictions obtained usingspglm()
orspgautor
objects.- newdata_size
The
size
value for each observation innewdata
used when predicting for the binomial family.- var_correct
A logical indicating whether to return the corrected prediction variances when predicting via models fit using
spglm()
orspgautor()
. The default isTRUE
.
Value
For splm
or spautor
objects, if se.fit
is FALSE
, predict()
returns
a vector of predictions or a matrix of predictions with column names
fit
, lwr
, and upr
if interval
is "confidence"
or "prediction"
. If se.fit
is TRUE
, a list with the following components is returned:
fit
: vector or matrix as abovese.fit:
standard error of each fit
For splm_list
or spautor_list
objects, a list that contains relevant quantities for each
list element.
For splmRF
or spautorRF
objects, a vector of predictions. For splmRF_list
or spautorRF_list
objects, a list that contains relevant quantities for each list element.
Details
For splm
and spautor
objects, the (empirical) best linear unbiased predictions (i.e., Kriging
predictions) at each site are returned when interval
is "none"
or "prediction"
alongside standard errors. Prediction intervals
are also returned if interval
is "prediction"
. When
interval
is "confidence"
, the estimated mean is returned
alongside standard errors and confidence intervals for the mean. For splm_list
and spautor_list
objects, predictions and associated intervals and standard errors are returned
for each list element.
For splmRF
or spautorRF
objects, random forest spatial residual
model predictions are computed by combining the random forest prediction with
the (empirical) best linear unbiased prediction for the residual. Fox et al. (2020)
call this approach random forest regression Kriging. For splmRF_list
or spautorRF
objects,
predictions are returned for each list element.
References
Fox, E.W., Ver Hoef, J. M., & Olsen, A. R. (2020). Comparing spatial regression to random forests for large environmental data sets. PloS one, 15(3), e0229509.
Examples
spmod <- splm(sulfate ~ 1,
data = sulfate,
spcov_type = "exponential", xcoord = x, ycoord = y
)
predict(spmod, sulfate_preds)
#> 1 2 3 4 5 6 7 8
#> 1.618435 24.371803 8.945102 16.452377 4.925139 26.795441 2.869041 14.255129
#> 9 10 11 12 13 14 15 16
#> 1.527998 14.278871 1.201078 17.125020 1.320418 20.139591 21.295541 11.237529
#> 17 18 19 20 21 22 23 24
#> 18.683866 9.663971 1.788953 23.631754 3.275535 2.350008 2.048598 2.010150
#> 25 26 27 28 29 30 31 32
#> 3.201960 1.493974 18.623392 7.817861 18.797743 5.125182 1.807167 4.075101
#> 33 34 35 36 37 38 39 40
#> 12.427273 3.152303 1.293478 6.957413 18.939770 7.297365 14.674697 15.520246
#> 41 42 43 44 45 46 47 48
#> 12.405048 24.918442 17.301734 26.864540 1.603280 8.549257 3.784855 14.937188
#> 49 50 51 52 53 54 55 56
#> 24.494371 1.956122 27.204132 13.574770 2.576245 1.914563 7.487318 17.136559
#> 57 58 59 60 61 62 63 64
#> 17.014448 1.337634 18.683246 12.688687 15.393346 23.574345 1.579663 17.036054
#> 65 66 67 68 69 70 71 72
#> 28.544693 1.288539 1.140777 6.171537 3.437062 10.789035 3.615948 17.829018
#> 73 74 75 76 77 78 79 80
#> 8.908869 7.301336 2.126375 1.660542 19.373718 1.280324 16.307787 15.288219
#> 81 82 83 84 85 86 87 88
#> 17.793902 3.636393 9.731890 1.843879 1.673207 7.792596 2.326289 1.614190
#> 89 90 91 92 93 94 95 96
#> 2.694977 19.431798 4.044955 7.952300 12.423394 25.946812 15.137847 16.654989
#> 97 98 99 100
#> 21.603763 20.798900 16.737467 16.730626
predict(spmod, sulfate_preds, interval = "prediction")
#> fit lwr upr
#> 1 1.618435 -6.4458691 9.682739
#> 2 24.371803 16.8475856 31.896020
#> 3 8.945102 1.0255301 16.864674
#> 4 16.452377 8.6427128 24.262041
#> 5 4.925139 -2.7823585 12.632637
#> 6 26.795441 19.2836818 34.307199
#> 7 2.869041 -5.0546545 10.792737
#> 8 14.255129 6.7727705 21.737488
#> 9 1.527998 -6.3288119 9.384809
#> 10 14.278871 6.6143954 21.943347
#> 11 1.201078 -6.7412477 9.143403
#> 12 17.125020 9.6129332 24.637107
#> 13 1.320418 -6.8475567 9.488393
#> 14 20.139591 12.7287130 27.550469
#> 15 21.295541 13.6157754 28.975307
#> 16 11.237529 3.2332125 19.241846
#> 17 18.683866 11.1027931 26.264939
#> 18 9.663971 1.7928039 17.535138
#> 19 1.788953 -6.1854294 9.763336
#> 20 23.631754 16.0720133 31.191496
#> 21 3.275535 -4.7284202 11.279490
#> 22 2.350008 -5.1135227 9.813539
#> 23 2.048598 -6.0317412 10.128936
#> 24 2.010150 -5.7631079 9.783408
#> 25 3.201960 -4.5302411 10.934161
#> 26 1.493974 -6.3460895 9.334038
#> 27 18.623392 11.1134630 26.133320
#> 28 7.817861 -0.3524618 15.988184
#> 29 18.797743 11.0398095 26.555676
#> 30 5.125182 -2.6982901 12.948653
#> 31 1.807167 -6.3415178 9.955852
#> 32 4.075101 -3.5663732 11.716575
#> 33 12.427273 4.6769673 20.177579
#> 34 3.152303 -4.4463365 10.750943
#> 35 1.293478 -6.8488219 9.435778
#> 36 6.957413 -0.9683646 14.883191
#> 37 18.939770 11.6346562 26.244884
#> 38 7.297365 -0.5228443 15.117575
#> 39 14.674697 6.7673697 22.582024
#> 40 15.520246 7.7948540 23.245638
#> 41 12.405048 4.4262734 20.383823
#> 42 24.918442 17.5864895 32.250394
#> 43 17.301734 9.6310247 24.972444
#> 44 26.864540 19.4240569 34.305024
#> 45 1.603280 -6.2004973 9.407057
#> 46 8.549257 0.4298523 16.668662
#> 47 3.784855 -3.6490992 11.218809
#> 48 14.937188 7.1596394 22.714737
#> 49 24.494371 16.8717764 32.116966
#> 50 1.956122 -6.0097993 9.922043
#> 51 27.204132 19.8157834 34.592480
#> 52 13.574770 6.0621552 21.087384
#> 53 2.576245 -5.1022238 10.254714
#> 54 1.914563 -6.2911949 10.120320
#> 55 7.487318 -0.3755634 15.350200
#> 56 17.136559 9.4820700 24.791047
#> 57 17.014448 9.2249276 24.803968
#> 58 1.337634 -6.4385724 9.113841
#> 59 18.683246 11.3039468 26.062546
#> 60 12.688687 4.7747305 20.602644
#> 61 15.393346 7.8685304 22.918162
#> 62 23.574345 16.0955211 31.053169
#> 63 1.579663 -6.2038712 9.363198
#> 64 17.036054 9.2258934 24.846214
#> 65 28.544693 21.0374571 36.051929
#> 66 1.288539 -6.8107592 9.387838
#> 67 1.140777 -6.9519175 9.233472
#> 68 6.171537 -1.7667540 14.109829
#> 69 3.437062 -4.6208291 11.494953
#> 70 10.789035 3.0605613 18.517508
#> 71 3.615948 -4.3318041 11.563701
#> 72 17.829018 10.5010624 25.156974
#> 73 8.908869 1.3774432 16.440295
#> 74 7.301336 -0.7182311 15.320904
#> 75 2.126375 -6.0821512 10.334901
#> 76 1.660542 -5.8572805 9.178365
#> 77 19.373718 11.6821400 27.065296
#> 78 1.280324 -6.5594020 9.120049
#> 79 16.307787 8.5576064 24.057967
#> 80 15.288219 7.3523742 23.224064
#> 81 17.793902 10.2290340 25.358770
#> 82 3.636393 -3.8379555 11.110742
#> 83 9.731890 1.9565364 17.507243
#> 84 1.843879 -5.9288922 9.616651
#> 85 1.673207 -6.0232268 9.369641
#> 86 7.792596 -0.3486329 15.933825
#> 87 2.326289 -5.6078937 10.260471
#> 88 1.614190 -6.0053643 9.233745
#> 89 2.694977 -4.9535614 10.343516
#> 90 19.431798 12.0731376 26.790459
#> 91 4.044955 -4.0206587 12.110569
#> 92 7.952300 0.1344633 15.770137
#> 93 12.423394 4.4963234 20.350464
#> 94 25.946812 18.4522301 33.441395
#> 95 15.137847 7.2417146 23.033980
#> 96 16.654989 9.0689535 24.241024
#> 97 21.603763 13.8886420 29.318883
#> 98 20.798900 13.5068048 28.090995
#> 99 16.737467 9.3086051 24.166329
#> 100 16.730626 8.7188384 24.742414
augment(spmod, newdata = sulfate_preds, interval = "prediction")
#> Simple feature collection with 100 features and 3 fields
#> Geometry type: POINT
#> Dimension: XY
#> Bounding box: xmin: -2283774 ymin: 582930.5 xmax: 1985906 ymax: 3037173
#> Projected CRS: NAD83 / Conus Albers
#> # A tibble: 100 x 4
#> .fitted .lower .upper geometry
#> * <dbl> <dbl> <dbl> <POINT [m]>
#> 1 1.62 -6.45 9.68 (-1771413 1752976)
#> 2 24.4 16.8 31.9 (1018112 1867127)
#> 3 8.95 1.03 16.9 (-291256.8 1553212)
#> 4 16.5 8.64 24.3 (1274293 1267835)
#> 5 4.93 -2.78 12.6 (-547437.6 1638825)
#> 6 26.8 19.3 34.3 (1445080 1981278)
#> 7 2.87 -5.05 10.8 (-1629090 3037173)
#> 8 14.3 6.77 21.7 (1302757 1039534)
#> 9 1.53 -6.33 9.38 (-1429838 2523494)
#> 10 14.3 6.61 21.9 (1131970 1096609)
#> # ... with 90 more rows
# \donttest{
sulfate$var <- rnorm(NROW(sulfate)) # add noise variable
sulfate_preds$var <- rnorm(NROW(sulfate_preds)) # add noise variable
sprfmod <- splmRF(sulfate ~ var, data = sulfate, spcov_type = "exponential")
predict(sprfmod, sulfate_preds)
#> 1 2 3 4 5 6 7
#> 0.9454275 20.3256222 18.2281829 12.2212795 1.3810121 19.8631422 7.1876770
#> 8 9 10 11 12 13 14
#> 7.9454704 0.8336677 13.0672872 -3.1892785 8.4446253 6.5442407 18.7787205
#> 15 16 17 18 19 20 21
#> 13.8871415 15.5005220 26.8557942 2.2578107 -3.6229680 17.5921390 3.5765248
#> 22 23 24 25 26 27 28
#> 4.5569616 -1.5113169 -0.4263755 -1.3223884 5.8694287 12.9131610 10.4940185
#> 29 30 31 32 33 34 35
#> 39.4824492 2.9003626 -2.0465190 -3.9872107 18.2765349 -3.8704985 6.3853238
#> 36 37 38 39 40 41 42
#> 8.1310436 10.2512472 0.5693266 20.7479217 13.6778628 6.7023790 33.1380560
#> 43 44 45 46 47 48 49
#> 13.7864690 25.1775263 -2.4338986 8.2844704 13.3588427 20.1665807 23.8357553
#> 50 51 52 53 54 55 56
#> -3.5505718 25.6720901 12.7154866 10.0596849 4.6756348 19.9533687 18.2868695
#> 57 58 59 60 61 62 63
#> 12.5973124 -5.4404805 14.5595911 21.7584555 21.7012873 24.5214840 11.4205815
#> 64 65 66 67 68 69 70
#> 18.6506569 38.9204158 9.8582106 5.1101864 2.1120523 2.3116527 19.0546543
#> 71 72 73 74 75 76 77
#> 11.6172971 17.8593750 13.8972157 3.1356734 2.1373544 -1.9391285 20.3017700
#> 78 79 80 81 82 83 84
#> 2.6878419 18.0480650 12.4145141 12.3960673 7.2549168 7.0172272 -4.5550982
#> 85 86 87 88 89 90 91
#> 7.6953132 14.6852843 11.3248981 0.6157642 13.8416768 28.3935562 -2.7093238
#> 92 93 94 95 96 97 98
#> 14.0187140 18.7069706 32.3944434 20.9514243 12.4592965 15.2446538 21.2156343
#> 99 100
#> 28.4389537 12.9176720
# }