gpt4 book ai didi

r - 求解 "Error in if (obs <= ei) 2 * pv else 2 * (1 - pv) : missing value where TRUE/FALSE needed"for ape package Moran's I function in R

转载 作者:行者123 更新时间:2023-12-04 13:09:51 26 4
gpt4 key购买 nike

开发商!
我遇到了错误消息

Error in if (obs <= ei) 2 * pv else 2 * (1 - pv) : missing value whereTRUE/FALSE needed


阻止我从 ape 包中获取 Moran 的 I 函数的值。这是我所做的:
library(ape)

nrstp <- data.frame(
X = c(300226.9, 300224.6, 300226.4, 300226.1, 300224.0, 300226.4, 300225.7, 300226.4, 300226.1, 300226.4, 300226.3, 300226.3, 300227.1),
Y = c(5057949, 5057952, 5057950, 5057950, 5057956, 5057950, 5057950, 5057950, 5057950, 5057950, 5057950, 5057950, 5057949),
V3 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))

nrstp = data.frame(nrstp)
dist = as.matrix(dist(cbind(nrstp$X, nrstp$Y)))
invdist = 1/dist
invdist[is.infinite(invdist)] <- 0
moranI = Moran.I(nrstp$V3, invdist)
此代码的目的是从一系列点计算 Moran's I 以检查空间自相关。到目前为止,这似乎是 R 中 Moran's I 的唯一功能。经过几次测试(我有数千组点),这个错误似乎只发生在只有一个值的输入向量上(我尝试了其他数字而不是 0 ,它仍然会引发此错误)。
有人可以帮我改进这段代码吗?或者他们是否有更好的建议来计算 Moran's I 或从线串测试空间自相关(这些点组是一个线串的原点以及距该原点 10 米缓冲区内其他线串最近的点)?
感谢您提供任何帮助!

最佳答案

控制流选择if(condition) do something要求 condition 的值不是 NA .
在您的情况下,obs <= ei结果 NA .这就是错误消息 missing value where TRUE/FALSE needed 的原因生成。
了解如何obs <= ei结果 NA ,您可以在Moran.I中查看详细信息功能:

Moran.I
function (x, weight, scaled = FALSE, na.rm = FALSE, alternative = "two.sided")
{
if (dim(weight)[1] != dim(weight)[2])
stop("'weight' must be a square matrix")
n <- length(x)
if (dim(weight)[1] != n)
stop("'weight' must have as many rows as observations in 'x'")
ei <- -1/(n - 1)
nas <- is.na(x)
if (any(nas)) {
if (na.rm) {
x <- x[!nas]
n <- length(x)
weight <- weight[!nas, !nas]
}
else {
warning("'x' has missing values: maybe you wanted to set na.rm = TRUE?")
return(list(observed = NA, expected = ei, sd = NA,
p.value = NA))
}
}
ROWSUM <- rowSums(weight)
ROWSUM[ROWSUM == 0] <- 1
weight <- weight/ROWSUM
s <- sum(weight)
m <- mean(x)
y <- x - m
cv <- sum(weight * y %o% y)
v <- sum(y^2)
obs <- (n/s) * (cv/v)
if (scaled) {
i.max <- (n/s) * (sd(rowSums(weight) * y)/sqrt(v/(n -
1)))
obs <- obs/i.max
}
S1 <- 0.5 * sum((weight + t(weight))^2)
S2 <- sum((apply(weight, 1, sum) + apply(weight, 2, sum))^2)
s.sq <- s^2
k <- (sum(y^4)/n)/(v/n)^2
sdi <- sqrt((n * ((n^2 - 3 * n + 3) * S1 - n * S2 + 3 * s.sq) -
k * (n * (n - 1) * S1 - 2 * n * S2 + 6 * s.sq))/((n -
1) * (n - 2) * (n - 3) * s.sq) - 1/((n - 1)^2))
alternative <- match.arg(alternative, c("two.sided",
"less", "greater"))
pv <- pnorm(obs, mean = ei, sd = sdi)
if (alternative == "two.sided")
pv <- if (obs <= ei)
2 * pv
else 2 * (1 - pv)
if (alternative == "greater")
pv <- 1 - pv
list(observed = obs, expected = ei, sd = sdi, p.value = pv)
}
<bytecode: 0x000001cd5e0715d0>
<environment: namespace:ape>
通过分配 x = nrstp$V3weight = invdist , 你会得到 mean(x) = 0 .这导致 y=0 , cv = 0 , v=0 ,最后 obs = NaN .最后,
obs <= ei
[1] NA
要克服这个问题,您需要确保 obs 中的每一个和 ei不是 NA .在您的情况下,如果 mean(x)不为零, obs <= ei不会 NA .但是,因为我对这个特定主题一无所知,所以我不确定是否非零 mean(x)始终是正确的解决方案。

关于r - 求解 "Error in if (obs <= ei) 2 * pv else 2 * (1 - pv) : missing value where TRUE/FALSE needed"for ape package Moran's I function in R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66663994/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com