Non-linear model fit

Hi everyone. I am working on fitting a non-linear least squares model with R. My data points are virus contentration (ydata) over a timeline of 33 days (xdata). The decrease in virus concentration should be modeled with the following function:

log(c/c0) = (-lambda0/alpha)*(exp(-alpha*time)-1)
#c=virus conc., c0=initial virus conc., lambda0=initial inactivation rate, alpha =resistivity coefficient, time= days

I tried the nls function and ggplots. The fitted line does not "follow the data points" well. The fit is not good. In the following, you can see my R code. Can someone find a better solution for this?

Jan<-c(40312342.96,43718079.07,9376618.33,9797684.99,8833426.24,9618517.21,9618517.21,2446992.71, 2405524.38,3771978.56,1825224.29)
plot(t,Jan, main="e+d+s+")

C0 <- Jan[1]
C_prime <- log(Jan/C0)
y <- C_prime

lambda0 = 0.1 #"initial inactivation rate"
alpha = 0.01 #"resistivity coefficient"

fit = nls(y~((-lambda0/alpha)*(exp(-alpha*x)-1)),start=list(lambda0=0.1,alpha=0.01))

xs<-seq(min(x), max(x), length.out=100)
lines(xs, predict(fit, data.frame(x=xs)))

ggplot(mydata,aes(x,y)) +
geom_point() +
geom_smooth(method="nls", formula=y~(-lambda0/alpha)*(exp(-alpha*x)-1), se=FALSE, start=list(lambda0=0.1,alpha=0.01))

:) Thanks in advance! Lisanne


Point Mass at Zero
Would working on the log-scale for time improve your model fit?
Plots attached for time and log(time) models.
Hej, Thanks for the fast reply. :)
If I use log(time), the RSS is getting smaller, but the parameters (alpha and lambda0) are still not significant.

x2<-c(0,0.000, 1.792, 2.197, 2.485, 2.708, 2.944, 3.091, 3.258, 3.367, 3.497)
fit2 =nls(y~((-lambda0/alpha)*(exp(-alpha*x2-1))),start=list(lambda0=0.01,alpha=0.01))

lambda0 alpha
-0.506 -0.688
residual sum-of-squares: 1.5

Estimate Std. Error t value Pr(>|t|)
lambda0 -0.506 0.128 -3.95 0.0034
alpha -0.688 0.148 -4.65 0.0012

I thought that the fitted model should "follow the the data points" more closely. Am I wrong? Is there a better way of fitting a curve than using the nls function?
Last edited:
significance level of <0,05
As the p-value for lambda0 is 0.0033 it would actually be a good estimate. The p-value for alpha is 0.1222 and is not significant = not a good estimate.
Hi again! I am still working with the same data and I would like to know the best way to plot the data on the original scale and to add the regression curve.

Jan<-c(40312342.96,43718079.07,9376618.33,9797684.99,8833426.24,9618517.21,9618517.21,2446992.71, 2405524.38,3771978.56,1825224.29)

model, model parameters:
Formula: y ~ ((-lambda0/alpha) * (exp(-alpha * x) - 1))

Estimate Std. Error t value Pr(>|t|)
lambda0 -0.16263 0.04104 -3.963 0.00329 **
alpha 0.04243 0.02487 1.706 0.12222

Thank you in advance for your help!