2014-09-16 3 views
0

다음 코드로 부트 스트랩 재 샘플링을 시도하고 있습니다. http://spark.rstudio.com/ahmed/bootstrap/에서이 코드가 있습니다. 내 데이터 세트를 사용하여 웹 사이트에서 동일한 플롯을 그리고 싶습니다. data1 및 data2는 하나의 열만 포함하는 텍스트 파일입니다.부트 스트랩 리샘플링

a = read.table("/home/Desktop/data1.txt") 
b = read.table("/home/Desktop/data2.txt") 
diff.observed = mean(b) - mean(a) 
Warning messages: 
1: In mean.default(b) : argument is not numeric or logical: returning NA 
2: In mean.default(a) : argument is not numeric or logical: returning NA 
> diff.observed 
[1] NA 

나는 관측 된 데이터 세트의 평균값을 얻지 못한다.

alpha = 0.05 

# Number of replicates 
n = 1000 

# Difference between means of bootstrapped datasets (n replicates) 
diff.bootstrap = NULL 

for (i in 1 : n) { 
    # Sample with replacement 
    a.bootstrap = sample (a, length(a), TRUE) 
    b.bootstrap = sample (b, length(b), TRUE) 

    diff.bootstrap[i] = mean(b.bootstrap) - mean(a.bootstrap) 
} 
plot(density(a.bootstrap)) 
plot(density(b.bootstrap)) 
plot(density(diff.bootstrap)) 
Error in density.default(diff.bootstrap) : 'x' contains missing values 

나는 그 실수를 이해하지 못한다. 귀하의 제안은 감사하겠습니다!

+2

은'mean' 방법은 R 버전 3.0.0 이후 소멸됩니다. – Roland

+0

이것은 코드의 오류와 관련이 없지만'boot'와'bootstrap' 패키지를 살펴보고'for' 루프보다 훨씬 성능이 좋은 대안을 찾아 볼 수 있습니다. – nrussell

답변

0

그런 다음 mean이 작동해야 matrix로 데이터를로드 할 수 있습니다 data.frames에 대한

a = as.matrix(read.table ("data1.txt")) 
b = as.matrix(read.table ("data2.txt")) 
diff.observed = mean(b) - mean(a) 
관련 문제