Message-ID: <9dac775c-b4b1-6ae2-1bf7-577e6925ff59@sapo.pt>
Date: 2022-03-23T05:50:28Z
From: Rui Barradas
Subject: bootstraping by groups
In-Reply-To: <72024f64-8866-bb2f-cd79-df2fce7605e0@sapo.pt>
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 at 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 at 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.