Skip to content
Prev 10500 / 29559 Next

gridded time series analysis

Advait,

When asking for help, please provide a self-contained example whenever
possible, as below.

library(raster)
v1 <- matrix(rep(1:12, 16), nrow=16)
v2 <- matrix(rep(1:12, 16), nrow=16, byrow=TRUE)
b <- brick(ncol=4, nrow=4)
b1 <- setValues(b, v1)
b2 <- setValues(b, v2)
s <- stack(b1, b2)

fun <- function(x) {
    resp = x[1:12]
    expl = x[13:24]
    mdl = lm(resp ~ expl)
    res = c(mdl$coef[1] , mdl$coef[2])
    return(res)
}

# now do:
b2 <- calc(s, fun)

# as an alternative to the above you could do
# but I would not recommend this

datCoef = apply(getValues(s), 1, fun2)
b1 <- setValues(s, t(datCoef))

# I think you were complicating matters with plyr.
# But to get those values in a brick you could do something like
b1 <- setValues(s, as.matrix(datCoef)[,3:4])
# But I am not sure if you would get the values in the correct cells
# (the number matches, but perhaps not the order).

Best, Robert



On Wed, Dec 22, 2010 at 10:39 AM, Advait Godbole
<advaitgodbole at gmail.com> wrote: