代码之家  ›  专栏  ›  技术社区  ›  Joshua Rosenberg Choens

Bootstrap在R中使用purrr::rerun()的平均值的标准误差

  •  1
  • Joshua Rosenberg Choens  · 技术社区  · 6 年前

    我试着用 purrr::rerun() R中的函数。例如,这里我试图找到 萼片长

    sample_the_mean <- function(x) {
        the_sample <- sample(x, replace = TRUE)
        mean(the_sample)
    }
    
    sample_the_mean(iris$Sepal.Length)
    
    #> [1] 5.894667
    

    用过一次似乎还不错。这是我的 purrr::重新运行()

        out <- purrr::rerun(10, sample_the_mean, x = iris$Sepal.Length)
    
        out[[1]]
    
    #> [[1]]
    #> function (x) 
    #> {
    #>     the_sample <- sample(x, replace = TRUE)
    #>     mean(the_sample)
    #> }
    #> 
    #> $x
    #>   [1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4
    #>  [18] 5.1 5.7 5.1 5.4 5.1 4.6 5.1 4.8 5.0 5.0 5.2 5.2 4.7 4.8 5.4 5.2 5.5
    #>  [35] 4.9 5.0 5.5 4.9 4.4 5.1 5.0 4.5 4.4 5.0 5.1 4.8 5.1 4.6 5.3 5.0 7.0
    #>  [52] 6.4 6.9 5.5 6.5 5.7 6.3 4.9 6.6 5.2 5.0 5.9 6.0 6.1 5.6 6.7 5.6 5.8
    #>  [69] 6.2 5.6 5.9 6.1 6.3 6.1 6.4 6.6 6.8 6.7 6.0 5.7 5.5 5.5 5.8 6.0 5.4
    #>  [86] 6.0 6.7 6.3 5.6 5.5 5.5 6.1 5.8 5.0 5.6 5.7 5.7 6.2 5.1 5.7 6.3 5.8
    #> [103] 7.1 6.3 6.5 7.6 4.9 7.3 6.7 7.2 6.5 6.4 6.8 5.7 5.8 6.4 6.5 7.7 7.7
    #> [120] 6.0 6.9 5.6 7.7 6.3 6.7 7.2 6.2 6.1 6.4 7.2 7.4 7.9 6.4 6.3 6.1 7.7
    #> [137] 6.3 6.4 6.0 6.9 6.7 6.9 5.8 6.8 6.7 6.7 6.3 6.5 6.2 5.9
    

    呼噜声

    1 回复  |  直到 4 年前
        1
  •  2
  •   Maurits Evers    6 年前

    以下工作:

    set.seed(2018)
    purrr::rerun(10, sample_the_mean(iris$Sepal.Length))
     #[[1]]
    #[1] 5.73
    #
    #[[2]]
    #[1] 5.810667
    #
    #[[3]]
    #[1] 5.868667
    #
    #[[4]]
    #[1] 5.902
    #
    #[[5]]
    #[1] 5.844
    #
    #[[6]]
    #[1] 5.746667
    #
    #[[7]]
    #[1] 5.877333
    #
    #[[8]]
    #[1] 5.853333
    #
    #[[9]]
    #[1] 5.821333
    #
    #[[10]]
    #[1] 5.768
    

    你可以看到 ?rerun ... 指要重新运行的表达式。所以在您的例子中,需要指定一个表达式作为 sample_the_mean(iris$Sepal.Length) ,它将被捕获为一个quosure,然后进行求值。可能是类型 rerun