错误“未找到对象”当使用“alm()”时在函数内部但不在函数外部

发布于 2025-01-14 22:26:31 字数 658 浏览 2 评论 0原文

我使用 greybox 包中的 alm() 函数来拟合位置和比例模型。但是,每当我将变量传递给 alm()formula = 参数时,我都会收到错误。这发生在函数内部。外部函数可以工作。返回的错误是“as.formula(form) 中的错误:找不到对象“form””。下面是一个可重现的示例。有修复的想法吗? 可重现的例子:

library(greybox)
df <- data.frame(x = rnorm(100),
                 y = rnorm(100))

test_string = "y ~ x"

alm(as.formula(test_string), data = df)
#Passing formula to alm() works!

test_fun = function(form, df){
  #alm(scaled_meaning ~ short_dis_km, data = x)
  alm(as.formula(form), data = df)
}

test_fun(form = test_string, df = df)
#Passing formula to alm() does not work!

I'm using the alm() function from the greybox package to fit a location and scale model. However, I get an error whenever I pass a variable to the formula = argument of alm(). This only happens inside functions. Outside functions it work. The error returned is "Error in as.formula(form) : object 'form' not found". Below is a reproducible example. Any ideas for a fix?
Reproducible example:

library(greybox)
df <- data.frame(x = rnorm(100),
                 y = rnorm(100))

test_string = "y ~ x"

alm(as.formula(test_string), data = df)
#Passing formula to alm() works!

test_fun = function(form, df){
  #alm(scaled_meaning ~ short_dis_km, data = x)
  alm(as.formula(form), data = df)
}

test_fun(form = test_string, df = df)
#Passing formula to alm() does not work!

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

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

发布评论

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

评论(2

初吻给了烟 2025-01-21 22:26:31

这可行,但这是一项肮脏的工作。
我认为这是一个与全球环境或其他相关的问题。如果将 form 声明为全局变量,则可以避免此问题。但它并不干净。

library(greybox)
df <- data.frame(x = rnorm(100),
                 y = rnorm(100))

test_string = "y ~ x"

alm(as.formula(test_string), data = df)
#Passing formula to alm() works!

test_fun = function(form, df){
  #alm(scaled_meaning ~ short_dis_km, data = x)
  form <<- form
  f<-alm(as.formula(form), data = df)
  rm(form)
  f
}

test_fun(form = test_string, df = df)
#Passing formula to alm() does not work!

This works, but it is dirty work.
I think it is an issue related to GlobalEnvironment or something. If you declare form as a Global Variable you can avoid this problem. But it is not clean.

library(greybox)
df <- data.frame(x = rnorm(100),
                 y = rnorm(100))

test_string = "y ~ x"

alm(as.formula(test_string), data = df)
#Passing formula to alm() works!

test_fun = function(form, df){
  #alm(scaled_meaning ~ short_dis_km, data = x)
  form <<- form
  f<-alm(as.formula(form), data = df)
  rm(form)
  f
}

test_fun(form = test_string, df = df)
#Passing formula to alm() does not work!
ˉ厌 2025-01-21 22:26:31

在包修复范围错误之前,您可以包装在 do.call 中,由于某种原因,这修复了范围问题:

library(greybox)
df <- data.frame(x = rnorm(100),
                 y = rnorm(100))

test_string = "y ~ x"

alm(as.formula(test_string), data = df)
#Passing formula to alm() works!

test_fun = function(form, df){
  #alm(scaled_meaning ~ short_dis_km, data = x)
  alm(as.formula(form), data = df)
}

test_fun(form = test_string, df = df)
#Passing formula to alm() does not work!

test_fun2 = function(form, df){
  #alm(scaled_meaning ~ short_dis_km, data = x)
  do.call(alm,list(as.formula(form),data=df))
}

test_fun2(form = test_string, df=df)
# This one works!

Until the package fixes the scoping error, you can wrap in do.call, for some reason this fixes the scoping issue:

library(greybox)
df <- data.frame(x = rnorm(100),
                 y = rnorm(100))

test_string = "y ~ x"

alm(as.formula(test_string), data = df)
#Passing formula to alm() works!

test_fun = function(form, df){
  #alm(scaled_meaning ~ short_dis_km, data = x)
  alm(as.formula(form), data = df)
}

test_fun(form = test_string, df = df)
#Passing formula to alm() does not work!

test_fun2 = function(form, df){
  #alm(scaled_meaning ~ short_dis_km, data = x)
  do.call(alm,list(as.formula(form),data=df))
}

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