library(ggplot2)
## Registered S3 methods overwritten by 'tibble':
## method from
## format.tbl pillar
## print.tbl pillar
#Definition of the data matrix:
M = matrix(c(0,100,15,65,30,40,45,25,60,13,90,7,120,3), nrow=7, ncol=2,byrow = TRUE)
dimnames(M) = list(c("1", "2", "3","4","5","6","7"),c("x", "y"))
#Conversion of the matrix into a dataframe:
data=as.data.frame(M)
#Definition of the equation for non-linear curve fitting:
equation = function(x,y0,halftime)
+ y0*0.5^(x/halftime)
#Curve fit (non-linear least square method) based on the equation and with starting values for the fit:
fit1 = nls(y~equation(x,myy0,myhalftime),data=data,start=list(myy0=100,myhalftime=15))
#Extraction of fitted parameters:
myy0=coef(fit1)["myy0"]
myhalftime=coef(fit1)["myhalftime"]
#Definition of a fitting function, so that a smooth curve can be plotted (instead of fitting only y-values for the given x-values):
fun1=function(x) myy0 * (0.5)^(x/myhalftime)
#Plotting the data points and the fitting function + the halftime value at y=50:
ggplot(data=data,aes(x,y))+geom_point(size=4, alpha=.5)+geom_function(aes(x,y),fun=fun1)+annotate("text",x=5, y=60, label="t(1/2):")+annotate("text",x=15, y=50, label=myhalftime)