R:当在geom_smooth的自定义函数中使用时,不会选择其他参数 [英] R: nls not picking up additional arguments when used in custom function in geom_smooth
问题描述
这是一个涉及我以前的问题的问题具有facet_grid和不同拟合功能的geom_smooth 。在那个问题中,我试图在ggplot2的facet网格中的每个方面使用 geom_smooth
中的不同拟合函数。 Marco Sandri 善意地提供了一个答案,我试图适应使用用户定义的公式而不是现有的公式(例如, lm
, loess
)。这是我的代码。
This is a question that relates to my earlier question geom_smooth with facet_grid and different fitting functions. In that question, I was trying to use a different fitting function in geom_smooth
for each facet in a facet grid for ggplot2. Marco Sandri kindly provided an answer that I am trying to adapt to use user-defined formulas rather than existing formulas (e.g., lm
, loess
). Here is the my code.
# Load library
library(ggplot2)
# Load data
data(mtcars)
# Smoothing function with different behaviour depending on the panel
custom.smooth <- function(formula, data,...){
smooth.call <- match.call()
if(as.numeric(unique(data$PANEL)) == 6) {
# Nonlinear regression
method.name <- eval(parse(text="nls"))
# Specify formula
formula <- as.formula("y ~ a * x^b")
# Add initial parameters
smooth.call[["start"]] <- c(a = 10, b = -0.5)
}else{
# Linear regression
method.name <- eval(parse(text="lm"))
}
# Add function name
smooth.call[[1]] <- method.name
# Perform fit
eval.parent(smooth.call)
}
# Plot data with custom fitting function
p <- ggplot(mtcars,aes(x = disp, y = mpg)) + geom_point() + facet_grid(gear ~ am)
p <- p + geom_smooth(method = "custom.smooth")
print(p)
,我定义了一个函数 custom.smooth
来选择适合的模型。在这个例子中,所有的模型都是线性回归,除了面板6,这是一个用户定义的函数 y〜a * x ^ b
。运行此代码会产生以下错误:
$ b
In this code, I define a function custom.smooth
that chooses the model to be fit. In this example, all models are linear regressions except for panel 6, which is a user-defined function y ~ a*x^b
. Running this code gives the error:
警告消息:计算在
stat_smooth()中失败
:单数
初始参数估计的梯度矩阵
Warning message: Computation failed in
stat_smooth()
: singular gradient matrix at initial parameter estimates
然而,当我运行 nls code>对这些初始参数的面板6中的数据我没有这样的错误(即,
nls(mpg〜a * disp ^ b,mtcars%>%filter(gear == 5 ,am == 1),start = c(a = 10,b = -0.5))
)。这让我觉得 nls
没有看到我指定的开始值。我也尝试在 geom_smooth
函数中指定这些参数,如下所示:
Nevertheless, when I run nls
on the data in panel 6 with these initial parameters I get no such error (i.e., nls(mpg ~ a * disp^b, mtcars %>% filter(gear == 5, am == 1), start = c(a = 10, b = -0.5))
). This makes me think that nls
isn't seeing the start values I specify. I have also tried specifying these parameters in the geom_smooth
function like this:
p <- p + geom_smooth(method = "custom.smooth", method.args = list(start = c(a = 10, b = -0.5)))
但我碰到同样的问题。任何想法如何让我的初始值为 nls
?还是有另一个原因为什么代码不工作?
but I run into the same issue. Any ideas how I can get my start values to nls
? Or is there another reason why the code isn't working?
推荐答案
这是解决方案,它极大地受益于这篇文章。我不知道为什么以前的版本没有工作,但这似乎工作正常。
Here's the solution, which greatly benefited from this post. I don't know why the previous version didn't work, but this seems to work fine.
# Load library
library(ggplot2)
# Load data
data(mtcars)
# Smoothing function with different behaviour depending on the panel
custom.smooth <- function(formula, data,...){
smooth.call <- match.call()
if(as.numeric(unique(data$PANEL)) == 6) {
# Nonlinear regression
smooth.call[[1]] <- quote(nls)
# Specify formula
smooth.call$formula <- as.formula("y ~ a * x ^ b")
# Add initial parameters
smooth.call$start <- c(a = 300, b = -0.5)
}else{
# Linear regression
smooth.call[[1]] <- quote(lm)
}
# Perform fit
eval.parent(smooth.call)
}
# Plot data with custom fitting function
p <- ggplot(mtcars,aes(x = disp, y = mpg)) + geom_point() + facet_grid(gear ~ am)
p <- p + geom_smooth(method = "custom.smooth", se = FALSE)
print(p)
这篇关于R:当在geom_smooth的自定义函数中使用时,不会选择其他参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!