I am trying to fit a non-linear model using nls
in R, however, I can't seem to get the model specification correct. Any advice on how to correct the model, would be much appreciated. A minimal example below.
# Import dummy data
data <- read.csv("https://raw.githubusercontent.com/guysutton/nls_singular_repo/main/data2.csv")
head(data)
I think the specification of the fixed effects is what is causing the issue. I am not experienced with fitting 'nls' and picking starting values, as several other StackOverflow posts have indicated that the error I am getting could be because of poor starting values. I have tried starting pH(1:3, in increments of 0.1).
# Fit NLS model
mod <- stats::nls(
# Response variable
y ~
# Predictors
( (x1 + x2) / (-0.6258*pH^4+11.847*pH^3-79.884*pH^2+230.97*(pH)-242.64) ),
start = list(pH = 1),
control = nls.control(maxiter = 1000),
data = data
)
Error in nls(y ~ ((x1 + x2)/(-0.6258 * pH^4 + 11.847 * pH^3 - 79.884 * : singular gradient
However, when I take the fixed effect expression and manually input values for the first row of data, I get the correct value.
# This produces the correct value for the first row of data when I sub in the values
(x1 + x2) / (-0.6258*pH^4+11.847*pH^3-79.884*pH^2+230.97*(pH)-242.64)
(1.50 + 0.45) / (-0.6258*4.41^4+11.847*4.41^3-79.884*4.41^2+230.97*(4.41)-242.64)
[1] 1.132759
Any advice on what might be causing my issue, and how to potentially solve this issue, would be much appreciated.