我有一个函数,我从sum
函数获得NA,第一个sum
效果很好,但第二个sum
不起作用并返回NA
。
这是功能:
Gramm.Pred.Err <- function(acts , grammProbs)
{
acts <- as.numeric(acts)
grammProbs <- as.numeric(grammProbs)
print("acts is:")
print(acts)
print("grammProbs is:")
print(grammProbs)
false.ind = grammProbs == 0
hit.ind= grammProbs>0
print("false.ind is:")
print(false.ind)
print("hit.ind is:")
print(hit.ind)
hit.acts = acts[hit.ind]
hit.probs = grammProbs[hit.ind]
print("hit.acts is:")
print(hit.acts)
print("hit.probs is:")
print(hit.probs)
misses.ind = hit.acts< hit.probs
hits = sum(hit.acts)
falses = sum( acts[false.ind])
misses = sum( hit.probs[misses.ind] - hit.acts[misses.ind] )
print("misses.ind is:")
print(misses.ind)
print("hits is:")
print(hits)
print("falses is:")
print(falses)
print("misses is:")
print(misses)
print("final:")
print(1-(hits/(hits+falses+misses)))
return (1-(hits/(hits+falses+misses)))
}
act和grammProbs矢量:
activations.vector <- c( "2.08101344", "-1.41434467", "0.07817803", "0.45509970", "1.27916718", "-0.84691423", "2.01260424", "-1.42960405", "-1.47239423",
"0.68798345", "-0.86126810", "1.61871290", "-1.49541676", "3.70249152", "1.60749793", "-2.68202949", "0.58367389", "-0.15213574",
"9.20287609", "1.62072563", "4.33229876", "-0.16497207", "-0.16517217", "-13.28754520")
prob.vector <- c( "2.0000000", "0.0000000", "0.1666670", "0.1666670", "0.0833333", "0.0833333", "0.0833333", "0.0833333", "0.0000000", "0.0000000", "0.0833333", "0.0833333",
"0.0000000", "0.0000000", "0.0833333", "0.0833333", "0.0000000", "0.0000000", "0.0000000", "0.0000000", "0.0000000", "0.0000000", "0.0000000", "0.0000000")
对于这些,我得到以下结果:
gpe.matrix <- matrix(, 10, 24)
gpe.matrix[1,] <- Gramm.Pred.Err(acts = activations.vector,grammProbs = prob.vector)
[1] "acts is:"
[1] 2.08101344 -1.41434467 0.07817803 0.45509970 1.27916718 -0.84691423 2.01260424 -1.42960405 -1.47239423
[10] 0.68798345 -0.86126810 1.61871290 -1.49541676 3.70249152 1.60749793 -2.68202949 0.58367389 -0.15213574
[19] 9.20287609 1.62072563 4.33229876 -0.16497207 -0.16517217 -13.28754520
[1] "grammProbs is:"
[1] 2.0000000 0.0000000 0.1666670 0.1666670 0.0833333 0.0833333 0.0833333 0.0833333 0.0000000 0.0000000 0.0833333 0.0833333
[13] 0.0000000 0.0000000 0.0833333 0.0833333 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
[25] 0.0000000
[1] "false.ind is:"
[1] FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE TRUE TRUE FALSE FALSE TRUE TRUE TRUE TRUE
[21] TRUE TRUE TRUE TRUE TRUE
[1] "hit.ind is:"
[1] TRUE FALSE TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE
[21] FALSE FALSE FALSE FALSE FALSE
[1] "hit.acts is:"
[1] 2.08101344 0.07817803 0.45509970 1.27916718 -0.84691423 2.01260424 -1.42960405 -0.86126810 1.61871290 1.60749793
[11] -2.68202949
[1] "hit.probs is:"
[1] 2.0000000 0.1666670 0.1666670 0.0833333 0.0833333 0.0833333 0.0833333 0.0833333 0.0833333 0.0833333 0.0833333
[1] "misses.ind is:"
[1] FALSE TRUE FALSE FALSE TRUE FALSE TRUE TRUE FALSE FALSE TRUE
[1] "hits is:"
[1] 3.312458
[1] "falses is:"
[1] NA
[1] "misses is:"
[1] 6.241638
[1] "final:"
[1] NA
非常感谢您的建议。
答案 0 :(得分:1)
我逐行查看了你的功能,发现了问题:
falses = sum(act [false.ind])
acts是长度为24的向量,false.ind是长度为25的向量。因此,您正在尝试对不存在的向量元素进行子集化。这会产生NA。
如果你想得到没有NA的向量之和,你可以在总结这样的向量元素之前删除NA:
falses = sum(act [false.ind],na.rm = T)