R:nls在geom_smooth中的自定义函数中使用时不拾取附加参数

R:nls在geom_smooth中的自定义函数中使用时不拾取附加参数,r,ggplot2,nls,R,Ggplot2,Nls,这是一个与我先前的问题有关的问题。在这个问题上,我尝试在ggplot2的镶嵌面网格中为每个镶嵌面使用不同的拟合函数。请提供一个答案,我正试图适应使用用户定义的公式,而不是现有的公式(例如,lm,lm)。这是我的密码 # Load library library(ggplot2) # Load data data(mtcars) # Smoothing function with different behaviour depending on the panel custom.smooth

这是一个与我先前的问题有关的问题。在这个问题上,我尝试在ggplot2的镶嵌面网格中为每个镶嵌面使用不同的拟合函数。请提供一个答案,我正试图适应使用用户定义的公式,而不是现有的公式(例如,
lm
lm
)。这是我的密码

# 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)

但我遇到了同样的问题。你知道如何将我的起始值设置为
nls
?或者代码不起作用还有其他原因吗?

这是一个解决方案,从中受益匪浅。我不知道为什么以前的版本不起作用,但这似乎很好

# 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)
#加载库
图书馆(GG2)
#加载数据
数据(mtcars)
#根据面板的不同,具有不同行为的平滑功能

我不认为这是你的出发点。即使使用
nls
(将返回警告)中的默认起始值,模型也会收敛,而且通过
geom_smooth
nls
线绘制到档位5 am 1数据而不给出起始值,效果很好。你说得对。我没有检查过。嗯。线性拟合是有效的,我是这样指定公式的吗?以前的版本不起作用,因为你没有用新公式更新调用,你只是创建了一个名为
formula
的变量,但没有对它做任何操作。啊。我以为函数被传递了一个名为
formula
的变量,所以我需要做的就是更改它的值。你的函数帮了我很多,但现在我有另一个问题,也许你已经有了解决方案,请看这篇文章:
# 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)