Search code examples
rggplot2weibull

fit a scatter plot with Weibull curve with ggplot2


I'm not sure if this is duplicate question. But I really hope to get help from here.

I want to plot a graph like in the attachment below, fitting a 2-parameters Weibull curve. The x-axis is days and y-axis is biomaker level, with a cut off is 0.5.

what i want

This is a sample data.

`biomaker level`    days    result
1.5515  81  Positive
0.712   5   Positive
1.831   15  Positive
1.738   30  Positive
1.519   9   Positive
1.2145  21  Positive
2.2085  19  Positive
2.15    18  Positive
2.1845  20  Positive
2.248   18  Positive
2.098   14  Positive
2.2645  36  Positive
2.273   55  Positive
2.213   9   Positive
2.2515  15  Positive
2.245   14  Positive
1.894   68  Positive
2.265   25  Positive
2.2305  25  Positive
1.7955  84  Positive
1.649   85  Positive
1.4635  16  Positive
1.3775  98  Positive
1.008   114 Positive
1.44    35  Positive
0.1845  2   Negative

I have tried this solution but I don't know what the initial values are. It seems this is possible, but what does "127" mean in: nls(y~127*dweibull(x,shape,scale), start=c(shape=3,scale=100))? How can I get this constant from my data?


Solution

  • In that question, 127 is sum of y. But in some reply, it saying that approach have some issues and provide an expedient way. There exist two ways, SSweibull and dweibull.

    1. SSweibull

    df1 <- df1 %>% arrange(days)
    x <- df1$days
    y <- df1$biomaker.level
    yy <- cumsum(y) #max(yy) = 46.0095 ~ 46
    nls(yy ~ SSweibull(x, Asym, Drop, lrc, pwr))
    
    Nonlinear regression model
      model: yy ~ SSweibull(x, Asym, Drop, lrc, pwr)
       data: parent.frame()
      Asym   Drop    lrc    pwr 
    41.698 44.194 -5.675  1.783 
     residual sum-of-squares: 154.5
    
    Number of iterations to convergence: 10 
    Achieved convergence tolerance: 8.941e-06
    
    plot(yy ~ x)
    curve(SSweibull(x, 41.698, 44.194, -5.675, 1.783), add = TRUE)
    

    enter image description here

    2. dweibull

    nls(y ~ 46 * dweibull(x, shape, scale), start = c(shape = 3, scale = 20))
    
    Nonlinear regression model
      model: y ~ 46 * dweibull(x, shape, scale)
       data: parent.frame()
     shape  scale 
     2.375 23.378 
     residual sum-of-squares: 27.93
    
    Number of iterations to convergence: 9 
    Achieved convergence tolerance: 4.33e-06
    
    plot(y ~ x)
    curve(46 * dweibull(x, 2.375, 23.378), add = TRUE)
    

    enter image description here