R分位数:某些分位数为-9999

R quantiles: -9999 for some quantiles

我想使用 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 值并为所有多边形生成结果,无论像素数如何。

跟随计算的输出 table。百分位数 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 值。某些网格只是具有较低的像素数。

更新:是的,数据有 -9999 的 NA 值,愚蠢且容易出错。感谢您的帮助!

从我的角度来看,您的结果是有道理的。 -9999 在较低的(0.1 和 0.15)分位数中 "classified"。

> 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

我会做:

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))