Non-linear regression analysis with a floor function on (X)

75 Views Asked by At

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

enter image description here

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

There are 1 best solutions below

2
G. Grothendieck On

This type of model does not work well with algorithms based on derivatives. You could just evaluate the model at 1000 random points in a cube and take the best.

f <- function(x) {
  10/(1+1.5*(floor((x/2.5)^0.7)))
}
x <- seq(0, 100, length.out = 50)
dat <- data.frame(x = x, y = f(x))

library(nls2)
set.seed(123)

start_values <- data.frame(k = 1:2, s = 0:1, t = c(1, 3))
fit <- nls2(y ~ 10/(1+k*(floor((x/t)^s))), data = dat,
           algorithm = "random", start = start_values,
           control = nls.control(maxiter = 1000))
fit
## Nonlinear regression model
##   model: y ~ 10/(1 + k * (floor((x/t)^s)))
##    data: dat
##      k      s      t 
## 1.6088 0.7009 2.5114 
##  residual sum-of-squares: 0.2312
##
## Number of iterations to convergence: 1000 
## Achieved convergence tolerance: NA

plot(y ~ x, dat)
lines(fitted(fit) ~ x, dat, col = "red")

screenshot