我正在尝试使用 drc 包中的Gompertz函数在纵向数据上拟合增长模型。为了同时在多个国家/地区生成模型,我嵌套了数据并使用了tidyverse的 map 函数。使用 purrr :: map 的建模步骤运行良好,但是当我尝试使用扫帚中的 glance 选项获取模型摘要时,出现一个错误,即没有任何扫视方法存在于类drc的对象中。但是,根据以下网页,扫视似乎对drc对象有效:https://rdrr.io/github/tidyverse/broom/man/glance.drc.html
下面是一个示例,其中包括涉及2个国家和6个时间点的玩具数据集。
#已测试数据集
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
#安装软件包tidyverse和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 ...
#按国家/地区嵌套数据
> by_country = data %>% group_by(iso_code)%>% nest()
#在嵌套数据上运行drc gompertz模型
> by_country_g = by_country %>% mutate(model= purrr::map(data,~ drm((total_cases)/max(total_cases) ~ num_days, fct=G.4(), data=.)))
#检查模型内容
> 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
一目了然地提取模型摘要
> 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.
答案 0 :(得分: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