Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorporar operaciones entre 2 o más pilas SpatRaster #1029

Closed
djwillichile opened this issue Feb 22, 2023 · 4 comments
Closed

Incorporar operaciones entre 2 o más pilas SpatRaster #1029

djwillichile opened this issue Feb 22, 2023 · 4 comments

Comments

@djwillichile
Copy link

Estoy intentando realizar operaciones cómo correlación o regresión entre dos series de tiempo (pilas SpatRaster). En este caso, 16 capas de NDVI y 16 capas de Precipitación. Para el caso de la correlación el resultado esperado son dos capas SpatRaster (coeficiente de correlación y valor p), mientras que, para la regresión, el resultado esperado son tres capas SpatRaster (intercepto, pendiente y coeficiente de determinación). ¿Hay alguna forma de implementar estas operaciones?

@djwillichile
Copy link
Author

entoendo que antes para esto se utilizaba la función overlay, pero ya no encuentro esta función

S4 method for SpatRaster,SpatRaster

overlay(x, y, fun, ..., filename="", overwrite=FALSE, wopt=list())

@rhijmans
Copy link
Member

This site is to report bugs and request new functionality. For general questoins I suggest you use stackoverflow.

You can do something like this:

library(terra)
s <- rast(system.file("ex/logo.tif", package="terra"))   
set.seed(1)
r <- setValues(s, runif(size(s), 100, 200))

xcor = app(c(s, r), fun=\(x)cor(x[1:3], x[4:6]))
xreg = app(c(s, r), fun=\(x) coefficients(lm(x[4:6] ~ x[1:3])))

But seeing this, I think I will add a new method capp that would allow for

capp(s, r, fun=cor)
capp(s, r, fun=\(x,y) coefficients(lm(y~x)))

@djwillichile
Copy link
Author

djwillichile commented Feb 22, 2023 via email

@rhijmans
Copy link
Member

This can now be done more elegantly with lapp and a SpatRasterDataset.

library(terra)
s <- rast(system.file("ex/logo.tif", package="terra"))   
set.seed(1)
r <- setValues(s, runif(size(s), 100, 200))

# make a SpatRasterDataset
d <- sds(s,r)

xc <- lapp(d, cor)
xr <- lapp(d, fun=\(x, y) coefficients(y ~ x))

# the latter can now also be done with 
rr <- regress(r, s)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants