https://scholar.princeton.edu/sites/default/files/bstewart/files/srg_crossval.pdf
在这些幻灯片之后,代码使用了'%<>%'运算符。
有人知道这是什么或使用什么包吗?
代码读取
# Function to divide data into folds randomly
fold <- function(data, k) {
data <- data[sample(nrow(data)),] # Shuffle data
data %<>% mutate(fold = cut(seq(1:nrow(data)), breaks = k, labels=FALSE))
return(data)
}
# Function to cross-validate data on given model (curried)
cv.predict.logit <- function(data, dv, model.fx, k) {
data %<>% fold(k) # Fold data
aucs <- c()
for(i in 1:k) {
# Divide data into train and test sets
train <- data %>% filter(fold != i)
test <- data %>% filter(fold == i)
# Estimate model on training data
mx <- model.fx(data=train)
# Predict on test data and calculate AUC
preds <- predict(mx, newdata=test, type="response")
AUC <- somers2(preds, test[[dv]])[1]
aucs[i] <- AUC
}
return(mean(aucs, na.rm=TRUE)) # Yield mean AUC
}
# Function to rerun CV results n times and average AUCs
crossval <- function(data, dv, model.fx, k, n) {
aucs <- replicate(n, cv.predict.logit(data, dv, model.fx, k))
return(aucs)
}