non-linear regression analysis using a floor function over the independent variable?

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) {
10/(1+1.5*(floor((x/2.5)^0.7)))
}

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

library(ggplot2)
# 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
p

# adding points to the plot
p + geom_point()


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

#See the regression

# imports library
library(minpack.lm)

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))
summary(fit)


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

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.