0

I am facing some problems with NoData/NA while creating RSF-plot in R using raster data imported from ArcGIS 10.

My rasters are
a) Attributes of rivers (reclassified in ArcGIS 10 to0, 1 and NoData outside the rivers) b) Distances from houses

I need the final map to be restricted to the rivers but somehow all the surrounding area is included in the resulting map, showing even highly qualified areas off the rivers. When plotting the single rasters of the rivers, they seem fine, with no hidden data outside the river

I guess the mistake somewhere happens where I sum the rasters. I assumed that when I sum rasters, only cells with no-NA data are included, but that might be my big mistake. How do I exclude therefore the areas outside the rivers in the easiest and effective way?

Below you find the script I am using.

# Import Raster to R using the library(raster)
Raster1 <-raster("Raster1")
Raster2 <-raster("Raster2")
Raster3 <-raster("Raster3")
RasterD <-raster("RasterD")

# Create a raster stack and do raster multiplications
model_stack<-raster::stack(Raster1,Raster2, Raster3, RasterD)

# assign the beta coefficients (stemming from glmer)
mean_search<-fixef(mod) 

# multiply the rasters by the coefficients
model_stack_coef<-model_stack*mean_search

# Sum over all rasters - creates a single raster
pre_ssf <- calc(model_stack_coef, fun=sum)

# multiply by the power of exp
ssf<-calc(pre_ssf,fun=function(x){exp(x)})

# standardize between 0 and 1
ssf_p<-calc(ssf,fun=function(x){(x-min(x))/(max(x)-min(x))})
Irene
  • 1
  • this suggests that your raster do not have `NA` values but are perhaps zero. If any of the cells in `model_stack` has an `NA`, the result for that cell will also be `NA`. Your visual assessment is probably wrong. Try `plot(Raster1); click(model_stack)` (and click on the plot to see values) – Robert Hijmans Oct 09 '15 at 22:09
  • Thank you, Robert. It a very handy command! My rasters seem fine but now I get a blank map after standardization. I tried to trim the outliers using "ssf_trim<-calc(ssf,fun=function(x) ifelse(x>quantile(x,0.995),quantile(x,0.995),x)})" but I get an error there 'Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) : cannot use this function'. I assume it has to do with the NA's, too? – Irene Oct 10 '15 at 13:04
  • Your function does not work because your "}" is not matched. – Robert Hijmans Oct 10 '15 at 23:11
  • Sorry, there was a mistake in the copy paste process: it is "ssf_trim<-calc(ssf,fun=function(x){ifelse(x>quantile(x,0.995),quantile(x,0.995),x)})", which gives the error – Irene Oct 11 '15 at 09:46

1 Answers1

0

You can check your function for all NA values:

fun=function(x){ifelse(x>quantile(x,0.995),quantile(x,0.995),x)}
fun(c(NA,NA,NA))
#Error in quantile.default(x, 0.995) : 
#  missing values and NaN's not allowed if 'na.rm' is FALSE

And fix it:

fun=function(x){ifelse(x>quantile(x,0.995,na.rm=T),quantile(x,0.995, na.rm=T),x)}
fun(c(NA,NA,NA))
# [1] NA NA NA
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63