第1回R勉強会@東京、の復習

そんなわけで先日、第1回R勉強会@東京に参加してきました。
私のような分析屋よりも、ハッカーギーク?な人が中心という印象。


当日の id:yokkuns さんの発表資料はここにあります。
個人的に関心のある部分だけ復習してみます。
ところどころ、補足も入れてます。


まず、データの入出力について。
scan()、read.table()を紹介されていましたが、私の場合はcsv形式のデータを扱うことが多く、専らread.csv()を使っています。

# 作業ディレクトリの設定
setwd("C:/R/dat")

# 作業ディレクトリに読み込みたいファイルを置いておきます。
DATA <- read.csv("test.csv")


データを読み込んだら、まずはその中身を確認したくなりまする。
そんなときはsummary()、head()などが便利。
Rに入っている学習用のデータセット"iris"、いわゆる「フィッシャーのアヤメのデータ」を使います

# summary()はデータセットのサマリーを返す。四分位数と平均値、最小値、最大値。
# 因子ベクトルの場合は度数分布表。
summary(iris)
>   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   
>  Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100  
>  1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300  
>  Median :5.800   Median :3.000   Median :4.350   Median :1.300  
>  Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199  
>  3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800  
>  Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  
>        Species  
>  setosa    :50  
>  versicolor:50  
>  virginica :50  


# head()はデータセットの先頭5行を返す。
head(iris)
>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
> 1          5.1         3.5          1.4         0.2  setosa
> 2          4.9         3.0          1.4         0.2  setosa
> 3          4.7         3.2          1.3         0.2  setosa
> 4          4.6         3.1          1.5         0.2  setosa
> 5          5.0         3.6          1.4         0.2  setosa
> 6          5.4         3.9          1.7         0.4  setosa

# class()はクラス、mode()はデータ型、dim()は行数と列数を返す。
class(iris)
> [1] "data.frame"
mode(iris)
> [1] "list"
dim(iris)


続いてデータの視覚化。
多彩なグラフが描けるのもRの強みですね。
棒グラフ、円グラフ、ヒストグラム、折れ線グラフ、箱ひげ図、散布図、散布図行列が紹介されていました。
私の場合はsunflowerplot()、matplot()、mosaicplot()あたりを使いますね
ここではTwitter上で話の出ていた幹葉図を。

# 幹葉図(Stem-and-Leaf Diagram)
stem(iris$Petal.Width)
> 
>   The decimal point is 1 digit(s) to the left of the |
> 
>    1 | 00000
>    2 | 00000000000000000000000000000
>    3 | 0000000
>    4 | 0000000
>    5 | 0
>    6 | 0
>    7 | 
>    8 | 
>    9 | 
>   10 | 0000000
>   11 | 000
>   12 | 00000
>   13 | 0000000000000
>   14 | 00000000
>   15 | 000000000000
>   16 | 0000
>   17 | 00
>   18 | 000000000000
>   19 | 00000
>   20 | 000000
>   21 | 000000
>   22 | 000
>   23 | 00000000
>   24 | 000
>   25 | 000


次に記述統計。


まず、1変数の記述統計。

# 平均
mean(iris$Petal.Width)
> [1] 1.199333
mean(iris$Petal.Width, trim=0.05) # 5%トリム平均
> [1] 1.191176

# 中央値
median(iris$Petal.Width)
> [1] 1.3

# 同順位がある場合の中央値
library(psych)
interp.median(iris$Petal.Width)
> [1] 1.569231

# 最頻値
# これは当日もいろいろな話が出ていたのですが、この方法がスマートだと思います
# Kickstarting R
# <http://cran.r-project.org/doc/contrib/Lemon-kickstart/kr_dstat.html>
X <- factor(c(rep("A", 10), rep("B", 30), rep("C", 20)))
Xt <- table(X)
which(Xt == max(Xt))

# 散布度(バラツキ)
var(iris$Petal.Width)   # 分散(不偏分散)
> [1] 0.5810063

sd(iris$Petal.Width)    # 標準偏差(不偏標準偏差)
> [1] 0.7622377

IQR(iris$Petal.Width)/2 # 四分位偏差
> [1] 0.75

mad(iris$Petal.Width)   # 中央絶対偏差
> [1] 1.03782

# 標準化
sx <- scale(X)
sx2 <- scale(X) * 10 + 50 # 偏差値


次に、2変数の記述統計。

# ピアソンの積率相関係数
cor(iris[,1:4])
>              Sepal.Length Sepal.Width Petal.Length Petal.Width
> Sepal.Length    1.0000000  -0.1175698    0.8717538   0.8179411
> Sepal.Width    -0.1175698   1.0000000   -0.4284401  -0.3661259
> Petal.Length    0.8717538  -0.4284401    1.0000000   0.9628654
> Petal.Width     0.8179411  -0.3661259    0.9628654   1.0000000


最後に検定を少しだけ。

# t検定(Welchの方法)
t.test(iris$Sepal.Length[iris$Species=="setosa"],
       iris$Sepal.Length[iris$Species=="virginica"])
> 
>         Welch Two Sample t-test
> 
> data:  iris$Sepal.Length[iris$Species == "setosa"] and iris$Sepal.Length[iris$Species == "virginica"] 
> t = -15.3862, df = 76.516, p-value < 2.2e-16
> alternative hypothesis: true difference in means is not equal to 0 
> 95 percent confidence interval:
>  -1.786760 -1.377240 
> sample estimates:
> mean of x mean of y 
>     5.006     6.588 

# 対応ありのt検定
t.test(iris$Sepal.Length, iris$Petal.Length, paired=TRUE)
> 
>         Paired t-test
> 
> data:  iris$Sepal.Length and iris$Petal.Length 
> t = 22.8132, df = 149, p-value < 2.2e-16
> alternative hypothesis: true difference in means is not equal to 0 
> 95 percent confidence interval:
>  1.904708 2.265959 
> sample estimates:
> mean of the differences 
>                2.085333 


あとはTips的な話としてヘルプの出し方。

help("chisq.test") #ヘルプの基本
?chisq.test        #これでもOK

help.search("chisq") #キーワードをヘルプ内で検索
??chisq              #これでもOK


Rがまったく初めての方は、まず以下のコマンドを実行してみると「よく分からないけど、なんか凄そう」と思えるんじゃないでしょうか。

demo(graphics)
demo(image)
demo(persp)


次回も楽しみです。