如何将输入传递到用户定义函数中的命名列表
How to pass input into a named list within a user-defined function
我正在尝试创建一个用户定义的函数,用于从逻辑回归模型中输出比值比 (95% CI)。
我基本上停留在第 2 行,在那里我使用 oddsratio::or_glm()
函数预测优势比。我需要将预测器输入传递到参数之一的命名列表中。 paste()
方法似乎不起作用...
不知道有没有人可以帮忙?
这是函数:
compute_ors = function(outcome, predictor){
fit.glm = glm(paste(outcome, " ~ ", predictor), data = mydata, family = binomial)
x = oddsratio::or_glm(mydata, model = fit.glm, incr = list(predictor = 0.1), ci = 0.95)
## How can I pass the 'predictor' variable as a named list in the 'incr=' argument of the 'or_glm' function?
return(x)
}
compute_ors("died", "b_fi.score")
这是一个模拟数据:
library(oddsratio)
mydata = structure(list(died = c(1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L), b_fi.score = c(0.111111111111111,
0.0555555555555556, 0.0185185185185185, 0.148148148148148, 0.0555555555555556,
0.0277777777777778, 0.0277777777777778, 0.166666666666667, 0.0925925925925926,
0.0925925925925926, 0.0740740740740741, 0.166666666666667, 0.0185185185185185,
0.111111111111111, 0.0555555555555556, 0.101851851851852, 0.0925925925925926,
0.138888888888889, 0.0462962962962963, 0.0925925925925926, 0.0555555555555556,
0.0185185185185185, 0.0555555555555556, 0.259259259259259, 0.0925925925925926,
0.101851851851852, 0.0925925925925926, 0.0833333333333333, 0.0555555555555556,
0.111111111111111, 0.0555555555555556, 0.111111111111111, 0.111111111111111,
0.0925925925925926, 0.222222222222222, 0.0740740740740741, 0.037037037037037,
0.12962962962963, 0.0555555555555556, 0.148148148148148, 0.037037037037037,
0.12962962962963, 0.111111111111111, 0.0740740740740741, 0.0925925925925926,
0.0740740740740741, 0.0740740740740741, 0.175925925925926, 0.12962962962963,
0.0740740740740741)), row.names = c(61L, 88L, 140L, 155L, 162L,
234L, 260L, 466L, 552L, 567L, 618L, 643L, 754L, 817L, 912L, 921L,
928L, 978L, 989L, 995L, 1021L, 1031L, 1050L, 1064L, 1101L, 1156L,
1170L, 1180L, 1181L, 1206L, 1211L, 1221L, 1228L, 1230L, 1274L,
1276L, 1286L, 1290L, 1318L, 1329L, 1340L, 1495L, 1509L, 1546L,
1576L, 1661L, 1685L, 1703L, 1705L, 1714L), class = "data.frame")
您可以使用 setNames(list(0.1), predictor)
而不是 list(predictor = 0.1)
可以通过赋值 (:=
) 运算符
传入 dplyr::lst
compute_ors = function(outcome, predictor){
fit.glm = glm(reformulate(predictor, response = outcome),
data = mydata, family = binomial)
x = oddsratio::or_glm(mydata, model = fit.glm,
incr = dplyr::lst(!!predictor := 0.1), ci = 0.95)
return(x)
}
-测试
compute_ors("died", "b_fi.score")
predictor oddsratio ci_low (2.5) ci_high (97.5) increment
1 b_fi.score 1.993 0.186 14.559 0.1
我正在尝试创建一个用户定义的函数,用于从逻辑回归模型中输出比值比 (95% CI)。
我基本上停留在第 2 行,在那里我使用 oddsratio::or_glm()
函数预测优势比。我需要将预测器输入传递到参数之一的命名列表中。 paste()
方法似乎不起作用...
不知道有没有人可以帮忙?
这是函数:
compute_ors = function(outcome, predictor){
fit.glm = glm(paste(outcome, " ~ ", predictor), data = mydata, family = binomial)
x = oddsratio::or_glm(mydata, model = fit.glm, incr = list(predictor = 0.1), ci = 0.95)
## How can I pass the 'predictor' variable as a named list in the 'incr=' argument of the 'or_glm' function?
return(x)
}
compute_ors("died", "b_fi.score")
这是一个模拟数据:
library(oddsratio)
mydata = structure(list(died = c(1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L), b_fi.score = c(0.111111111111111,
0.0555555555555556, 0.0185185185185185, 0.148148148148148, 0.0555555555555556,
0.0277777777777778, 0.0277777777777778, 0.166666666666667, 0.0925925925925926,
0.0925925925925926, 0.0740740740740741, 0.166666666666667, 0.0185185185185185,
0.111111111111111, 0.0555555555555556, 0.101851851851852, 0.0925925925925926,
0.138888888888889, 0.0462962962962963, 0.0925925925925926, 0.0555555555555556,
0.0185185185185185, 0.0555555555555556, 0.259259259259259, 0.0925925925925926,
0.101851851851852, 0.0925925925925926, 0.0833333333333333, 0.0555555555555556,
0.111111111111111, 0.0555555555555556, 0.111111111111111, 0.111111111111111,
0.0925925925925926, 0.222222222222222, 0.0740740740740741, 0.037037037037037,
0.12962962962963, 0.0555555555555556, 0.148148148148148, 0.037037037037037,
0.12962962962963, 0.111111111111111, 0.0740740740740741, 0.0925925925925926,
0.0740740740740741, 0.0740740740740741, 0.175925925925926, 0.12962962962963,
0.0740740740740741)), row.names = c(61L, 88L, 140L, 155L, 162L,
234L, 260L, 466L, 552L, 567L, 618L, 643L, 754L, 817L, 912L, 921L,
928L, 978L, 989L, 995L, 1021L, 1031L, 1050L, 1064L, 1101L, 1156L,
1170L, 1180L, 1181L, 1206L, 1211L, 1221L, 1228L, 1230L, 1274L,
1276L, 1286L, 1290L, 1318L, 1329L, 1340L, 1495L, 1509L, 1546L,
1576L, 1661L, 1685L, 1703L, 1705L, 1714L), class = "data.frame")
您可以使用 setNames(list(0.1), predictor)
而不是 list(predictor = 0.1)
可以通过赋值 (:=
) 运算符
dplyr::lst
compute_ors = function(outcome, predictor){
fit.glm = glm(reformulate(predictor, response = outcome),
data = mydata, family = binomial)
x = oddsratio::or_glm(mydata, model = fit.glm,
incr = dplyr::lst(!!predictor := 0.1), ci = 0.95)
return(x)
}
-测试
compute_ors("died", "b_fi.score")
predictor oddsratio ci_low (2.5) ci_high (97.5) increment
1 b_fi.score 1.993 0.186 14.559 0.1