isciences / exactextractr

R package for fast and accurate raster zonal statistics
https://isciences.gitlab.io/exactextractr/
274 stars 26 forks source link

Standard deviation #33

Closed tcalegario closed 3 years ago

tcalegario commented 4 years ago

Dear,

Thanks for this package. Its so fast.

There is a way to evaluate standard deviation, range and median with this package?

Thanks a lot

Best wishes

tcalegario commented 4 years ago

exact_extract(raster, poly, function(values, coverage_fraction) sd(values, na.rm=TRUE))

Is it right?

dbaston commented 4 years ago

Is it right?

That will give you the sample standard deviation, not taking cell coverage fractions into account. You may also want to investigate exact_extract(raster, poly, 'stdev') which will give you the population standard deviation, taking cell coverage fractions into account. This avoids passing all of the pixel values to an R function, so it can work on rasters/polygons of any size without excessive memory usage. You'd need to pull the latest version from GitHub for this to work (it's not yet supported in the CRAN version)

You can calculate range from min & max (exact_extract(raster, poly, c('min', 'max'))).

I haven't implemented median directly, since there isn't an exact way to do it without storing all pixel values in memory. But you can use the R function exact_extract(raster, poly, function(values, coverage_fraction) median(values, na.rm)) although it does not take cell coverage fraction into account.

tcalegario commented 4 years ago

Dear dbaston

Thanks a lot.

This package is awesome. I`m sharing with all my worknet.

By GitHub version, you mean that available in this link?

https://github.com/isciences/exactextractr

Best regards

A.

dbaston commented 4 years ago

Thanks! Yes, you can install it from GitHub with remotes::install_github('isciences/exactextractr'). Eventually I'll update the version on CRAN to include the standard deviation.

tcalegario commented 4 years ago

I installed it but didn`t recognize the function "stdev"

install.packages("isciences/exactextractr")

exact_extract(raster, poli, fun = c("mean", "stdev","max", "min"))

As result:

r in CPP_stats(x, weights, wkb, fun, max_cells_in_memory) : Unknown stat: stdev In addition: Warning message: In .local(x, y, ...) : Polygons transformed to raster CRS (EPSG:NA)

dbaston commented 4 years ago

install.packages installs from CRAN; use remotes::install_github('isciences/exactextractr')

tcalegario commented 4 years ago

LoL!

Noob mistake.

Thanks man!!

tcalegario commented 4 years ago

I uninstalled exactextract from R and installed it again using your code (version 0.4.0).

Result was the same. :/

Do you know what may be the problem?