将对象传递到用户定义的功能中

发布于 2025-01-25 01:13:05 字数 384 浏览 2 评论 0原文

我想创建一个用户定义的函数,以在变量列表上运行Coxph。这个公式运行良好:


summary(coxph(
  Surv(var1, factor(var1)) ~ var3, 
  data = df_wts))

但是,如果我尝试将其转换为函数,我会继续得到“未找到的对象”警告:


cox_fun <- function(x, y) {
summary(coxph(
  Surv(x, factor(y)) ~ var3, 
  data = df_wts))

尝试了所有内容,Enquo(x),{{}},[[]],! ,as.name()等。

关于为什么会发生这种情况的任何解释?环境?谢谢!

I would like to create a user defined function to run coxph over a list of variables. This formula runs just fine:


summary(coxph(
  Surv(var1, factor(var1)) ~ var3, 
  data = df_wts))

But then if I try to turn it into a function, I keep getting an "object not found" warning:


cox_fun <- function(x, y) {
summary(coxph(
  Surv(x, factor(y)) ~ var3, 
  data = df_wts))

Have tried everything, enquo(x), {{}}, [[]], !!, as.name(), etc.

Any explanation for why this is happening? Environment? Thanks!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九八野马 2025-02-01 01:13:05

首先,您的var3似乎没有定义。您的var1是您的第二个代码xy有点奇怪,最后您应该用} 。尝试此代码:

cox_fun <- function(x, y) {
  summary(coxph(
    Surv(x, factor(x)) ~ y, 
    data = df_wts))}

First your var3 seems not defined. Your var1 is your second code x and y which is a bit strange and finally you should end the function with }. Try this code:

cox_fun <- function(x, y) {
  summary(coxph(
    Surv(x, factor(x)) ~ y, 
    data = df_wts))}
紫罗兰の梦幻 2025-02-01 01:13:05

弄清楚了。主要问题是(我认为)环境没有解决。我通过添加对数据帧本身的调用来修复它。不确定这是否有用,但是以下是更新的代码

添加了一些额外的内容:

我想只输入一个值,所以我命名了具有粘贴函数的变量

,然后我添加了数据库来确保实际处理过的cover() 然后正确地

将功能应用于我的变量列表上。干杯!


# Calculate Cox Function 
cox_fun <- function(i) {
  x <- paste0("fu_time_", enexprs(i))
  y <- paste0("fu_status_", enexprs(i))
  cox <- summary(coxph(
    Surv(df_wts[[x]], factor(df_wts[[y]]))~ prophied + 
      age + male + race_ethnicity + patient_regional_location + Obesity + Smoking + total_admits + chf + copd + diabwc + mld + cevd, 
    weights = ipw, 
    id = patient_id, 
    data = df_wts))
  tribble(~Outcome, ~`Odds Ratio`, ~`95% CI`, ~`P Value`,
          paste(i),
          round(cox$conf.int[1,1], digits = 2),
          paste0(round(cox$conf.int[1,3], digits = 2), "-", round(cox$conf.int[1,4], digits = 2)),
          round(cox$coefficients[1,6], digits = 2))
  
}

# Generate DF 
do.call(rbind, lapply(ade_int, cox_fun)) %>% 
  #write_csv(paste(Sys.Date(), "Table 3.csv")) 
  print(n=50)

figured this out. Main issue was (I think) that the environment wasn't carrying over. I fixed it by adding in calls to the dataframe itself. Not sure if this is helpful, but here's the updated code

A few extra things added:

I wanted to just input a single value, so I named variables with a paste function

Then I added that with the database to make sure Surv() actually processed it properly

Then a quick function to apply the function over my list of variables. Cheers!


# Calculate Cox Function 
cox_fun <- function(i) {
  x <- paste0("fu_time_", enexprs(i))
  y <- paste0("fu_status_", enexprs(i))
  cox <- summary(coxph(
    Surv(df_wts[[x]], factor(df_wts[[y]]))~ prophied + 
      age + male + race_ethnicity + patient_regional_location + Obesity + Smoking + total_admits + chf + copd + diabwc + mld + cevd, 
    weights = ipw, 
    id = patient_id, 
    data = df_wts))
  tribble(~Outcome, ~`Odds Ratio`, ~`95% CI`, ~`P Value`,
          paste(i),
          round(cox$conf.int[1,1], digits = 2),
          paste0(round(cox$conf.int[1,3], digits = 2), "-", round(cox$conf.int[1,4], digits = 2)),
          round(cox$coefficients[1,6], digits = 2))
  
}

# Generate DF 
do.call(rbind, lapply(ade_int, cox_fun)) %>% 
  #write_csv(paste(Sys.Date(), "Table 3.csv")) 
  print(n=50)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文