Skip to content

Commit

Permalink
add prcomp method
Browse files Browse the repository at this point in the history
  • Loading branch information
edzer committed Mar 18, 2024
1 parent 0815fe1 commit 5f20b77
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 138 deletions.
55 changes: 28 additions & 27 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -90,39 +90,40 @@ VignetteBuilder: knitr
Encoding: UTF-8
RoxygenNote: 7.3.1
Collate:
'init.R'
'stars.R'
'read.R'
'sf.R'
'aggregate.R'
'cubble.R'
'datasets.R'
'dimensions.R'
'values.R'
'plot.R'
'tidyverse.R'
'transform.R'
'ops.R'
'write.R'
'raster.R'
'sp.R'
'spacetime.R'
'extract.R'
'factors.R'
'geom.R'
'gt.R'
'init.R'
'intervals.R'
'mdim.R'
'mosaic.R'
'ncdf.R'
'ncproxy.R'
'OpenStreetMap.R'
'ops.R'
'plot.R'
'prcomp.R'
'proxy.R'
'factors.R'
'rasterize.R'
'raster.R'
'read.R'
'rotate.R'
'sample.R'
'sf.R'
'spacetime.R'
'spatstat.R'
'sp.R'
'stars.R'
'subset.R'
'tidyverse.R'
'tile.R'
'transform.R'
'values.R'
'warp.R'
'aggregate.R'
'write.R'
'xts.R'
'intervals.R'
'geom.R'
'mosaic.R'
'spatstat.R'
'OpenStreetMap.R'
'sample.R'
'extract.R'
'datasets.R'
'tile.R'
'mdim.R'
'cubble.R'
'gt.R'
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ S3method(merge,stars_proxy)
S3method(plot,nc_proxy)
S3method(plot,stars)
S3method(plot,stars_proxy)
S3method(prcomp,stars)
S3method(prcomp,stars_proxy)
S3method(predict,stars)
S3method(predict,stars_proxy)
S3method(print,dimensions)
Expand Down Expand Up @@ -234,6 +236,7 @@ importFrom(stats,aggregate)
importFrom(stats,complete.cases)
importFrom(stats,na.omit)
importFrom(stats,na.pass)
importFrom(stats,prcomp)
importFrom(stats,predict)
importFrom(stats,quantile)
importFrom(stats,runif)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# version 0.6-5

* add `prcomp()` methods for `stars` and `stars_proxy` objects, working on attributes or last dimension

* `st_rasterize()` with `align=TRUE` returns `NA` values where there are no data; #668

* `read_mdim()` reads tables with composity type, returning a `data.frame` in that case; #659
Expand Down
2 changes: 1 addition & 1 deletion R/init.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' @importFrom graphics image.default image par plot title box text axis plot.new plot.window rasterImage layout lcm contour hist strwidth
#' @importFrom grDevices dev.capabilities dev.size grey rgb col2rgb cm
#' @importFrom utils head tail setTxtProgressBar txtProgressBar packageVersion methods modifyList
#' @importFrom stats na.omit runif aggregate setNames predict quantile var complete.cases na.pass time
#' @importFrom stats na.omit runif aggregate setNames predict quantile var complete.cases na.pass time prcomp
#' @importFrom tools file_ext
#' @importFrom methods as slotNames new slot
#' @importFrom abind abind adrop asub
Expand Down
30 changes: 30 additions & 0 deletions R/prcomp.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#' @name prcomp
#' @param downsample see \link{st_as_stars}
#' @export
prcomp.stars_proxy = function(x, ..., downsample = 0) {
prcomp(st_as_stars(x, downsample = downsample), ...)
}

#' Principle components of stars object
#'
#' Compute principle components of stars object
#' @param x object of class `stars` or `stars_proxy`
#' @param quiet logical; if `TRUE`, suppress message that PCs will be computed on last dimension; see details
#' @param ... see \link[stats]{prcomp}
#' @details if `x` has only one attribute, principle components will be computed in the space of the last dimension of `x`
#' to predict PC scores into a `stars` object, use \link{predict.stars}; see example below
#' @name prcomp
#' @value object of class `prcomp`, see \link[stats]{prcomp}
#' @export
#' @examples
#' l7 = split(st_as_stars(L7_ETMs), 3) # use bands as features
#' l7 |> prcomp() |> plot()
#' l7 |> prcomp() |> predict(l7, model = _) |> merge() |> plot()
prcomp.stars = function(x, ..., quiet = FALSE) {
if (length(x) == 1) {
if (!quiet)
message(paste0("computing PCs over dimension `", tail(names(dim(x)), 1), "'"))
x = split(x, length(dim(x)))
}
prcomp(as.data.frame(lapply(x, as.vector)), ...)
}
12 changes: 6 additions & 6 deletions man/c.stars.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions man/plot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions man/prcomp.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions man/predict.stars.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions man/print_stars.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions man/redimension.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5f20b77

Please sign in to comment.