0

I am trying to fit growth models on longitudinal data using the Gompertz function in the drc package. To generate models on several countries simultaneously, I nested the data and used the map function from tidyverse. The modeling step using purrr::map runs fine, but when I try to get the model summaries using the glance option in broom, I am getting an error that no glance method exists for objects of class drc. However, glance appears to be operative on drc objects, as per this webpage: https://rdrr.io/github/tidyverse/broom/man/glance.drc.html

Below is an example, including the toy dataset involving 2 countries and 6 timepoints for each.

#Dataset tested

iso_code    total_cases num_days  
IND 1   1  
IND 3   10  
IND 3   20  
IND 3   30  
IND 165 50  
IND 979 60  
SGP 3   1  
SGP 18  10  
SGP 47  20  
SGP 86  30  
SGP 187 50  
SGP 455 60  

#installing packages tidyverse and drc

> library(tidyverse)
> library(drc)

> data = read.delim("test_data.txt", sep='\t', header=T)
> dim(data)

[1] 12  3

> str(data)

'data.frame':   12 obs. of  3 variables:
 $ iso_code   : chr  "IND" "IND" "IND" "IND" ...
 $ total_cases: int  1 3 3 3 165 979 3 18 47 86 ...
 $ num_days   : int  1 10 20 30 50 60 1 10 20 30 ...

#nesting data by country

> by_country = data %>% group_by(iso_code)%>% nest()

#running drc gompertz model on nested data

> by_country_g = by_country %>% mutate(model= purrr::map(data,~  drm((total_cases)/max(total_cases) ~ num_days, fct=G.4(), data=.)))

#checking model contents

> by_country_g$model

[[1]]

A 'drc' model.

Call:
drm(formula = (total_cases)/max(total_cases) ~ num_days, data = .,     fct = G.4())

Coefficients:
b:(Intercept)  c:(Intercept)  d:(Intercept)  e:(Intercept)  
    -0.164861       0.002555       1.531310      54.838386  


[[2]]

A 'drc' model.

Call:
drm(formula = (total_cases)/max(total_cases) ~ num_days, data = ., fct = G.4())

Coefficients:
b:(Intercept)  c:(Intercept)  d:(Intercept)  e:(Intercept)  
      -0.1257         0.0845         1.4638        52.9056  

#extract model summaries by glance

> summary_g = by_country_g %>% mutate(glance = purrr::map(model, broom::glance))%>% unnest(glance)


Error: Problem with mutate() input glance.  
x No glance method for objects of class drc  
ℹ Input glance is purrr::map(model, broom::glance).  
ℹ The error occured in group 1: iso_code = "IND".
Run rlang::last_error() to see where the error occurred.
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213

1 Answers1

0
library(tidyverse)
library(drc)

data %>% 
  nest(data = -iso_code) %>%
  mutate(model= map(data,~  drm((total_cases)/max(total_cases) ~ num_days, 
                    fct=G.4(), data=.)), 
         glance = map(model, broom::glance)) %>%
  unnest(glance)

#   iso_code data             model     AIC    BIC logLik    df.residual
#  <chr>    <list>           <list>  <dbl>  <dbl> <logLik>        <int>
#1 IND      <tibble [6 × 2]> <drc>  -59.8  -60.8  34.884966           2
#2 SGP      <tibble [6 × 2]> <drc>   -7.39  -8.43  8.694533           2
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213