简体   繁体   English

在 function 中按组使用 lapply,在 R 中使用 2 arguments

[英]Use lapply by group in a function with 2 arguments in R

I would like to calculate the roc of several variables in different groups, fixing only the response variable, here's what I've been trying below:我想计算不同组中几个变量的 roc,只修复响应变量,这是我在下面尝试的:

library(pROC)
data(aSAH)
lapply(dplyr::select(aSAH,c(s100b,ndka)),roc,response = aSAH$outcome)

And output:和 output:

$s100b

Call:
roc.default(response = ..1, predictor = X[[i]])

Data: X[[i]] in 72 controls (..1 Good) < 41 cases (..1 Poor).
Area under the curve: 0.7314

$ndka

Call:
roc.default(response = ..1, predictor = X[[i]])

Data: X[[i]] in 72 controls (..1 Good) < 41 cases (..1 Poor).
Area under the curve: 0.612

But i need to apply this in each gender and in selected variables.但我需要在每个性别和选定的变量中应用它。 Just like a group_by(gender) %>% roc()就像group_by(gender) %>% roc()

Thanks!谢谢!

Using by . by .

by(aSAH, aSAH$gender, function(x) 
  lapply(x[c("s100b", "ndka")], function(y) roc(y, response=x$outcome)))

# aSAH$gender: Male
# $s100b
# 
# Call:
#   roc.default(response = x$outcome, predictor = y)
# 
# Data: y in 22 controls (x$outcome Good) < 20 cases (x$outcome Poor).
# Area under the curve: 0.7727
# 
# $ndka
# 
# Call:
#   roc.default(response = x$outcome, predictor = y)
# 
# Data: y in 22 controls (x$outcome Good) < 20 cases (x$outcome Poor).
# Area under the curve: 0.5523
# 
# --------------------------------------------------- 
#   aSAH$gender: Female
# $s100b
# 
# Call:
#   roc.default(response = x$outcome, predictor = y)
# 
# Data: y in 50 controls (x$outcome Good) < 21 cases (x$outcome Poor).
# Area under the curve: 0.72
# 
# $ndka
# 
# Call:
#   roc.default(response = x$outcome, predictor = y)
# 
# Data: y in 50 controls (x$outcome Good) < 21 cases (x$outcome Poor).
# Area under the curve: 0.6671

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM