【问题标题】:error: nrow(ref) and nrow(target) must be > 0?错误:nrow(ref) 和 nrow(target) 必须 > 0?
【发布时间】:2017-03-17 06:58:51
【问题描述】:

基于Unable to writeRaster for signature "rasterPCA", "character",我得到了一组气候变量的两个栅格,分别是PC1和PC2。但是,无论具有相同的范围和分辨率,当加载到 R 中时,我的全局环境中的单元格数量都会有所不同。

以下是我正在使用的代码,来自 Hamann et al., 2015 的附录,我收到此错误:

library(SDMTools)     # install package to read and write ESRI ASCII grids
library(yaImpute)     # install package for k-nearest neighbour (kNN) search

lg1 <- asc2dataframe("C:\\Users\\rameshv\\LGM\\4_PCAforR\\PC_1.asc") # principal component grids
lg2 <- asc2dataframe("C:\\Users\\rameshv\\LGM\\4_PCAforR\\PC_2.asc")
present1  <-asc2dataframe("C:\\Users\\rameshv\\Present\\4_PCAforR\\PC_1.asc")
present2  <- asc2dataframe("C:\\Users\\rameshv\\Present\\4_PCAforR\\PC_2.asc")

idxy <- cbind(id=1:nrow(lg1),lg1[,1:2])   # data frame of IDs and XY coords
b <- (max(lg1$var.1)-min(lg1$var.1))/120  # bin size for 120 PC1 bins

l1 <- round(lg1$var.1/b)              # convert PC1 to 120 bins via rounding
l2 <- round(lg2$var.1/b)              # convert PC2 to <120 bins via rounding
p1 <- round(present1$var.1/b)               # same for present PC1
p2 <- round(present2$var.1/b)               # same for present PC2
l  <- paste(l1,l2)                         # PC1/PC2 combinations in LGM climate
p  <- paste(p1,p2)                         # PC1/PC2 combinations in present climate
u  <- unique(p)[order(unique(p))]          # list of unique PC1/PC2 combinations

sid <- c()                                 # empty vector for source IDs
tid <- c()                                 # empty vector for target IDs
d   <- c()                                 # empty vector for distances

for(i in u){                          # loop for each unique PC1/PC2 combination
lxy <- idxy[which(l==i),]           # coordinates of i-th combination in LGM
pxy <- idxy[which(p==i),]           # coordinates of i-th combination in present
sid <- c(sid, lxy$id)               # append i-th PC1/PC2 combination to previous 

if(nrow(pxy)>0){                    # kNN search unless no-analogue climate
  knn <- data.frame(ann(as.matrix(pxy[,-1]), as.matrix(lxy[,-1]), k=1)$knnIndexDist)      
  tid <- c(tid, pxy[knn[,1],"id"]) # the IDs of the closest matches  
  d <- c(d, sqrt(knn[,2]))         # their corresponding geographic distances
}
 else {                              # else statement for no-analogue climates
 tid <- c(tid, rep(NA,nrow(lxy))) # flag destinations as missing for no analogues
 d <- c(d, rep(Inf,nrow(lxy)))    # flag distances as infinity for no analogues
 }
}

在 for 循环结束时,我收到以下错误:

Error in ann(as.matrix(pxy[, -1]), as.matrix(lxy[, -1]), k = 1) : 
error: nrow(ref) and nrow(target) must be > 0

我不确定这个错误是否与单元格数量的差异有关?有什么建议吗?

编辑:

根据 Bastien 的评论,我调查了结构并得到以下信息:

> str(as.matrix(pxy[,-1]))
  num [1:27, 1:2] 8.1 8.14 8.22 8.97 9.01 ...
  - attr(*, "dimnames")=List of 2
  ..$ : chr [1:27] "1" "8" "33" "583" ...
  ..$ : chr [1:2] "y" "x"

> str(as.matrix(lxy[,-1]))
  logi[0 , 1:2] 
  - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:2] "y" "x"

建议?

