'How do I write a simple function that incorporates a function from an R package?
This runs fine when I specify everything, but just trying to generalize it a bit with "score" and "outcome" and it fails (see the end). Any idea how to do this? (I have the indices thing because I want to bootstrap this later)
library(PRROC)
df <- iris %>% filter(Species != "virginica") %>% mutate(outcome_versi = ifelse(Species == "versicolor", 1, 0)) %>% select(Sepal.Length, outcome_versi)
#Iris single AUC
fc <- function(data, indices){
d <- data[indices,]
versi.y <- d %>% filter(outcome_versi == 1) %>% select(Sepal.Length)
versi.n <- d %>% filter(outcome_versi == 0)%>% select(Sepal.Length)
prroc.sepal.length <-pr.curve(scores.class0 = versi.y$Sepal.Length, scores.class1 = versi.n$Sepal.Length, curve=T)
return(prroc.sepal.length$auc.integral)
}
fc(df)
#AUC = 0.94
#Iris single AUC - functionalized
fcf <- function(score, outcome, data, indices){
d <- data[indices,]
test.pos <- d %>% filter(outcome==1) %>% select(score)
test.neg <- d %>% filter(outcome==0) %>% select(score)
prroc.test <-pr.curve(scores.class0 = test.pos$score, scores.class1 = test.neg$score, curve=T)
return(prroc.test$auc.integral)
}
fcf(data=df, score=Sepal.Length, outcome = outcome_versi)
#Error: 'outcome' not found```
Solution 1:[1]
Does this work?
fcf <- function(score, outcome, data, indices){
d <- data[indices,]
test.pos <- d %>% filter(outcome==1) %>% select(all_of(score))
test.neg <- d %>% filter(outcome==0) %>% select(all_of(score))
prroc.test <-pr.curve(scores.class0 = test.pos$score, scores.class1 = test.neg$score, curve=T)
return(prroc.test$auc.integral)
}
fcf(data=df, score='Sepal.Length', outcome = 'outcome_versi')
I don't have the required package to test. But I assume it's because you've asked for a column in the df but that isn't a variable by itself.
N.B. if you have an older version of dplyr you might need to make use of rlang quasiquotation
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Quixotic22 |
