我试图使用刚果民主共和国封装中的Gompertz函数来拟合纵向数据上的增长模型。为了在几个国家同时生成模型,我嵌套了数据并使用了tidyverse的map函数。使用purrr::map的建模步骤运行得很好,但是当我试图使用扫帚中的try选项获取模型摘要时,我会得到一个错误,即对于类drc的对象不存在瞥视方法。然而,根据这个网页:https://rdrr.io/github/tidyverse/broom/man/glance.drc.html,对drc对象的浏览似乎是有效的。
下面是一个例子,包括玩具数据集涉及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.发布于 2021-05-30 06:23:22
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 2https://stackoverflow.com/questions/62494544
复制相似问题