I have created a data set to understand better an equation and apply it to predict behavior.

The equation is y = 10/(1+k*⌊((x/t)^s)⌋).

To create the data set and see if it is working properly, I did the following:

# creating the floor function f(x)
f = function(x) {

# specifying the domain of f(x)
x = seq(0, 100, length.out = 50) # x contains 50 points between 0 and 100

# creating a data frame that contains x and f(x)
dat = data.frame(x = x, y = f(x))
p = ggplot(dat, aes(x = x, y = y)) + 
  geom_step() # geom_step creates a stairs plot

# adding points to the plot
p + geom_point()

I got this result, which is what I expect from it

Then, I wanted to check a regression analysis over this data set using the following function:

#See the regression

# imports library

start_values <- c(k=1, s=0.3, t=2)
fit <- nls(dat$y ~ 10/(1+k*(floor((dat$x/t)^s))),
           data = dat,
           algorithm = "port",
        start = start_values,
        control = nls.control(maxiter = 1000))

But I get the following error:

Error in nlsModel(formula, mf, start, wts, upper, scaleOffset = scOff, : singular gradient matrix at initial parameter estimates

What should I do to avoid it? or which analysis should I perform then? I'm not an expert on stats.

Thanks for your help!


1 Answer 1


The problem is that the package you are using (minpack.lm) implements a version of the Levenberg-Marquardt algorithm, which requires gradients. Your function f is nondifferentiable at the values of $x$ where the vertical steps occur.

Possible workarounds include looking for a least-squares algorithm that works with nondifferentiable / nonconvex objective functions, or perhaps settling for a heuristic that produces a "reasonable" but not optimal fit.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.