[This article was first published on MeanMean, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
During my dissertation, I spent a lot of time working on spatial kernel estimates.
Where spatial kernel estimates are defined as a convolution of a spatial suppport < svg xmlns:xlink="http://www.w3.org/1999/xlink" width="9.034ex" height="2.509ex" style="vertical-align: -0.671ex;" viewBox="0 -791.3 3889.6 1080.4" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-73">< use x="469" y="0" xlink:href="#MJMAIN-2C">< g transform="translate(914,0)">< use x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="663" y="-213" xlink:href="#MJMAIN-30">< use x="2115" y="0" xlink:href="#MJMAIN-2208">< use x="3061" y="0" xlink:href="#MJMATHI-44"> ,
< svg xmlns:xlink="http://www.w3.org/1999/xlink" width="32.164ex" height="5.676ex" style="vertical-align: -2.338ex;" viewBox="0 -1437.2 13848.4 2443.8" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMAIN-28">< use x="389" y="0" xlink:href="#MJMATHI-66">< use x="1162" y="0" xlink:href="#MJMAIN-2217">< use x="1884" y="0" xlink:href="#MJMATHI-67">< use x="2365" y="0" xlink:href="#MJMAIN-29">< use x="2754" y="0" xlink:href="#MJMAIN-28">< g transform="translate(3144,0)">< use x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="663" y="-213" xlink:href="#MJMAIN-30">< use x="4067" y="0" xlink:href="#MJMAIN-29">< use x="4735" y="0" xlink:href="#MJMAIN-3D">< use x="5791" y="0" xlink:href="#MJSZ2-222B">< use x="6902" y="0" xlink:href="#MJMATHI-66">< use x="7453" y="0" xlink:href="#MJMAIN-28">< use x="7842" y="0" xlink:href="#MJMATHI-73">< use x="8312" y="0" xlink:href="#MJMAIN-29">< use x="8701" y="0" xlink:href="#MJMATHI-67">< use x="9182" y="0" xlink:href="#MJMAIN-28">< g transform="translate(9571,0)">< use x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="663" y="-213" xlink:href="#MJMAIN-30">< use x="10717" y="0" xlink:href="#MJMAIN-2212">< use x="11717" y="0" xlink:href="#MJMATHI-73">< use x="12187" y="0" xlink:href="#MJMAIN-29">< use x="12576" y="0" xlink:href="#MJMATHI-64">< use x="13100" y="0" xlink:href="#MJMATHI-73">< use x="13569" y="0" xlink:href="#MJMAIN-2E">
A simple example of this estimate is a Gaussian filter or blur in more common parlance.
In the Guassian filter, < svg xmlns:xlink="http://www.w3.org/1999/xlink" width="1.116ex" height="2.009ex" style="vertical-align: -0.671ex;" viewBox="0 -576.1 480.5 865.1" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-67"> is the normal density function < svg xmlns:xlink="http://www.w3.org/1999/xlink" width="1.385ex" height="2.509ex" style="vertical-align: -0.671ex;" viewBox="0 -791.3 596.5 1080.4" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-3D5">, with the location parameter < svg xmlns:xlink="http://www.w3.org/1999/xlink" width="6.645ex" height="2.176ex" style="vertical-align: -0.838ex;" viewBox="0 -576.1 2861 936.9" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-3BC">< use x="881" y="0" xlink:href="#MJMAIN-3D">< g transform="translate(1937,0)">< use x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="663" y="-213" xlink:href="#MJMAIN-30"> and scale parameter < svg xmlns:xlink="http://www.w3.org/1999/xlink" width="1.33ex" height="1.676ex" style="vertical-align: -0.338ex;" viewBox="0 -576.1 572.5 721.6" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-3C3"> equal to the bandwidth < svg xmlns:xlink="http://www.w3.org/1999/xlink" width="1.339ex" height="2.176ex" style="vertical-align: -0.338ex;" viewBox="0 -791.3 576.5 936.9" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-68">.
The kernelDensityEstimates package provides a means to apply these estimates to raster S4 objects from the
raster R package.
Each function preserves NA values, and uses OpenMP for acceleration.
However, all processing is done in memory, so there is no support for extremely large images.
The rasterKernelEstimates R package is currently not on CRAN, but will be after the documentation is cleaned up.
Furthermore, the implementation of the kernels is currently non-optimal, and a fair bit of work can be done to speed them up a bit more.
For those interested in helping out, the code is on my github page.
A short example of each function is provided below with some basic performance metrics.
All timings are done on a Late 2013 MacBook Pro 13″ with a dual-core 2.4Ghz i5 running R 3.3.1 compiled under gcc 6.1.0.
The function rasterLocalSums calculates a weighted sum of pixel values in a spatial neighborhood defined by the matrix W,
< svg xmlns:xlink="http://www.w3.org/1999/xlink" width="21.865ex" height="6.009ex" style="vertical-align: -3.505ex;" viewBox="0 -1078.4 9414 2587.3" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-66">< use x="550" y="0" xlink:href="#MJMAIN-28">< g transform="translate(940,0)">< use x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="663" y="-213" xlink:href="#MJMAIN-30">< use x="1863" y="0" xlink:href="#MJMAIN-29">< use x="2530" y="0" xlink:href="#MJMAIN-3D">< g transform="translate(3586,0)">< use x="75" y="0" xlink:href="#MJSZ2-2211">< g transform="translate(0,-1117)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="469" y="0" xlink:href="#MJMAIN-2208">< g transform="translate(803,0)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-53">< use transform="scale(0.574)" x="755" y="-243" xlink:href="#MJMAIN-30">< use x="5349" y="0" xlink:href="#MJMATHI-77">< use x="6065" y="0" xlink:href="#MJMAIN-28">< use x="6455" y="0" xlink:href="#MJMATHI-73">< use x="6924" y="0" xlink:href="#MJMAIN-29">< use x="7314" y="0" xlink:href="#MJMATHI-78">< use x="7886" y="0" xlink:href="#MJMAIN-28">< use x="8276" y="0" xlink:href="#MJMATHI-73">< use x="8745" y="0" xlink:href="#MJMAIN-29">< use x="9135" y="0" xlink:href="#MJMAIN-2E">
This is similar to the raster library function focal when fun is not specified.
In fact, the results are identical when padding is used in the focal call.
library(raster)
library(rasterKernelEstimates)
set.seed(100)
n <- 500
# create a raster object
r <- raster::raster( matrix(rnorm(n^2),n,n))
# create a weight matrix
W <- raster::focalWeight(r,c(1,0.04),type='Gauss')
# apply the weight with rasterKernelEstimates
run.time <- proc.time()
rLocalKDE1 <- rasterLocalSums(r,W)
print(proc.time() - run.time)
## user system elapsed
## 0.975 0.004 0.325
# apply the weight with the raster packages focal
run.time <- proc.time()
rLocalKDE2 <- raster::focal(r,W,pad=TRUE)
print(proc.time() - run.time)
## user system elapsed
## 0.667 0.008 0.678
# plot original image
plot(r)
# plot the smoothed image
plot(rLocalKDE1)
# print out the max abs difference
print(
sprintf(
"The maximum absolute difference = %f.",
max(abs(values(rLocalKDE1) - values(rLocalKDE2)
),na.rm = T))
)
## [1] "The maximum absolute difference = 0.000000."
rasterLocalQuantiles
The function rasterLocalQuantiles calculates the < svg xmlns:xlink="http://www.w3.org/1999/xlink" width="2.865ex" height="2.843ex" style="vertical-align: -0.671ex;" viewBox="0 -934.9 1233.7 1223.9" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-71">< g transform="translate(464,362)">< use transform="scale(0.707)" xlink:href="#MJMAIN-74">< use transform="scale(0.707)" x="389" y="0" xlink:href="#MJMAIN-68"> quantile of pixel values in a spatial neighborhood defined by the matrix W;
< svg xmlns:xlink="http://www.w3.org/1999/xlink" width="53.501ex" height="4.843ex" style="vertical-align: -1.838ex;" viewBox="0 -1293.7 23035 2085" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-66">< use x="550" y="0" xlink:href="#MJMAIN-28">< g transform="translate(940,0)">< use x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="663" y="-213" xlink:href="#MJMAIN-30">< use x="1863" y="0" xlink:href="#MJMAIN-29">< use x="2530" y="0" xlink:href="#MJMAIN-3D">< g transform="translate(3586,0)">< use xlink:href="#MJSZ2-7B">< use x="667" y="0" xlink:href="#MJMATHI-61">< use x="1197" y="0" xlink:href="#MJMATHI-72">< use x="1648" y="0" xlink:href="#MJMATHI-67">< use x="2129" y="0" xlink:href="#MJMATHI-6D">< use x="3007" y="0" xlink:href="#MJMATHI-61">< g transform="translate(3537,0)">< use x="0" y="0" xlink:href="#MJMATHI-78">< g transform="translate(572,-187)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-78">< use transform="scale(0.707)" x="572" y="0" xlink:href="#MJMAIN-28">< use transform="scale(0.707)" x="962" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="1431" y="0" xlink:href="#MJMAIN-29">< use transform="scale(0.707)" x="1821" y="0" xlink:href="#MJMAIN-3A">< use transform="scale(0.707)" x="2099" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="2569" y="0" xlink:href="#MJMAIN-2208">< g transform="translate(2288,0)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-53">< use transform="scale(0.574)" x="755" y="-243" xlink:href="#MJMAIN-30">< use x="7289" y="0" xlink:href="#MJMATHI-50">< use x="8041" y="0" xlink:href="#MJMATHI-72">< g transform="translate(8659,0)">< use x="0" y="0" xlink:href="#MJMAIN-28">< use x="389" y="0" xlink:href="#MJMATHI-58">< use x="1242" y="0" xlink:href="#MJMAIN-28">< use x="1631" y="0" xlink:href="#MJMATHI-73">< use x="2101" y="0" xlink:href="#MJMAIN-29">< use x="2768" y="0" xlink:href="#MJMAIN-3C">< use x="3824" y="0" xlink:href="#MJMATHI-78">< use x="4397" y="0" xlink:href="#MJMAIN-28">< use x="4786" y="0" xlink:href="#MJMATHI-73">< use x="5256" y="0" xlink:href="#MJMAIN-29">< use x="5645" y="0" xlink:href="#MJMAIN-29">< use x="14972" y="0" xlink:href="#MJMAIN-2264">< g transform="translate(15750,0)">< g transform="translate(397,0)">< rect stroke="none" width="450" x="0" y="220">< use x="580" y="714" xlink:href="#MJMATHI-71">< g transform="translate(60,-687)">< use xlink:href="#MJMAIN-31">< use x="500" y="0" xlink:href="#MJMAIN-30">< use x="1001" y="0" xlink:href="#MJMAIN-30">< use x="17890" y="0" xlink:href="#MJMAIN-2C">< use x="18335" y="-1" xlink:href="#MJSZ2-7D">< use x="22756" y="0" xlink:href="#MJMAIN-2C">
where < svg xmlns:xlink="http://www.w3.org/1999/xlink" width="4.88ex" height="2.843ex" style="vertical-align: -0.838ex;" viewBox="0 -863.1 2101 1223.9" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-58">< use x="852" y="0" xlink:href="#MJMAIN-28">< use x="1242" y="0" xlink:href="#MJMATHI-73">< use x="1711" y="0" xlink:href="#MJMAIN-29"> is a random variable on the support of < svg xmlns:xlink="http://www.w3.org/1999/xlink" width="2.479ex" height="2.509ex" style="vertical-align: -0.671ex;" viewBox="0 -791.3 1067.4 1080.4" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-53">< use transform="scale(0.707)" x="867" y="-213" xlink:href="#MJMAIN-30">, a spatial neighborhood about point < svg xmlns:xlink="http://www.w3.org/1999/xlink" width="2.145ex" height="2.009ex" style="vertical-align: -0.671ex;" viewBox="0 -576.1 923.4 865.1" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="663" y="-213" xlink:href="#MJMAIN-30">.
This result should generally be reproducible using the raster library function focal when fun is specified as quantile.
Because quantiles do not necessarily fall exactly on order statistics there is a need to map quantiles between a function of the order statistics.
In the rasterLocalQuantiles function this mapping is done to the closest odd-valued-index order statistic.
E.g. if the quantile falls between the first and second order statistic.
This approach matches the inverse empirical cdf transform used by the type=1 transform in the stats::quantile function.
Note that the quantile function applied to raster objects calculates the quantile from all pixels, not local quantiles.
As far as applications go, this function is particularly useful when working with data with a large number of outliers.
library(raster)
library(rasterKernelEstimates)
set.seed(100)
n <- 100
# create a raster object with extreme values
r <- raster::raster( matrix(rcauchy(n^2),n,n))
# create a weight matrix
W <- matrix(1,nrow=3,ncol=3)
# calculate local median
run.time <- proc.time()
rLocalKDE1 <- rasterLocalQuantiles(r,W,q=30)
print(proc.time() - run.time)
## user system elapsed
## 0.018 0.001 0.010
# calculate local median
run.time <- proc.time()
rLocalKDE2 <- raster::focal(
r, W, pad=TRUE,
fun=function(x) stats::quantile(x,probs=0.3,na.rm=T,type=1),
padValue=NA)
print(proc.time() - run.time)
## user system elapsed
## 1.245 0.010 1.268
# plot boxplot of original image
boxplot(r)
# plot the smoothed image
plot(rLocalKDE1)
# print out the max abs difference
print(
sprintf(
"The maximum absolute difference = %f.",
max(abs(values(rLocalKDE1) - values(rLocalKDE2)
),na.rm = T))
)
## [1] "The maximum absolute difference = 0.000000."
rasterLocalMoments
The function rasterLocalMoments calculates up to the second moment over the weighted matricies Wmu and Wvar,
< svg xmlns:xlink="http://www.w3.org/1999/xlink" width="26.087ex" height="7.176ex" style="vertical-align: -3.005ex;" viewBox="0 -1796 11231.7 3089.6" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-3BC">< use x="79" y="20" xlink:href="#MJMAIN-5E">< use x="603" y="0" xlink:href="#MJMAIN-28">< g transform="translate(993,0)">< use x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="663" y="-213" xlink:href="#MJMAIN-30">< use x="1916" y="0" xlink:href="#MJMAIN-29">< use x="2583" y="0" xlink:href="#MJMAIN-3D">< g transform="translate(3362,0)">< g transform="translate(397,0)">< rect stroke="none" width="450" x="0" y="220">< g transform="translate(60,958)">< use x="0" y="0" xlink:href="#MJSZ1-2211">< g transform="translate(1056,-287)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="469" y="0" xlink:href="#MJMAIN-2208">< g transform="translate(803,0)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-53">< use transform="scale(0.574)" x="755" y="-243" xlink:href="#MJMAIN-30">< g transform="translate(2918,0)">< use x="0" y="0" xlink:href="#MJMATHI-77">< use transform="scale(0.707)" x="1013" y="-213" xlink:href="#MJMATHI-3BC">< use x="4162" y="0" xlink:href="#MJMAIN-28">< use x="4551" y="0" xlink:href="#MJMATHI-73">< use x="5021" y="0" xlink:href="#MJMAIN-29">< use x="5410" y="0" xlink:href="#MJMATHI-78">< use x="5983" y="0" xlink:href="#MJMAIN-28">< use x="6372" y="0" xlink:href="#MJMATHI-73">< use x="6842" y="0" xlink:href="#MJMAIN-29">< g transform="translate(970,-771)">< use x="0" y="0" xlink:href="#MJSZ1-2211">< g transform="translate(1056,-287)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="469" y="0" xlink:href="#MJMAIN-2208">< g transform="translate(803,0)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-53">< use transform="scale(0.574)" x="755" y="-243" xlink:href="#MJMAIN-30">< g transform="translate(2918,0)">< use x="0" y="0" xlink:href="#MJMATHI-77">< use transform="scale(0.707)" x="1013" y="-213" xlink:href="#MJMATHI-3BC">< use x="4162" y="0" xlink:href="#MJMAIN-28">< use x="4551" y="0" xlink:href="#MJMATHI-73">< use x="5021" y="0" xlink:href="#MJMAIN-29">
and
< svg xmlns:xlink="http://www.w3.org/1999/xlink" width="38.725ex" height="7.676ex" style="vertical-align: -3.005ex;" viewBox="0 -2011.3 16673.3 3304.9" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-3C3">< use transform="scale(0.707)" x="810" y="583" xlink:href="#MJMAIN-32">< use x="263" y="462" xlink:href="#MJMAIN-5E">< use x="1026" y="0" xlink:href="#MJMAIN-28">< g transform="translate(1416,0)">< use x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="663" y="-213" xlink:href="#MJMAIN-30">< use x="2339" y="0" xlink:href="#MJMAIN-29">< use x="3006" y="0" xlink:href="#MJMAIN-3D">< g transform="translate(3785,0)">< g transform="translate(397,0)">< rect stroke="none" width="450" x="0" y="220">< g transform="translate(60,958)">< use x="0" y="0" xlink:href="#MJSZ1-2211">< g transform="translate(1056,-287)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="469" y="0" xlink:href="#MJMAIN-2208">< g transform="translate(803,0)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-53">< use transform="scale(0.574)" x="755" y="-243" xlink:href="#MJMAIN-30">< g transform="translate(2918,0)">< use x="0" y="0" xlink:href="#MJMATHI-77">< use transform="scale(0.707)" x="1013" y="-213" xlink:href="#MJMATHI-3C3">< use x="4140" y="0" xlink:href="#MJMAIN-28">< use x="4529" y="0" xlink:href="#MJMATHI-73">< use x="4999" y="0" xlink:href="#MJMAIN-29">< g transform="translate(5388,0)">< use x="0" y="0" xlink:href="#MJMAIN-28">< use x="389" y="0" xlink:href="#MJMATHI-78">< use x="962" y="0" xlink:href="#MJMAIN-28">< use x="1351" y="0" xlink:href="#MJMATHI-73">< use x="1821" y="0" xlink:href="#MJMAIN-29">< use x="2432" y="0" xlink:href="#MJMAIN-2212">< g transform="translate(3433,0)">< use x="0" y="0" xlink:href="#MJMATHI-3BC">< use x="79" y="20" xlink:href="#MJMAIN-5E">< use x="4036" y="0" xlink:href="#MJMAIN-28">< g transform="translate(4426,0)">< use x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="663" y="-213" xlink:href="#MJMAIN-30">< use x="5349" y="0" xlink:href="#MJMAIN-29">< use x="5739" y="0" xlink:href="#MJMAIN-29">< use transform="scale(0.707)" x="8667" y="675" xlink:href="#MJMAIN-32">< g transform="translate(3351,-771)">< use x="0" y="0" xlink:href="#MJSZ1-2211">< g transform="translate(1056,-287)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="469" y="0" xlink:href="#MJMAIN-2208">< g transform="translate(803,0)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-53">< use transform="scale(0.574)" x="755" y="-243" xlink:href="#MJMAIN-30">< g transform="translate(2918,0)">< use x="0" y="0" xlink:href="#MJMATHI-77">< use transform="scale(0.707)" x="1013" y="-213" xlink:href="#MJMATHI-3C3">< use x="4140" y="0" xlink:href="#MJMAIN-28">< use x="4529" y="0" xlink:href="#MJMATHI-73">< use x="4999" y="0" xlink:href="#MJMAIN-29">< use x="16394" y="0" xlink:href="#MJMAIN-2E">
The difference between the mean call here and rasterLocalSums is the normalization of the weights. This ensures the weights sum to one.
library(raster)
library(rasterKernelEstimates)
set.seed(100)
n <- 200
# create a raster object of two populations
r <- raster::raster(
cbind( matrix(rnorm(n^2),n,n), matrix(rnorm(n^2,2,2),n,n)) )
# create a weight matrix
W <- raster::focalWeight(r,c(1,0.04),type='Gauss')
# calculate the weighted local mean and variance
run.time <- proc.time()
rLocalKDE1 <- rasterLocalMoments(r,W)
print(proc.time() - run.time)
## user system elapsed
## 0.343 0.002 0.102
# plot original image
plot(r)
# plot the weighted mean
plot(rLocalKDE1$mu)
# plot the weighted variance
plot(rLocalKDE1$var)
rasterLocalCategorical Modes
The function rasterLocalCategoricalModes calculates a categorical mode from a raster image over the weighted spatial neighborhood devined by the matrix W,
< svg xmlns:xlink="http://www.w3.org/1999/xlink" width="42.577ex" height="7.843ex" style="vertical-align: -3.505ex;" viewBox="0 -1867.7 18331.8 3376.7" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-66">< use x="550" y="0" xlink:href="#MJMAIN-28">< g transform="translate(940,0)">< use x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="663" y="-213" xlink:href="#MJMAIN-30">< use x="1863" y="0" xlink:href="#MJMAIN-29">< use x="2530" y="0" xlink:href="#MJMAIN-3D">< g transform="translate(3586,0)">< use xlink:href="#MJSZ4-7B">< use x="806" y="0" xlink:href="#MJMATHI-63">< use x="1517" y="0" xlink:href="#MJMAIN-3A">< use x="2074" y="0" xlink:href="#MJMATHI-6D">< use x="2952" y="0" xlink:href="#MJMATHI-61">< g transform="translate(3482,0)">< use x="0" y="0" xlink:href="#MJMATHI-78">< g transform="translate(572,-155)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-63">< use transform="scale(0.707)" x="433" y="0" xlink:href="#MJMAIN-2208">< g transform="translate(778,0)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-43">< use transform="scale(0.574)" x="881" y="-243" xlink:href="#MJMAIN-30">< g transform="translate(5963,0)">< use xlink:href="#MJSZ4-7B">< g transform="translate(806,0)">< use x="75" y="0" xlink:href="#MJSZ2-2211">< g transform="translate(0,-1117)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="469" y="0" xlink:href="#MJMAIN-2208">< g transform="translate(803,0)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-53">< use transform="scale(0.574)" x="755" y="-243" xlink:href="#MJMAIN-30">< use x="2568" y="0" xlink:href="#MJMATHI-77">< use x="3285" y="0" xlink:href="#MJMAIN-28">< use x="3674" y="0" xlink:href="#MJMATHI-73">< use x="4144" y="0" xlink:href="#MJMAIN-29">< g transform="translate(4533,0)">< use x="0" y="0" xlink:href="#MJAMS-49">< g transform="translate(389,-187)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-78">< use transform="scale(0.707)" x="572" y="0" xlink:href="#MJMAIN-28">< use transform="scale(0.707)" x="962" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="1431" y="0" xlink:href="#MJMAIN-29">< use transform="scale(0.707)" x="1821" y="0" xlink:href="#MJMAIN-3D">< use transform="scale(0.707)" x="2599" y="0" xlink:href="#MJMATHI-63">< use x="7168" y="-1" xlink:href="#MJSZ4-7D">< use x="13938" y="-1" xlink:href="#MJSZ4-7D">
where < svg xmlns:xlink="http://www.w3.org/1999/xlink" width="2.716ex" height="2.509ex" style="vertical-align: -0.671ex;" viewBox="0 -791.3 1169.4 1080.4" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-43">< use transform="scale(0.707)" x="1011" y="-213" xlink:href="#MJMAIN-30"> is the set of unique categories in r.
library(raster)
library(rasterKernelEstimates)
set.seed(100)
n <- 100
# create a categorical raster
r <- raster::raster(
matrix( sample(-4:4,size=n^2,replace=T),n,n)
)
# create a weight matrix
W <- matrix(1,9,9)
# calculate the weighted local mean and variance
run.time <- proc.time()
rLocalKDE1 <- rasterLocalCategoricalModes(r,W)
print(proc.time() - run.time)
## user system elapsed
## 0.030 0.000 0.011
# plot original image
plot(r)
# plot the weighted mean
plot(rLocalKDE1)
To leave a comment for the author, please follow the link and comment on their blog: MeanMean.