如何用文本而不是表达式来提供 deparse 解析序列? ipwpoint函数的分母入口
how to feed a deparse parse sequence with text instead of expression ? denominator entry of ipwpoint function
我在使用 ipw
包中的 ipwpoint
函数时遇到问题。
这是一个函数 ipwpoint
:
的工作示例
library(ipw)
df <- data.frame(ttt = sample(0:1,500,replace = T),
cofounder1 = rnorm(500),
cofounder2 = rnorm(500))
ipwt <- ipwpoint(
exposure = ttt ,
family = "binomial",
link = "logit",
numerator = ~ 1,
denominator = ~cofounder1 + cofounder2 ,
data = df
)
现在假设我想定义相同的回归,但分母来自变量名称的向量
vars <- c("cofounder1","cofounder2")
我想我可以使用一个公式:
formula <- as.formula(paste0("~",paste0(vars,collapse = "+")))
ipwt <- ipwpoint(
exposure = ttt ,
family = "binomial",
link = "logit",
numerator = ~ 1,
denominator = formula,
data = df
)
但这不起作用:
Error in eval(parse(text = paste(deparse(tempcall$exposure, width.cutoff = 500), :
object 'tttformula' not found
因为该函数所做的是解析一个表达式,然后将其粘贴到 exposure
,然后再次解析并计算它,请参阅 https://github.com/cran/ipw/blob/master/R/ipwpoint.R#L87。所以它粘贴了“公式”而不是实际的公式。
使用文字也不行:
ipwt <- ipwpoint(
exposure = ttt ,
family = "binomial",
link = "logit",
numerator = ~ 1,
denominator = paste0("~",paste0(vars,collapse = "+")),
data = df
)
如何从 c("cofounder1","cofounder2")
向量定义回归?
如何将整个函数调用解析为字符串?:
function_call<-paste0("ipwt <- ipwpoint(
exposure = ttt ,
family = \"binomial\",
link = \"logit\",
numerator = ~ 1,
denominator = ~",vars[1]," + ",vars[2]," ,
data = df
)")
eval(parse(text = function_call))
我在使用 ipw
包中的 ipwpoint
函数时遇到问题。
这是一个函数 ipwpoint
:
library(ipw)
df <- data.frame(ttt = sample(0:1,500,replace = T),
cofounder1 = rnorm(500),
cofounder2 = rnorm(500))
ipwt <- ipwpoint(
exposure = ttt ,
family = "binomial",
link = "logit",
numerator = ~ 1,
denominator = ~cofounder1 + cofounder2 ,
data = df
)
现在假设我想定义相同的回归,但分母来自变量名称的向量
vars <- c("cofounder1","cofounder2")
我想我可以使用一个公式:
formula <- as.formula(paste0("~",paste0(vars,collapse = "+")))
ipwt <- ipwpoint(
exposure = ttt ,
family = "binomial",
link = "logit",
numerator = ~ 1,
denominator = formula,
data = df
)
但这不起作用:
Error in eval(parse(text = paste(deparse(tempcall$exposure, width.cutoff = 500), : object 'tttformula' not found
因为该函数所做的是解析一个表达式,然后将其粘贴到 exposure
,然后再次解析并计算它,请参阅 https://github.com/cran/ipw/blob/master/R/ipwpoint.R#L87。所以它粘贴了“公式”而不是实际的公式。
使用文字也不行:
ipwt <- ipwpoint(
exposure = ttt ,
family = "binomial",
link = "logit",
numerator = ~ 1,
denominator = paste0("~",paste0(vars,collapse = "+")),
data = df
)
如何从 c("cofounder1","cofounder2")
向量定义回归?
如何将整个函数调用解析为字符串?:
function_call<-paste0("ipwt <- ipwpoint(
exposure = ttt ,
family = \"binomial\",
link = \"logit\",
numerator = ~ 1,
denominator = ~",vars[1]," + ",vars[2]," ,
data = df
)")
eval(parse(text = function_call))