2017-10-13 8 views
0

なぜデータをテーブルとしてロードすると、データをスタックとしてロードするときに、異なるアノバ結果が生成されるのですか?なぜデータをテーブルとしてロードすると、データをスタックとしてロードする際に、異なるアノノ結果が生成されるのですか?

(1)次の表を読み込み、anovaを取得します。

31 42 14 80 
42 26 25 106 
84 21 19 83 
26 60 36 69 
14 35 44 48 
16 80 28 76 
29 49 80 39 
32 38 76 84 
45 65 15 91 
30 71 82 39 

> raw <- read.table("demotablenolabels.txt", sep="\t", header=FALSE) 
> rawstack = stack(raw) 
> rawstack$sample = rep(rownames(raw),4) 
> repeated = aov(values ~ ind + sample, data=rawstack) 
> summary(repeated) 
      Df Sum Sq Mean Sq F value Pr(>F) 
ind   3 7553 2517.7 4.036 0.0171 * 
sample  9 1565 173.9 0.279 0.9751 
Residuals 27 16843 623.8 

(2)次のrawstackデータをファイルに保存してロードし、別のanova結果を取得します。

values ind sample 
31 V1 1 
42 V1 2 
84 V1 3 
26 V1 4 
14 V1 5 
16 V1 6 
29 V1 7 
32 V1 8 
45 V1 9 
30 V1 10 
42 V2 1 
26 V2 2 
21 V2 3 
60 V2 4 
35 V2 5 
80 V2 6 
49 V2 7 
38 V2 8 
65 V2 9 
71 V2 10 
14 V3 1 
25 V3 2 
19 V3 3 
36 V3 4 
44 V3 5 
28 V3 6 
80 V3 7 
76 V3 8 
15 V3 9 
82 V3 10 
80 V4 1 
106 V4 2 
83 V4 3 
69 V4 4 
48 V4 5 
76 V4 6 
39 V4 7 
84 V4 8 
91 V4 9 
39 V4 10 

> stackwithlabels <- read.table("demostackwithlabels.txt", sep="\t", header=TRUE) 
> repeatedstack = aov(values ~ ind + sample, data=stackwithlabels) 
> summary(repeatedstack) 
      Df Sum Sq Mean Sq F value Pr(>F) 
ind   3 7553 2517.7 4.918 0.00591 ** 
sample  1 492 492.1 0.961 0.33356 
Residuals 35 17916 511.9 

(3)私はスタックをラベルに変換してテーブルに戻し、この手順を繰り返すことで元のアノバ結果(1参照)を得る。あなたの元のデータフレームrawstacksample

> stackwithlabels[c(3)] <- list(NULL) 
> rawwithoutlabels = unstack(stackwithlabels) 
> restackwithoutlabels = stack(rawwithoutlabels) 
> restackwithoutlabels$sample = rep(rownames(raw),4) 
> rerepeatedstack = aov(values ~ ind + sample, data=restackwithoutlabels) 
> summary(rerepeatedstack) 
      Df Sum Sq Mean Sq F value Pr(>F) 
ind   3 7553 2517.7 4.036 0.0171 * 
sample  9 1565 173.9 0.279 0.9751 
Residuals 27 16843 623.8   
+0

コードと通常のテキストを区別するために質問をフォーマットできますか? – Jakob

答えて

0

文字ベクトルとして表現されます。

str(rawstack$sample) 
# chr [1:40] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "1" "2" "3" "4" "5" "6" "7" "8" ... 

したがって、sampleaovに因子として解釈され、10なっている - 自由1 = 9 degressを。

summary(aov(values ~ ind + sample, data = rawstack)) 
#    Df Sum Sq Mean Sq F value Pr(>F) 
# ind   3 7553 2517.7 4.036 0.0171 * 
# sample  9 1565 173.9 0.279 0.9751 
# Residuals 27 16843 623.8     
# --- 
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

テキストファイルにデータを書き込み、バックRにデータを読み込む場合は、sampleが数値ベクトルとして表現されます。

stackwithlabels <- read.table(text = capture.output(write.table(rawstack))) 
str(stackwithlabels$sample) 
# int [1:40] 1 2 3 4 5 6 7 8 9 10 ... 

したがって、aovの結果は異なります。 sampleは1自由度を持つことに注意してください。

summary(aov(values ~ ind + sample, data = stackwithlabels)) 
#    Df Sum Sq Mean Sq F value Pr(>F) 
# ind   3 7553 2517.7 4.918 0.00591 ** 
# sample  1 492 492.1 0.961 0.33356 
# Residuals 35 17916 511.9     
# --- 
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 
+0

数値ベクトルを文字列ベクトルに変換するにはどうすればよいですか? – user2302840

+0

私はそれを見つけました:stackwithlabels $ sample < - as.character(stackwithlabels $ sample) – user2302840

+0

それは:stackwithlabels $ sample < - as.character(stackwithlabels $ sample) – user2302840

関連する問題