Re: [R] Issues when trying to fit a nonlinear regression model

2023-08-20 Thread Paul Bernal
Thank you so much for your valuable feedback Berwin.

Have a great day.
Cheers,
Paul

El El dom, 20 de ago. de 2023 a la(s) 10:21 p. m., Berwin A Turlach <
berwin.turl...@gmail.com> escribió:

> G'day Paul,
>
> On Sun, 20 Aug 2023 12:15:08 -0500
> Paul Bernal  wrote:
>
> > Any idea on how to proceed in this situation? What could I do?
>
> You are fitting a simple asymptotic model for which nls() can find good
> starting values if you use the self starting models (SSxyz()).  Well,
> Doug (et al.) choose to parameterise the asymptotic model differently,
> but you can easily change to your parameterisation if you want:
>
> ```
> fm1 <- nls(y ~ SSasymp(x, Asym, R0, lrc), data=mod14data2_random)
> theta1 <- coef(fm1)["Asym"]
> theta2 <- coef(fm1)["Asym"] - coef(fm1)["R0"]
> theta3 <- exp(coef(fm1)["lrc"])
>
> fm2 <- nls(y ~ theta1 - theta2 * exp(-theta3*x),
>start=list(theta1=theta1, theta2=theta2, theta3=theta3),
>data=mod14data2_random)
> summary(fm2)
> ```
>
> Cheers,
>
> Berwin
>

[[alternative HTML version deleted]]

__
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.


Re: [R] Issues when trying to fit a nonlinear regression model

2023-08-20 Thread Berwin A Turlach
G'day Paul,

On Sun, 20 Aug 2023 12:15:08 -0500
Paul Bernal  wrote:

> Any idea on how to proceed in this situation? What could I do?

You are fitting a simple asymptotic model for which nls() can find good
starting values if you use the self starting models (SSxyz()).  Well,
Doug (et al.) choose to parameterise the asymptotic model differently,
but you can easily change to your parameterisation if you want:

```
fm1 <- nls(y ~ SSasymp(x, Asym, R0, lrc), data=mod14data2_random)
theta1 <- coef(fm1)["Asym"]
theta2 <- coef(fm1)["Asym"] - coef(fm1)["R0"]
theta3 <- exp(coef(fm1)["lrc"])

fm2 <- nls(y ~ theta1 - theta2 * exp(-theta3*x), 
   start=list(theta1=theta1, theta2=theta2, theta3=theta3),
   data=mod14data2_random)
summary(fm2)
```

Cheers,

Berwin

__
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.


Re: [R] Issues when trying to fit a nonlinear regression model

2023-08-20 Thread Paul Bernal
Thanks a lot Bert, I appreciate your help.

Kind regards,

Paul

El El dom, 20 de ago. de 2023 a la(s) 2:39 p. m., Bert Gunter <
bgunter.4...@gmail.com> escribió:

