首页 文章

Geom_smooth:添加组时,迭代次数超过50次?

提问于
浏览
0

我在建模方面很陌生 . 我有三组数据(通过 period ),我想通过散点图上的线条显示 .

我想出了如何将我的方法和公式放在 geom_smooth 中,我能够显示一行 .

但是,当我想为每个组添加行数(可以通过 ggplot(.., aes(..,group = period)) 来完成)时,我收到了警告:

Warning message:
Computation failed in `stat_smooth()`:
number of iterations exceeded maximum of 50

并且不显示该行 .

我的工作代码:

ggplot(tab, aes(x=distance, y=grad)) + #
  geom_point() + theme_bw() +
  geom_smooth(method = "nls",
              formula = y ~ a*x^(-b),
              method.args = list(start=c(a=20, b=0.01)),  # 
              se = F)

结果:

enter image description here

代码提供错误(在 aes 中添加 group = period ),而不是每组显示行:

ggplot(tab, aes(x=distance, y=grad, group = period)) + #
  geom_point() + theme_bw() +
  geom_smooth(method = "nls",
              formula = y ~ a*x^(-b),
              method.args = list(start=c(a=20, b=0.01)),  # 
              se = F)

您是否有一些想法如何通过geom_smooth函数增加ggplot2中的迭代次数?我找到了一些信息来增加相对于R基础建模的 control=nls.control(maxiter=200) https://stat.ethz.ch/pipermail/r-help/2006-June/107606.html的迭代次数,但我找不到ggplot2的解决方案或方向 .

1 回答

  • 0

    基于@Axeman评论,我将 control=nls.control(maxiter=200) 添加到了

    method.args = list(start=c(a=20, b=0.01), 
                                     control=nls.control(maxiter=200))
    

    因此整个脚本是:

    ggplot(tab, aes(x=distance, y=grad, group = period, col = period)) + #
      geom_point(col = "grey") + theme_bw() +
      geom_smooth(method = "nls",
                  formula = y ~ a*x^(-b),
                  method.args = list(start=c(a=20, b=0.01), 
                                     control=nls.control(maxiter=200)),  # 
                  se = F)
    

    结果是:
    enter image description here

相关问题