My aim : I want to fit my curve on R with this equation and find k and To.
For example, I did that just below but I have three problems (and maybe it's not the good solution):
It give me a linear regression (photo_2 )
My coefficient aren't free (I need to find a solution where my coefficients can change in function of the better result found by R).
My last line code doesn't works
t <- PLR2decroissance$pupil_timestamp_secondes # these are my time points
k <- 2.5 # time constant
To <- 0.48 # growth constant
y <- (-k*t+To)+t^-0.5 # my equation
# visualise
par(mfrow = c(1, 2))
plot(t, y)
lines(PLR2decroissance$pupil_timestamp_secondes , PLR2decroissance$diameter_3d_corrige , col="red")
Thank you for your answer. When you sayed " It would help if you could post your data in a readable form, perhaps using dput() " : you want I share my datas on the chat (if it's that, I can try).
However I'm novice on R and I don't understand how I can estimate k and To. I'm lost ..
I don't know how I can copying and pasting my datas (I'm sorry but I'm verry new in this field). So I exported my dataframe in a csv and I copy and past 64 row of my 2 columns "pupil_timestamp_secondes" and "diameter_3d_corrige". Is it works like that ?
Thank you very much for your answer. I was on the good road, because yesterday, I tried with the "nls" function but I don't understand how use this function, even if I use internet. So thank you for your help.
Now, I tried your solution since 30 min :
x = PLR2decroissance$pupil_timestamp_secondes
y = PLR2decroissance$diameter_3d_corrige
plot(x,y, col="blue") # This part works for me
formula <- y~ (-ax+b)+x^-0.5
a_start <- .48
b_start <- 2.57
m<-nls(y ~ I(a exp(-b*x)+ x^-0.5),start=list(a=a_start,b=b_start)) # It appears an error for this row
lines(x,predict(m),col="red",lty=2,lwd=3)
Now I have this message : " Error: unexpected symbol in "m<-nls(y ~ I(a exp" "
In addition, sorry, but I don't understand why we use "a_start" and "b_start" (I saw that at this link nls function - RDocumentation but I don't understand the uses) and this part with the "I" function " I(a exp "
I'm thinking, after, when it works, I will have in my dataframe "m" the coefficients a and b ?
However, if I want to use the exposed formula in the beginning, how I can do it ? Because I showed to my advisor some weeks ago a linear regression on this curve and he sayed in this case it's better to use a formula with a linear regression + an exponential because we have a curve at the ending of the graph.
That's what you now have. You've estimated k and To exactly for that equation. (Well, actually the coefficient from the linear regression is -k, (my fault)).
Currently, with your (Photo_6) formula and an other formula (Photo_5), I have two differents linear regression, so I have a straight line and not a curve :
A regression is linear if it is linear in the parameters to be estimated. It doesn't matter if some of the things on the right happen to be nonlinear functions so long as the parameters to be estimated are outside those functions. That's the case in your model.
To get the parameters it turned out to be easiest to take the square root term to the left. That means that the predicted values you used in abline aren't exactly what you want.
By the way, are you sure you don't want a coefficient in front of t^-0.5 in your equation?
Thank you very much for these explanations. So if I understand, even if my equation look like non-linear, if my parameters are linear .. I will have a linear function. So now, I have an other question : why in excel I have a curve for this formula ?
Maybe a coefficient in front of t^-0.5 in my equation will be better ? What is your proposition ?
And about the abline function, I'm understanding, this function graph a straight line. So maybe it's the reason why it draw a straight line .. but with the "lines" function it doesn't works also, it draw a straight line also. But if I think about your explanations, it's normal because my parameters are linear. So I come back to my question on the top of this post, why in excel I have a curve for this formula ?
Thank you, your code works but it give a straight line.
I would like realise a curve like this video : Nonlinear curve fitting in R using mosaic and nls - YouTube. It explain, we can fit a straight line but we can also find the curve who fitting with datas. I did what it say in this video, but it doesn't works an other once, I have a straight line.
The code is really similar to @fcas80 and I share to you what I did :
Thank you very much for your answer. With a help, I found the code, so I share the code who can give me a curve like the curve on Excel, maybe you can understand me better :
# y ~ (-k*x+To)+x^-0.5
mod <- glm(y ~ pupil_timestamp_secondes + sqrt(pupil_timestamp_secondes), df, family="gaussian")
summary(mod)
plot(y~pupil_timestamp_secondes, df, type="l")
lines(df$pupil_timestamp_secondes, predict(mod), col = 'blue')