growthcurver icon indicating copy to clipboard operation
growthcurver copied to clipboard

Keeping model fits when using SummarizeGrowthByPlate

Open padpadpadpad opened this issue 3 years ago • 2 comments

Hi growthcurver team

Just like to say how useful the package is. The one problem I have had using it is that it is hard to evaluate model fits when using SummarizeGrowthByPlate, and actually the SummarizeGrowth function is pretty perfect and returns everything anyone needs.

So I wrote a little script to loop through a plate and run SummarizeGrowth on each well. Its available on a public gist at the moment https://gist.github.com/padpadpadpad/996fb298c648bac4ce06a7974c8b9808, but thought writing it up into a vignette for the package might be a nice idea.

This allows people to easily plot all the curves, keep all the traits calculated by the package, and most importantly keeps all the models of all the fits.

# load in packages
library(growthcurver) 
library(tidyverse)

# load in example data
d <- growthdata                          # load some sample, simulated data
gc_fit <- SummarizeGrowthByPlate(d)  # do the analysis
plot(gc_fit)                             # plot your data and the best fit
gc_fit$model %>% broom::augment()

gc_fit$vals

# want to grab the fit of all the wells in the plate so we can look at their output together

# setup output dataframe. This is the key step, want every individual curve in there
output <- tibble(well = colnames(d)[colnames(d) != 'time']) %>%
  mutate(model = list(NA),
         vals = list(NA),
         data = list(NA))

# run a for loop to do each fit
for(i in 1:nrow(output)){
  well <- output$well[i]
  gc_fit <- SummarizeGrowth(d$time, d[, well])
  
  output$model[[i]] <- gc_fit$model
  output$vals[[i]] <- gc_fit$vals %>% unlist() %>% bind_rows() %>% mutate(across(k:auc_e, as.numeric))
  output$data[[i]] <- gc_fit$data
}

# get predictions for each well
d_preds <- mutate(output, preds = map(model, broom::augment)) %>%
  unnest(preds) %>%
  select(-vals, - data, -model)

# plot these
ggplot(d_preds) +
  geom_point(aes(t, n)) +
  geom_line(aes(t, .fitted), col = 'red') +
  facet_wrap(~well)

# get summary of fits for each well
d_check <- mutate(output, model_check = map(model, broom::glance)) %>%
  unnest(model_check) %>%
  select(-vals, - data, -model)

# get the values of all the desired traits
d_traits <- unnest(output, vals) %>%
  select(- data, -model)

head(d_traits)

padpadpadpad avatar Feb 08 '22 09:02 padpadpadpad

This is a great idea - thanks for contributing it! I'll add the capability to the vignette for the next release of Growthcurver.

sprouffske avatar Mar 04 '22 12:03 sprouffske

Happy to help where I can.

padpadpadpad avatar Mar 04 '22 13:03 padpadpadpad