0
我有n個變量,每個變量有100行。爲了從1重新取樣到nrows,下面的代碼給出了預期的結果,但是它是單調乏味和不切實際的。要重現的情況下,讓該y具有5行suposse:R:重新採樣1到帶有循環的nrow(y)
y<-rnorm(n=5, mean=10, sd=2)
R=1000 #number of resamplings
boot.means = numeric(R)
for (i in 1:R) { boot.sample = sample(y, 1, replace=T)
boot.means[i] = mean(boot.sample) }
m1<-mean(boot.means)
d1<-sd(boot.means)
cv1 =(d1*100)/m1
R=1000 #number of resamplings
boot.means = numeric(R)
for (i in 1:R) { boot.sample = sample(y, 2, replace=T)
boot.means[i] = mean(boot.sample) }
m2<-mean(boot.means)
d2<-sd(boot.means)
cv2 =(d2*100)/m2
R=1000 #number of resamplings
boot.means = numeric(R)
for (i in 1:R) { boot.sample = sample(y, 3, replace=T)
boot.means[i] = mean(boot.sample) }
m3<-mean(boot.means)
d3<-sd(boot.means)
cv3 =(d3*100)/m3
R=1000 #number of resamplings
boot.means = numeric(R)
for (i in 1:R) { boot.sample = sample(y, 4, replace=T)
boot.means[i] = mean(boot.sample) }
m4<-mean(boot.means)
d4<-sd(boot.means)
cv4 =(d4*100)/m4
R=1000 #number of resamplings
boot.means = numeric(R)
for (i in 1:R) { boot.sample = sample(y, 5, replace=T)
boot.means[i] = mean(boot.sample) }
m5<-mean(boot.means)
d5<-sd(boot.means)
cv5 =(d5*100)/m5
CV.OK<-(c(cv1,cv2,cv3,cv4,cv5))
plot(CV.OK)
我想用類似下面的代碼,但它給出了意想不到的效果。請,有人可以幫助我。謝謝。
R = 1000 #number of resamplings
boot.sample=seq(1,5, by=1)
boot.means = numeric(R)
boot.sd = numeric(R)
m = 5
d = 5
for (i in 1:5) {
for (j in 1:R) {
boot.sample[i] = sample(y, i, replace=T)
boot.means[j] = mean(boot.sample[i])
boot.sd[j] = sd(boot.sample[i])
m[i]=mean(boot.means[j])
d[i]=mean(boot.sd[j])
}
}
CV.Fail<-(d*100)/m
是的,它按我的預期工作。但是,仍然存在的一個問題是必須指定引導次數,即1000. –
這應該通過'sample_vect'變量指定。因此,如果您選擇'sample_vect = c(1:100)',它最終將需要多達100個樣本並計算彙總統計。 – sluedtke