xchen95 2025-06-20 11:43 采纳率: 0%
浏览 6

log报错说:错误于eval(expr, p): 找不到对象'f_str,如何解决?(关键词-一致性)

为了保证输出结果的一致性,我将MASS::polr()放进了一个函数,然后对输出的结果进行了一定的调整。log报错说:错误于eval(expr, p): 找不到对象'f_str'。“模型拟合完成”也被print在了log窗口里,貌似是broom::tidy的问题,请问这个错误的原因是什么以及怎么解决?


model_ordinal_regression <- function(data, y, x, f_x=NA, digits_coef=3, digits_p=3) {
  # Piece up the function
  if(is.na(f_x)){
    f_str <- paste("`", y, "` ~ `", paste(x, collapse = "` + `"), "`", sep="")
  }else{
    f_str <- paste("`", y, "` ~ ", f_x, sep="")
  }

  
  # Modeling
  olr.fit = MASS::polr(as.formula(f_str), data=data)
  print("模型拟合完成")
  
  # coefficients
  print("系数提取中...")
  f = as.formula(f_str)
  out.coef.raw = broom::tidy(olr.fit, conf.int = TRUE, conf.level = 0.95) |>
    mutate(`P-value` = 2 * (1 - pnorm(abs(statistic)))) |>
    rename(
      Variable = term,
      `Coefficient` = estimate,
      `Standar Error` = std.error,
      `Z-value` = statistic,
      `Lower 95% CI` = conf.low,
      `Upper 95% CI` = conf.high
    )
  print("系数提取完成")
  
  # report
  out.coef.report = out.coef.raw |>
    mutate(
      `Odds Ratio` = style_number(exp(`Coefficient`), digits = digits_coef), 
      `Coefficient (95% CI)` = paste(style_number(`Coefficient`, digits = digits_coef),
                                     " (", style_number(`Lower 95% CI`, digits = digits_coef), ", ",
                                     style_number(`Upper 95% CI`, digits = digits_coef), ")", sep=""),
      `P-value` = style_pvalue(`P-value`, digits = digits_p)
    ) |> 
    dplyr::select(`Variable`, `Odds Ratio`, `Coefficient (95% CI)`, `P-value`)
  print("报告表生成完成")
  
  # Porportional Odds Assumption is necessary
  assumption = brant(olr.fit)
  print("假设检验完成")
  
  # 输出结果
  out_list = list(
    "coef" = out.coef.raw,
    "report" = out.coef.report,
    "assumption" = assumption,
    "model" = olr.fit
  )
  print("输出结果完成")
  
  return(out_list)
}

summary = model_ordinal_regression(seer_multi, "SurvivalStatus", NA, f_x = "Sex + Race + Grade + Stage + PrimarySite + Chemotherapy")

img

  • 写回答

5条回答 默认 最新

  • 阿里嘎多学长 2025-06-20 11:43
    关注

    阿里嘎多学长整理AIGC生成,因移动端显示问题导致当前答案未能完全显示,请使用PC端查看更加详细的解答过程

    解决方案

    你遇到的问题是R语言中的log报错,错误信息是:错误于eval(expr, p): 找不到对象'f_str'。这个错误通常是因为在R语言中,eval()函数无法找到对象'f_str'。

    在你的情况下,你将MASS::polr()函数放进了一个函数,然后对输出的结果进行了一定的调整,这可能会导致eval()函数无法找到对象'f_str'。

    解决方案是:

    1. 检查你的代码是否正确无误,确保你没有在eval()函数中使用了错误的对象名称。
    2. 如果你确实使用了错误的对象名称,可以尝试使用get()函数来获取对象'f_str'的值,然后使用eval()函数来执行表达式。
    3. 如果你使用了MASS::polr()函数,可能需要检查函数的参数和返回值是否正确。

    以下是一个简单的示例代码,演示如何使用get()函数来获取对象'f_str'的值:

    f_str <- "hello"
    expr <- "f_str"
    p <- list()
    eval(expr, p)
    

    在上面的代码中,我们首先定义了对象f_str,然后使用eval()函数来执行表达式f_str。由于get()函数可以获取对象的值,所以eval()函数可以正确地找到对象'f_str'。

    希望这个解决方案能够帮助你解决问题。如果你仍然遇到问题,可以提供更多的代码和信息,我将尽力帮助你解决问题。

    评论

报告相同问题?

问题事件

  • 创建了问题 6月20日