Search code examples
rggplot2logistic-regression

How to pass nlpr (n parameter logistic regression) to stat_smooth in ggplot?


My data is as follows

 # A tibble: 24 x 3
     time  OD600     strain
    <dbl>  <dbl>      <chr>
 1 0.0001 0.0001 M12-611020
 2 1.0000 0.0880 M12-611020
 3 3.0000 0.2110 M12-611020
 4 4.0000 0.2780 M12-611020
 5 4.5000 0.4040 M12-611020
 6 5.0000 0.6060 M12-611020
 7 5.5000 0.7780 M12-611020
 8 6.0000 0.9020 M12-611020
 9 6.5000 1.0240 M12-611020
10 8.0000 1.1000 M12-611020
11 0.0001 0.0001 M12-611025
12 1.0000 0.0770 M12-611025
13 3.0000 0.0880 M12-611025
14 4.0000 0.1250 M12-611025
15 5.0000 0.3040 M12-611025
16 5.5000 0.4210 M12-611025
17 6.0000 0.5180 M12-611025
18 6.5000 0.6160 M12-611025
19 7.0000 0.7180 M12-611025
20 7.5000 0.8520 M12-611025
21 8.0000 0.9400 M12-611025
22 8.5000 0.9500 M12-611025
23 9.0000 0.9680 M12-611025

I have 2 "strains" in the data.frame each with thier own set's of values for "time" and "OD600".

I have so far been plotting using ggplot as follows (removing asthetics for simplicity) using "loess" to fit a curve:

growth_curve_SE <- growth_curve + 
  stat_smooth(aes(group=strain,fill=strain, colour = strain) ,method = "loess", se = T, alpha=0.2 , span = 0.8) + 
  geom_point(aes(fill=factor(strain)),alpha=0.5 , size=3,shape = 21,colour = "black", stroke = 1)

What I ultimately want to achieve is fitting a 5 parameter logictical regression rather then "loess" for the method as it is a better model for the data and fits a more accurate curve.

I used the package "nplr" to fit the regression for multiple strains using a list split as per strain:

strain_list <- split(multi_strain, multi_strain$strain)

np2 <- lapply(strain_list, function(tmp) {nplr(tmp$time, tmp$OD600, useLog = F)})

Which fits the regression:

$`M12-611020`
Instance of class nplr 

Call:
nplr(x = tmp$time, y = tmp$OD600, useLog = F)
weights method: residuals

5-P logistic model
Bottom asymptote: 0.03026607 
Top asymptote: 1.104278 
Inflexion point at (x, y): 5.297454 0.6920488 
Goodness of fit: 0.9946967 
Weighted Goodness of fit: 0.9998141 
Standard error: 0.0308006 0.01631115 


$`M12-611025`
Instance of class nplr 

Call:
nplr(x = tmp$time, y = tmp$OD600, useLog = F)
weights method: residuals

5-P logistic model
Bottom asymptote: -0.0009875526 
Top asymptote: 0.9902298 
Inflexion point at (x, y): 6.329304 0.5919818 
Goodness of fit: 0.9956551 
Weighted Goodness of fit: 0.9998606 
Standard error: 0.02541948 0.01577407 

Any ideas how I can achieve the same in ggplot using the "stat_smooth" command to use the 5 parameter logistic regression, either with or without the "nplr" package?


Solution

  • For anyone interested, I found a work around for this.

    The nplr packages allows you to output the curve as a series of x and y co-ordiantes as follows:

    x <- getXcurve(data)
    y <-getYcurve(data)
    

    From this I used "geom_line" function using these x and y parameters and that gave me the (n)parameter logistic regression I was after inthe form of a logn. Layer one the "geom_point" as abice and you get a good looking graph