In general, by adjusting a model to a data set, we seek to adjust the simplest possible model in the largest data set available. Remember this when reading my answer below.
The problem with your code is in charge richards<-nls(y~a/((1+exp(b-c*x))^(1/d)),start = list(a=22.3,b=18.7,c=0.165,d=2.1))
, which does not inform the R
what is the data set in which the function nls
should adjust the curve.
x<-c(60,90,120,150)
y<-c(1.14, 4.22, 19.3, 22.3)
dados<-data.frame(DAP=x, MSTP=y)
richar<-function(x){22.3/((1+exp(18.7-0.167*x))^(1/2.1))}
library(ggplot2)
ggplot(dados)+
geom_point(aes(x=DAP, y=MSTP))+
stat_function(fun=richar)
richards<-nls(y~a/((1+exp(b-c*x))^(1/d)),
start = list(a=22.3,b=18.7,c=0.165,d=2.1),
data = dados)
#> Warning in min(x): no non-missing arguments to min; returning Inf
#> Warning in max(x): no non-missing arguments to max; returning -Inf
#> Error in nls(y ~ a/((1 + exp(b - c * x))^(1/d)), start = list(a = 22.3, : step factor 0.000488281 reduced below 'minFactor' of 0.000976562
summary(richards)
#> Error in summary(richards): object 'richards' not found
Created on 2020-07-16 by the reprex package (v0.3.0)
Note that I have now received another error message. By definition, a nonlinear regression model with m parameters can be adjusted to a set with n observations, provided that m ≤ n. In your case, m = 4 = n. This can lead to several problems, such as numerical instabilities. See how the solution to your problem is found using a simulated data set with only three points more than the original problem.
x <- seq(60, 150, length.out = 7)
y <- richar(x) + rnorm(7, sd = 1)
dados2<-data.frame(DAP=x, MSTP=y)
ggplot(dados2)+
geom_point(aes(x=DAP, y=MSTP))+
stat_function(fun=richar)
richards<-nls(y~a/((1+exp(b-c*DAP))^(1/d)),
start = list(a=22.3,b=18.7,c=0.165,d=2.1),
data = dados2)
summary(richards)
#>
#> Formula: y ~ a/((1 + exp(b - c * DAP))^(1/d))
#>
#> Parameters:
#> Estimate Std. Error t value Pr(>|t|)
#> a 20.9785 0.7074 29.655 8.42e-05 ***
#> b 28.3889 16.9568 1.674 0.193
#> c 0.2451 0.1407 1.742 0.180
#> d 4.0256 2.9087 1.384 0.260
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Residual standard error: 0.9551 on 3 degrees of freedom
#>
#> Number of iterations to convergence: 6
#> Achieved convergence tolerance: 9.361e-06
Created on 2020-07-16 by the reprex package (v0.3.0)
I find it very difficult that your problem has any numerical solution with such a complex model (4 parameters) and so few points (4 observations). From here there are at least two possible approaches:
Use a simpler function such as logistical function, which has only two parameters to adjust. The problem with this approach, I believe, is that Richards' function is being used because, in the original problem, each of its four parameters must have an appropriate meaning for the real problem being considered here.
Use more data. The problem with this approach is just getting this data. This costs time and money and in some cases it is not even possible to obtain data from the same experiment, either because there is no money or because the bodies used in the trials were destroyed.
I vote up but in since m n is the other way around, isn’t it? The number of observations should be higher since n m.
– Rui Barradas
Perfectly. I think I was still sleepy when I wrote the answer and got confused in this argument. Thank you for the correction.
– Marcus Nunes