I'm trying to bootstrap a weighted difference between two data variables x and y:
DF_Difference = data.frame(
x_M = c(-0.3, 0.3, -0.18, 0.02, 0.07, 0.11, 0.20, 0.8, 0.3, -0.4), # data x
x_W = c(50, 40, 70, 5, 15, 30, 32, 13, 9, 19), # weights of x
y_M = c(-0.6, 0.25, 0.1, 0.3, 0.3, -0.05, -0.5, 1, 0.05, -0.6), #data y
y_W = c(70, 8, 10, 39, 9, 49, 90, 77, 23, 75) # weights of y
)
I use the following function to do so:
weighteddifference = function(d,i){
DF_Difference = d[i,]
Difference = DF_Difference$x_M - DF_Difference$y_M
WeightsDifference = 1 / (1/DF_Difference$x_W + 1/DF_Difference$y_W)
MeanDiff = wtd.mean(x=Difference, weights=WeightsDifference)
return(MeanDiff)
}
bootstrap =
boot(data = DF_Difference[, , drop=F],
statistic = weighteddifference,
R = 1000)
This code works, but I'm unsure how the function deals with the data and weights: are they still linked together? So does for example x = -0.3 still have a weight of 50? I tried to achieve this by using i as the indices but I doubt whether this is correct.