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 ----- #####

cor & cov – calc. cor & cov matrices

cor & cog – calc. cor & cov. 相関行列と共分散行列を算出
使用例: cor(DATA, na.rm=T/F), cov(DATA, na.rm=T/F),
概要:
cor DATAにある全ての変数間の相関係数を算出。
cor DATAにある全ての変数間の共分散を算出。
備考: methodにpearson, kendall (tau), spearman (rho)がある。
関連:

# usage #
cor(DATA)
cov(DATA)
##### ----- examples ----- #####
> dat<-read.csv("http://www.matsuka.info/data_folder/tdkReg01.csv")
> cor(dat)
           material      price     design      sales
material  1.0000000  0.5666676 -0.1564963 -0.2713228
price     0.5666676  1.0000000  0.3850283 -0.5840447
design   -0.1564963  0.3850283  1.0000000  0.2397999
sales    -0.2713228 -0.5840447  0.2397999  1.0000000
> cov(dat)
           material     price     design      sales
material   4.583265   25.0551  -4.496327  -18.41551
price     25.055102  426.5408 106.718367 -382.41633
design    -4.496327  106.7184 180.107755  102.02939
sales    -18.415510 -382.4163 102.029388 1005.12490

##### ----- end examples ----- #####

all & any – logic “ALL” & “ANY”

all & any – logic “ALL” & “ANY”
使用例1: all(vec1, na.rm=T/F), any(vec2, na.rm=T/F),
概要1:
all – vec1が全て真の場合、真を出力し、そうでない場合偽を出力する。
any – vec2が1つでも真の場合、真を出力し、全て偽の場合偽を出力する。
関連:

# usage #
all(vec1)
any(vec2)
##### ----- examples ----- #####
> someVec=1:5
> all(someVec>0)
[1] TRUE
> any(someVec>0)
[1] TRUE
> all(someVec>1)
[1] FALSE
> any(someVec>1)
[1] TRUE
> all(someVec>5)
[1] FALSE
> any(someVec>5)
[1] FALSE
##### ----- end examples ----- #####

repeat & break – flow control “REPEAT” & “BREAK”

repeat & break – flow control “REPEAT” & “BREAK”
使用例1: repeat{rcmd;if(expression){break}}
概要1: expressionが満たされていない限りrcmdが繰り返し実行される。
    (備考:expressionが満たされていても、rcmdが一度は必ず実行される)
使用例2: repeat{if{expression){break}; rcmd2}
概要2: expressionが満たされていない限りrcmdが繰り返し実行される。
    (備考:expressionが満たされていている場合、rcmdが一度も実行されない)
関連: for, while,

# usage #
repeat {
  rcmd
  if (expression) {break}
}
# and/or
repeat {
  if (expression) {break}
  rcmd
}

##### ----- examples ----- #####
# 1から5までカウントし、表示する       #
> counter=0
> repeat {
+   counter=counter+1
+   print(counter)
+   if (counter==5) {break}
+ }
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

# printYesNoが真の場合、1から3までカウントし、終わりに"done"と表示する#

# printYesNoが真の場合の例。3までカウントすることが終了条件      #
> printYesNo=T;counter=0
> repeat{
+  counter=counter+1
+  print(counter)
+  if (counter==3){
+    print("Done")
+    break
+  }
+  if (printYesNo==F){break}
+ }
[1] 1
[1] 2
[1] 3
[1] "Done"

# printYesNoが偽も場合の例。終了条件をみたしているが実行している    #
> printYesNo=F;counter=0
> repeat{
+  counter=counter+1
+  print(counter)
+  if (counter==3){
+    print("Done")
+    break
+  }
+  if (printYesNo==F){break}
+ }
[1] 1

# printYesNoが偽も場合の例。終了条件の位置を変更すると実行されない   #
> printYesNo=F;counter=0
> repeat{
+  if (printYesNo==F){break}
+  counter=counter+1
+  print(counter)
+  if (counter==3){
+    print("Done")
+    break
+  }
+ }
##### ----- 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 ----- #####

