首页 > 解决方案 > 什么包包含 R 中的 '%<>%' 运算符

问题描述

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)
}

标签: r

解决方案


这是包中的一个分配管道magrittr

将对象转发到函数或调用表达式中,并使用结果值更新 lhs 对象。


推荐阅读