padpadpadpad / nls.multstart

Non-linear regression in R allowing multiple start parameters
18 stars 4 forks source link

Error in nls_multstart(speed ~ sharpeschoolhigh_1981(temp = temperature, : There must be as many parameter starting bounds as there are parameters #17

Open AlannRath opened 2 years ago

AlannRath commented 2 years ago

Hey everyone, (first post here 😃 )

I have been running the same script once or twice already but this time, for some reason it does not work. I have a very simple dataset with the speed of individuals at different temperature and I want to draw and fit a thermal response curve following the sharpe-Schoolfield model

`start_vals <- get_start_vals(speed$temp, speed$medianSpeed, model_name = 'sharpeschoolhigh_1981')

low_lims <- get_lower_lims(speed$temp, speed$medianSpeed, model_name = 'sharpeschoolhigh_1981')

upper_lims <- get_upper_lims(speed$temp, speed$medianSpeed, model_name = 'sharpeschoolhigh_1981')`

So this step works fine, but then I do: fit <- nls_multstart(speed~sharpeschoolhigh_1981(temp = temperature, r_tref,e,eh,th, tref = 15), data = speed, iter = 500, start_lower = start_vals - 10, start_upper = start_vals + 10, lower = low_lims, upper = upper_lims, supp_errors = 'Y')

As simple as that. However, I still have the issue

Error in nls_multstart(speed ~ sharpeschoolhigh_1981(temp = temperature, : There must be as many parameter starting bounds as there are parameters

I tried to look at my start_vals vector and I got NA for 'eh'. I don't know if there is any issue related to that.

Any help would be more than welcome,

Thanks Alann

padpadpadpad commented 2 years ago

Hi Alann

Sorry I have only just seen this, was on holiday last week.

The issue will come from you having NA for the eh start value. You need as many start values (4), as the number of free parameters. I am interested as to why you are getting NA for the eh value?

Why are you using speed$medianSpeed in get_start_vals() but speed as the response variable in nls_multstart()?

Cheers Dan

AlannRath commented 2 years ago

Hi Dan,

Thank you very much for the reply, appreciate it. I tried t omodify slightly the code according to your comment.

First I check if I had NA for the get_start_vals() but I do not, here are the results: r_tref e eh th 11.41887 10.00000 20.00000 32.50000

I also checked if i had NA for the low_lims() and upper_lims(), I do not have NA in those.

I changed the speed to median$Speed in the nls_multstart(), but here again I still got the same issue. I am wondering if there is any way that the R version i am using is impacting the packages ?

In any case, I am also happy to share part of the code and data to reproduce the error

Thanks again for the response Alann

padpadpadpad commented 2 years ago

Happy to have a look at the code if you send them across. Feel free to email them if you do not want them publicly available yet.

Cheers Dan

mathesong commented 1 year ago

@AlannRath : another possibiliity is that you could have a column in your data (or environment if you're not using the data argument) with the same name as one of the parameters that you're estimating? The model figures out how many parameters you want to estimate based on what it doesn't find available to it.

Actually, @padpadpadpad , I run into this problem reasonably often, and I usually solve it by going debugging to figure out how many models the model is estimating, and compare that to how many I have in the lower and upper. Do you reckon it's worthwhile making a more informative error message here, where we state which parameters the model is considering as known, and which it thinks it's estimating, compared to how many parameters are in the upper and lower vectors? It would be a bit of a chunky error message, but it would certainly make these issues quicker and easier to debug... If you think this sounds reasonable, I can give it a try sometime in the coming week or two...

Cheers, G

padpadpadpad commented 1 year ago

Yea that does sound useful. This is probably something I used to do all the time too.

It feels relatively simple to do a check early on in the function call comparing columns/the environment to those in the formula/lower and upper vectors.

Could put a check at an early part of nls_multstart when we are setting things up.