如何在整洁的数据框中将自举样本上的均值映射

时间:2018-09-23 00:04:09

标签: r tidyverse

我正在尝试映射一个函数,该函数将从先前在数据帧中映射的引导程序样本中计算出p_hat。我在功能或使用地图方面遇到困难,可以说这是一项正在进行的工作。

library(tidyverse)
library(rsample)
ttSample <- data.frame(grad =  c(0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1))

bootstrapper <- function(n) {bootstraps(data = ttSample, times = n)}
meanTaker <- function(columnVal) 
{
  for(i in 1:length(columnVal)) 
  {
     meanHolder <- vector("double", nrow(columnVal[[i]]))
     for(j in 1:nrow(columnVal[[i]]))
     {
         meanHolder[[j]] <- mean(as.data.frame(columnVal$splits[[j]])$grad)
     }
   mean(meanHolder)
   }
}

 bootFrame <-  data.frame(n = rep(c(250, 1000, 5000, 10000), 3), 
    confLev = rep(c(0.9, 0.95, 0.99))) %>%
     arrange(n, confLev) %>%
     mutate(alpha = 1 - confLev,
       upperCI = confLev + (alpha / 2),
       lowerCI = confLev - (alpha / 2),
       samples = map(ttSample, list),
       boots = map(.x = .$n, .f = bootstrapper)) %>%
     mutate(p_hat = map(.x = .$boots, .f = meanTaker))

1 个答案:

答案 0 :(得分:0)

您需要记住,map将函数应用于输入的每个元素,而不是整个输入。考虑到这一点,我们可以将meanTaker重写为

meanTaker <- function(boot) {
    grads <- sapply(boot$splits, function (split) split$data$grad)
    mean(grads)
}