使用R中的nls()拟合复杂非线性函数会导致梯度奇异错误。

huangapple go评论171阅读模式
英文:

Fitting complex non-linear function with nls() in R gives singular gradient error

问题

我正在尝试使用R的nls函数拟合口服药物的药代动力学函数。该函数可以在这里看到:

dat <- data.frame(
        Time = c(10, 15, 20, 30, 40, 60, 90, 120, 180, 210, 240, 300, 360),
        C_PO = c(0,0.28,0.55,1.2,2,1.95,1.85,1.6,
                 0.86,0.78,0.6,0.21,0.18))

plot(dat$C_PO ~ dat$Time, data = dat, log = "y")

fit <- nls(C_PO~ka*100* (exp(-k*Time) - exp(-ka*Time))/(v*(ka- k)),
    data = dat,
    start = list(ka = 0.09, k = 0.01, v = 50))
#> Error in nls(C_PO ~ ka * 100 * (exp(-k * Time) - exp(-ka * Time))/(v * : singular gradient

该代码尝试根据浓度值C_PO随时间段Time进行非线性模型拟合。初始值是使用图形剥离方法计算的。然而,错误消息显示"singular gradient"。如何修复这个问题?

英文:

I am trying to fit oral pharmacokinetic function using R nls. The function can be seen here:
使用R中的nls()拟合复杂非线性函数会导致梯度奇异错误。

I tried the following:

dat &lt;- data.frame(
        Time = c(10, 15, 20, 30, 40, 60, 90, 120, 180, 210, 240, 300, 360),
        C_PO = c(0,0.28,0.55,1.2,2,1.95,1.85,1.6,
                 0.86,0.78,0.6,0.21,0.18))

plot(dat$C_PO ~ dat$Time, data = dat, log = &quot;y&quot;)

fit &lt;- nls(C_PO~ka*100* (exp(-k*Time) - exp(-ka*Time))/(v*(ka- k)),
    data = dat,
    start = list(ka = 0.09, k = 0.01, v = 50))
#&gt; Error in nls(C_PO ~ ka * 100 * (exp(-k * Time) - exp(-ka * Time))/(v * : singular gradient

The code is trying to fit a non-linear model according to the concentration values C_PO over a time period Time. The initial values were calculated using graph stripping methods. However, the error message reads "singular gradient." How can this be fixed?

答案1

得分: 1

你可以使用gslnls包来拟合非线性模型,如下所示:

library(gslnls)

dat <- data.frame(
  Time = c(10, 15, 20, 30, 40, 60, 90, 120, 180, 210, 240, 300, 360),
  C_PO = c(0, 0.28, 0.55, 1.2, 2, 1.95, 1.85, 1.6,
           0.86, 0.78, 0.6, 0.21, 0.18))

plot(dat$C_PO ~ dat$Time, data = dat, log = "y")

fit <- gsl_nls(C_PO ~ ka * 100 * (exp(-k * Time) - exp(-ka * Time))/(v * (ka - k)), 
               data = dat, 
               start = c(ka = 0.09, k = 0.01, v = 50), 
               control = gsl_nls_control(maxiter = 500))

summary(fit)

#> Formula: C_PO ~ ka * 100 * (exp(-k * Time) - exp(-ka * Time))/(v * (ka -
#> k))
#>
#> Parameters:
#> Estimate Std. Error t value Pr(>|t|)
#> ka 0.0132 4.0027 0.003 0.997
#> k 0.0132 4.0025 0.003 0.997
#> v 21.0181 6372.7704 0.003 0.997
#>
#> Residual standard error: 0.3363 on 10 degrees of freedom
#>
#> Number of iterations to convergence: 18
#> Achieved convergence tolerance: 0

英文:

You can use gslnls package for fitting non-linear model like

library(gslnls)

dat &lt;- data.frame(
  Time = c(10, 15, 20, 30, 40, 60, 90, 120, 180, 210, 240, 300, 360),
  C_PO = c(0,0.28,0.55,1.2,2,1.95,1.85,1.6,
           0.86,0.78,0.6,0.21,0.18))

plot(dat$C_PO ~ dat$Time, data = dat, log = &quot;y&quot;)

fit &lt;- gsl_nls(C_PO ~ ka*100* (exp(-k*Time) - exp(-ka*Time))/(v*(ka- k)), 
                    data = dat, 
                    start = c(ka = 0.09, k = 0.01, v = 50), 
                    control = gsl_nls_control(maxiter = 500))

summary(fit)

#&gt; Formula: C_PO ~ ka * 100 * (exp(-k * Time) - exp(-ka * Time))/(v * (ka - 
#&gt;     k))
#&gt; 
#&gt; Parameters:
#&gt;     Estimate Std. Error t value Pr(&gt;|t|)
#&gt; ka    0.0132     4.0027   0.003    0.997
#&gt; k     0.0132     4.0025   0.003    0.997
#&gt; v    21.0181  6372.7704   0.003    0.997
#&gt; 
#&gt; Residual standard error: 0.3363 on 10 degrees of freedom
#&gt; 
#&gt; Number of iterations to convergence: 18 
#&gt; Achieved convergence tolerance: 0

huangapple
  • 本文由 发表于 2023年6月16日 09:29:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定