user4773130
user4773130

Reputation:

Resampling in R

Consider the following data:

library(Benchmarking)
d <- data.frame(x1=c(200,200,3000), x2=c(200,200,1000), y=c(100,100,3))

So I have 3 observations.

Now I want to select 2 observations randomly out of d three times (without repetition - there is three combinations in total). For each of these three times I want to calculate the following:

e <- dea(d[c('x1', 'x2')], d$y)
weighted.mean(eff(e), d$y)

That is, I will get three numbers, which I want to calculate an average of. Can someone show how to do this with a loop function in R?

Example:

There is three combinations in total, so I can only get the same result in this case. If I do the calculation manually, I will get the three following result:

0.977  0.977  1

(The result could of course be in a another order).

And the mean of these two numbers is:

0.984

This is a simple example. In my case I have a lot of combinations, where I don't select all of the combinations (e.g. there could be say 1,000,000 combinations, where I only select 1,000 of them).

Upvotes: 1

Views: 227

Answers (2)

I think it's better if you use sample.int and replicate instead of doing all the combinations, see my example:

nsample <- 2 # Number of selected observations
nboot <- 10  # Number of times you repeat the process

replicate(nboot, with(d[sample.int(nrow(d), nsample), ], 
                      weighted.mean(eff(dea(data.frame(x1, x2), y)), y)))

Upvotes: 1

Fer Arce
Fer Arce

Reputation: 149

I have check also the link you bring regarding this issue, so if I got it right, I mean, you want to extract two rows (observations) each time without replacement, you can use sample:

SelObs <- sample(1:nrow(d),2)
# for getting the selected observations just
dSel <- d[SelObs,]

And then do your calculations

If you want those already selected observation to not be selected in a nex random selection, it is similar, but you need an index

Obs <- 1:nrow(d)
SelObs <- sample(Obs, 2)
dSel <- d[SelObs, ]
# and now, for removing those already selected 
Obs <- Obs[-SelObs]
# and keep going with next random selections and the above code

Upvotes: 0

Related Questions