if & else – flow control “IF” & “ELSE”

if & else – flow control “IF” & “ELSE”
使用例1: if(expression){rcmd}
概要1: expressionが満たされるとrcmdが実行される
使用例2: if(expression){rcmd1} else {rcmd2}
概要2: expressionが満たされるとrcmd1が実行され、満たされないとrcmd2が実行される
関連: for, while, else, break, repeat, length,

# usage #
if (expression) {
  rcmd
}
# and/or
if (expression) {
  rcmd1
  } else {rcmd2
}

##### ----- examples ----- #####
# 正規分布に従う乱数を1000個発生させ、最大値が3以上であるか調べる#
> rn=rnorm(1000)
> if (max(rn)>=3) {
+   print("maximum is larger than or equal to 3")
+ } else { print("maximum is less than 3")
+ }
[1] "maximum is larger than or equal to 3"
##### ----- end examples ----- #####

while – flow control “WHILE”

while – flow control “WHILE”
使用例: while(expression){rcmd}
目的: rcmdをexpressionが満たされる限り繰り返す(繰り返す条件が決まっている場合)
関連: for, if, break, repeat, length, if

# usage #
while (expression) {
  rcmd
}

##### ----- examples ----- #####
# 正規分布に従う乱数を発生させ、3以上の数値が出た場合に #
# その数値と繰り返し回数を表示する                    #
> rn=0;counter=0;
> while (rn<3) {
+  counter=counter+1
+  rn=rnorm(1)
+ }
> print(c(rn, counter))
[1]    3.1233 1461.0000

# 終了条件は明確だが、繰り返し回数の上限を設定する例   #
# 例1と同じ内容だが、繰り返しの上限を500とする       #
> rn=0;counter=0;maxItr=500;
> while (rn<3 & counter < maxItr) {
+  counter=counter+1
+  rn=rnorm(1)
+ }
> print(c(rn, counter))
[1]  -1.194285 500.000000
##### ----- end examples ----- #####

for – flow control “FOR”

for – flow control “FOR”
使用例: for(counter in vec){rcmd}
目的: rcmdをvecの長さ分繰り返す(繰り返す回数が既知の場合)
関連: while, if, break, length, if

# usage #
for(counter in vec) {
  rcmd
}

##### ----- examples ----- #####
# "hello"を3回表示する #
> for(i_loop in 1:3){print("hello")}
[1] "hello"
[1] "hello"
[1] "hello"

# vec1を要素を順番に1つづ表示する #
> vec1=c("you","are","wonderful")
> for(i_char in vec1){print(i_char)}
[1] "you"
[1] "are"
[1] "wonderful"
# 異なった方法
> for(i_loop in 1:length(vec1)){print(vec1[i_loop])}
[1] "you"
[1] "are"
[1] "wonderful"

# BREAK を使った例 ー 繰り返しの停止条件を組み込む   #
# 正規分布に従う乱数を発生させ、3以上の数値が出た場合に #
# 繰り返し停止し、その数値と繰り返し回数を表示する      #
> for (i_counter in 1:10000) {
+  rn=rnorm(1)
+  if (rn>=3) {break}
+ }
> print(c(rn, i_counter))
[1]    3.194892 1140.000000
##### ----- end examples ----- #####

c – concatenation

c – concatenation 連結
使用例: A <- c(var1,...) 目的: 入力引数(var1,...)を連結する 関連: rep, seq, matrix

# usage #
A <- c(var1,...)

##### ----- examples ----- #####
# 2つの数値を連結 #
> vec1 <- c(1,2)
> vec1
[1] 1 2

# ベクトルと1つの数値を連結 #
> vec2 <- c(vec1,3)
> vec2
[1] 1 2 3

# 2つのベクトルを連結 #
> vec3 <- c(vec1,vec2)
> vec3
[1] 1 2 1 2 3
##### —– end examples —– #####