Skip to contents

Predicted values and intervals based on a fitted model object.

Usage

# S3 method for class 'splm'
predict(
  object,
  newdata,
  se.fit = FALSE,
  interval = c("none", "confidence", "prediction"),
  level = 0.95,
  local,
  ...
)

# S3 method for class 'spautor'
predict(
  object,
  newdata,
  se.fit = FALSE,
  interval = c("none", "confidence", "prediction"),
  level = 0.95,
  local,
  ...
)

# S3 method for class 'splm_list'
predict(
  object,
  newdata,
  se.fit = FALSE,
  interval = c("none", "confidence", "prediction"),
  level = 0.95,
  local,
  ...
)

# S3 method for class 'spautor_list'
predict(
  object,
  newdata,
  se.fit = FALSE,
  interval = c("none", "confidence", "prediction"),
  level = 0.95,
  local,
  ...
)

# S3 method for class 'splmRF'
predict(object, newdata, local, ...)

# S3 method for class 'spautorRF'
predict(object, newdata, local, ...)

# S3 method for class 'splmRF_list'
predict(object, newdata, local, ...)

# S3 method for class 'spautorRF_list'
predict(object, newdata, local, ...)

# S3 method for class '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 class '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 class '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 class '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 by formula(object) and all variables representing coordinates. If an sf object, newdata must contain all variables used by formula(object) and coordinates are obtained from the geometry of newdata. 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 to TRUE or FALSE based on the observed data sample size (i.e., sample size of the fitted model object) – if the sample size exceeds 10,000, local is set to TRUE, otherwise it is set to FALSE. This default behavior occurs because main computational burden of the big data approximation depends almost exclusively on the observed data sample size, not the number of predictions desired (which we feel is not intuitive at first glance). If local is FALSE, 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. If method = "all", all observations are used and size is ignored. If method = "distance", the size data observations closest (in terms of Euclidean distance) to the observation requiring prediction are used. If method = "covariance", the size 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 using splm() or spglm().

  • size: The number of data observations to use when method is "distance" or "covariance". The default is 100. Only used with models fit using splm() or spglm().

  • parallel: If TRUE, parallel processing via the parallel package is automatically used. This can significantly speed up computations even when method = "all" (i.e., no big data approximation is used), as predictions are spread out over multiple cores. The default is FALSE.

  • ncores: If parallel = 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. If local is TRUE, defaults for local are chosen such that local is transformed into list(size = 100, method = "covariance", parallel = FALSE).

...

Other arguments. Only used for models fit using splmRF() or spautorRF() where ... indicates other arguments to ranger::predict.ranger().

type

The scale (response or link) of predictions obtained using spglm() or spgautor objects.

newdata_size

The size value for each observation in newdata 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() or spgautor(). The default is TRUE.

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 above

  • se.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 × 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)
#> # ℹ 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 
#> -2.3978573 21.8343114 12.7840847 10.4363763 20.0909914 33.7915482  9.9820154 
#>          8          9         10         11         12         13         14 
#> 22.5489027 -1.7436025 22.3953771  7.2975696 28.4473955  9.5337326 21.4004095 
#>         15         16         17         18         19         20         21 
#> 12.1407819  5.9417736  9.7618139  5.9873147  6.2303639 22.4295190  3.8430066 
#>         22         23         24         25         26         27         28 
#>  8.5377398 -1.8077164 -4.7835386 12.7099227 -2.8355000 29.2371207  7.5842707 
#>         29         30         31         32         33         34         35 
#>  9.6848971 -3.2264817 -5.4195718 -2.1578651 10.8135794  3.1404403  7.7137885 
#>         36         37         38         39         40         41         42 
#> -4.9283116 14.6012469  3.1032150 20.2128598  5.7388609  5.5101986 25.5446490 
#>         43         44         45         46         47         48         49 
#>  9.4530052 19.7454231  2.2045568  2.6570397  3.5545005 14.8868615 38.5217510 
#>         50         51         52         53         54         55         56 
#>  7.0153391 36.5458937  6.9518827  8.3651705 -4.9088181  4.9496796 20.9868627 
#>         57         58         59         60         61         62         63 
#> 20.7950217  9.0867983  9.2450115 18.3566363  7.7829309 14.7252566  5.4013552 
#>         64         65         66         67         68         69         70 
#> 12.3279112 33.4559282  4.4396752  0.1663599  0.6860726 16.3821799  2.9182475 
#>         71         72         73         74         75         76         77 
#>  2.0347433 21.6360278  8.6530528  4.4706933 12.1503975  8.5165676 18.3106074 
#>         78         79         80         81         82         83         84 
#>  4.8423632  7.9196940  2.5108236 13.4197102  2.2176850  8.6250756 12.6967600 
#>         85         86         87         88         89         90         91 
#> -4.9463766  2.3343730  7.6698240 -3.5530521 -3.3407028 15.3038129  3.4581906 
#>         92         93         94         95         96         97         98 
#> 14.3959609 16.1006814 22.7670679 13.5520810 10.7217827 13.1812187 11.9919883 
#>         99        100 
#> 15.2375168 17.7198041 
# }