# R: aggregate raster with 'mode' function - how does it work?

I want to aggregate a very fine resolution raster at various coarser resolutions (that represents many 1000s of categorical polygons).

The aggregate{raster} function is what i've traditionally used, the factor arguement reduces the resolution while the 'fun' allows me to sum, max or whatever.

However, when using fun=modal, how does aggregate behave? If I simply reduce the resolution by a factor of 2 (i.e. quarter the total number of cells) how does aggregate work with new multimodal cells?

example code:

``````ras <- raster(nrows=10, ncols=10,xmn=0, xmx=10, ymn=0, ymx=10)
ras[1:50] <- 1
ras[51:100] <- 2
x1 <- c(9,10,20,34,54,56,67,88,99)
ras[x1] <- NA
plot(ras)
grid(nx = 10, ny = 10, col = "lightgray", lty = "dotted",lwd = 1)

ras2 <- aggregate(ras,fact=2,expand=FALSE,fun=modal,na.rm=T)
x11()
plot(ras2)
grid(nx = 10, ny = 10, col = "lightgray", lty = "dotted",lwd = 1)
``````

Ideally, when reducing by a factor, if there is a multimodal result I'd like aggregate to randomly assign the new cell one of the modal values and not always choose the same (if that's indeed what it does). Larger factors such as 5 etc are less likely to cause this issue but it is still entirely possible.

FURTHERMORE: in the aggregate function above, na.rm is set to TRUE because i dont want every single NA cell to set the new, coarser, cell automatically to NA. However in the top right of the data, that new cell should really be NA as it has 3 smaller NA values. Do i just need to re-assign NA to 999 before running? and how will it work if there are 2 NA cells and 2 cells of another value?

• How many distinct categories are there? With small numbers of categories there exist convenient, flexible, and reasonably fast solutions. – whuber Jan 19 '16 at 23:58
• @ whuber, 9 distinct categories (that are themselves aggregation categories of about 120 categories) but i'd like the flexibility to re-classify the original 120 to whatever i like in the future – Sam Jan 20 '16 at 10:04

Ideally, when reducing by a factor, if there is a multimodal result I'd like aggregate to randomly assign the new cell one of the modal values and not always choose the same (if that's indeed what it does).

That is not what it does. See `?modal` and the `ties` argument.

Your question is really about the `modal` function which you pass on to `aggregate` (both in package `raster`). So read the help file of `modal` and pick the arguments you like to make it behave how you want it to. If you cannot do that, find a better one elsewhere, or write your own.

The default behavior of `modal` is to break ties randomly, as illustrated here:

``````set.seed(9)
table(sapply(1:1000, function(i) modal(c(1,1,2,2))))

#  1   2
#507 493
``````

You have a further question about `na.rm` stating that

in the top right of the data, that new cell should really be NA as it has 3 smaller NA values.

I suppose you mean is that there are 3 `NA`s and 1 `1` such that `NA` should be the mode. Perhaps that should be allowed as an option, but currently `NA` itself cannot be the modal value. The workaround you propose should be OK.

• thanks for your answer, I had not looked at the modal help page, but about every other help page i could find! excellent, i had a pretty good feeling modal's default is random but now I know, cheers – Sam Jan 20 '16 at 20:13
• revisiting this problem, do you know of any methods to pass a Random Rule to raster::aggregate? i.e. choose a random cell within the aggregation process, instead of mean/mode etc – Sam Jun 3 '16 at 10:30
• That is a different question. Probably something like `function(x, ...) sample(x, 1)` perhaps with additional logic for NA values – Robert Hijmans Aug 23 '16 at 9:41
• A workaround to get "NA" as the modal value could be to assign all NAs to a large/small integer value (outside dataset values), run `aggregate` and `modal` and then substitute back `NA` for the dummy integer value. Does that sound reasonable? – user3386170 Mar 29 '18 at 17:45

In a bimodal distribution the fun=modal argument will result in assigning the value with the peak frequency in the distribution. Not sure what other behavior you expect?

Statistically speaking, I do not see any support for randomly selecting a different peak in the distribution just because the distribution is bimodal. The only way I can see justifying this is if a peak in the distribution is with a certain range of the mode, eg., within 1% of the mode. This is, in essence, identifying modes that are equivalent, given some measure of error. However, if the value of the mode is, say 100 and the next peak in a bimodal distribution is 50 it makes no sense to randomly select between the two values, the mode is 100 period. This same logic applies to multimodal distributions.

If you want to account for a specific outcome in a bimodal distribution you will have to write your own function and pass it to raster::aggregate but this may be tricky. I would also point out that rarely are peaks in a multimodal distribution equivalent.

If you are consistently expecting a bimodal distribution you may want to consider that you are in fact dealing with a binomial process, which is quite unlikely considering your description of "many 1000s of categorical polygons". Even though your data is nominal, with that many values, it may be behaving as a continuous process and a mode function that is intended for nominal data may be quite sensitive to the binning strategy used to identify modes and could erroneously identify additional peaks. One should note that approaches for identifying modes in continuous data are different than nominal (ie., density or spline estimates verses histogram frequency approaches).

• @Jeffery Evans, thanks for your comments. But i believe my bimodal data does have equal frequencies if you are only aggregating by a factor of 2? i.e. 2 cells of one value and 2 cells of another value – Sam Jan 20 '16 at 10:10