[R-sig-Geo] Problems with NA values and lm() in calc() in raster package
Lars Dalby
lars.dalby at gmail.com
Thu Apr 12 19:54:14 CEST 2012
Hi Robert
Thanks for your reply!
Your suggestion solved the problem, because in my case I do have cells where
there is NA in all layers.
But, I'm still a little puzzled about why it is not running in the case
where there is NA in a cell in only one of the layers.
lm() normally works in a case like this, so I am not sure why it is
returning an error here?
require(raster)
# From help to calc function
# create data
r <- raster(nrow=10, ncol=10)
s1 <- s2<- list()
for (i in 1:12) { s1[i] <- setValues(r, rnorm(ncell(r), i, 3) )
s2[i] <- setValues(r, rnorm(ncell(r), i, 3) )}
s1 <- stack(s1)
s2 <- stack(s2)
s <- stack(s1, s2)
r <- setValues(r, rnorm(ncell(r), 1, 3) )
plot(r)
r[1] <- NA #Setting top left corner to NA
plot(r)
s <- stack(s, r) # Now only one of the rasters has a NA value
# So this is equivalent to doing a lm with one NA value:
a <- rnorm(10, 5, 2)
b <- c(1:9, NA)
lm(a~b) # This seem to work ok.
# But with the brick:
fun <- function(x) { lm(x ~ time)$coefficients[2] }
time <- nlayers(s)
x5 <- calc(s, fun) # Error in model.frame.default(formula = x ~ time,
drop.unused.levels = TRUE) : variable lengths differ (found for 'time')
Best regards
Lars
Lars Dalby
Msc. PhD student
Ecoinformatics & Biodiversity
Department of Bioscience, Aarhus University
Denmark
--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Problems-with-NA-values-and-lm-in-calc-in-raster-package-tp7455990p7460272.html
Sent from the R-sig-geo mailing list archive at Nabble.com.
More information about the R-sig-Geo
mailing list