如何将变量粘贴到公式中

发布于 2025-02-11 00:44:24 字数 1474 浏览 1 评论 0原文

当我尝试将变量粘贴到公式中时,我会遇到一个特殊的错误。我有以下变量:

# Choose explanatory variables 
vars_int <- c("age", 
              "male", 
              "race_ethnicity", 
              "patient_regional_location", 
              "marital_status", 
              "total_admits", 
              "chf", 
              "copd", 
              "diabwc", 
              "mld", 
              "cevd",
              "Obesity", 
              "Smoking")

当我将它们直接复制到公式中时,一切都很好:


# Generate inverse probability weights 
wts_17 <-  ipwpoint(
  exposure = exposure,
  family = "binomial",  
  link = "logit",
  denominator = ~ 
    age + 
    male + 
    race_ethnicity + 
    patient_regional_location + 
    marital_status + 
    total_admits + 
    chf + 
    copd + 
    diabwc + 
    mld + 
    cevd + 
    Obesity +
    Smoking,   
  data = df_17)

但是,当我尝试使用过去的功能进行操作时,一切都会崩溃:


paste("~", paste(vars_int, collapse = " + "))
[1] "~ age + male + race_ethnicity + patient_regional_location + marital_status + total_admits + chf + copd + diabwc + mld + cevd + Obesity + Smoking"

> wts_17 <-  ipwpoint(
+   exposure = exposure,
+   family = "binomial",  
+   link = "logit",
+   denominator = paste("~", paste(vars_int, collapse = " + ")),  
+   data = df_17)
Error in ipwpoint(exposure = exposure, family = "binomial", link = "logit",  : 
  Invalid denominator formula specified

有人知道为什么会发生这种情况吗?

I'm getting a peculiar error when I try to paste variables into my formula. I have the following variables:

# Choose explanatory variables 
vars_int <- c("age", 
              "male", 
              "race_ethnicity", 
              "patient_regional_location", 
              "marital_status", 
              "total_admits", 
              "chf", 
              "copd", 
              "diabwc", 
              "mld", 
              "cevd",
              "Obesity", 
              "Smoking")

When I copy them directly into a formula, everything works fine:


# Generate inverse probability weights 
wts_17 <-  ipwpoint(
  exposure = exposure,
  family = "binomial",  
  link = "logit",
  denominator = ~ 
    age + 
    male + 
    race_ethnicity + 
    patient_regional_location + 
    marital_status + 
    total_admits + 
    chf + 
    copd + 
    diabwc + 
    mld + 
    cevd + 
    Obesity +
    Smoking,   
  data = df_17)

But then when I try to do it using a past function, everything falls apart:


paste("~", paste(vars_int, collapse = " + "))
[1] "~ age + male + race_ethnicity + patient_regional_location + marital_status + total_admits + chf + copd + diabwc + mld + cevd + Obesity + Smoking"

> wts_17 <-  ipwpoint(
+   exposure = exposure,
+   family = "binomial",  
+   link = "logit",
+   denominator = paste("~", paste(vars_int, collapse = " + ")),  
+   data = df_17)
Error in ipwpoint(exposure = exposure, family = "binomial", link = "logit",  : 
  Invalid denominator formula specified

Does anyone know why this is happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文