获得R中多个模型的均值和多个置信区间

时间:2019-02-26 22:02:32

标签: r purrr

我希望同时获得R中多个模型的95%和90%置信区间以及均值。

数据



data <- data.frame(occup = c(2,3,5,4,2,2,6,1,2,0),
                   unoccup = c(1,2,0,3,0,4,1,1,2,2),
                   month = c("feb", "feb", "feb", "feb", "feb", "mar", "mar", "mar", "mar", "mar"))

我有功能

binomNLL_ratio = function(p, k, N) {
  -sum(dbinom(k, prob = p, size = N, log=TRUE))
}

所需的库

library(purrr)
library(bbmle)

运行脚本

data %>%
   split(.$month) %>% map(~mle2(minuslog = binomNLL_ratio, start = list(p = 0.5), data = list(N = .$occup + .$unoccup, k = .$occup))) %>%
  map(confint, level = 0.95)

这很好地给了我每个月95%的置信区间。我还可以将0.95替换为0.9以获得90%的置信度,或者将map(confint)替换为map(coef)以获取每个月模型的均值。

尽管如此,理想情况下,我还是希望在同一数据框中获得每个模型的95%CI,90%CI和均值。如何传递多个函数和参数以获得所需的结果?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

这是一个棘手的小虫子,我花了太多时间试图使invoke_map()工作。最后,我选择了一种稍微有点怪异的处理方式,但是它返回了带有confint()coef()值的小标题:

library(tidyverse)
library(bbmle)
#> Loading required package: stats4
#> 
#> Attaching package: 'bbmle'
#> The following object is masked from 'package:dplyr':
#> 
#>     slice

data <- data.frame(occup = c(2,3,5,4,2,2,6,1,2,0),
                   unoccup = c(1,2,0,3,0,4,1,1,2,2),
                   month = c("feb", "feb", "feb", "feb", "feb", "mar", "mar", "mar", "mar", "mar"))

binomNLL_ratio = function(p, k, N) {
  -sum(dbinom(k, prob = p, size = N, log=TRUE))
}

data %>%
  split(.$month) %>% 
  map(~mle2(minuslog = binomNLL_ratio, start = list(p = 0.5), 
            data = list(N = .$occup + .$unoccup, k = .$occup))) %>% 
  imap_dfr(~ {tibble(confints = confint(.x, level = .95), 
                coefs = coef(.x),
                month = .y)})
#> Warning in dbinom(k, prob = p, size = N, log = TRUE): NaNs produced

#> Warning in dbinom(k, prob = p, size = N, log = TRUE): NaNs produced

#> Warning in dbinom(k, prob = p, size = N, log = TRUE): NaNs produced

#> Warning in dbinom(k, prob = p, size = N, log = TRUE): NaNs produced
#> # A tibble: 4 x 3
#>   confints coefs month
#>      <dbl> <dbl> <chr>
#> 1    0.523 0.727 feb  
#> 2    0.881 0.727 feb  
#> 3    0.317 0.524 mar  
#> 4    0.725 0.524 mar

reprex package(v0.2.1)于2019-02-26创建