This is my data.

x <- c(0.5,3.0,22.2,46.0,77.3,97.0,98.9,100.0)
plot(x, pch = 19)

enter image description here

I want to fit a curve through these points and then calculate the slope at different points. Could anyone tell me how to do this in R


Previously I had fitted a Gompertz that gave me a 'maximum' slope. I want to know what are the slopes for other points.


1 Answer 1


Your question is very broad. There are many ways to do this, even without assuming a specific function. For the following I assume that you have a good reason to use the Gompertz model.

First let's fit the model:

y <- c(0.5,3.0,22.2,46.0,77.3,97.0,98.9,100.0)
x <- seq_along(y)
plot(x, y)

fit <- nls(y ~ SSgompertz(x, Asym, b2, b3), data = data.frame(x, y))
curve(predict(fit, newdata = data.frame(x)), add = TRUE)

resulting plot illustrating data and fit

Now, in order to get the desired slopes, you'll need to calculate the first derivative of the fitted function. That is simple highschool maths. In fact, it's so simple that even R can do it although it is not a computer algebra system.

#assign coefficients into global environment
list2env(as.list(coef(fit)), .GlobalEnv)
#create function that returns the gradient
dGomp <- deriv((y ~ Asym*exp(-b2*b3^x)), "x", func = TRUE)

#the model slopes:
c(attr(dGomp(x), "gradient"))
##[1]  0.1010109  6.9594864 27.3791349 31.0194397 20.4539646 10.6588397  5.0141801  2.2561393

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.