nls help

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

nls help

chuck.01
Hello,
I have data like the following:

datum <- structure(list(Y = c(415.5, 3847.83333325, 1942.833333325, 1215.22222233333,
950.142857325, 2399.5833335, 804.75, 579.5, 841.708333325, 494.053571425
), X = c(1.081818182, 0.492727273, 0.756363636, 0.896363636,
1.518181818, 0.499166667, 1.354545455, 1.61, 1.706363636, 1.063636364
)), .Names = c("Y", "X"), row.names = c(NA, -10L), class = "data.frame")


with(datum, plot(Y~X))

As you can see there is a non-linear association between X and Y, and I would like to fit an appropriate model.  I was thinking an exponential decay model might work well.

I tried the following (a and k starting values are based off of a lm() fit), but get an error.

fit <- nls(Y ~ a*exp(-k * X), datum, start=c(a=3400, k=1867))

Error in nlsModel(formula, mf, start, wts) :
  singular gradient matrix at initial parameter estimates

I have never attempted to fit a non-linear model before, and thus the model may be inappropriately specified, or it is also possible that I have no idea what I am doing.

Would someone please offer some advice.

Thanks.
Chuck
Reply | Threaded
Open this post in threaded view
|

Re: nls help

Dieter Menne
chuck.01 wrote
datum <- structure(list(Y = c(415.5, 3847.83333325, 1942.833333325, 1215.22222233333,
950.142857325, 2399.5833335, 804.75, 579.5, 841.708333325, 494.053571425
), X = c(1.081818182, 0.492727273, 0.756363636, 0.896363636,
1.518181818, 0.499166667, 1.354545455, 1.61, 1.706363636, 1.063636364
)), .Names = c("Y", "X"), row.names = c(NA, -10L), class = "data.frame")

with(datum, plot(Y~X))

As you can see there is a non-linear association between X and Y, and I would like to fit an appropriate model.  I was thinking an exponential decay model might work well.
I tried the following (a and k starting values are based off of a lm() fit), but get an error.

fit <- nls(Y ~ a*exp(-k * X), datum, start=c(a=3400, k=1867))

Error in nlsModel(formula, mf, start, wts) :
  singular gradient matrix at initial parameter estimates
Try

plot(datum$X,datum$Y)

For more complex cases, plot the initial function you are trying to fit, but in this case it is easy to see that k is more in the order of 2.

So try with k=2.

Dieter

Reply | Threaded
Open this post in threaded view
|

Re: nls help

Michael Weylandt
In reply to this post by chuck.01
It's a scaling problem:

If you do this:

datum <- datum[order(datum$X),]

with(datum, plot(Y~X))
with(datum, lines(X, 3400*exp(-1867*X)))

you'll see that your initial guess is just so far gone that the nls()
optimizer can't handle it.

If you try a more reasonable initial guess it works fine:

fit <- nls(Y ~ a*exp(-k * X), datum, start=c(a=3400, k=1.867))

Michael

On Wed, Nov 30, 2011 at 12:14 PM, chuck.01 <[hidden email]> wrote:

> Hello,
> I have data like the following:
>
> datum <- structure(list(Y = c(415.5, 3847.83333325, 1942.833333325,
> 1215.22222233333,
> 950.142857325, 2399.5833335, 804.75, 579.5, 841.708333325, 494.053571425
> ), X = c(1.081818182, 0.492727273, 0.756363636, 0.896363636,
> 1.518181818, 0.499166667, 1.354545455, 1.61, 1.706363636, 1.063636364
> )), .Names = c("Y", "X"), row.names = c(NA, -10L), class = "data.frame")
>
>
> with(datum, plot(Y~X))
>
> As you can see there is a non-linear association between X and Y, and I
> would like to fit an appropriate model.  I was thinking an exponential decay
> model might work well.
>
> I tried the following (a and k starting values are based off of a lm() fit),
> but get an error.
>
> fit <- nls(Y ~ a*exp(-k * X), datum, start=c(a=3400, k=1867))
>
> Error in nlsModel(formula, mf, start, wts) :
>  singular gradient matrix at initial parameter estimates
>
> I have never attempted to fit a non-linear model before, and thus the model
> may be inappropriately specified, or it is also possible that I have no idea
> what I am doing.
>
> Would someone please offer some advice.
>
> Thanks.
> Chuck
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/nls-help-tp4123876p4123876.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> 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.
>

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: nls help

chuck.01
Ah, I see, thank you both.



Michael Weylandt wrote
It's a scaling problem:

If you do this:

datum <- datum[order(datum$X),]

with(datum, plot(Y~X))
with(datum, lines(X, 3400*exp(-1867*X)))

you'll see that your initial guess is just so far gone that the nls()
optimizer can't handle it.

If you try a more reasonable initial guess it works fine:

fit <- nls(Y ~ a*exp(-k * X), datum, start=c(a=3400, k=1.867))

Michael

On Wed, Nov 30, 2011 at 12:14 PM, chuck.01 <[hidden email]> wrote:
> Hello,
> I have data like the following:
>
> datum <- structure(list(Y = c(415.5, 3847.83333325, 1942.833333325,
> 1215.22222233333,
> 950.142857325, 2399.5833335, 804.75, 579.5, 841.708333325, 494.053571425
> ), X = c(1.081818182, 0.492727273, 0.756363636, 0.896363636,
> 1.518181818, 0.499166667, 1.354545455, 1.61, 1.706363636, 1.063636364
> )), .Names = c("Y", "X"), row.names = c(NA, -10L), class = "data.frame")
>
>
> with(datum, plot(Y~X))
>
> As you can see there is a non-linear association between X and Y, and I
> would like to fit an appropriate model.  I was thinking an exponential decay
> model might work well.
>
> I tried the following (a and k starting values are based off of a lm() fit),
> but get an error.
>
> fit <- nls(Y ~ a*exp(-k * X), datum, start=c(a=3400, k=1867))
>
> Error in nlsModel(formula, mf, start, wts) :
>  singular gradient matrix at initial parameter estimates
>
> I have never attempted to fit a non-linear model before, and thus the model
> may be inappropriately specified, or it is also possible that I have no idea
> what I am doing.
>
> Would someone please offer some advice.
>
> Thanks.
> Chuck
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/nls-help-tp4123876p4123876.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> 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.
>

______________________________________________
[hidden email] mailing list
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.