[R] Request for some help about uncertainty analysis using bootstrap approach
Rui Barradas
ru|pb@rr@d@@ @end|ng |rom @@po@pt
Tue May 31 13:03:18 CEST 2022
Hello,
You can use package boot to bootstrap the statistic for you.
Write a function to compute the new column and assign the column means
to the new variable Z or, like in the code below Z2 (so that you can
compare to the Z column of simple averages).
library(dplyr)
library(boot)
boot_uncert <- function(data, indices) {
data[indices, ] %>%
group_by(Group) %>%
mutate(Y = mean(X, na.rm = TRUE),
Z = coalesce(X, Y)) %>%
pull(Z)
}
Df1 <- Df1 %>%
group_by(Group) %>%
mutate(Y = mean(X, na.rm = TRUE),
Z = coalesce(X, Y)) %>%
ungroup()
set.seed(2022)
R <- 1e3
Df1 %>%
mutate(Z2 = colMeans(boot(., boot_uncert, R = R)$t, na.rm = TRUE),
Z2 = coalesce(X, Z2))
Hope this helps,
Rui Barradas
Às 22:23 de 29/05/2022, Bhaskar Mitra escreveu:
> Hello Everyone,
>
> I have a query about uncertainty analysis and would really appreciate some
> help in this regard.
>
> I intend to gapfill the NAs in the “X” column of the dataframe (Df1). I
> have grouped the data using the column “Group” ,
> determined the mean and generated the “Z” column.
>
> While I am using the mean and standard error approach to generate the
> uncertainty analysis, can we use the bootstrap approach to
> generate the uncertainty for the “Z” column? Any help in this regard will
> be really appreciated.
>
> Regards,
> Bhaskar
> ---------------------------------------------------------------
>
> Df1 <-
>
> Group X Y Z
> 1 2 3 2
> 1 NA 3 3
> 1 3 3 3
> 1 4 3 4
> 2 2 2 1
> 2 NA 2 3
> 2 NA 2 3
> 2 4 2 4
> 3 2 2 2
> 3 NA 2 2
> 3 2 2 2
>
> -------------------------------------------------------------------------------
> Codes:
>
> Df1 <- Df1 %>% group_by(Group) %>% summarise(Y= mean(X), na.rm=T)
>
> Df1 <- Df1%>% mutate(Z= coalesce(X,Y))
>
> [[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.
More information about the R-help
mailing list