[R] bootstraping by groups

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Wed Mar 23 06:50:28 CET 2022


Hello,

Sorry, there's an error.

Às 22:33 de 22/03/2022, Rui Barradas escreveu:
> Hello,
> 
> The error is in
> 
> boot.pop. <- boot(daT, bootprop, 999)
> 
> 
> Here are basse R and tidyverse solutions. But first, create a test data 
> set. The values in vector 'year' are the iris species names, I haven't 
> changed them.
> 
> 
> daT <- iris[3:5]
> names(daT) <- c("BothTimes", "onlyoneTime", "year")
> 
> 
> And the bootstrap function.
> 
> 
> library(boot)
> 
> bootprop <- function(data, index){
>    d <- data[index, ]
>    sum(d[["BothTimes"]], na.rm = TRUE)/sum(d[["onlyoneTime"]], na.rm = 
> TRUE)
> }
> 
> 
> 
> 1. Base R
> 
> Only one package needs to be loaded and the code is simple.
> 
> 
> # split the data by year
> sp <- split(daT, daT$year)
> lapply(sp, \(x) boot(x, bootprop, 999)$t0)
> 
> 
> 2. tidyverse
> 
> Also load packagges dplyr and purrr. To call purrr::map_dfr an extra 
> function is needed.
> 
> 
> library(dplyr)
> library(purrr)
> 
> boot_fun <- function(data, FUN, R){
>    boot.prop <- boot(data, FUN, R)
>    c(boot.prop = boot.prop$t0)
> }

This function should be


boot_fun <- function(data, FUN, R){
   boot.prop <- boot(data, FUN, R)
   c(boot.prop = mean(boot.prop$t))
}


Hope this helps,

Rui Barradas
> 
> daT %>%
>    group_split(year) %>%
>    map_dfr(boot_fun, bootprop, R = 999)
> 
> 
> Hope this helps,
> 
> Rui Barradas
> 
> Às 20:45 de 22/03/2022, Marna Wagley escreveu:
>> Hi All,
>> I have many classes and was trying to estimate the value using a
>> bootstrapping approach for each group with the following code. 
>> However, it
>> did not work when I added a group in the code. Do you have any 
>> suggestions?
>> thanks,
>>
>>
>> bootprop <- function(data, index){
>>    d <- data[index, ]
>>    sum(d[["BothTimes"]], na.rm = TRUE)/sum(d[["onlyoneTime"]], na.rm = 
>> TRUE)
>> }
>>
>> daT %>%
>>    group_by(year) %>%
>>    boot.pop. <- boot(daT, bootprop, 999) %>%
>> boot.pop.$t0)
>>
>>     [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide 
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list