japan.r #2 introduction to r
TRANSCRIPT
![Page 1: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/1.jpg)
Japan.R #2 @ 統計数理研究所2011-11-26
はじめての
阪上 辰也
2Saturday, November 26, 11
![Page 2: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/2.jpg)
0. 自己紹介
3Saturday, November 26, 11
![Page 3: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/3.jpg)
0. 自己紹介
•阪上 辰也 (SAKAUE, Tatsuya)
• 所属: ... 特任講師(言語習得研究)• Nagoya.R / HiRoshima.R 主催• ID: sakaue にて• 詳しくは...
5Saturday, November 26, 11
![Page 4: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/4.jpg)
6Saturday, November 26, 11
![Page 5: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/5.jpg)
Agenda1. R 概論2. 関数と変数が命3. 外部データの読込4. あっという間に作図5. R で検定
8Saturday, November 26, 11
![Page 6: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/6.jpg)
Agenda1. R 概論2. 関数と変数が命3. 外部データの読込4. あっという間に作図5. R で検定
9Saturday, November 26, 11
![Page 7: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/7.jpg)
とは何か• 統計処理を目的とするプログラミング言語• 作者:Ross Ihaka & Robert Gentleman (R & R)
• Ross Ihaka and Robert Gentleman. R: A language for data analysis and graphics. Journal of Computational and Graphical Statistics, 5(3):299-314, 1996.
• http://biostat.mc.vanderbilt.edu/twiki/pub/Main/Je!reyHorner/JCGSR.pdf
• 無償+オープンソースのソフトウェア
10Saturday, November 26, 11
![Page 8: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/8.jpg)
要するに統計ソフト
11Saturday, November 26, 11
![Page 9: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/9.jpg)
の何が良いのか
•無償(IBM SPSS Statistics は約10万円で保守なし...)
•統計処理の種類が豊富•グラフィックスが美しい
12Saturday, November 26, 11
![Page 10: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/10.jpg)
の弱点•文字ベースの操作
• プログラムを組む感覚を養成• (確実に伝わるという利点あり)
•多方面の知識が必要• 英語+統計+計算機 に関する知識
13Saturday, November 26, 11
![Page 11: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/11.jpg)
Ugeeeeeeeee
14Saturday, November 26, 11
![Page 12: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/12.jpg)
Excel や SPSS は...
•高価(気軽に試せない)•処理が信頼できず
• Excel のひどい話(青木先生@群馬大)
•グラフィックスが美しくない15Saturday, November 26, 11
![Page 13: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/13.jpg)
ExcelSPSS
16Saturday, November 26, 11
![Page 14: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/14.jpg)
の基本操作-Install / Update / Uninstall-
17Saturday, November 26, 11
![Page 15: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/15.jpg)
1. http://www.r-project.org/ にアクセス2. “download R” をクリック3. JAPAN の中から、どれか1つを選ぶ4. 使っている OS のリンクをクリック
のインストール
18Saturday, November 26, 11
![Page 16: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/16.jpg)
のインストール
• Windows: : http://cran.md.tsukuba.ac.jp/bin/windows/base/
•「Download R 2.14.0 for Windows」をクリック
• Mac OS X: http://cran.md.tsukuba.ac.jp/bin/macosx/
•「R-2.14.0.pkg (latest version)」をクリック
19Saturday, November 26, 11
![Page 17: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/17.jpg)
20Saturday, November 26, 11
![Page 18: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/18.jpg)
21Saturday, November 26, 11
![Page 19: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/19.jpg)
22Saturday, November 26, 11
![Page 20: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/20.jpg)
23Saturday, November 26, 11
![Page 21: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/21.jpg)
24Saturday, November 26, 11
![Page 22: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/22.jpg)
25Saturday, November 26, 11
![Page 23: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/23.jpg)
のアップデート1. http://www.r-project.org/ にアクセス2. 自分が使っているバージョンより新しいものが出ているか確認
3. 新規にインストール用ファイルをダウンロードの上、インストール
26Saturday, November 26, 11
![Page 24: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/24.jpg)
のアップデート• 注意: 重大なセキュリティ上の問題が ない限り,むやみに更新しない
• パッケージ(付加機能)の動作不良回避
• 補足:Windows 版は新旧の共生可• 何がどう共有されているか不明
27Saturday, November 26, 11
![Page 25: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/25.jpg)
のアンインストール• Windows
• [ Windows XP] コンパネ→プログラムの追加と削除• [ Windows 7 ] コンパネ→プログラムのアンインストール
• Mac OS X• /Applications にある アイコンと /Library の中の
Frameworks にある R. framework を削除(CleanApp 可)
28Saturday, November 26, 11
![Page 26: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/26.jpg)
の起動• Windows• START → Program → R → R 2.14.0
• Mac OS X• /Applications 内の R アイコンをクリック
R の「コンソール」表示を確認
29Saturday, November 26, 11
![Page 27: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/27.jpg)
の終了
• Windows も Mac OS X も1.「q()」と入力2.今は作業スペースの保存をしない3.ウィンドウが消えて、R 終了
30Saturday, November 26, 11
![Page 28: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/28.jpg)
の環境設定
• 基本はそのままでOK(細かいことは省略)
• 日本語の文字化けが起こったら(Win)• [編集]→[GUI プリファレンス]を選択• Font を [MS Mincho] に設定
31Saturday, November 26, 11
![Page 29: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/29.jpg)
で困ったら...
• まずは help(sth) を実行• seekR (http://seekr.jp/)
• R SEEK (http://www.rseek.org/)
• RjpWiki (http://www.okada.jp.org/RWiki/)
• R-Tips (http://cse.naro.a!rc.go.jp/takezawa/r-tips/r.html)
• R による統計処理 (http://aoki2.si.gunma-u.ac.jp/R/)
32Saturday, November 26, 11
![Page 30: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/30.jpg)
概論まとめ
• は統計ソフト•無償+何でも統計+美しい作図•困ったら help(sth) !!
33Saturday, November 26, 11
![Page 31: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/31.jpg)
ExcelSPSS
34Saturday, November 26, 11
![Page 32: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/32.jpg)
Agenda1. R 概論2. 関数と変数が命3. 外部データの読込4. あっという間に作図5. R で検定
35Saturday, November 26, 11
![Page 33: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/33.jpg)
Agenda1. R 概論2. 関数と変数が命3. 外部データの読込4. あっという間に作図5. R で検定
36Saturday, November 26, 11
![Page 34: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/34.jpg)
前置きが長くなりましたがどんどん実行しましょう
37Saturday, November 26, 11
![Page 35: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/35.jpg)
まずは四則演算•式を入力し [Enter] を押す
• > 3+5 [Enter]
• > 10-3 [Enter]
• > 2*3 [Enter]
• > 100/20 [Enter]
• > (12 + 34 -56) * 78 / 90 [Enter]
38Saturday, November 26, 11
![Page 36: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/36.jpg)
もうちょっと複雑に•次の違いは?•> 100^1/2•> 100^(1/2)
39Saturday, November 26, 11
![Page 37: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/37.jpg)
いままでのものは単なる計算式でしたが
40Saturday, November 26, 11
![Page 38: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/38.jpg)
といえば
関数が命
41Saturday, November 26, 11
![Page 39: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/39.jpg)
関数とは指定した値に対して何らかの処理をして結果を返すもの
42Saturday, November 26, 11
![Page 40: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/40.jpg)
何言ってんの?
http://www.flickr.com/photos/fmgbain/4382010455/“I don't know !” by fmgbain
43Saturday, November 26, 11
![Page 41: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/41.jpg)
なにはともあれ実行してみましょう
44Saturday, November 26, 11
![Page 42: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/42.jpg)
sqrt():平方根•> sqrt(2)
•> sqrt(144)
•> sqrt(104976)• [応用] 2の立方根はどう求める?
• 「2の3分の1乗」と考える• > 2^(1/3)
45Saturday, November 26, 11
![Page 43: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/43.jpg)
「関数(値)」が命• には多数の関数がある•q() や help(sth) も関数•必要な関数をしつこく実行
46Saturday, November 26, 11
![Page 44: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/44.jpg)
といえば
変数も命
47Saturday, November 26, 11
![Page 45: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/45.jpg)
変数とは1つ以上の値をまとめていれておく「箱」のこと
48Saturday, November 26, 11
![Page 46: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/46.jpg)
ではベクトルとも呼ばれ複数の値を1つにまとめたものを指す
49Saturday, November 26, 11
![Page 47: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/47.jpg)
統計処理などを行うには複数の値をまとめて扱う
ことが必要でそれを使い回すために
変数を使う50Saturday, November 26, 11
![Page 48: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/48.jpg)
ちなみに変数に数値を入れることを
「代入」といいます51Saturday, November 26, 11
![Page 49: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/49.jpg)
また何言ってんの?
http://www.flickr.com/photos/fmgbain/4382010455/“I don't know !” by fmgbain
52Saturday, November 26, 11
![Page 50: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/50.jpg)
なにはともあれ実行してみましょう
53Saturday, November 26, 11
![Page 51: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/51.jpg)
変数の名前は “hako”
•> hako <- c(1,2,3,4,5)•> hako
•c() 関数:concatenate/combine
•値を1つにまとめる関数54Saturday, November 26, 11
![Page 52: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/52.jpg)
c()関数の “<-” は何?
hako <- c(1,2,3,4,5) の “<-” は左向きの矢印(←)
を表現
55Saturday, November 26, 11
![Page 53: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/53.jpg)
コンソールで「+」が出たら...
• 入力中に誤って[Enter] を押すと,待機状態を示す「+」が出る
• [STOP] ボタンでキャンセル• [Esc] キーでキャンセル
56Saturday, November 26, 11
![Page 54: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/54.jpg)
履歴機能と補完機能
•履歴:上下の矢印キー•補完:Tabキー
57Saturday, November 26, 11
![Page 55: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/55.jpg)
現在、hako という名前の「変数」の中に
1から5までの5つの数字がまとめて入っている状態
58Saturday, November 26, 11
![Page 56: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/56.jpg)
では使い回してみましょう
59Saturday, November 26, 11
![Page 57: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/57.jpg)
以下の関数を実行
•> sqrt(hako)
•> log(hako)
60Saturday, November 26, 11
![Page 58: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/58.jpg)
もし変数がなかったら何度も関数を
繰り返す羽目になるsqrt(1), sqrt(2) ... sqrt(5)log(1), log (2) ... log (5)
61Saturday, November 26, 11
![Page 59: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/59.jpg)
一括処理のために変数(ベクトル)の利用は不可欠
62Saturday, November 26, 11
![Page 60: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/60.jpg)
では変数をもっと使い回してみましょう
63Saturday, November 26, 11
![Page 61: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/61.jpg)
以下の関数で実行•基本統計量を求める:summary()
•度数分布表を作る:table()
•合計する:sum()
•データの個数を求める:length()
64Saturday, November 26, 11
![Page 62: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/62.jpg)
続いてこちらも実行• 平均値を求める:mean()
• 最大値と最小値を求める:max(), min()
• 中央値を求める:median()
• 標準偏差を求める:sd()
65Saturday, November 26, 11
![Page 63: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/63.jpg)
ここまでのまとめ•Rは「関数」と「変数」が命•関数を使って数値を処理
•q(), help(), sqrt(), log(), c()
•変数を使って一括処理!66Saturday, November 26, 11
![Page 64: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/64.jpg)
今日はメモを頼りに実行できれば OK
67Saturday, November 26, 11
![Page 65: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/65.jpg)
Agenda1. R 概論2. 関数と変数が命3. 外部データの読込4. あっという間に作図5. R で検定
68Saturday, November 26, 11
![Page 66: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/66.jpg)
Agenda1. R 概論2. 関数と変数が命3. 外部データの読込4. あっという間に作図5. R で検定
69Saturday, November 26, 11
![Page 67: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/67.jpg)
Sub topics
1. 行列のおはなし2. データフレーム3. 外部データの読込
70Saturday, November 26, 11
![Page 68: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/68.jpg)
さきほど一行で表せる数値を扱いましたが
71Saturday, November 26, 11
![Page 69: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/69.jpg)
実際のデータは複数行あるはず
72Saturday, November 26, 11
![Page 70: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/70.jpg)
例えば...
•身長と体重•年齢と年収•覚えた単語数とTOEICスコア
73Saturday, November 26, 11
![Page 71: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/71.jpg)
表にすれば...身長 体重
A 180 75B 170 65C 165 60D 175 70E 190 80
74Saturday, November 26, 11
![Page 72: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/72.jpg)
複数の行や列で表されるデータを扱うために
Rでは「行列」を使う
75Saturday, November 26, 11
![Page 73: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/73.jpg)
行列とは
数値が縦横に並べられたもの
76Saturday, November 26, 11
![Page 74: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/74.jpg)
1 2 3
4 5 6
7 8 9
77Saturday, November 26, 11
![Page 75: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/75.jpg)
行78Saturday, November 26, 11
![Page 76: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/76.jpg)
1 2 3
4 5 6
7 8 9
79Saturday, November 26, 11
![Page 77: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/77.jpg)
1 2 3
4 5 6
7 8 9
80Saturday, November 26, 11
![Page 78: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/78.jpg)
1 2 3
4 5 6
7 8 9
81Saturday, November 26, 11
![Page 79: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/79.jpg)
列82Saturday, November 26, 11
![Page 80: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/80.jpg)
1 2 3
4 5 6
7 8 9
83Saturday, November 26, 11
![Page 81: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/81.jpg)
1 2 3
4 5 6
7 8 9
84Saturday, November 26, 11
![Page 82: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/82.jpg)
1 2 3
4 5 6
7 8 9
85Saturday, November 26, 11
![Page 83: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/83.jpg)
ひとまず「行列」という存在を知ってやってください
86Saturday, November 26, 11
![Page 84: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/84.jpg)
そんな行列を扱うために
matrix() 関数を使う
87Saturday, November 26, 11
![Page 85: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/85.jpg)
matrix() 関数
•行列を作る関数•matrix(要素, 行の数, 列の数)
•デフォでは列方向に配置
88Saturday, November 26, 11
![Page 86: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/86.jpg)
このようなデータなら身長 体重
A 180 75B 170 65C 165 60D 175 70E 190 80
89Saturday, November 26, 11
![Page 87: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/87.jpg)
karada <- matrix(c(180, 170, 165, 175, 190, 75, 65, 60, 70, 80), 5, 2)
行列を作ってみる
1. c() 関数で、数値を連結2. matrix() 関数で、行列に変換
• 「5行で2列」にするという指定をする3. karada という名の変数に代入
90Saturday, November 26, 11
![Page 88: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/88.jpg)
> karada
と入力して変数の中身を確認
91Saturday, November 26, 11
![Page 89: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/89.jpg)
1 4 7
2 5 8
3 6 9
matrix(1:9,nrow=3,ncol=3)
92Saturday, November 26, 11
![Page 90: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/90.jpg)
1 2 3
4 5 6
7 8 9
matrix(1:9,nrow=3,ncol=3,byrow=TRUE)
93Saturday, November 26, 11
![Page 91: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/91.jpg)
行列の要素を取り出す
1. 行列を作り、変数に代入2. 変数の中から見たい行や列を指定
94Saturday, November 26, 11
![Page 92: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/92.jpg)
行列の要素を取り出すdemo <- matrix(1:30,nrow=5,ncol=6)
http://gyazo.com/76c58d5d6c8426a44f160897cda99671.png
95Saturday, November 26, 11
![Page 93: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/93.jpg)
行列の要素を取り出す2行目を表示 → demo[2,]2列目を表示 → demo[,2]
http://gyazo.com/6726084afd9e1cc4b03df85fe6bc0f29.png
96Saturday, November 26, 11
![Page 94: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/94.jpg)
行列の要素を取り出す2行目と4行目を表示 → demo[c(2,4),]
2列目と4列目を表示 → demo[, c(2,4)]
97Saturday, November 26, 11
![Page 95: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/95.jpg)
行列の要素を取り出す
http://gyazo.com/a116c0e2f1284ea7d38bf7024d92f1cc.png98Saturday, November 26, 11
![Page 96: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/96.jpg)
行列まとめ
• 横方向が行、縦方向が列• デフォルトでの数値の並びに注意• 必要な要素を適宜取り出して値を確認
99Saturday, November 26, 11
![Page 97: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/97.jpg)
Sub topics
1. 行列のおはなし2. データフレーム3. 外部データの読込
100Saturday, November 26, 11
![Page 98: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/98.jpg)
データフレームとは
•いろんなデータを突っ込んだもの• いろんな=質的データ+量的データ• 突っ込む=一緒に並んでいること
101Saturday, November 26, 11
![Page 99: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/99.jpg)
名前 血液型 身長 体重坂本 B 175 65
高橋 B 177 70
阿部 B 174 75
長野 A 179 70
松本 O 170 60
データフレームとは
102Saturday, November 26, 11
![Page 100: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/100.jpg)
データフレームとは
• Excel の WS とほぼ同じイメージ
• データが大量になれば必須のもの• 既存データの読込>自力で作る
103Saturday, November 26, 11
![Page 101: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/101.jpg)
自力で作るなら...
• data.frame()関数を使う (p.240)
• ベクトルを連結して作る (p.243)
• 行列をデータフレームに変換 (p.243)
104Saturday, November 26, 11
![Page 102: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/102.jpg)
じゃあ実際に自力で作るかといったら...
105Saturday, November 26, 11
![Page 103: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/103.jpg)
ああー聞こえないー
http://www."ickr.com/photos/trojanguy/3046207115/“173.365 - Come on, feel the noise” by Je! the Trojan
106Saturday, November 26, 11
![Page 104: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/104.jpg)
そこで外部データの読込を行おう
107Saturday, November 26, 11
![Page 105: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/105.jpg)
1. 行列のおはなし2. データフレーム3. 外部データの読込
Sub topics
108Saturday, November 26, 11
![Page 106: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/106.jpg)
ここはトラブル多発地域です
うまくいかない時はまず説明をお聴きください
109Saturday, November 26, 11
![Page 107: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/107.jpg)
おそらく...
• Excel でのデータ管理が一般的
• そのデータをそのまま活かしたい→では、やりましょう
110Saturday, November 26, 11
![Page 108: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/108.jpg)
作業ディレクトリの設定
•作業ディレクトリとは...
•データを読み込む場所•データを書き出す場所
111Saturday, November 26, 11
![Page 109: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/109.jpg)
作業ディレクトリの設定
• Windows:ファイル → ディレクトリの変更• 今日は “MyDocuments” に設定
• Mac OS X:環境設定 → 一般(起動)
• Linux:up to you...
112Saturday, November 26, 11
![Page 110: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/110.jpg)
作業ディレクトリの設定
• コンソールからの確認・設定も可能• getwd()関数:現在の設定を確認
• > getwd()
• setwd()関数:現在の設定を変更• > setwd("/Users/sakaue/Desktop/")
113Saturday, November 26, 11
![Page 111: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/111.jpg)
データの読込 その1
• read.csv() 関数
• CSV 形式のファイルを読み込む• CSV: Comma Separated Value
114Saturday, November 26, 11
![Page 112: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/112.jpg)
1. demo.csv を作業ディレクトリへ移動• XLS/XLSX 形式のものは保存時に変換• CSV ファイルの文字コードは UTF-8 に
2. > test <- read.csv(“demo.csv”) で代入
3. > test [Enter] で中身を確認
データの読込 その1
115Saturday, November 26, 11
![Page 113: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/113.jpg)
CSVとかダルいんですけど...
という方に
116Saturday, November 26, 11
![Page 114: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/114.jpg)
データの読込 その2
• read.delim() 関数
• 表形式のデータ読み込む• delim: delimiter(区切り文字)
117Saturday, November 26, 11
![Page 115: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/115.jpg)
1. demo.xls を開き,マウスで範囲指定する2. > test2 <- read.delim("clipboard") で代入
3. > test2 [Enter] で中身を確認
データの読込 その2
Mac の場合: read.delim(pipe(“pbpaste”))
118Saturday, November 26, 11
![Page 116: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/116.jpg)
読み込んだら関数で処理> table(test2[,1])
• 度数分布を確認する> mean(test2[,2])
• 平均値を求める> hist(test2[,2])
• ヒストグラムを描く
1列目対象
2列目対象
2列目対象
119Saturday, November 26, 11
![Page 117: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/117.jpg)
外部データの読込
• 作業ディレクトリの設定に注意• CSV 形式に変換後、read.csv()関数で読込
• 必要な行 or 列を指定して処理
120Saturday, November 26, 11
![Page 118: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/118.jpg)
http://www.flickr.com/photos/rosengrant/4751386872/
CSV 形式にして読み込めば
重たいExcelファイルにおさらばですよ!
“y2.d175 | Lasershow! Relax!” by B Rosen
121Saturday, November 26, 11
![Page 119: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/119.jpg)
ExcelSPSS
122Saturday, November 26, 11
![Page 120: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/120.jpg)
Agenda1. R 概論2. 関数と変数が命3. 外部データの読込4. あっという間に作図5. R で検定
123Saturday, November 26, 11
![Page 121: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/121.jpg)
Agenda1. R 概論2. 関数と変数が命3. 外部データの読込4. あっという間に作図5. R で検定
124Saturday, November 26, 11
![Page 122: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/122.jpg)
データを集めたら数値要約だけでなくデータの視覚化も必要
125Saturday, November 26, 11
![Page 123: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/123.jpg)
というわけで作図のお時間です
126Saturday, November 26, 11
![Page 124: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/124.jpg)
基本的な作図手順
1. 変数に値を代入する2. 作図用の関数を使って処理する
127Saturday, November 26, 11
![Page 125: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/125.jpg)
たった2段階ですExcel や SPSS と来たら... orz
128Saturday, November 26, 11
![Page 126: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/126.jpg)
まずはヒストグラム
【事例】年齢のヒストグラムを
作成する
129Saturday, November 26, 11
![Page 127: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/127.jpg)
まずはヒストグラム> age <- c(18, 23, 14, 19, 21, 29, 22, 21, 23, 19, 20, 20, 26, 18, 14, 6, 8, 16, 23, 20)> hist(age)
130Saturday, November 26, 11
![Page 128: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/128.jpg)
次は散布図【事例】英語の試験と英作文の語数の相関を見る
131Saturday, November 26, 11
![Page 129: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/129.jpg)
続いて散布図> score <- c(60, 50, 72, 43, 50, 55, 43, 50, 85, 40)> words <- c(340, 190, 465, 170, 130, 225,140, 310, 580, 120)> plot(score,words)
132Saturday, November 26, 11
![Page 130: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/130.jpg)
続いて箱ひげ図【事例】
大学生1年生と高校1年生が書いた作文の語数の「幅」を
比較する133Saturday, November 26, 11
![Page 131: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/131.jpg)
続いて箱ひげ図> high <- c(350, 285, 315, 340, 210, 185, 120, 740, 425, 155)> coll <- c(365, 570, 645, 540, 645, 665, 880, 550, 410, 585)> boxplot(high, coll, names=c("High", "Coll"))
134Saturday, November 26, 11
![Page 132: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/132.jpg)
作図手順のまとめ1. 変数に値を代入する2. 作図用の関数を使って処理する3. 形式を指定して保存
+人に伝承(twitter, FB)
135Saturday, November 26, 11
![Page 133: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/133.jpg)
http://www.flickr.com/photos/rosengrant/4751386872/
手軽に美しく作図できる を使えるようになればExcel におさらば!
“y2.d175 | Lasershow! Relax!” by B Rosen
136Saturday, November 26, 11
![Page 134: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/134.jpg)
Agenda1. R 概論2. 関数と変数が命3. 外部データの読込4. あっという間に作図5. R で検定
137Saturday, November 26, 11
![Page 135: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/135.jpg)
Agenda1. R 概論2. 関数と変数が命3. 外部データの読込4. あっという間に作図5. R で検定
138Saturday, November 26, 11
![Page 136: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/136.jpg)
この世の様々な現象を数値データ+検定から捉えてみたい
を使う経緯
139Saturday, November 26, 11
![Page 137: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/137.jpg)
t 検定
カイ二乗検定パワーアナリシス
分散分析
共分散分析
判別分析
主成分分析
クラスカル・ウォリス検定
符号検定
マクマネー検定
因子分析
140Saturday, November 26, 11
![Page 138: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/138.jpg)
で「t 検定」
141Saturday, November 26, 11
![Page 139: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/139.jpg)
検定の前に仮説を設ける
•帰無仮説 H0:「差はない」「効果はない」•対立仮説 H1:「差がある」「効果がある」• H0が正しいという前提で,検定統計量 (t, !2, F ) を求める
• 検定統計量が極端で滅多に得られないような値となれば,帰無仮説を棄却し,対立仮説を採択する
142Saturday, November 26, 11
![Page 140: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/140.jpg)
t 検定
•2つのグループの平均値の差の検定するのに使う手法
•R では,t.test() であっさりと実行
143Saturday, November 26, 11
![Page 141: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/141.jpg)
• (例)英語の好きな組と嫌いな組が受けたテストの平均値に差があるかどうか
> like <- c(6,10,6,10,7,8,7,9,10,4)> dislike <- c(3,5,6,4,4,8,4,5,4,7)> t.test(like,dislike,var.equal=TRUE)
t 検定
144Saturday, November 26, 11
![Page 142: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/142.jpg)
> t.test(like,dislike,var.equal=TRUE)
Two Sample t-test
data: like and dislike t = 3.3041, df = 18, p-value = 0.003946alternative hypothesis: true difference in means is not equal to 0 # 両側検定95 percent confidence interval: # 信頼区間 0.9831754 4.4168246 sample estimates:mean of x mean of y 7.7 5.0
t 検定
145Saturday, November 26, 11
![Page 143: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/143.jpg)
t 検定の前に...
http://aoki2.si.gunma-u.ac.jp/lecture/Average/bunsan1.html
“等分散であるかどうかを確かめてから,普通の
t 検定を使うか,Welch の方法による t 検定にするかを決めるというのは,よくない。最初からWelch の方法による t 検定を使えばよい。”
146Saturday, November 26, 11
![Page 144: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/144.jpg)
問答無用にWelch !
よく分からないが(解説希望),1つのこと(有意差の有無)を知りたいのに,予備の検定を含めた2段階の検定を行うのが「気持ち悪い」らしい。
147Saturday, November 26, 11
![Page 145: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/145.jpg)
ソース! ソース!―(等分散の検定はせず)最初から Welch で行け―
• http://oku.edu.mie-u.ac.jp/~okumura/blog/node/2262
• http://aoki2.si.gunma-u.ac.jp/lecture/BF/index.html
• Donald W. Zimmerman, ``Some properties of preliminary tests of equality of variances in the two-sample location problem'', The Journal of General Psychology, Vol.123, pp.217-231 (1996)
• The unequal variance t-test is an underused alternative to Student's t-test and the Mann-Whitney U test -- Ruxton 17 (4): 688 -- Behavioral Ecology
• 馬車馬のように: [統計]Mann-WhitneyのU検定と不等分散(http://qdai.way-
nifty.com/qjes/2005/02/mannwhitneyu.html
148Saturday, November 26, 11
![Page 146: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/146.jpg)
> t.test(like,dislike,var.equal=FALSE)
Welch Two Sample t-test
data: like and dislike t = 3.3041, df = 16.795, p-value = 0.004249alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: 0.9743014 4.4256986 sample estimates:mean of x mean of y 7.7 5.0
という訳で...
149Saturday, November 26, 11
![Page 147: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/147.jpg)
ところで t 検定の t は何なのかと...
• t 分布の t なんだけど...
• ゴセットの発表した統計量(Student の t 分布)に,(論敵の)フィッシャーが t という記号を充てた
• 別に特別な意味は無く,論文中の標本分散s^2に対する式だったことから,s の次の t を使ったらしい...
• 「ウィリアム・ゴセット」http://ja.wikipedia.org/wiki/%E3%82%A6%E3%82%A3%E3%83%AA%E3%82%A2%E3%83%A0%E3%83%BB%E3%82%B4%E3%82%BB%E3%83%83%E3%83%88
• 「自由度」http://ja.wikipedia.org/wiki/%E8%87%AA%E7%94%B1%E5%BA%A6
• http://mat.isc.chubu.ac.jp/fpr/fpr1997/0119.html
• http://www.pol.geophys.tohoku.ac.jp/~hanawa/ori/ori/054.html
150Saturday, November 26, 11
![Page 148: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/148.jpg)
t 検定
カイ二乗検定パワーアナリシス
分散分析
共分散分析
判別分析
主成分分析
クラスカル・ウォリス検定
符号検定
マクマネー検定
因子分析
151Saturday, November 26, 11
![Page 149: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/149.jpg)
で「カイ二乗検定」
152Saturday, November 26, 11
![Page 150: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/150.jpg)
カイ二乗検定•独立性の検定のために使う(連関の有無)• (例)数学が好き嫌いと統計の好き嫌いの間に有意な連関があるか
• (例)あるコーパス中の表現Aと表現Bの頻度の差
•ざっくり言えば,期待度数と観測度数のズレが偶然出ちゃったかどうかを調べる
153Saturday, November 26, 11
![Page 151: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/151.jpg)
事例: 接続詞 “しかし” の生起位置の比較
文頭 文中 文末 合計頻度 109 347 8 493
[文頭] しかし, ....
[文中] ..., しかし, ....
[文末] ..., しかし.
注)実は,英語の “however” の例
154Saturday, November 26, 11
![Page 152: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/152.jpg)
> freq <- c(109,347,8)> chisq.test(freq,correct=FALSE)
Chi-squared test for given probabilities
data: freq X-squared = 391.7371, df = 2, p-value < 2.2e-16
# 手作業なら,カイ二乗分布表の自由度2のところを確認する# http://homepage2.nifty.com/nandemoarchive/toukei_kiso/t_F_chi.htm
生起位置の比較のためにカイ二乗検定を実行
155Saturday, November 26, 11
![Page 153: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/153.jpg)
t 検定
カイ二乗検定パワーアナリシス
分散分析
共分散分析
判別分析
主成分分析
クラスカル・ウォリス検定
符号検定
マクマネー検定
因子分析
156Saturday, November 26, 11
![Page 154: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/154.jpg)
で「分散分析」
157Saturday, November 26, 11
![Page 155: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/155.jpg)
分散分析• 3つ以上の平均値の差の検定のために使う
• t 検定は使えない:検定の多重性の問題あり=本当は差がないのに,差があると判断してしまう確率(第1種の誤り)が上がってしまう
• ANOVA(アノーバ、ANalysis Of VAriance)
• F という検定統計量を利用(F 分布)
158Saturday, November 26, 11
![Page 156: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/156.jpg)
Aクラス Bクラス Cクラス10 9 68 7 49 4 36 5 99 2 25 8 67 4 29 2 48 8 3
10 4 9
分散分析
159Saturday, November 26, 11
![Page 157: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/157.jpg)
> test <- read.csv("demo.csv", head=T)> anova(lm(Class ~ Score, data=test))Analysis of Variance Table
Response: Class Df Sum Sq Mean Sq F value Pr(>F) Score 1 5.2389 5.2389 9.9376 0.00384 **Residuals 28 14.7611 0.5272 ---Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
分散分析
160Saturday, November 26, 11
![Page 158: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/158.jpg)
•差がありそうかなさそうかを判断するために各種検定を行う•平均値の差の検定:t 検定と分散分析•独立性の検定 :カイ二乗検定•数行のコマンドであっさり検定!•データ準備の手間は多少かかるかも...
で検定のまとめ
161Saturday, November 26, 11
![Page 159: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/159.jpg)
この世の現象を数値データ+検定から捉えてみよう!
を使って
162Saturday, November 26, 11
![Page 160: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/160.jpg)
One more thing...
163Saturday, November 26, 11
![Page 161: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/161.jpg)
Hi oshima.
広島を中心とした統計解析環境 R に関する勉強会
164Saturday, November 26, 11
![Page 162: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/162.jpg)
HiRoshima.Rとは• 広島・中国地域を中心とした R の勉強会
• 2-3ヶ月に1度開催(目標)
• 参加者の多くは大学・教育関係者• 一般企業にお勤めの方も...
165Saturday, November 26, 11
![Page 163: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/163.jpg)
なぜ HiRoshima.R?• 広島にやってきたことだし...
• 全国各地の R 勉強会に触発される• Tsukuba.R / Tokyo.R / Osaka.R / Nagoya.R
• Internet (computer) literacy が高い?• 話も自然と高度に...
166Saturday, November 26, 11
![Page 164: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/164.jpg)
• R ユーザー(特に入門者)を増やしたい
• HiRoshima.R を入り口・足場に
• 悩める者はここに集え!!
なぜ HiRoshima.R?
168Saturday, November 26, 11
![Page 165: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/165.jpg)
第1回の参加申込者(22名)の利用頻度
高頻度14%
低頻度64%
初めて23%
169Saturday, November 26, 11
![Page 166: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/166.jpg)
• 異分野交流(→Lightning Talks)• 人文科学・社会科学・自然科学の研究、一般企業それぞれで何してますか?
• 自分の活動・研究のヒントになるかも
なぜ HiRoshima.R?
171Saturday, November 26, 11
![Page 167: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/167.jpg)
HiRoshima.Rで何をするか• 入門者向け
• 「R いいよ R」• インストール,起動・終了
• 初級者向け• データ処理,関数の利用法,作図• 統計の基礎,検定
172Saturday, November 26, 11
![Page 168: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/168.jpg)
• 中級+ 者向け(Speaker 募集中!!)• 統計の理論解説• パッケージ利用• 複雑かつ美しいグラフィックス作成• R で何かをシミュレーション• R で何かをプログラミング
HiRoshima.Rで何をするか
173Saturday, November 26, 11
![Page 169: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/169.jpg)
• 3部構成の予定• 第1部:入門者講習• 第2部:#1 の続編• 第3部:Lightning Talks
• 希望の部にご参加ください
HiRoshima.Rで何をするか
174Saturday, November 26, 11
![Page 170: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/170.jpg)
•Speaker になってください• 入門者講習の講師• Lightning Talks
• ネタは R にかすれば何でもOK• 時間は5分からで OK
お知らせとお願い
176Saturday, November 26, 11
![Page 171: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/171.jpg)
•Feedback をください• blog / twitter 経由
• #HiRoshimaR / @sakaue
• 感想,要望,賞賛,建設的批判
お知らせとお願い
177Saturday, November 26, 11
![Page 172: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/172.jpg)
参考文献
178Saturday, November 26, 11
![Page 173: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/173.jpg)
参考文献
179Saturday, November 26, 11
![Page 174: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/174.jpg)
参考文献(続)
180Saturday, November 26, 11
![Page 175: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/175.jpg)
1,785円 3,780円2,940円
参考文献(続)
181Saturday, November 26, 11
![Page 176: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/176.jpg)
ExcelSPSS
182Saturday, November 26, 11
![Page 177: Japan.R #2 Introduction to R](https://reader035.vdocument.in/reader035/viewer/2022070315/5555ab47d8b42a52568b4913/html5/thumbnails/177.jpg)
ご静聴ありがとうございました
twitter: @sakaue
e-mail: tsakaue<AT>hiroshima-u.ac.jp
183Saturday, November 26, 11