Rによる独立2群のt検定
CSV形式で,以下のようなコレステロールと飲用しているサプリメントの種類に関するデータがあったとします(これをexample_a.csvというファイル名で保存します).
choles,suppl
118,A
132,A
120,A
115,A
113,A
129,B
126,B
134,B
135,B
131,B
このデータを用いて,独立2群のt検定を行うには以下のようなコマンドを実行します.
まずは,データのcsvファイル(example_a.csv)を読み込み,"ex.a"という名前をつけます.ターミナルからRを起動して,以下のコマンドを実行します.
> ex.a <- read.csv("example_a.csv")
> ex.a
choles suppl
1 118 A
2 132 A
3 120 A
4 115 A
5 113 A
6 129 B
7 126 B
8 134 B
9 135 B
10 131 B
> str(ex.a)
'data.frame': 10 obs. of 2 variables:
$ choles: int 118 132 120 115 113 129 126 134 135 131
$ suppl : chr "A" "A" "A" "A" ...
'data.frame': 10 obs. of 2 variables:
$ choles: int 118 132 120 115 113 129 126 134 135 131
$ suppl : chr "A" "A" "A" "A" ...
> ex.a $ suppl <- factor(ex.a $suppl)
> str(ex.a)
'data.frame': 10 obs. of 2 variables:
$ choles: int 118 132 120 115 113 129 126 134 135 131
$ suppl : Factor w/ 2 levels "A","B": 1 1 1 1 1 2 2 2 2 2
> attach(ex.a)
> t.test(choles ~ suppl, var.equal=T)
Two Sample t-test
data: choles by suppl
t = -3.0732, df = 8, p-value = 0.01527
alternative hypothesis: true difference in means between group A and group B is not equal to 0
95 percent confidence interval:
-19.954001 -2.845999
sample estimates:
mean in group A mean in group B
119.6 131.0
> t.test(ex.a$choles ~ ex.a$suppl, var.equal=T)
Two Sample t-test
data: ex.a$choles by ex.a$suppl
t = -3.0732, df = 8, p-value = 0.01527
alternative hypothesis: true difference in means between group A and group B is not equal to 0
95 percent confidence interval:
-19.954001 -2.845999
sample estimates:
mean in group A mean in group B
119.6 131.0
0 件のコメント :
コメントを投稿