On Wed, 1 Jul 2020 15:24:35 +0200 Luigi Marongiu <marongiu.lu...@gmail.com> wrote:
>You are right: The vector X is actually Y -- the response I would like >to fit the curve upon. I understood I should fit nls.lm with a >function that describes the data (Holling or Gomperz), initial >parameters, and the actual values (Y). In return, I get the optimized >values for the parameters. But when I re-run the function that >describes the data with the optimized parameters, I get values close >to zero. The function to be minimized should have access to all three: the parameters to optimize, the independent and dependent variable values. Only then there is enough information to compute residuals and minimise their sum of squares. holly <- function(p, x, y) (p$a * x^2) / (p$b^2 + x^2) - y # residuals = y.hat(x, params) - y.actual O <- nls.lm(par = list(a = 3261, b = 10), fn = holly, x = X, y = Y) summary(O) # Parameters: # Estimate Std. Error t value Pr(>|t|) # a 4380.4979 106.8144 41.01 <2e-16 *** # b 30.3779 0.9995 30.39 <2e-16 *** # --- # Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 # # Residual standard error: 157.5 on 58 degrees of freedom # Number of iterations to termination: 7 # Reason for termination: Relative error in the sum of squares is at # most `ftol'. In our previous examples we ended up asking R to minimise y.hat(x) calculated at wrong x values instead of minimising residuals. -- Best regards, Ivan ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.