私はこの質問に関して結論に至らなかったので、言い換えてもう一度質問すると思いました。
データセットを 10,000 回サブサンプリングして、各応答の平均と 95% CI を生成したいと考えています。
以下は、データ セットがどのように構成されているかの例です。
x <- read.table(tc <- textConnection("
study expt variable value1 value2
1 1 A 1.0 1.1
1 2 B 1.1 2.1
1 3 B 1.2 2.9
1 4 C 1.5 2.3
2 1 A 1.7 0.3
2 2 A 1.9 0.3
3 1 A 0.2 0.5"), header = TRUE); close(tc)
各研究/変数の組み合わせを 1 回だけサブサンプリングしたいと思います。たとえば、サブセット化されたデータセットは次のようになります。
study expt variable value1 value2
1 1 A 1.0 1.1
1 2 B 1.1 2.1
1 4 C 1.5 2.3
2 1 A 1.7 0.3
3 1 A 0.2 0.5
行 3 と 6 がなくなっていることに注意してください。どちらも変数を 2 回測定したためです (最初のケースでは B、2 番目のケースでは A)。
サブサンプリングされたデータセットを何度も描画したいので、各変数の 95% CI で value1 と value2 の全体的な平均を導き出すことができます。したがって、サブサンプリングルーチン全体の後の出力は次のようになります。
variable mean_value1 lower_value1 upper_value1 mean_value2 etc....
A 2.3 2.0 2.6 2.1
B 2.5 2.0 3.0 2.5
C 2.1 1.9 2.3 2.6
サブセットを取得するために必要なコードを次に示します。
subsample<-function(x, B){
samps<-ddply(x, .(study,variable), nrow)[,3] #for each study/variable combination,
#how many experiments are there
expIdx<-which(!duplicated(x$study)) #what is the first row of each study
n<-length(samps) #how many studies are there
sapply(1:B, function(a) { #use sapply for the looping, as it's more efficient than for
idx<-floor(runif(n, rep(0,n), samps)) #get the experiment number-1 for each study
x$value[idx+expIdx] #now get a vector of values
})
どんな助けでも大歓迎です。これは複雑だと認識していますので、説明が必要な場合はお知らせください。