修改lm或黄土函数以在ggplot2的geom_smooth中使用它 [英] modify lm or loess function to use it within ggplot2's geom_smooth
问题描述
我需要修改 lm
(或最终 loess
)函数,以便在ggplot2的 geom_smooth
(或 stat_smooth
)。
例如,这是如何 stat_smooth
正常使用:
> qplot(data = diamonds,carat,price,facets =〜clarity)+ stat_smooth(method ='lm')`
我想定义一个自定义的 lm2
函数作为方法
参数的值c $ c> stat_smooth ,所以我可以自定义它的行为。
> lm2 < - 函数(公式,数据,...)
{
print(head(data))
return(lm(formula,data,...))
}
> qplot(data = diamonds,carat,price,facets =〜clarity)+ stat_smooth(method ='lm2')
请注意,我在 stat_smooth
中使用了 method ='lm2'
作为参数。
当我执行这段代码时出现错误:
lockquote
eval中的错误(expr,envir,enclos):'nthcdr'需要一个列表来减少CDR
我不明白这一点。 lm2
方法在 stat_smooth
之外运行时效果很好。我玩了这一点,并且出现了不同类型的错误,但是由于我不熟悉R的调试工具,所以我很难调试它们。老实说,我没有得到我应该放在 return()
调用中的内容。
使用...作为函数调用中的一个参数,我不完全理解(它与...作为列表类型对象有关)有一些奇怪之处。
这是一个通过将函数调用作为对象工作的版本,设置要调用的函数为lm,然后在我们自己的调用者的上下文中计算调用。这个评估的结果是我们的返回值(在R中,函数中最后一个表达式的值是返回的值,所以我们不需要明确的 return
)。
foo < - 函数(公式,数据,...){
print(head(data))
x <-match.call()
x [[1]]< -quote(lm)
eval.parent(x)
}
$ c
如果你想给lm调用添加参数,你可以这样做:
x $ na.action< - 'na.exclude'
如果您想在调用lm之前将参数放到foo中,您可以像这样做
x $无用< - NULL
顺便说一句, geom_smooth
和 stat_smooth
传递任何额外的参数给smoothing函数,所以如果你只需要设置一些额外的参数,你不需要创建自己的函数
qplot(data = diamonds,carat,price,facets =〜cla rity)+
stat_smooth(method =loess,span = 0.5)
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.
解决方案 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)
这篇关于修改lm或黄土函数以在ggplot2的geom_smooth中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!