修改 lm 或 loess 函数以在 ggplot2 的 geom_smooth 中使用它

发布于 2024-08-23 14:23:02 字数 965 浏览 1 评论 0原文

我需要修改 lm (或最终 loess)函数,以便我可以在 ggplot2 的 geom_smooth (或 stat_smooth)中使用它>)。

例如,这就是 stat_smooth 的正常使用方式:

> qplot(data=diamonds, carat, price, facets=~clarity) + stat_smooth(method='lm')`

我想定义一个自定义 lm2 函数,用作 method 参数的值stat_smooth,这样我就可以自定义它的行为。

> lm2 <- function(formula, data, ...)
  {
      print(head(data))
      return(lm(formula, data, ...))
  }
> qplot(data=diamonds, carat, price, facets=~clarity) + stat_smooth(method='lm2')

请注意,我在 stat_smooth 中使用了 method='lm2' 作为参数。 当我执行此代码时出现错误:

eval(expr, envir, enclos) 中的错误:“nthcdr”需要一个 CDR 列表

,我不太明白。在 stat_smooth 之外运行时,lm2 方法运行良好。我玩了一下,遇到了不同类型的错误,但由于我对 R 的调试工具不太熟悉,所以很难调试它们。老实说,我不明白应该在 return() 调用中放入什么内容。

I need to modify the lm (or eventually loess) function so I can use it in ggplot2's geom_smooth (or stat_smooth).

For example, this is how stat_smooth is used normally:

> qplot(data=diamonds, carat, price, facets=~clarity) + stat_smooth(method='lm')`

I would like to define a custom lm2 function to use as value for the method parameter in stat_smooth, so I can customize its behaviour.

> lm2 <- function(formula, data, ...)
  {
      print(head(data))
      return(lm(formula, data, ...))
  }
> qplot(data=diamonds, carat, price, facets=~clarity) + stat_smooth(method='lm2')

Note that I have used method='lm2' as parameter in stat_smooth.
When I execute this code a get the error:

Error in eval(expr, envir, enclos) : 'nthcdr' needs a list to CDR down

Which I don't understand very well. The lm2 method works very well when run outside of stat_smooth. I played with this a bit and I have got different types of error, but since I am not comfortable with R's debug tools it is difficult for me to debug them. Honestly, I don't get what I should put inside the return() call.

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

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

发布评论

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

评论(1

顾忌 2024-08-30 14:23:02

在函数调用中使用 ... 作为参数有一些奇怪的地方,我不完全理解(它与 ... 作为列表类型对象有关)。

这是一个版本,它的工作原理是将函数调用作为对象,将要调用的函数设置为 lm,然后在我们自己的调用者的上下文中评估该调用。计算的结果就是我们的返回值(在 R 中,函数中最后一个表达式的值就是返回值,因此我们不需要显式的返回值)。

foo <- function(formula,data,...){
   print(head(data))
   x<-match.call()
   x[[1]]<-quote(lm)
   eval.parent(x)
}

如果你想在 lm 调用中添加参数,你可以这样做:

x$na.action <- 'na.exclude'

如果你想在调用 lm 之前删除 foo 的参数,你可以这样做

x$useless <- NULL

顺便说一句,geom_smoothstat_smooth 将任何额外参数传递给平滑函数,因此如果您只需要设置一些额外参数,则无需创建自己的函数

qplot(data=diamonds, carat, price, facets=~clarity) + 
  stat_smooth(method="loess",span=0.5)

There is some weirdness in using ... as an argument in a function call that I don't fully understand (it has something to do with ... being a list-type object).

Here is a version that works by taking the function call as an object, setting the function to be called to lm and then evaluating the call in the context of our own caller. The result of this evaluation is our return value (in R the value of the last expression in a function is the value returned, so we do not need an explicit return).

foo <- function(formula,data,...){
   print(head(data))
   x<-match.call()
   x[[1]]<-quote(lm)
   eval.parent(x)
}

If you want to add arguments to the lm call, you can do it like this:

x$na.action <- 'na.exclude'

If you want to drop arguments to foo before you call lm, you can do it like this

x$useless <- NULL

By the way, geom_smooth and stat_smooth pass any extra arguments to the smoothing function, so you need not create a function of your own if you only need to set some extra arguments

qplot(data=diamonds, carat, price, facets=~clarity) + 
  stat_smooth(method="loess",span=0.5)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文