0

在 R 中调用函数时,如何检索结果值。例如,我使用了 'roc' 函数,我需要提取 AUC 值和 CI(在以下示例中分别为 0.6693 和 0.6196-0.7191)。

> roc(tmpData[,lenCnames], fitted(model), ci=TRUE)

Call:
roc.default(response = tmpData[, lenCnames], predictor = fitted(model),     ci = TRUE)

Data: fitted(model) in 127 controls (tmpData[, lenCnames] 0) < 3248 cases (tmpData[, lenCnames] 1).
Area under the curve: 0.6693
95% CI: 0.6196-0.7191 (DeLong)

我可以使用以下内容来获取这些带有关联文本的值。

> z$auc
Area under the curve: 0.6693
> z$ci
95% CI: 0.6196-0.7191 (DeLong)

有没有办法只获取值而不是文本。

我现在知道如何使用“正则表达式”或“strsplit”函数来获取这些值,但我怀疑应该有其他方法可以直接访问这些值。

4

1 回答 1

3

提问时使用可重复的示例很有帮助。最好也参考您要询问的库(“pROC”),因为它没有使用基本 R 加载。pROC具有从对象中提取aucci.auc对象的roc函数。

>library("pROC")
>data(aSAH)
# Basic example
>z <- roc(aSAH$outcome, aSAH$s100b,
    levels=c("Good", "Poor"))

# Examining the class of 'auc' output shows us that it is also of class 'numeric'
> class(auc(z))
[1] "auc"     "numeric"
# calling 'as.numeric' will extract the value
> as.numeric(auc(z))
[1] 0.7313686

# calling 'as.numeric' on the 'ci.auc' object extracts three values.
as.numeric(ci(z))
[1] 0.6301182 0.7313686 0.8326189

# The ones we want are 1 and 3
> as.numeric(ci(z))[c(1,3)]
[1] 0.6301182 0.8326189

使用 、 和 函数str通常classattributes帮助您弄清楚如何从对象中获取您想要的东西。

于 2013-01-18T17:32:39.890 回答