Forgive me this is my first time asking a question online.
First: setting up some data for ease of asking question.
location <- c(1, 2, 3, 4)
numerator_estimate <- c(625, 180, 210, 1753)
numerator_variance <- c(22165, 2451, 11610, 172968)
denominator_estimate <- c(2278 , 4742, 1115, 26892)
denominator_variance <- c(15870, 688, 7172, 1908288)
my_df <-data.frame(location, numerator_estimate, numerator_variance, denominator_estimate, denominator_variance)
This function bootstraps the SE of a quotient given the estimate and variance of both the numerator and denominator
calculate_quotient_se <- function(numerator_estimate_f, numerator_variance_f, denominator_estimate_f, denominator_variance_f, iterations = 10000){
numerator_sim <- rnorm(n = iterations, mean = numerator_estimate_f, sd = sqrt(numerator_variance_f))
denominator_sim <- rnorm(n = iterations, mean = denominator_estimate_f, sd = sqrt(denominator_variance_f))
quotient_sim <- numerator_sim/denominator_sim
quotient_sim_se <- sd(quotient_sim)
return(quotient_sim_se)
}
This function calculates the quotient, and is included to show that the calculate_quotient_se is not working, but another function does work.
calculate_quotient <- function(numerator_estimate_f,denominator_estimate_f){
quotient <- numerator_estimate_f/denominator_estimate_f
}
my_df2 <- my_df %>%
mutate(quotient_se = calculate_quotient_se(numerator_estimate, numerator_variance, denominator_estimate, denominator_variance, iterations = 10000),
quotient = calculate_quotient(numerator_estimate, denominator_estimate))
my_df2
Note how the quotient_se is only works for the the first row, and that se is copied for each additional row down.
It doesn't work this way either:
my_df$q_se <- calculate_quotient_se(numerator_estimate, numerator_variance, denominator_estimate, denominator_variance, iterations = 10000)
my_df
It will work if I type everything in like this:
(x1 <- calculate_quotient_se(625, 22165, 2278, 15870))
(x2 <- calculate_quotient_se(180, 2451, 4742, 688))
(x3 <- calculate_quotient_se(210, 11610, 1115, 7172))
(x4 <- calculate_quotient_se(1753, 172968, 26892, 1908288))
Any suggestions on how I can get the simulated SE in the dataframe for more calculations?