model used for prediction (e.g. from caret or tidymodels)
…
further arguments passed to the model-specific predict method
output_names
optional character vector for output variable(s)
keep_bands
logical; keep bands of input data cube, defaults to FALSE, i.e. original bands will be dropped
Details
The model-specific predict method will be automatically chosen based on the class of the provided model. It aims at supporting models from the packages tidymodels, caret, and simple models as from lm or glm.
For multiple output variables or output in form of lists or data.frames, output_names must be provided and match names of the columns / items of the result object returned from the underlying predict method. For example, predictions using tidymodels return a tibble (data.frame) with columns like .pred_class (classification case). This must be explicitly provided as output_names. Similarly, predict.lm and the like return lists if the standard error is requested by the user and output_names hence should be set to c("fit","se.fit").
For more complex cases or when predict expects something else than a data.frame, this function may not work at all.
Note
This function returns a proxy object, i.e., it will not immediately start any computations.
Examples
# create image collection from example Landsat data only# if not already done in other examplesif (!file.exists(file.path(tempdir(), "L8.db"))) { L8_files <-list.files(system.file("L8NY18", package ="gdalcubes"),".TIF", recursive =TRUE, full.names =TRUE)create_image_collection(L8_files, "L8_L1TP", file.path(tempdir(), "L8.db"), quiet =TRUE)}v =cube_view(extent=list(left=388941.2, right=766552.4,bottom=4345299, top=4744931, t0="2018-04", t1="2018-06"),srs="EPSG:32618", nx =497, ny=526, dt="P3M")L8.col =image_collection(file.path(tempdir(), "L8.db"))x = sf::st_read(system.file("ny_samples.gpkg", package ="gdalcubes"))
Reading layer `ny_samples' from data source
`/home/marius/R/x86_64-pc-linux-gnu-library/4.3/gdalcubes/ny_samples.gpkg'
using driver `GPKG'
Simple feature collection with 39 features and 1 field
Geometry type: POINT
Dimension: XY
Bounding box: xmin: 446109.4 ymin: 4376976 xmax: 743200.9 ymax: 4675647
Projected CRS: WGS 84 / UTM zone 18N
raster_cube(L8.col, v) |>select_bands(c("B02","B03","B04","B05")) |>extract_geom(x) -> trainx$FID =rownames(x) train =merge(train, x, by ="FID")train$iswater =as.factor(train$class =="water")log_model <-glm(iswater ~ B02 + B03 + B04 + B05, data = train, family ="binomial")
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
# predict.cubeModel prediction```{r include=FALSE}library(gdalcubes)```## DescriptionApply a trained model on all pixels of a data cube.## Usage```rpredict.cube(object, model, ..., output_names =c("pred"), keep_bands =FALSE)```## Arguments| Argument | Description ||:------------|:----------------------------------|| object | a data cube proxy object (class cube) || model | model used for prediction (e.g. from `caret` or `tidymodels`) || ... | further arguments passed to the model-specific predict method || output_names | optional character vector for output variable(s) || keep_bands | logical; keep bands of input data cube, defaults to FALSE, i.e. original bands will be dropped |## DetailsThe model-specific predict method will be automatically chosen based on the class of the provided model. It aims at supporting models from the packages `tidymodels`, `caret`, and simple models as from `lm` or `glm`. For multiple output variables or output in form of lists or data.frames, `output_names` must be provided and match names of the columns / items of the result object returned from the underlying predict method. For example, predictions using `tidymodels` return a tibble (data.frame) with columns like `.pred_class` (classification case).This must be explicitly provided as `output_names`. Similarly, `predict.lm` and the like return listsif the standard error is requested by the user and `output_names` hence should be set to `c("fit","se.fit")`.For more complex cases or when predict expects something else than a `data.frame`, this function may not work at all.## NoteThis function returns a proxy object, i.e., it will not immediately start any computations.## Examples```{r}# create image collection from example Landsat data only# if not already done in other examplesif (!file.exists(file.path(tempdir(), "L8.db"))) { L8_files <-list.files(system.file("L8NY18", package ="gdalcubes"),".TIF", recursive =TRUE, full.names =TRUE)create_image_collection(L8_files, "L8_L1TP", file.path(tempdir(), "L8.db"), quiet =TRUE)}v =cube_view(extent=list(left=388941.2, right=766552.4,bottom=4345299, top=4744931, t0="2018-04", t1="2018-06"),srs="EPSG:32618", nx =497, ny=526, dt="P3M")L8.col =image_collection(file.path(tempdir(), "L8.db"))x = sf::st_read(system.file("ny_samples.gpkg", package ="gdalcubes"))raster_cube(L8.col, v) |>select_bands(c("B02","B03","B04","B05")) |>extract_geom(x) -> trainx$FID =rownames(x) train =merge(train, x, by ="FID")train$iswater =as.factor(train$class =="water")log_model <-glm(iswater ~ B02 + B03 + B04 + B05, data = train, family ="binomial")raster_cube(L8.col, v) |>select_bands(c("B02","B03","B04","B05")) |>predict(model=log_model, type="response") |>plot(key.pos=1)```