R绘制循环中生成的x,y点

时间:2015-02-15 21:33:01

标签: r

我有一个for循环,它在某些数据上运行一个函数。

返回c(x,y)

我想绘制那些x,y点。

predEvaluation <- function(act,pred,thresh) {
    pred = ifelse(pred > thresh, 1, 0)
    TP = sum(pred & act)
    FP = sum(pred & !act)
    TN = sum(!pred & !act)
    FN = sum(!pred & act)
    TPR = TP/(TP + FN)
    TNR = TP/(TN + FP)
    sensitivity = TPR #how good at detecting positives
    specifity = TNR # How good at avoiding false positives
    precision = TP/(TP+FP) # how many of the positives were correct
    recall = sensitivity
    accuracy = (TP + TN)/((TP + FN) + (TN + FP)) #amount correct
    return(c(FP,TP))
}

q5 = data.frame(c(1,0,1,1,0,0,1,0,1,0),c(.98,.92,.85,.77,.71,.64,.50,.39,.34,.31))
colnames(q5) <- c("act","pred")

for(i in q5$pred)
    print(predEvaluation(q5$act,q5$pred,i))

结果

[1] 0 1
[1] 1 1
[1] 1 2
[1] 1 3
[1] 2 3
[1] 3 3
[1] 3 4
[1] 4 4
[1] 4 5
[1] 5 5

我正在尝试将数据放入一个数据框或其他内容,然后绘制它。我知道有一些方法可以逐行向数据框添加行,但它们不是正确的方法。

1 个答案:

答案 0 :(得分:2)

您可以使用for系列函数循环apply值,而不是使用p5$pred循环:

x <- do.call(rbind.data.frame, lapply(q5$pred, function(i) predEvaluation(q5$act,q5$pred,i)))

# or:
# x <- as.data.frame(t(sapply(q5$pred, function(i) predEvaluation(q5$act,q5$pred,i))))

names(x) <- c('pred', 'value')
plot(x)

enter image description here