【问题讨论】:

  • 错误出现在 yaImpute 的 ann 函数中,该函数表示您的 ref 或目标集的大小为 0。运行 str(as.matrix(pxy[, -1]))str(as.matrix(lxy[, -1])) 以确保结构正常。您的数据管理可能存在问题
  • @Bastien 这很有趣。你说的对。我目前正在接受上述内容。将其添加到编辑中。见上文。
  • 你检查过l1l2 中的内容吗?
  • @LoBu ,是的,我检查了它们。它们都是数字并且有值(它们都不是空的)。
  • 查看代码,您的某些 u 似乎不存在于 l 中(无论这些变量是什么)。因此lxy &lt;- idxy[which(l==i),] 可能会给出一个“虚假”的结果。

标签: r raster sp


【解决方案1】:

LoBu 是正确的,您的 lxy 数组为空。目前还不清楚哪里出了问题 - 您的 PCA 可能失败,b 的 bin 大小计算可能失败。如果您尝试匹配像 Hamann 等人那样的分箱气候值,您的 pxy 数据框也太短了。 2015 年。如果不近距离查看您的数据,就无法说出如何解决此问题 - 我建议检查您的 PCA 输出和基础气候栅格。

你的学习区域有多大?附录 3 是迄今为止最复杂的气候模拟方法。我建议尝试附录 2,它速度较慢,但​​如果您不是在高分辨率下工作,它就可以正常工作。这是代码,以供快速参考。

library(SDMTools)       # install package to read and write ESRI ASCII grids
present <- asc2dataframe("C:\Your Path\MAT6190.asc")
future  <- asc2dataframe("C:\Your Path\MAT2020s.asc")

t <- 0.25               # plus/minus threshold to define climate match
t <- 1/(t*2)            # inverse for rounding, double for plus/minus

x <- present$x                    # vector of grid cell x coordinates
y <- present$y                    # vector of grid cell y coordinates
p <- round(present$var.1*t)/t     # vector of rounded present climate values 
f <- round(future$var.1*t)/t      # vector of rounded future climate values 
d <- vector(length=length(p))     # empty vector to write distance to climate match

u     <- unique(p)[order(unique(p))]    # list of unique climate values in p
match <- function(u){c(which(u==f))}    # function finding climate matches of u with f
m     <- sapply(u, match)               # list of climate matches for unique values

for(i in 1:length(p)){                  # loop for all grid cells of p
   mi   <- m[[which(u==p[i])]]          # recalls list of climate matches for p[i]
   d[i] <- sqrt(min((x[i]-x[mi])^2 + (y[i]-y[mi])^2))    # distance to closest match
   }

# writes out log10 speed and distance multiplied by 100 in ESRI ASCII format
# conversion: -200=0.01km, -100=0.1km, 0=1km, 100=10km, 200=100km etc.
d[d==Inf] <- 100000  # sets no analogue to 10,000km
out=cbind(y,x,logDist=round(log10(d)*100),logSpeed=round(log10(d/50)*100))
dataframe2asc(out)

【讨论】:

  • 谢谢@QBarber。事情就是这样。我有多个气候变量,分辨率不是很高。大约 5 公里乘 5 公里。基本上运行附录 2 中的代码将一次完成一个变量,对吗?那么,您如何对所有气候速度预测进行“平均”呢?此外,关于 PCA 和 bin 大小,它对我来说效果很好。您的示例数据和我的数据之间的唯一区别是我有大约 50,000 个元素,而您从 815345 个元素开始。这有关系吗?
  • 你愿意看一下Quinn的数据吗?我可以通过电子邮件发送给您。
  • 让它工作。我的 for 循环中似乎有一个小错误:)
  • 嗨,Vijay,很高兴你让它工作。 50,000 个元素非常适合附录 2,因为它效率较低但更易于实现。
  • 谢谢奎恩。看起来附录 3 是为正向速度计算而不是反向计算而设计的,但我设置的变量不同。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-19
  • 1970-01-01
  • 2015-11-21
  • 2015-05-30
  • 1970-01-01
  • 2021-05-29
  • 2015-10-09
相关资源
最近更新 更多