如何修复我的非线性回归 nlsLM 方法中一直出现的错误消息

问题描述 投票:0回答:1

我仍在尝试获取数据的指数曲线。我有时间函数的丰度值,我想在第一点上应用指数回归。自从有人关闭了我之前的帖子后,我找到了一个包裹,我不知道为什么。

我的数据:

C5
0.0 33804.52
0.84 39076.28
1.83 26521.87
3.83 118641.90
4.83 190218.34
5.82 327796.59
6.82 566427.37
7.85 1047815.21
8.82 1711886.33
9.82 2845895.73
10.82 2773359.88
11.82 3006184.04

我的代码:

C5_abund <- read.csv(file = "C5_R.csv", sep=';')
C5_abund2 <- C5_abund[C5_abund$Day <= 9.82, ] #exponetial regresion applied on theses point

get_eqn <- function(fit) {
  eqn <- paste0("y = ", round(coef(fit)[1], 3), " * exp(", round(coef(fit)[2], 3), " * x)")
  return(eqn)
}


fitC5 <- nlsLM(C5 ~ a*exp(b*Day), data=C5_abund2, start=list(a=33804.52, b=0.5)) 
# using a= abundance at first day of experiment and b=theorical rate of growth
eqnC5 <- get_eqn(fitC5)
#y = 16470.994 * exp(0.525 * x)


rss <- sum(resid(fitC5)^2)
tss <- sum((C5_abund2$C5 - mean(C5_abund2$C5))^2)
rsq2C5 <- 1 - (rss/tss)

# rsq C5 :  0.9995


ggplot() +
  geom_point(data = C5_abund, aes(x = Day, y = C5),shape=16) +
  geom_smooth(data = C5_abund2, aes(x = Day, y = C5), method = "nlsLM", 
              formula = y ~ a * exp(b * x), se = FALSE,
              start = list(a = 1, b = 1), size=0.5, linetype = "dashed", color="black") +
  annotate("text", x = 2, y =800000, label = "atop(y == 16470.994 * e ^ {0.525*x}, R ^ 2 == 0.9995 )",
           parse = TRUE, color = "black", size =4) +
  labs(title = "Abundance Dynamic of C5 culture over days", x = "Time (days)", y = "Cell concentration (cell/mL)") +
  theme_base()+
  theme(legend.position = "bottom",
        plot.title = element_text(size = 14, hjust = 0.5),
        axis.title = element_text(size = 12),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        legend.background = element_rect(fill = "white", size = 0.5, linetype = "solid",
                                         color = "black"))

所以一切似乎都正常,但我总是收到相同的错误消息:

警告信息: 1:忽略未知参数:启动 2: In (function (formula, data = parent.frame(), start, jac = NULL, : 没有为某些参数指定起始值。 将“a”、“b”初始化为“1”。 考虑指定“开始”或使用 selfStart 模型

我多次尝试更改起始值,但没有成功。我真的非常需要帮助,因为我认为该曲线不正确,因为它正在选择起始值 =1

r ggplot2 exponential non-linear-regression
1个回答
0
投票

tl;dr 你需要使用

method.args=list(start(a=1, b=1))
(或其他)。

请注意,您没有给我们一个minimal示例:读入数据并对其进行子集化后,您只需要

ggplot
调用即可看到问题。

在下面的例子中我有

  • 稍微简化了一些事情(包括
    x
    /
    y
    映射在主要的
    ggplot
    调用中,因此它们不必重复);
  • 包括
    geom_smooth()
    元素,使用两个起始值来表明它 无关紧要 — 在这种情况下,
    nlsLM
    能够从任意一组起始值得到正确的解决方案
library(minpack.lm)
library(ggplot2)
ggplot(mapping = aes(x = Day, y = C5)) +
  geom_point(data = C5_abund) +
  geom_smooth(data = C5_abund2,
              method = "nlsLM", 
              formula = y ~ a * exp(b * x),
              se = FALSE,
              method.args = list(start = list(a = 1, b = 1)),
              linewidth=0.5, linetype = "dashed", color="black") +
  geom_smooth(data = C5_abund2,
              method = "nlsLM", 
              formula = y ~ a * exp(b * x),
              se = FALSE,
              method.args = list(start=list(a=33804.52, b=0.5)),
              linewidth=2, color=adjustcolor("red", alpha.f = 0.2))

© www.soinside.com 2019 - 2024. All rights reserved.