> Basic algebra and exponentials/logs. I leave those details to you or
> another HelpeR.
>
> -- Bert
>
> On Sun, Aug 20, 2023 at 12:17 PM Paul Bernal 
> wrote:
>
>> Dear Bert,
>>
>> Thank you for your extremely valuable feedback. Now, I just want to
>> understand why the signs for those starting values, given the following:
>> > #Fiting intermediate model to get starting values
>> > intermediatemod <- lm(log(y - .37) ~ x, data=mod14data2_random)
>> > summary(intermediatemod)
>>
>> Call:
>> lm(formula = log(y - 0.37) ~ x, data = mod14data2_random)
>>
>> Residuals:
>> Min  1Q  Median  3Q Max
>> -0.7946 -0.0908  0.0379  0.  0.5917
>>
>> Coefficients:
>> Estimate Std. Error t value Pr(>|t|)
>> (Intercept) -1.816930.25806   -7.04  8.8e-06 ***
>> x   -0.055380.00964   -5.75  6.8e-05 ***
>> ---
>> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>>
>> Residual standard error: 0.323 on 13 degrees of freedom
>> Multiple R-squared:  0.717, Adjusted R-squared:  0.696
>> F-statistic:   33 on 1 and 13 DF,  p-value: 6.76e-05
>>
>> Kind regards,
>> Paul
>>
>> El dom, 20 ago 2023 a las 14:07, Bert Gunter ()
>> escribió:
>>
>>> Oh, sorry; I changed signs in the model, fitting
>>> theta0 + theta1*exp(theta2*x)
>>>
>>> So for theta0 - theta1*exp(-theta2*x) use theta1= -.exp(-1.8) and theta2
>>> = +.055 as starting values.
>>>
>>> -- Bert
>>>
>>>
>>>
>>>
>>>
>>> On Sun, Aug 20, 2023 at 11:50 AM Paul Bernal 
>>> wrote:
>>>
 Dear Bert,

 Thank you so much for your kind and valuable feedback. I tried finding
 the starting values using the approach you mentioned, then did the
 following to fit the nonlinear regression model:
 nlregmod2 <- nls(y ~ theta1 - theta2*exp(-theta3*x),
   start =
 list(theta1 = 0.37,
  theta2 = exp(-1.8),
  theta3 = -0.05538), data=mod14data2_random)
 However, I got this error:
 Error in nls(y ~ theta1 - theta2 * exp(-theta3 * x), start =
 list(theta1 = 0.37,  :
   step factor 0.000488281 reduced below 'minFactor' of 0.000976562
 nlregmod2 <- nlxb(y ~ theta1 - theta2*exp(-theta3*x),
   start =
 list(theta1 = 0.37,
  theta2 = exp(-1.8),
  theta3 = -0.05538), data=mod14data2_random)
 summary(nlregmod2)
 Object has try-error or missing parameters
 nlregmod2
 And I get some NA values when retrieving the statistics for the fitted
 model:
 residual sumsquares =  0.0022973  on  15 observations
 after  2235Jacobian and  2861 function evaluations
   namecoeff  SE   tstat  pval  gradient
JSingval
 theta1   9330.89NA NA NA
 5.275e-11  967470
 theta2   9330.41NA NA NA
  -5.318e-11   1.772
 theta3   -3.0032e-07NA NA NA
 1.389e-05   8.028e-12

 Kind regards,
 Paul


 El dom, 20 ago 2023 a las 13:21, Bert Gunter ()
 escribió:

> I got starting values as follows:
> Noting that the minimum data value is .38, I fit the linear model
> log(y - .37) ~ x to get intercept = -1.8 and slope = -.055. So I used .37,
> exp(-1.8)  and -.055 as the starting values for theta0, theta1, and theta2
> in the nonlinear model. This converged without problems.
>
> Cheers,
> Bert
>
>
> On Sun, Aug 20, 2023 at 10:15 AM Paul Bernal 
> wrote:
>
>> Dear friends,
>>
>> This is the dataset I am currently working with:
>> >dput(mod14data2_random)
>> structure(list(index = c(14L, 27L, 37L, 33L, 34L, 16L, 7L, 1L,
>> 39L, 36L, 40L, 19L, 28L, 38L, 32L), y = c(0.44, 0.4, 0.4, 0.4,
>> 0.4, 0.43, 0.46, 0.49, 0.41, 0.41, 0.38, 0.42, 0.41, 0.4, 0.4
>> ), x = c(16, 24, 32, 30, 30, 16, 12, 8, 36, 32, 36, 20, 26, 34,
>> 28)), row.names = c(NA, -15L), class = "data.frame")
>>
>> I did the following to try to fit a nonlinear regression model:
>>
>> #First, Procedure to Find Starting (initial) Values For Theta1,
>> Theta2, and
>> Theta3
>>
>> mymod2 <- y ~ theta1 - theta2*exp(-theta3*x)
>>
>> strt2 <- c(theta1 = 1, theta2 = 2, theta3 = 3)
>>
>> trysol2<-nlxb(formula=mymod2, data=mod14data2_random, start=strt2,
>> trace=TRUE)
>> trysol2
>> trysol2$coefficients[[3]]
>>
>> #Fitting nonlinear Regression Model Using Starting Values From
>> Previous Part
>> nonlinearmod2 <- nls(mymod2, start = list(theta1 =
>> trysol2$coefficients[[1]],
>>  theta2 = trysol2$coefficients[[2]],

Re: [R] Issues when trying to fit a nonlinear regression model

2023-08-20 Thread Bert Gunter
Basic algebra and exponentials/logs. I leave those details to you or
another HelpeR.

-- Bert

On Sun, Aug 20, 2023 at 12:17 PM Paul Bernal  wrote:

> Dear Bert,
>
> Thank you for your extremely valuable feedback. Now, I just want to
> understand why the signs for those starting values, given the following:
> > #Fiting intermediate model to get starting values
> > intermediatemod <- lm(log(y - .37) ~ x, data=mod14data2_random)
> > summary(intermediatemod)
>
> Call:
> lm(formula = log(y - 0.37) ~ x, data = mod14data2_random)
>
> Residuals:
> Min  1Q  Median  3Q Max
> -0.7946 -0.0908  0.0379  0.  0.5917
>
> Coefficients:
> Estimate Std. Error t value Pr(>|t|)
> (Intercept) -1.816930.25806   -7.04  8.8e-06 ***
> x   -0.055380.00964   -5.75  6.8e-05 ***
> ---
> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
> Residual standard error: 0.323 on 13 degrees of freedom
> Multiple R-squared:  0.717, Adjusted R-squared:  0.696
> F-statistic:   33 on 1 and 13 DF,  p-value: 6.76e-05
>
> Kind regards,
> Paul
>
> El dom, 20 ago 2023 a las 14:07, Bert Gunter ()
> escribió:
>
>> Oh, sorry; I changed signs in the model, fitting
>> theta0 + theta1*exp(theta2*x)
>>
>> So for theta0 - theta1*exp(-theta2*x) use theta1= -.exp(-1.8) and theta2
>> = +.055 as starting values.
>>
>> -- Bert
>>
>>
>>
>>
>>
>> On Sun, Aug 20, 2023 at 11:50 AM Paul Bernal 
>> wrote:
>>
>>> Dear Bert,
>>>
>>> Thank you so much for your kind and valuable feedback. I tried finding
>>> the starting values using the approach you mentioned, then did the
>>> following to fit the nonlinear regression model:
>>> nlregmod2 <- nls(y ~ theta1 - theta2*exp(-theta3*x),
>>>   start =
>>> list(theta1 = 0.37,
>>>  theta2 = exp(-1.8),
>>>  theta3 = -0.05538), data=mod14data2_random)
>>> However, I got this error:
>>> Error in nls(y ~ theta1 - theta2 * exp(-theta3 * x), start = list(theta1
>>> = 0.37,  :
>>>   step factor 0.000488281 reduced below 'minFactor' of 0.000976562
>>> nlregmod2 <- nlxb(y ~ theta1 - theta2*exp(-theta3*x),
>>>   start =
>>> list(theta1 = 0.37,
>>>  theta2 = exp(-1.8),
>>>  theta3 = -0.05538), data=mod14data2_random)
>>> summary(nlregmod2)
>>> Object has try-error or missing parameters
>>> nlregmod2
>>> And I get some NA values when retrieving the statistics for the fitted
>>> model:
>>> residual sumsquares =  0.0022973  on  15 observations
>>> after  2235Jacobian and  2861 function evaluations
>>>   namecoeff  SE   tstat  pval  gradient
>>>JSingval
>>> theta1   9330.89NA NA NA   5.275e-11
>>>  967470
>>> theta2   9330.41NA NA NA  -5.318e-11
>>>   1.772
>>> theta3   -3.0032e-07NA NA NA   1.389e-05
>>>   8.028e-12
>>>
>>> Kind regards,
>>> Paul
>>>
>>>
>>> El dom, 20 ago 2023 a las 13:21, Bert Gunter ()
>>> escribió:
>>>
 I got starting values as follows:
 Noting that the minimum data value is .38, I fit the linear model log(y
 - .37) ~ x to get intercept = -1.8 and slope = -.055. So I used .37,
 exp(-1.8)  and -.055 as the starting values for theta0, theta1, and theta2
 in the nonlinear model. This converged without problems.

 Cheers,
 Bert


 On Sun, Aug 20, 2023 at 10:15 AM Paul Bernal 
 wrote:

> Dear friends,
>
> This is the dataset I am currently working with:
> >dput(mod14data2_random)
> structure(list(index = c(14L, 27L, 37L, 33L, 34L, 16L, 7L, 1L,
> 39L, 36L, 40L, 19L, 28L, 38L, 32L), y = c(0.44, 0.4, 0.4, 0.4,
> 0.4, 0.43, 0.46, 0.49, 0.41, 0.41, 0.38, 0.42, 0.41, 0.4, 0.4
> ), x = c(16, 24, 32, 30, 30, 16, 12, 8, 36, 32, 36, 20, 26, 34,
> 28)), row.names = c(NA, -15L), class = "data.frame")
>
> I did the following to try to fit a nonlinear regression model:
>
> #First, Procedure to Find Starting (initial) Values For Theta1,
> Theta2, and
> Theta3
>
> mymod2 <- y ~ theta1 - theta2*exp(-theta3*x)
>
> strt2 <- c(theta1 = 1, theta2 = 2, theta3 = 3)
>
> trysol2<-nlxb(formula=mymod2, data=mod14data2_random, start=strt2,
> trace=TRUE)
> trysol2
> trysol2$coefficients[[3]]
>
> #Fitting nonlinear Regression Model Using Starting Values From
> Previous Part
> nonlinearmod2 <- nls(mymod2, start = list(theta1 =
> trysol2$coefficients[[1]],
>  theta2 = trysol2$coefficients[[2]],
>  theta3 = trysol2$coefficients[[3]]), data =
> mod14data2_random)
>
> And I got this error:
> Error in nlsModel(formula, mf, start, wts, scaleOffset = scOff,
> nDcentral =
> nDcntr) :
>   singular gradient matrix at initial parameter estimates
>

Re: [R] Issues when trying to fit a nonlinear regression model

2023-08-20 Thread Paul Bernal
Dear Bert,

Thank you for your extremely valuable feedback. Now, I just want to
understand why the signs for those starting values, given the following:
> #Fiting intermediate model to get starting values
> intermediatemod <- lm(log(y - .37) ~ x, data=mod14data2_random)
> summary(intermediatemod)

Call:
lm(formula = log(y - 0.37) ~ x, data = mod14data2_random)

Residuals:
Min  1Q  Median  3Q Max
-0.7946 -0.0908  0.0379  0.  0.5917

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.816930.25806   -7.04  8.8e-06 ***
x   -0.055380.00964   -5.75  6.8e-05 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.323 on 13 degrees of freedom
Multiple R-squared:  0.717, Adjusted R-squared:  0.696
F-statistic:   33 on 1 and 13 DF,  p-value: 6.76e-05

Kind regards,
Paul

El dom, 20 ago 2023 a las 14:07, Bert Gunter ()
escribió:

> Oh, sorry; I changed signs in the model, fitting
> theta0 + theta1*exp(theta2*x)
>
> So for theta0 - theta1*exp(-theta2*x) use theta1= -.exp(-1.8) and theta2 =
> +.055 as starting values.
>
> -- Bert
>
>
>
>
>
> On Sun, Aug 20, 2023 at 11:50 AM Paul Bernal 
> wrote:
>
>> Dear Bert,
>>
>> Thank you so much for your kind and valuable feedback. I tried finding
>> the starting values using the approach you mentioned, then did the
>> following to fit the nonlinear regression model:
>> nlregmod2 <- nls(y ~ theta1 - theta2*exp(-theta3*x),
>>   start =
>> list(theta1 = 0.37,
>>  theta2 = exp(-1.8),
>>  theta3 = -0.05538), data=mod14data2_random)
>> However, I got this error:
>> Error in nls(y ~ theta1 - theta2 * exp(-theta3 * x), start = list(theta1
>> = 0.37,  :
>>   step factor 0.000488281 reduced below 'minFactor' of 0.000976562
>> nlregmod2 <- nlxb(y ~ theta1 - theta2*exp(-theta3*x),
>>   start =
>> list(theta1 = 0.37,
>>  theta2 = exp(-1.8),
>>  theta3 = -0.05538), data=mod14data2_random)
>> summary(nlregmod2)
>> Object has try-error or missing parameters
>> nlregmod2
>> And I get some NA values when retrieving the statistics for the fitted
>> model:
>> residual sumsquares =  0.0022973  on  15 observations
>> after  2235Jacobian and  2861 function evaluations
>>   namecoeff  SE   tstat  pval  gradient
>>  JSingval
>> theta1   9330.89NA NA NA   5.275e-11
>>  967470
>> theta2   9330.41NA NA NA  -5.318e-11
>>   1.772
>> theta3   -3.0032e-07NA NA NA   1.389e-05
>>   8.028e-12
>>
>> Kind regards,
>> Paul
>>
>>
>> El dom, 20 ago 2023 a las 13:21, Bert Gunter ()
>> escribió:
>>
>>> I got starting values as follows:
>>> Noting that the minimum data value is .38, I fit the linear model log(y
>>> - .37) ~ x to get intercept = -1.8 and slope = -.055. So I used .37,
>>> exp(-1.8)  and -.055 as the starting values for theta0, theta1, and theta2
>>> in the nonlinear model. This converged without problems.
>>>
>>> Cheers,
>>> Bert
>>>
>>>
>>> On Sun, Aug 20, 2023 at 10:15 AM Paul Bernal 
>>> wrote:
>>>
 Dear friends,

 This is the dataset I am currently working with:
 >dput(mod14data2_random)
 structure(list(index = c(14L, 27L, 37L, 33L, 34L, 16L, 7L, 1L,
 39L, 36L, 40L, 19L, 28L, 38L, 32L), y = c(0.44, 0.4, 0.4, 0.4,
 0.4, 0.43, 0.46, 0.49, 0.41, 0.41, 0.38, 0.42, 0.41, 0.4, 0.4
 ), x = c(16, 24, 32, 30, 30, 16, 12, 8, 36, 32, 36, 20, 26, 34,
 28)), row.names = c(NA, -15L), class = "data.frame")

 I did the following to try to fit a nonlinear regression model:

 #First, Procedure to Find Starting (initial) Values For Theta1, Theta2,
 and
 Theta3

 mymod2 <- y ~ theta1 - theta2*exp(-theta3*x)

 strt2 <- c(theta1 = 1, theta2 = 2, theta3 = 3)

 trysol2<-nlxb(formula=mymod2, data=mod14data2_random, start=strt2,
 trace=TRUE)
 trysol2
 trysol2$coefficients[[3]]

 #Fitting nonlinear Regression Model Using Starting Values From Previous
 Part
 nonlinearmod2 <- nls(mymod2, start = list(theta1 =
 trysol2$coefficients[[1]],
  theta2 = trysol2$coefficients[[2]],
  theta3 = trysol2$coefficients[[3]]), data =
 mod14data2_random)

 And I got this error:
 Error in nlsModel(formula, mf, start, wts, scaleOffset = scOff,
 nDcentral =
 nDcntr) :
   singular gradient matrix at initial parameter estimates

 Any idea on how to proceed in this situation? What could I do?

 Kind regards,
 Paul

 [[alternative HTML version deleted]]

 __
 R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
 

Re: [R] Issues when trying to fit a nonlinear regression model

2023-08-20 Thread Bert Gunter
Oh, sorry; I changed signs in the model, fitting
theta0 + theta1*exp(theta2*x)

So for theta0 - theta1*exp(-theta2*x) use theta1= -.exp(-1.8) and theta2 =
+.055 as starting values.

-- Bert





On Sun, Aug 20, 2023 at 11:50 AM Paul Bernal  wrote:

> Dear Bert,
>
> Thank you so much for your kind and valuable feedback. I tried finding the
> starting values using the approach you mentioned, then did the following to
> fit the nonlinear regression model:
> nlregmod2 <- nls(y ~ theta1 - theta2*exp(-theta3*x),
>   start =
> list(theta1 = 0.37,
>  theta2 = exp(-1.8),
>  theta3 = -0.05538), data=mod14data2_random)
> However, I got this error:
> Error in nls(y ~ theta1 - theta2 * exp(-theta3 * x), start = list(theta1 =
> 0.37,  :
>   step factor 0.000488281 reduced below 'minFactor' of 0.000976562
> nlregmod2 <- nlxb(y ~ theta1 - theta2*exp(-theta3*x),
>   start =
> list(theta1 = 0.37,
>  theta2 = exp(-1.8),
>  theta3 = -0.05538), data=mod14data2_random)
> summary(nlregmod2)
> Object has try-error or missing parameters
> nlregmod2
> And I get some NA values when retrieving the statistics for the fitted
> model:
> residual sumsquares =  0.0022973  on  15 observations
> after  2235Jacobian and  2861 function evaluations
>   namecoeff  SE   tstat  pval  gradient
>  JSingval
> theta1   9330.89NA NA NA   5.275e-11
>967470
> theta2   9330.41NA NA NA  -5.318e-11
> 1.772
> theta3   -3.0032e-07NA NA NA   1.389e-05
> 8.028e-12
>
> Kind regards,
> Paul
>
>
> El dom, 20 ago 2023 a las 13:21, Bert Gunter ()
> escribió:
>
>> I got starting values as follows:
>> Noting that the minimum data value is .38, I fit the linear model log(y -
>> .37) ~ x to get intercept = -1.8 and slope = -.055. So I used .37,
>> exp(-1.8)  and -.055 as the starting values for theta0, theta1, and theta2
>> in the nonlinear model. This converged without problems.
>>
>> Cheers,
>> Bert
>>
>>
>> On Sun, Aug 20, 2023 at 10:15 AM Paul Bernal 
>> wrote:
>>
>>> Dear friends,
>>>
>>> This is the dataset I am currently working with:
>>> >dput(mod14data2_random)
>>> structure(list(index = c(14L, 27L, 37L, 33L, 34L, 16L, 7L, 1L,
>>> 39L, 36L, 40L, 19L, 28L, 38L, 32L), y = c(0.44, 0.4, 0.4, 0.4,
>>> 0.4, 0.43, 0.46, 0.49, 0.41, 0.41, 0.38, 0.42, 0.41, 0.4, 0.4
>>> ), x = c(16, 24, 32, 30, 30, 16, 12, 8, 36, 32, 36, 20, 26, 34,
>>> 28)), row.names = c(NA, -15L), class = "data.frame")
>>>
>>> I did the following to try to fit a nonlinear regression model:
>>>
>>> #First, Procedure to Find Starting (initial) Values For Theta1, Theta2,
>>> and
>>> Theta3
>>>
>>> mymod2 <- y ~ theta1 - theta2*exp(-theta3*x)
>>>
>>> strt2 <- c(theta1 = 1, theta2 = 2, theta3 = 3)
>>>
>>> trysol2<-nlxb(formula=mymod2, data=mod14data2_random, start=strt2,
>>> trace=TRUE)
>>> trysol2
>>> trysol2$coefficients[[3]]
>>>
>>> #Fitting nonlinear Regression Model Using Starting Values From Previous
>>> Part
>>> nonlinearmod2 <- nls(mymod2, start = list(theta1 =
>>> trysol2$coefficients[[1]],
>>>  theta2 = trysol2$coefficients[[2]],
>>>  theta3 = trysol2$coefficients[[3]]), data =
>>> mod14data2_random)
>>>
>>> And I got this error:
>>> Error in nlsModel(formula, mf, start, wts, scaleOffset = scOff,
>>> nDcentral =
>>> nDcntr) :
>>>   singular gradient matrix at initial parameter estimates
>>>
>>> Any idea on how to proceed in this situation? What could I do?
>>>
>>> Kind regards,
>>> Paul
>>>
>>> [[alternative HTML version deleted]]
>>>
>>> __
>>> 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.
>>>
>>

[[alternative HTML version deleted]]

__
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.


Re: [R] Issues when trying to fit a nonlinear regression model

2023-08-20 Thread Ben Bolker
  I haven't looked to see whether you or Bert made an algebraic mistake 
in translating the parameters of the log-linear model to their 
equivalents for the nonlinear model, but nls() gives me the same answer 
as nls() in this case (I called my data 'dd2'):




n1 <- nlxb(y~theta1 - theta2*exp(-theta3*(x-8)),
 start = list(theta1 = 0.4, theta2 = -0.1, theta3 = 1/5),
 data = dd2)

cc <- coef(n1)
start2 <- with(as.list(cc),
   list(theta1 = theta1,
theta2 = theta2*exp(theta3*8),
theta3 = theta3))
unlist(start2)

n2 <- nlxb(y~theta1 - theta2*exp(-theta3*x),
 start = start2,
 data = dd2)
all.equal(unlist(start2), c(coef(n2)), tolerance = 1e-7)
cc2 <- coef(n2)

nlregmod2 <- nls(y ~ theta1 - theta2*exp(-theta3*x),
  start =
list(theta1 = cc2[["theta1"]],
 theta2 = cc2[["theta2"]],
 theta3 = cc2[["theta3"]]), data=dd2)


On 2023-08-20 2:50 p.m., Paul Bernal wrote:

Dear Bert,

Thank you so much for your kind and valuable feedback. I tried finding the
starting values using the approach you mentioned, then did the following to
fit the nonlinear regression model:
nlregmod2 <- nls(y ~ theta1 - theta2*exp(-theta3*x),
   start =
 list(theta1 = 0.37,
  theta2 = exp(-1.8),
  theta3 = -0.05538), data=mod14data2_random)
However, I got this error:
Error in nls(y ~ theta1 - theta2 * exp(-theta3 * x), start = list(theta1 =
0.37,  :
   step factor 0.000488281 reduced below 'minFactor' of 0.000976562
nlregmod2 <- nlxb(y ~ theta1 - theta2*exp(-theta3*x),
   start =
 list(theta1 = 0.37,
  theta2 = exp(-1.8),
  theta3 = -0.05538), data=mod14data2_random)
summary(nlregmod2)
Object has try-error or missing parameters
nlregmod2
And I get some NA values when retrieving the statistics for the fitted
model:
residual sumsquares =  0.0022973  on  15 observations
 after  2235Jacobian and  2861 function evaluations
   namecoeff  SE   tstat  pval  gradient
  JSingval
theta1   9330.89NA NA NA   5.275e-11
967470
theta2   9330.41NA NA NA  -5.318e-11
 1.772
theta3   -3.0032e-07NA NA NA   1.389e-05
8.028e-12

Kind regards,
Paul


El dom, 20 ago 2023 a las 13:21, Bert Gunter ()
escribió:


I got starting values as follows:
Noting that the minimum data value is .38, I fit the linear model log(y -
.37) ~ x to get intercept = -1.8 and slope = -.055. So I used .37,
exp(-1.8)  and -.055 as the starting values for theta0, theta1, and theta2
in the nonlinear model. This converged without problems.

Cheers,
Bert


On Sun, Aug 20, 2023 at 10:15 AM Paul Bernal 
wrote:


Dear friends,

This is the dataset I am currently working with:

dput(mod14data2_random)

structure(list(index = c(14L, 27L, 37L, 33L, 34L, 16L, 7L, 1L,
39L, 36L, 40L, 19L, 28L, 38L, 32L), y = c(0.44, 0.4, 0.4, 0.4,
0.4, 0.43, 0.46, 0.49, 0.41, 0.41, 0.38, 0.42, 0.41, 0.4, 0.4
), x = c(16, 24, 32, 30, 30, 16, 12, 8, 36, 32, 36, 20, 26, 34,
28)), row.names = c(NA, -15L), class = "data.frame")

I did the following to try to fit a nonlinear regression model:

#First, Procedure to Find Starting (initial) Values For Theta1, Theta2,
and
Theta3

mymod2 <- y ~ theta1 - theta2*exp(-theta3*x)

strt2 <- c(theta1 = 1, theta2 = 2, theta3 = 3)

trysol2<-nlxb(formula=mymod2, data=mod14data2_random, start=strt2,
trace=TRUE)
trysol2
trysol2$coefficients[[3]]

#Fitting nonlinear Regression Model Using Starting Values From Previous
Part
nonlinearmod2 <- nls(mymod2, start = list(theta1 =
trysol2$coefficients[[1]],
  theta2 = trysol2$coefficients[[2]],
  theta3 = trysol2$coefficients[[3]]), data =
mod14data2_random)

And I got this error:
Error in nlsModel(formula, mf, start, wts, scaleOffset = scOff, nDcentral
=
nDcntr) :
   singular gradient matrix at initial parameter estimates

Any idea on how to proceed in this situation? What could I do?

Kind regards,
Paul

 [[alternative HTML version deleted]]

__
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.





[[alternative HTML version deleted]]

__
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.


__

Re: [R] Issues when trying to fit a nonlinear regression model

2023-08-20 Thread Paul Bernal
Dear Bert,

Thank you so much for your kind and valuable feedback. I tried finding the
starting values using the approach you mentioned, then did the following to
fit the nonlinear regression model:
nlregmod2 <- nls(y ~ theta1 - theta2*exp(-theta3*x),
  start =
list(theta1 = 0.37,
 theta2 = exp(-1.8),
 theta3 = -0.05538), data=mod14data2_random)
However, I got this error:
Error in nls(y ~ theta1 - theta2 * exp(-theta3 * x), start = list(theta1 =
0.37,  :
  step factor 0.000488281 reduced below 'minFactor' of 0.000976562
nlregmod2 <- nlxb(y ~ theta1 - theta2*exp(-theta3*x),
  start =
list(theta1 = 0.37,
 theta2 = exp(-1.8),
 theta3 = -0.05538), data=mod14data2_random)
summary(nlregmod2)
Object has try-error or missing parameters
nlregmod2
And I get some NA values when retrieving the statistics for the fitted
model:
residual sumsquares =  0.0022973  on  15 observations
after  2235Jacobian and  2861 function evaluations
  namecoeff  SE   tstat  pval  gradient
 JSingval
theta1   9330.89NA NA NA   5.275e-11
   967470
theta2   9330.41NA NA NA  -5.318e-11
1.772
theta3   -3.0032e-07NA NA NA   1.389e-05
8.028e-12

Kind regards,
Paul


El dom, 20 ago 2023 a las 13:21, Bert Gunter ()
escribió:

> I got starting values as follows:
> Noting that the minimum data value is .38, I fit the linear model log(y -
> .37) ~ x to get intercept = -1.8 and slope = -.055. So I used .37,
> exp(-1.8)  and -.055 as the starting values for theta0, theta1, and theta2
> in the nonlinear model. This converged without problems.
>
> Cheers,
> Bert
>
>
> On Sun, Aug 20, 2023 at 10:15 AM Paul Bernal 
> wrote:
>
>> Dear friends,
>>
>> This is the dataset I am currently working with:
>> >dput(mod14data2_random)
>> structure(list(index = c(14L, 27L, 37L, 33L, 34L, 16L, 7L, 1L,
>> 39L, 36L, 40L, 19L, 28L, 38L, 32L), y = c(0.44, 0.4, 0.4, 0.4,
>> 0.4, 0.43, 0.46, 0.49, 0.41, 0.41, 0.38, 0.42, 0.41, 0.4, 0.4
>> ), x = c(16, 24, 32, 30, 30, 16, 12, 8, 36, 32, 36, 20, 26, 34,
>> 28)), row.names = c(NA, -15L), class = "data.frame")
>>
>> I did the following to try to fit a nonlinear regression model:
>>
>> #First, Procedure to Find Starting (initial) Values For Theta1, Theta2,
>> and
>> Theta3
>>
>> mymod2 <- y ~ theta1 - theta2*exp(-theta3*x)
>>
>> strt2 <- c(theta1 = 1, theta2 = 2, theta3 = 3)
>>
>> trysol2<-nlxb(formula=mymod2, data=mod14data2_random, start=strt2,
>> trace=TRUE)
>> trysol2
>> trysol2$coefficients[[3]]
>>
>> #Fitting nonlinear Regression Model Using Starting Values From Previous
>> Part
>> nonlinearmod2 <- nls(mymod2, start = list(theta1 =
>> trysol2$coefficients[[1]],
>>  theta2 = trysol2$coefficients[[2]],
>>  theta3 = trysol2$coefficients[[3]]), data =
>> mod14data2_random)
>>
>> And I got this error:
>> Error in nlsModel(formula, mf, start, wts, scaleOffset = scOff, nDcentral
>> =
>> nDcntr) :
>>   singular gradient matrix at initial parameter estimates
>>
>> Any idea on how to proceed in this situation? What could I do?
>>
>> Kind regards,
>> Paul
>>
>> [[alternative HTML version deleted]]
>>
>> __
>> 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.
>>
>

[[alternative HTML version deleted]]

__
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.


Re: [R] Issues when trying to fit a nonlinear regression model

2023-08-20 Thread Ben Bolker

  My answer is WAY longer than Bert Gunter's but maybe useful nonetheless.

  (Q for John Nash:  why does the coef() method for nlmrt objects 
return the coefficient vector **invisibly**?  That seems confusing!)


  Here's what I did:

* as a preliminary step, adjust the formula so that I don't have to 
think as hard to eyeball starting values from the plot: in particular, 
replace (x) with (x-8) so that the data start from x = 0


* with this adjustment, we can approximate as follows:
* theta1-theta2 is the value at (x' = 0) (or x = 8)
* theta1 is the value as x goes to infinity
* so since y(0) approx. 0.5 and y(inf) approx 0.4, we can use 
theta1 = 0.4, theta2 = -0.1
* theta3 gives the rate of decline. Since the curve drops by 
*approximately* a factor of e over the range from x'=0 to x'=5, 1/5 
should be a good starting value for this


n1 <- nlxb(y~theta1 - theta2*exp(-theta3*(x-8)),
 start = list(theta1 = 0.4, theta2 = -0.1, theta3 = 1/5),
 data = dd2)

residual sumsquares =  0.00076151  on  15 observations
after  6Jacobian and  9 function evaluations
  namecoeff  SE   tstat  pval  gradient 
   JSingval
theta1  0.391967  0.006128  63.96  0  -2.629e-11 
  4.085
theta2-0.0997234  0.007897 -12.63  2.733e-08  -1.035e-12 
 0.9956
theta3  0.107376   0.02365   4.54  0.0006777  -1.327e-11 
 0.3276


  Now if the formula is

theta1 - theta2*exp(-theta3*(x-8))

this is equivalent to

theta1 - theta2*exp(-theta3*x)*exp(8*theta3) =
theta1 - (theta2*exp(8*theta3))*exp(-theta3*x))

cc <- coef(n1)
start2 <- with(as.list(cc), list(theta1 = theta1, theta2 = 
theta2*exp(8*theta3), theta3 = theta3))

unlist(start2)
 theta1   theta2   theta3
 0.39197 -0.23543  0.10738

To confirm, rerun the fit with these starting values:

n1 <- nlxb(y~theta1 - theta2*exp(-theta3*x),
   start = start2,
  data = dd2)


nlmrt class object: x
residual sumsquares =  0.00076151  on  15 observations
after  4Jacobian and  5 function evaluations
  namecoeff  SE   tstat  pval  gradient 
   JSingval
theta1  0.391967  0.006128  63.96  0  -4.066e-12 
  4.228
theta2 -0.235429   0.04553 -5.171  0.0002328   3.151e-12 
 0.8508
theta3  0.107376   0.02365   4.54  0.0006777  -5.795e-12 
 0.1569




On 2023-08-20 1:15 p.m., Paul Bernal wrote:

Dear friends,

This is the dataset I am currently working with:

dput(mod14data2_random)

structure(list(index = c(14L, 27L, 37L, 33L, 34L, 16L, 7L, 1L,
39L, 36L, 40L, 19L, 28L, 38L, 32L), y = c(0.44, 0.4, 0.4, 0.4,
0.4, 0.43, 0.46, 0.49, 0.41, 0.41, 0.38, 0.42, 0.41, 0.4, 0.4
), x = c(16, 24, 32, 30, 30, 16, 12, 8, 36, 32, 36, 20, 26, 34,
28)), row.names = c(NA, -15L), class = "data.frame")

I did the following to try to fit a nonlinear regression model:

#First, Procedure to Find Starting (initial) Values For Theta1, Theta2, and
Theta3

mymod2 <- y ~ theta1 - theta2*exp(-theta3*x)

strt2 <- c(theta1 = 1, theta2 = 2, theta3 = 3)

trysol2<-nlxb(formula=mymod2, data=mod14data2_random, start=strt2,
trace=TRUE)
trysol2
trysol2$coefficients[[3]]

#Fitting nonlinear Regression Model Using Starting Values From Previous Part
nonlinearmod2 <- nls(mymod2, start = list(theta1 =
trysol2$coefficients[[1]],
  theta2 = trysol2$coefficients[[2]],
  theta3 = trysol2$coefficients[[3]]), data =
mod14data2_random)

And I got this error:
Error in nlsModel(formula, mf, start, wts, scaleOffset = scOff, nDcentral =
nDcntr) :
   singular gradient matrix at initial parameter estimates

Any idea on how to proceed in this situation? What could I do?

Kind regards,
Paul

[[alternative HTML version deleted]]

__
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.


__
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.


Re: [R] Issues when trying to fit a nonlinear regression model

2023-08-20 Thread Bert Gunter
I got starting values as follows:
Noting that the minimum data value is .38, I fit the linear model log(y -
.37) ~ x to get intercept = -1.8 and slope = -.055. So I used .37,
exp(-1.8)  and -.055 as the starting values for theta0, theta1, and theta2
in the nonlinear model. This converged without problems.

Cheers,
Bert


On Sun, Aug 20, 2023 at 10:15 AM Paul Bernal  wrote:

> Dear friends,
>
> This is the dataset I am currently working with:
> >dput(mod14data2_random)
> structure(list(index = c(14L, 27L, 37L, 33L, 34L, 16L, 7L, 1L,
> 39L, 36L, 40L, 19L, 28L, 38L, 32L), y = c(0.44, 0.4, 0.4, 0.4,
> 0.4, 0.43, 0.46, 0.49, 0.41, 0.41, 0.38, 0.42, 0.41, 0.4, 0.4
> ), x = c(16, 24, 32, 30, 30, 16, 12, 8, 36, 32, 36, 20, 26, 34,
> 28)), row.names = c(NA, -15L), class = "data.frame")
>
> I did the following to try to fit a nonlinear regression model:
>
> #First, Procedure to Find Starting (initial) Values For Theta1, Theta2, and
> Theta3
>
> mymod2 <- y ~ theta1 - theta2*exp(-theta3*x)
>
> strt2 <- c(theta1 = 1, theta2 = 2, theta3 = 3)
>
> trysol2<-nlxb(formula=mymod2, data=mod14data2_random, start=strt2,
> trace=TRUE)
> trysol2
> trysol2$coefficients[[3]]
>
> #Fitting nonlinear Regression Model Using Starting Values From Previous
> Part
> nonlinearmod2 <- nls(mymod2, start = list(theta1 =
> trysol2$coefficients[[1]],
>  theta2 = trysol2$coefficients[[2]],
>  theta3 = trysol2$coefficients[[3]]), data =
> mod14data2_random)
>
> And I got this error:
> Error in nlsModel(formula, mf, start, wts, scaleOffset = scOff, nDcentral =
> nDcntr) :
>   singular gradient matrix at initial parameter estimates
>
> Any idea on how to proceed in this situation? What could I do?
>
> Kind regards,
> Paul
>
> [[alternative HTML version deleted]]
>
> __
> 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.
>

[[alternative HTML version deleted]]

__
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.


[R] Issues when trying to fit a nonlinear regression model

2023-08-20 Thread Paul Bernal
Dear friends,

This is the dataset I am currently working with:
>dput(mod14data2_random)
structure(list(index = c(14L, 27L, 37L, 33L, 34L, 16L, 7L, 1L,
39L, 36L, 40L, 19L, 28L, 38L, 32L), y = c(0.44, 0.4, 0.4, 0.4,
0.4, 0.43, 0.46, 0.49, 0.41, 0.41, 0.38, 0.42, 0.41, 0.4, 0.4
), x = c(16, 24, 32, 30, 30, 16, 12, 8, 36, 32, 36, 20, 26, 34,
28)), row.names = c(NA, -15L), class = "data.frame")

I did the following to try to fit a nonlinear regression model:

#First, Procedure to Find Starting (initial) Values For Theta1, Theta2, and
Theta3

mymod2 <- y ~ theta1 - theta2*exp(-theta3*x)

strt2 <- c(theta1 = 1, theta2 = 2, theta3 = 3)

trysol2<-nlxb(formula=mymod2, data=mod14data2_random, start=strt2,
trace=TRUE)
trysol2
trysol2$coefficients[[3]]

#Fitting nonlinear Regression Model Using Starting Values From Previous Part
nonlinearmod2 <- nls(mymod2, start = list(theta1 =
trysol2$coefficients[[1]],
 theta2 = trysol2$coefficients[[2]],
 theta3 = trysol2$coefficients[[3]]), data =
mod14data2_random)

And I got this error:
Error in nlsModel(formula, mf, start, wts, scaleOffset = scOff, nDcentral =
nDcntr) :
  singular gradient matrix at initial parameter estimates

Any idea on how to proceed in this situation? What could I do?

Kind regards,
Paul

[[alternative HTML version deleted]]

__
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.