I have a data frame with 27 samples divided in 3 strata. I want to replicate 500 times a weighted mean, where the mean is calculate among the random selection of 3 samples for stratum and the weight is the relative area of the strata.
My idea was to create a loop of selection for each stratum and to compute the mean. I am able to compute the simple mean of the selection but I am not able to compute the weighted mean (I have no idea how to extract the weight and the value together):
#data
DF<-data.frame(v= c(16,42,63,15,42,63,85,16,43),
s= c(1,3,2,2,1,3,3,1,2),
w=c(0.2,0.5,0.3,0.3,0.2,0.5,0.5,0.2,0.3),
stringsAsFactors=T)
#simple mean
x<-c()
for (i in 1:3){
x.tm<-sample(subset(DF$v,DF$s==i),2,replace=T)
x<-c(x,x.tm)
d<-mean(x)}
furthermore, I'm confused about the replicate
function and the way to insert the weighted mean inside it. For example, trying with the simple mean I have obtained an empty list:
t<-replicate(500,{
for (i in 1:3){
x.tm<-sample(subset(DF$v,DF$s==i),2, replace=T)
x<-c(x,x.tm)
d<-mean(x)
}
})
I have also tried using the boot::boot command but the result was the same.