Re: [R] non-linear regression and root finding

2023-11-07 Thread Ivan Krylov
On Mon, 6 Nov 2023 14:55:39 -0500 J C Nash wrote: > However, I'm wondering if this approach is worth writing up, at least > as a vignette or blog post. It does need a shorter example and some > explanation of the "why" and some testing perhaps. Do you mean using this problem as a basis to

Re: [R] non-linear regression and root finding

2023-11-06 Thread Berwin A Turlach
G'day Troels, On Tue, 7 Nov 2023 07:14:02 +0100 Troels Ring wrote: > Be as it may, I wonder if not your method might work if only we KNOW > that pK1 is either positive OR negative, in which case we have pK1 = > -exp(theta1)? If pK1 can be either negative or positive (or 0 :-) ), and it is just

Re: [R] non-linear regression and root finding

2023-11-06 Thread Troels Ring
Thanks a lot, Berwin. Unfortunately, pK1 may well be negative and as I understand the literature it may be poorly defined as such, and also seems to be at a boundary, since when lower is set to say rep(-4,3) pK1 is returned as -4 while pK2 and pK3 are undisturbed. Perhaps the point is that pK1

Re: [R] non-linear regression and root finding

2023-11-06 Thread Berwin A Turlach
G'day Troels, On Mon, 6 Nov 2023 20:43:10 +0100 Troels Ring wrote: > Thanks a lot! This was amazing. I'm not sure I see how the conditiion > pK1 < pK2 < pK3 is enforced? One way of enforcing such constraints (well, in finite computer arithemtic only "<=" can be enforced) is to rewrite the

Re: [R] non-linear regression and root finding

2023-11-06 Thread J C Nash
I won't send to list, but just to the two of you, as I don't have anything to add at this time. However, I'm wondering if this approach is worth writing up, at least as a vignette or blog post. It does need a shorter example and some explanation of the "why" and some testing perhaps. If there's

Re: [R] non-linear regression and root finding

2023-11-06 Thread Troels Ring
Thanks a lot! This was amazing. I'm not sure I see how the conditiion pK1 < pK2 < pK3 is enforced? - it comes from the derivation via generalized Henderson-Hasselbalch but perhaps it is not really necessary. Anyway, the use of Vectorize did the trick! Best wishes Troels Den 06-11-2023 kl.

Re: [R] non-linear regression and root finding

2023-11-06 Thread J C Nash
Your script is missing something (in particular kw). I presume you are trying to estimate the pK values. You may have more success with package nlsr than nls(). nlsr::nlxb() tries to get the Jacobian of the model specified by a formula and do so by applying symbolic or automatic differentiation.

Re: [R] non-linear regression and root finding

2023-11-06 Thread Ivan Krylov
В Mon, 6 Nov 2023 17:53:49 +0100 Troels Ring пишет: > Hence I wonder if I could somehow have non linear regression to find > the 3 pK values. Below is HEPESFUNC which delivers charge in the > fluid for known pKs, HEPTOT and SID. Is it possible to have > root-finding in the formula with nls?

[R] non-linear regression and root finding

2023-11-06 Thread Troels Ring
Dear friends - I have a function for the charge in a fluid (water) buffered with HEPES and otherwise only containing Na and Cl so that [Na] - [Cl] = SID (strong ion difference) goes from -1 mM to 1 mM. With known SID and total HEPES concentration I can calculate accurately the pH if I know 3