# Non-linear model fit

#### Lisanne

##### New Member
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)
t<-c(0,1,6,9,12,15,19,22,26,29,33)
plot(t,Jan, main="e+d+s+")
mydata<-data.frame(t,Jan)

C0 <- Jan[1]
C_prime <- log(Jan/C0)
x<-t
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))
summary(fit)

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

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

#### ledzep

##### Point Mass at Zero
Would working on the log-scale for time improve your model fit?
Plots attached for time and log(time) models.

#### Lisanne

##### New Member
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<-log(t)
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))

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

summary(fit2)
Parameters:
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:

#### Dason

How are you defining "still not significant"?

#### Lisanne

##### New Member
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.

#### Dason

Looks to me like the p-value is 0.0012.
Which is exactly why I asked the question. If we're talking about statistical significance then the output posted seems to imply the parameters are significant.

#### Lisanne

##### New Member
Oh that´s true. I made a mistake.
Thanks again.

#### Lisanne

##### New Member
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.

data:
Jan<-c(40312342.96,43718079.07,9376618.33,9797684.99,8833426.24,9618517.21,9618517.21,2446992.71, 2405524.38,3771978.56,1825224.29)
t<-c(0,1,6,9,12,15,19,22,26,29,33)

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

Parameters:
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