Category Archives: calc&comp

t.test – conduct T-test

t.test- conduct T-test T検定
使用例1: t.test(DV, mu=VALUE)
使用例2: t.test(group1, group2)
概要1: one-sample t検定
概要2: two-sample t検定
備考: 片側検定や等分散に関しては下の例を参考にしてください
関連:

# usage #
t.test(DV,mu=VALUE)
t.test(group1,group2)
##### ----- examples ----- #####
# one-sample t検定 (両側検定)
> DV=c(142,108,104,138,105,105,88,86,118,132)
> t.test(DV,mu=100)

	One Sample t-test

data:  DV
t = 2.0385, df = 9, p-value = 0.07194
alternative hypothesis: true mean is not equal to 100
95 percent confidence interval:
  98.61766 126.58234
sample estimates:
mean of x 
    112.6 

# one-sample t検定 (片側検定)
# DVの値が有意にmu=100より高いか検定。
# 低いか検定する場合は、「alternative="less"」
> t.test(DV,mu=100,alternative="greater")

	One Sample t-test

data:  DV
t = 2.0385, df = 9, p-value = 0.03597
alternative hypothesis: true mean is greater than 100
95 percent confidence interval:
 101.2696      Inf
sample estimates:
mean of x 
    112.6 

# two-sample t検定 (等分散を仮定する場合)
> group1=c(142,108,104,138,105,105,88,86,118,132)
> group2=c(92,89,83,118,79,110,119,102,87,55)
> t.test(group1,group2,var.equal=T)

	Two Sample t-test

data:  group1 and group2
t = 2.1927, df = 18, p-value = 0.04171
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
  0.8033645 37.5966355
sample estimates:
mean of x mean of y 
    112.6      93.4 

# two-sample t検定 (等分散を仮定しない場合 - Welch 2 sample t-test)
> group3=c(142,108,104,138,105,105,88,86,118,132)
> t.test(group1,group3)

	Welch Two Sample t-test

data:  group1 and group3
t = 0, df = 18, p-value = 1
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -18.36462  18.36462
sample estimates:
mean of x mean of y 
    112.6     112.6 
##### ----- end examples ----- #####

sum, colSums, rowSums

sum, colSums, rowSums – calc. sums
使用例: sum(OBJ,na.rm=T/F)
概要:
sum OBJ全体の和を計算(欠損値を除外して計算する場合はna.rm=Tとする)
colSums 列毎にOBJの和を計算(e.g. 変数毎の和)
rowSums 行毎にOBJの和を計算(e.g. 被験者毎の和)
関連: var, sd, mean

# usage #
sum(OBJ)
colSums(OBJ)
rowSums(OBJ)

##### ----- examples ----- #####
> datMat<-matrix(rnorm(10),ncol=2)
> datMat
            [,1]        [,2]
[1,] -0.52014830 -1.09940927
[2,] -0.17118129 -1.75202117
[3,]  1.87727940 -0.06483163
[4,] -0.09374804 -1.19832566
[5,]  0.91919805  0.74479903
> sum(datMat) #全体の和
[1] -1.358389
> colSums(datMat) #列毎の和(e.g. 変数毎の和)
[1]  2.011400 -3.369789
> rowSums(datMat) #行毎の和(e.g. 被験者毎の和)
[1] -1.619558 -1.923202  1.812448 -1.292074  1.663997
 
# 欠損値がある場合の例
> datMat[1,1]=NaN
> datMat
            [,1]        [,2]
[1,]         NaN -1.09940927
[2,] -0.17118129 -1.75202117
[3,]  1.87727940 -0.06483163
[4,] -0.09374804 -1.19832566
[5,]  0.91919805  0.74479903
> sum(datMat)
[1] NaN
> sum(datMat,na.rm=T)
[1] -0.8382406
> colSums(datMat)
[1]       NaN -3.369789
> colSums(datMat,na.rm=T)
[1]  2.531548 -3.369789
> rowSums(datMat)
[1]       NaN -1.923202  1.812448 -1.292074  1.663997
> rowSums(datMat,na.rm=T)
[1] -1.099409 -1.923202  1.812448 -1.292074  1.663997
##### ----- end examples ----- #####

mean, colMeans, rowMeans

mean, colMeans, rowMeans – calc. means
使用例: mean(OBJ,na.rm=T/F)
概要:
mean OBJ全体の平均を計算(欠損値を除外して計算する場合はna.rm=Tとする)
colMeans 列毎にOBJの平均を計算(e.g. 変数毎の平均)
rowMeans 行毎にOBJの平均を計算(e.g. 被験者毎の平均)
関連: var, sd

# usage #
mean(OBJ)
colMeans(OBJ)
rowMeans(OBJ)

##### ----- examples ----- #####
> datMat<-matrix(rnorm(10),ncol=2)
> datMat
           [,1]        [,2]
[1,] -1.1604860  0.09106227
[2,] -0.3358416  0.36780023
[3,]  0.1917609 -0.62506053
[4,] -0.9900861 -0.86436989
[5,] -0.9549163 -2.05218273
> mean(datMat) #10個の数値の平均
[1] -0.633232
> colMeans(datMat) #列ごとの平均(e.g. 変数毎の平均)
[1] -0.6499138 -0.6165501
> rowMeans(datMat) #行ごとの平均(e.g. 被験者毎の平均
[1] -0.53471189  0.01597933 -0.21664982 -0.92722799 -1.50354951

# 欠損値がある場合の例
> datMat[1,1]=NaN
> datMat
           [,1]        [,2]
[1,]        NaN  0.09106227
[2,] -0.3358416  0.36780023
[3,]  0.1917609 -0.62506053
[4,] -0.9900861 -0.86436989
[5,] -0.9549163 -2.05218273
> mean(datMat)
[1] NaN
> mean(datMat,na.rm=T)
[1] -0.5746482
> colMeans(datMat)
[1]        NaN -0.6165501
> colMeans(datMat,na.rm=T)
[1] -0.5222708 -0.6165501
> rowMeans(datMat)
[1]         NaN  0.01597933 -0.21664982 -0.92722799 -1.50354951
> rowMeans(datMat,na.rm=T)
[1]  0.09106227  0.01597933 -0.21664982 -0.92722799 -1.50354951
##### ----- end examples ----- #####