我正在尝试创建一个函数,根据q的值计算特定的多样性指数。但是,运行它时我收到了一些错误消息,我不知道如何修复它。有人可以帮忙吗?感谢
q.div1 <- function(data, q, que)
{
if(abs(q-1) < 0.000001)
{
# Get rid of zero values from vector
no.zeros <- data[data > 0]
# Calculate proportions
p <- no.zeros/sum(no.zeros)
# Calculate D1
D1 <- exp(-sum(p * log(p)))
}
else if (is.infinite(q))
{
# Get rid of zero values from vector
no.zeros <- data[data > 0]
# Calculate proportions
p <- no.zeros/sum(no.zeros)
# Calculate D.Inf
D.Inf <- 1/max(p)
}
else if (q = 2)
{
# Get rid of zero values from vector
no.zeros <- data[data > 0]
# Calculate proportions
p <- (no.zeros/sum(no.zeros))^q
D2 <- sum(p)
}
else if (q = 0)
{
# Get rid of zero values from vector
no.zero <- data[data > 0]
# Calculate proportions
p <- no.zero/sum(no.zero)
# Raise to power of q
prop.q <- (p)^q
D0 <- sum(prop.q)
}
else
# Get rid of zero values from vector
no.zero <- data[data > 0]
# Calculate proportions
prop <- no.zero/sum(no.zero)
# Raise to power of q
prop.q <- (prop)^que
sum.pq <- sum(prop.que)
# Raise to power 1/(1-q)
pq <- sum.pq^(1/1-que)
}
答案 0 :(得分:2)
“我的”R遇到的唯一错误是你的“q”分配。 在评估逻辑标志时,您应该使用 q == 2 而不是q = 2。后者意味着您要为变量q指定数字2。 因此,请按“ q == ...”修改第28行和第38行。 干杯!