Skip to contents

Example dataset and loading libraries

library(flexFitR)
library(dplyr)
library(ggpubr)
set.seed(10)

dt <- data.frame(
  time = c(0, 29, 36, 42, 56, 76, 92, 100, 108),
  variable = c(0, 0, 0.67, 15.11, 77.38, 99.81, 99.81, 99.81, 99.81)
) |>
  mutate(variable = variable + rnorm(n = n(), mean = 5, sd = 6))

Expectation function and modeling

fn <- function(t, t0, t1, t2) t0 / (1 + exp((t1 - t) / t2))
mod <- dt |>
  modeler(
    x = time,
    y = variable,
    fn = "fn",
    parameters = c(t0 = 100, t1 = 50, t2 = 5)
  )
mod
#> 
#> Call:
#> variable ~ fn(time, t0, t1, t2) 
#> 
#> Residuals:
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#> -6.1496 -2.9462  2.1181  0.4716  3.2479  6.7654 
#> 
#> Optimization Results `head()`:
#>  uid  t0   t1   t2 sse
#>    1 101 49.2 4.01 169
#> 
#> Metrics:
#>  Groups      Timing Convergence Iterations
#>       1 0.3881 secs        100%   334 (id)

Plotting predictions and derivatives

a <- plot(mod, color = "blue", title = "Raw data")
b <- plot(mod, type = 4, n_points = 200, color = "black")
c <- plot(mod, type = 5, n_points = 200, color = "black")
d <- plot(mod, type = 6, n_points = 200, color = "black")

ggarrange(a, b, c, d)

plot derivatives