我希望使用DoBy包计算R中栅格值为栅格值的区域百分位数。某些百分位数工作完美,其他百分位数产生或多或少的NA值,在这种情况下为-9999。造成这种差异的原因是什么?
注意:所有NA值都出现在与我感兴趣的栅格不完全重叠的多边形中。但这并不能解释为什么有些百分位计算,有些则没有。
我应用的百分位函数:
quant1<-function(x,na.rm=TRUE){(quantile(x, c(.01), na.rm = TRUE))}
quant5<-function(x,na.rm=TRUE){(quantile(x, c(.05), na.rm = TRUE))}
quant15<-function(x,na.rm=TRUE){(quantile(x, c(.15), na.rm = TRUE))}
quant25<-function(x,na.rm=TRUE){(quantile(x, c(.25), na.rm = TRUE))}
quant75<-function(x,na.rm=TRUE){(quantile(x, c(.75), na.rm = TRUE))}
quant85<-function(x,na.rm=TRUE){(quantile(x, c(.85), na.rm = TRUE))}
quant95<-function(x,na.rm=TRUE){(quantile(x, c(.95), na.rm = TRUE))}
quant99<-function(x,na.rm=TRUE){(quantile(x, c(.99), na.rm = TRUE))}
功能的应用:
q1<-summaryBy(RASTERVALU~grid_code,data=depth, FUN=quant1, keep.names=T)
q5<-summaryBy(RASTERVALU~grid_code,data=depth, FUN=quant5, keep.names=T)
q15<-summaryBy(RASTERVALU~grid_code,data=depth, FUN=quant15, keep.names=T)
q25<-summaryBy(RASTERVALU~grid_code,data=depth, FUN=quant25, keep.names=T)
q75<-summaryBy(RASTERVALU~grid_code,data=depth, FUN=quant75, keep.names=T)
q85<-summaryBy(RASTERVALU~grid_code,data=depth, FUN=quant85, keep.names=T)
q95<-summaryBy(RASTERVALU~grid_code,data=depth, FUN=quant95, keep.names=T)
q99<-summaryBy(RASTERVALU~grid_code,data=depth, FUN=quant99, keep.names=T)
根据我的理解,这应该在计算之前移除NA值并产生所有多边形的结果,无论像素数是多少。
遵循计算的输出表。百分位数1和15最受NA值的困扰。
q1 q5 q15 q25 q75 q85 q95 q99
-9999 -82.7934989929199 -9999 -9999 -82.6172466278076 -82.5455478668213 -82.4335483551025 -82.3479396057129
-9999 -82.0279998779297 -9999 -9999 -81.7740020751953 -81.6869964599609 -81.6319034576416 -81.6039762878418
-9999 -68.5245018005371 -9999 -68.6367511749268 -68.4617481231689 -68.4400024414062 -68.4128520965576 -68.318980255127
-9999 -68.1529998779297 -9999 -68.6242504119873 -67.9940013885498 -67.9098526000977 -67.8197998046875 -67.6305767059326
-9999 -66.0965003967285 -9999 -9999 -65.9724998474121 -65.9219970703125 -65.7605514526367 -65.6639614868164
-9999 -65.2804985046387 -9999 -9999 -64.8652515411377 -64.7608005523682 -64.5118511199951 -64.4869895172119
-9999 -62.7045001983643 -9999 -9999 -62.5639991760254 -62.4821016311646 -62.2751508712769 -62.1119895172119
-9999 -62.5089988708496 -9999 -62.6920013427734 -62.3634986877441 -62.2785495758057 -62.1639013290405 -62.1359615707397
-9999 -62.3390007019043 -9999 -9999 -62.1464996337891 -62.088650894165 -62.031251335144 -61.9182501983643
-9999 -58.6590003967285 -9999 -9999 -58.3725004196167 -58.1434007644653 -57.8779983520508 -57.6537393188477
-9999 -58.4934997558594 -9999 -58.8072500228882 -58.3682489395142 -58.3038509368896 -58.1767482757568 -58.0499892425537
-9999 -56.1360015869141 -9999 -56.3297491073608 -56.0120010375977 -55.9688512802124 -55.8899494171143 -55.8329501724243
-9999 -53.9230003356934 -9999 -54.2092504501343 -53.8269996643066 -53.7830009460449 -53.6978017807007 -53.6589603805542
-9999 -48.9314994812012 -9999 -9999 -48.7105007171631 -48.6123992919922 -48.5179513931274 -48.4739890289307
-9999 -42.2854995727539 -9999 -42.8615007400513 -41.8812494277954 -41.6038501739502 -41.3591510772705 -40.5169882965088
-9999 -42.1614990234375 -9999 -42.2462491989136 -42.0267505645752 -41.9099998474121 -41.8459506988525 -41.8219386291504
-9999 -38.1544990539551 -9999 -9999 -37.8874998092651 -37.6593996047974 -37.4445497512817 -37.3039208602905
-9999 -33.1689987182617 -9999 -33.2529983520508 -33.1027507781982 -33.0769996643066 -33.0347497940063 -33.0139999389648
-9999 -29.6655006408691 -9999 -9999 -29.5527496337891 -29.5060005187988 -29.4157503128052 -29.3299599266052
-9999 -29.638500213623 -9999 -29.7770004272461 -29.5520000457764 -29.5209999084473 -29.4968997955322 -29.4849806022644
-9999 -29.0570001602173 -9999 -9999 -28.9180006980896 -28.8618495941162 -28.7558505058289 -28.6939602470398
-9999 -27.1759996414185 -9999 -9999 -27.0627498626709 -27.0209999084473 -26.9890003204346 -26.9459791564941
-9999 -25.918999671936 -9999 -25.9869995117188 -25.8700008392334 -25.8515495300293 -25.8089500427246 -25.7189898681641
有什么可以解释这种效果?
注意:我的数据没有NA值。某些网格的像素数较少。
更新:是的,NA数据的数据为-9999,愚蠢且容易出错。谢谢你的帮助!
答案 0 :(得分:1)
从我坐的地方,你的结果是有道理的。 -9999在较低(0.1和0.15)分位数中被“分类”。
> quantile(c(rep(-9999, 15), rnorm(50, mean = -50, sd = 5)), prob = c(0.1, 0.15, 0.25, 0.75))
10% 15% 25% 75%
-9999.00000 -9999.00000 -60.11869 -49.44009
另请注意,根据您的注释,quant5
函数可能应为quant50
。
答案 1 :(得分:1)
我愿意:
library(raster)
r <- raster(ncols=10, nrows=10)
r[] <- runif(ncell(r)) * 1:ncell(r)
z <- r
z[] <- rep(1:5, each=20)
quants <- c(0.01, 0.05, 0.15, 0.25, 0.75, 0.85, 0.95, 0.99)
q <- zonal(r, z, function(i, ...) quantile(i, quants))
colnames(q) <- c("zone", quants)
q
现在,您的数据显然具有-9999的值。这些不是NA
,它们是-9999。这就是为什么您的最低分位数值具有该值。如果你想让他们成为NA,你可以尝试:
NAvalue(r) <- -9999
q <- zonal(r, z, function(i, ...) quantile(i, quants, na.rm=TRUE))
或
x <- reclassify(r, cbind(-9999, NA))
q <- zonal(r, z, function(i, ...) quantile(i, quants, na.rm=TRUE))