HiroshimaR5_Intro

153
@sakaue ご挨拶:目的と活動内容

Transcript of HiroshimaR5_Intro

Page 1: HiroshimaR5_Intro

阪 上 辰 也 @sakaue

ご挨拶:目的と活動内容

Page 2: HiroshimaR5_Intro

Agenda

1. 自己紹介

2. HiRoshima.R とは

3. お知らせとお願い

Page 3: HiroshimaR5_Intro

Agenda

1. 自己紹介

2. HiRoshima.R とは

3. お知らせとお願い

Page 4: HiroshimaR5_Intro

1. 自己紹介

•阪上 辰也 (SAKAUE, Tatsuya)

• 所属:         外国語教育研究センター

• Nagoya.R /SappoRo.R 元主催, HiRoshima.R 主催

• 詳しくは...

Page 5: HiroshimaR5_Intro

1. 自己紹介

Page 6: HiroshimaR5_Intro

Agenda

1. 自己紹介

2. HiRoshima.R とは

3. お知らせとお願い

Page 7: HiroshimaR5_Intro

Agenda

1. 自己紹介

2. HiRoshima.R とは

3. お知らせとお願い

Page 8: HiroshimaR5_Intro

2. HiRoshima.Rとは

• 広島・中国地域を中心とした R の勉強会

• 2-3ヶ月... いや半年に1度開催(目標)

• 過去の参加者 : 多くは大学・教育関係者

• 企業にお勤めの方も...

Page 9: HiroshimaR5_Intro

なぜ HiRoshima.R?

• R ユーザー(特に入門者)を増やしたい

• HiRoshima.R を入り口・足場に

• 悩める方はどうぞいらっしゃーい

Page 10: HiroshimaR5_Intro

なぜ HiRoshima.R?

•異分野交流(Lightning Talks)

• 人文科学・社会科学・自然科学の研究、一般企業それぞれで何してますか?

• 自分の活動・研究のヒントになるかも

Page 11: HiroshimaR5_Intro

HiRoshima.R で何をするか

•入門者・初心者向け • インストール,起動・終了

• データ処理,関数の利用法,作図

• 統計の基礎,検定

Page 12: HiroshimaR5_Intro

•中級+ 者向け(発表者募集中) • 統計の理論解説

• パッケージ利用

• 高度なグラフィックス作成

• シミュレーション・プログラミング

HiRoshima.R で何をするか

Page 13: HiroshimaR5_Intro

•今後も2部構成の予定

• 第1部:入門者講習

• 第2部:Lightning Talks

• 希望の部にご参加ください

HiRoshima.R で何をするか

Page 14: HiroshimaR5_Intro

Agenda

1. 自己紹介

2. HiRoshima.R とは

3. お知らせとお願い

Page 15: HiroshimaR5_Intro

Agenda

1. 自己紹介

2. HiRoshima.R とは

3. お知らせとお願い

Page 16: HiroshimaR5_Intro

•発表者になってください • 入門者講習の講師

• Lightning Talks

• ネタは R にかすれば何でもOK • 時間は5分から

3. お知らせとお願い

Page 17: HiroshimaR5_Intro

•フィードバックをください • blog / twitter 経由

• #HiRoshimaR / @sakaue

• 感想,要望,賞賛,批判

3. お知らせとお願い

Page 18: HiroshimaR5_Intro

•会場を貸してください • 東広島はちょっと不便

• 広島市内なら参加者増も期待

• 異分野交流をより盛んに

3. お知らせとお願い

Page 19: HiroshimaR5_Intro

one more thing…

Page 20: HiroshimaR5_Intro

今後は

http://hiroshimar.connpass.com

へ移行します (のでメンバー登録だけでもどうぞ)

Page 21: HiroshimaR5_Intro
Page 22: HiroshimaR5_Intro

2015-09-19 HiRoshima.R #5 @ Hiroshima University

入門者講習

Page 23: HiroshimaR5_Intro

Tweet Me!

#HiRoshimaR

Page 24: HiroshimaR5_Intro

Agenda1. R の基本操作 2. 関数と変数が命 3. 外部データの読込と作図 4. RStudio で快適操作 5. パッケージの利用

Page 25: HiroshimaR5_Intro

Agenda1. R の基本操作 2. 関数と変数が命 3. 外部データの読込と作図 4. RStudio で快適操作 5. パッケージの利用

Page 26: HiroshimaR5_Intro

 とは何か• 統計処理を目的とするプログラミング言語

• 無償+オープンソースのソフトウェア

• 作者: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/JeffreyHorner/JCGSR.pdf

Page 27: HiroshimaR5_Intro

要するに 統計ソフト

Page 28: HiroshimaR5_Intro

  の何が良いのか

•無償(IBM SPSS Statistics は約10万円で保守なし...)

•統計処理の種類が豊富

•作図結果が美しい(簡素)

Page 29: HiroshimaR5_Intro

なぜ今  なのか

• 経費や研究費の節約

• 分析手法の明確化

• 可視化による傾向把握

Page 30: HiroshimaR5_Intro

 の弱点• 文字入力による操作

• プログラムを組む感覚を養成

• 確実に伝わるという利点も

• 多方面の知識が必要 • 英語+統計+計算機

• 参考書籍がお高め...

Page 31: HiroshimaR5_Intro

モウダメダー

Page 32: HiroshimaR5_Intro

Excel や SPSS は...

•高価(気軽に使えない)

•処理が信頼できず • Excel 使うな(奥村先生@三重大)

•作図結果が美しくない

Page 33: HiroshimaR5_Intro

  の基本操作 ‐Install / Update / Uninstall‐

Page 34: HiroshimaR5_Intro

1. http://www.r-project.org/ にアクセス

2. “download R” をクリック

3. JAPAN の中から1つを選ぶ

4. OS に応じたリンクをクリック

  のインストール

Page 35: HiroshimaR5_Intro

  のインストール

• Windows: : http://cran.ism.ac.jp/bin/windows/base/ •「Download R 3.2.1 for Windows」をクリック

• Mac OS X: http://cran.ism.ac.jp/bin/macosx/ •「R-3.2.1.pkg」をクリック

Page 36: HiroshimaR5_Intro
Page 37: HiroshimaR5_Intro
Page 38: HiroshimaR5_Intro
Page 39: HiroshimaR5_Intro
Page 40: HiroshimaR5_Intro
Page 41: HiroshimaR5_Intro
Page 42: HiroshimaR5_Intro

  のアップデート1. http://www.r-project.org/ にアクセス

2. 自分が使っているバージョンより新しいものが出ているか確認

3. 新規にインストール用ファイルをダウンロードしてインストール

Page 43: HiroshimaR5_Intro

  のアップデート• セキュリティ上の問題がない限りは,むやみに更新しない

• パッケージ(付加機能)の動作不良回避

• 新旧の共存は可能 • Windows/Mac: 別名でインストール • ただし何がどう共有されているかは不明

Page 44: HiroshimaR5_Intro

 のアンインストール• Windows 7/8

• コンパネ→プログラムのアンインストール

• Mac OS X • /Applications にある    アイコンと /Library の中の

Frameworks にある R. framework を削除(CleanApp 可)

Page 45: HiroshimaR5_Intro

  の起動

• Windows 7/8/10 • START → Program → R → R 3.2.2 を選択

• Mac OS X • /Applications 内の をダブルクリック

Page 46: HiroshimaR5_Intro

「コンソール」の表示を確認

Page 47: HiroshimaR5_Intro

  の終了

•コンソールに「q()」と入力 • 今は「作業スペース」は保存しない • ウィンドウが消えれば終了

Page 48: HiroshimaR5_Intro

  で困ったら...

•まずは Web 検索! • seekR: http://seekr.jp/

• R SEEK: http://www.rseek.org/ • RjpWiki: http://www.okada.jp.org/RWiki/ • R-Tips: http://cse.naro.affrc.go.jp/takezawa/r-tips/r.html

• 標準搭載の help(sth) でも確認可能

Page 49: HiroshimaR5_Intro

  で困ったら...•参照資料は豊富にあります

• R オンラインガイド(山口大学教育学部教育心理学コース)

• R 言語入門(ドットインストール)

• はじめての「R」(林真広 氏)

• R による統計処理(青木繁伸先生@群馬大学)

• 最近は書籍も増えました

Page 50: HiroshimaR5_Intro

  概論まとめ

•    は統計ソフト

•無償+何でも統計+美しく作図

•困ったら Web 検索!!

Page 51: HiroshimaR5_Intro

Agenda1. R の基本操作 2. 関数と変数が命 3. 外部データの読込と作図 4. RStudio で快適操作 5. パッケージの利用

Page 52: HiroshimaR5_Intro

Agenda1. R の基本操作 2. 関数と変数が命 3. 外部データの読込と作図 4. RStudio で快適操作 5. パッケージの利用

Page 53: HiroshimaR5_Intro

 といえば

関数 が命

Page 54: HiroshimaR5_Intro

関数とは 指定した値に対して 何らかの処理をして 結果を返すもの

Page 55: HiroshimaR5_Intro

sqrt():平方根

•> sqrt(2)

•> sqrt(144)

•> sqrt(104976)

Page 56: HiroshimaR5_Intro

「関数(値)」が命

• には多数の関数がある

• q() や help(sth) も関数

• 必要な関数をしつこく実行

Page 57: HiroshimaR5_Intro

  といえば

変数 も命

Page 58: HiroshimaR5_Intro

変数とは 1つ以上の値を まとめていれておく「箱」のこと

Page 59: HiroshimaR5_Intro

では ベクトルとも呼ばれ 複数の値を1つに まとめたものを指す

Page 60: HiroshimaR5_Intro

統計処理などを行うには 複数の値をまとめて扱う

ことが必要で それを使い回すために

変数を使う

Page 61: HiroshimaR5_Intro

ちなみに 変数に数値を 入れることを

「代入」といいます

Page 62: HiroshimaR5_Intro

なにはともあれ 実行してみましょう

Page 63: HiroshimaR5_Intro

変数の名前は “hako”

•> hako <- c(1,2,3,4,5) •> hako

• c() 関数:combine (cf. https://twitter.com/#!/sakaue/status/193708048030760960) • 値を1つにまとめる関数 • 逆に書いても(一応)OK

Page 64: HiroshimaR5_Intro

c()関数の “<-” は何?

hako <- c(1,2,3,4,5) の “<-” は

左向きの矢印(←) を表現

(入れ替えても動きます)

Page 65: HiroshimaR5_Intro

コンソールで「+」が出たら...

• 入力中に誤って[Enter] を押すと,待機状態を示す「+」が出る

• [STOP] ボタンでキャンセル

• [Esc] キーでキャンセル

Page 66: HiroshimaR5_Intro

履歴機能と補完機能

•履歴: [↑][↓] の矢印キー

•補完: [Tab]キー

Page 67: HiroshimaR5_Intro

いま “hako” という名前の 「変数」の中に

1から5までの5つの数字が まとめて入っている状態

Page 68: HiroshimaR5_Intro

以下の関数を実行

•> sqrt(hako)

•> log(hako)

Page 69: HiroshimaR5_Intro

もし変数がなかったら 何度も関数を

繰り返す羽目になる sqrt(1), sqrt(2) ... sqrt(5) log(1), log (2) ... log (5)

Page 70: HiroshimaR5_Intro

一括処理のために 変数(ベクトル)の 利用は不可欠

Page 71: HiroshimaR5_Intro

ここまでのまとめ•   は「関数」と「変数」が命

• 関数を使って数値を処理 • q(), help(), sqrt(), log(), c()

• 変数を使って一括処理!

Page 72: HiroshimaR5_Intro

覚える必要はなく メモ等を頼りに 実行できれば OK

Page 73: HiroshimaR5_Intro

Agenda1. R の基本操作 2. 関数と変数が命 3. 外部データの読込と作図 4. RStudio で快適操作 5. パッケージの利用

Page 74: HiroshimaR5_Intro

Agenda1. R の基本操作 2. 関数と変数が命 3. 外部データの読込と作図 4. RStudio で快適操作 5. パッケージの利用

Page 75: HiroshimaR5_Intro

Sub topics

1. 行列のおはなし

2. データフレーム

3. 外部データの読込

Page 76: HiroshimaR5_Intro

さきほど 一行で表せる数値を 扱いましたが

Page 77: HiroshimaR5_Intro

実際のデータは 複数行あるはず

Page 78: HiroshimaR5_Intro

例えば...

•身長と体重

•年齢と年収

•覚えた単語数とTOEIC スコア

Page 79: HiroshimaR5_Intro

表にすれば...身長 体重

A 180 75B 170 65C 165 60D 175 70E 190 80

Page 80: HiroshimaR5_Intro

複数の行や列で表されるデータを扱うために

Rでは「行列」を使う

Page 81: HiroshimaR5_Intro

行列とは

数値が縦横に並べられたもの

Page 82: HiroshimaR5_Intro

1 2 3

4 5 6

7 8 9

Page 83: HiroshimaR5_Intro

Page 84: HiroshimaR5_Intro

1 2 3

4 5 6

7 8 9

Page 85: HiroshimaR5_Intro

1 2 3

4 5 6

7 8 9

Page 86: HiroshimaR5_Intro

1 2 3

4 5 6

7 8 9

Page 87: HiroshimaR5_Intro

Page 88: HiroshimaR5_Intro

1 2 3

4 5 6

7 8 9

Page 89: HiroshimaR5_Intro

1 2 3

4 5 6

7 8 9

Page 90: HiroshimaR5_Intro

1 2 3

4 5 6

7 8 9

Page 91: HiroshimaR5_Intro

ひとまず 「行列」という存在を 知ってやってください

Page 92: HiroshimaR5_Intro

行列まとめ

• 横方向が行、縦方向が列

• デフォルトでの数値の並びに注意

• 必要な要素を適宜取り出して値を確認

Page 93: HiroshimaR5_Intro

Sub topics

1. 行列のおはなし

2. データフレーム

3. 外部データの読込

Page 94: HiroshimaR5_Intro

データフレームとは

• いろんなデータを突っ込んだもの

• いろんな=質的データ+量的データ

• 突っ込む=一緒に並んでいること

Page 95: HiroshimaR5_Intro

名前 血液型 身長 体重坂本 B 175 65

高橋 B 177 70

阿部 B 174 75

長野 A 179 70

松本 O 170 60

データフレームとは

Page 96: HiroshimaR5_Intro

データフレームとは

• Excel の WS とほぼ同じイメージ

• 既存データの読込>自力で作る

• data.frame() 関数が使える

Page 97: HiroshimaR5_Intro

1. 行列のおはなし

2. データフレーム

3. 外部データの読込

Sub topics

Page 98: HiroshimaR5_Intro

ここはトラブル多発地域です

うまくいかない時は まず説明をお聴きください

Page 99: HiroshimaR5_Intro

おそらく...

•Excel でデータ管理

•できればそのまま活かしたい

Page 100: HiroshimaR5_Intro

作業ディレクトリの設定

•作業ディレクトリとは...

•データを読み込む場所 •データを書き出す場所

Page 101: HiroshimaR5_Intro

作業ディレクトリの設定• Windows:ファイル → ディレクトリの変更

• “My Documents” にファイル移動

• Mac OS X:環境設定 → 一般(起動)

• ホームディレクトリにファイル移動

• Linux:up to you...

Page 102: HiroshimaR5_Intro

作業ディレクトリの設定• コンソールからの確認・設定も可能

• getwd()関数:現在の設定を確認

• > getwd()

• setwd()関数:現在の設定を変更 • > setwd("/Users/sakaue/Desktop/") • 実行注意!!

Page 103: HiroshimaR5_Intro

データの読込 その1

• read.csv() 関数

• CSV 形式のファイルを読み込む

• CSV: Comma Separated Value

Page 104: HiroshimaR5_Intro

1. hiro4.csv を作業ディレクトリへ移動

• XLS/XLSX 形式のものは保存時に変換 • CSV ファイルの文字コードは UTF-8 に

2. > test <- read.csv(choose.files(), header = TRUE) #Win

# test <- read.csv(file.choose(), header = TRUE) #Mac

3. > test [Enter] で中身を確認

データの読込 その1

Page 105: HiroshimaR5_Intro

CSV 変換なんて面倒... という方に

Page 106: HiroshimaR5_Intro

データの読込 その2

• read.delim() 関数

• 表形式のデータ読み込む

• delim: delimiter(区切り文字)

Page 107: HiroshimaR5_Intro

1. hiro5.xls を開いてマウスで範囲指定

2. > test2 <- read.delim("clipboard") で代入

3. > test2 [Enter] で中身を確認

データの読込 その2

Mac の場合: read.delim(pipe("pbpaste"))

Page 108: HiroshimaR5_Intro

読み込んだら関数で処理

> table(test[,3]) •度数分布を確認する

> mean(test[,3]) •平均値を求める

3列目対象

3列目対象

Page 109: HiroshimaR5_Intro

外部データの読込

• 作業ディレクトリの設定に注意

• CSV 形式に変換後,read.csv()関数で読込

• 必要な行 or 列を指定して処理

Page 110: HiroshimaR5_Intro

データを集めたら 数値要約だけでなく データの可視化も必要

Page 111: HiroshimaR5_Intro

というわけで 作図のお時間です

Page 112: HiroshimaR5_Intro

基本的な作図手順

1. 変数に値を代入

2. 作図用の関数で処理

Page 113: HiroshimaR5_Intro

たった2段階 Excel や SPSS ではこうはいかない

Page 114: HiroshimaR5_Intro

• ヒストグラム

• 散布図

• 棒グラフ

• 折れ線グラフ

• 箱ひげ図

作成できる図の種類

Page 115: HiroshimaR5_Intro

まずはヒストグラム

【事例】 得点のヒストグラムを

作成する

Page 116: HiroshimaR5_Intro

まずはヒストグラム

> pretest <- (test$Pre) > hist(pretest)

Page 117: HiroshimaR5_Intro

次は散布図【事例】

事前テストの得点と 事後テストの得点を 比較し,伸びを見る

Page 118: HiroshimaR5_Intro

続いて散布図> pretest <- (test$Pre) > posttest <- (test$Post) > plot(pretest, posttest) > plot(pretest,posttest, ylim=c(0,100),xlim=c(0,100))

Page 119: HiroshimaR5_Intro

続いて箱ひげ図【事例】

事前テストの得点と 事後テストの得点の <幅>を比較する

Page 120: HiroshimaR5_Intro

続いて箱ひげ図

> pretest <- (test$Pre) > posttest <- (test$Post) > boxplot(pretest, posttest, names=c("PRE", "POST"))

Page 121: HiroshimaR5_Intro

箱ひげ図

> boxplot(pretest, posttest, names=c("Pre", "Post"), horizontal=T)

Page 122: HiroshimaR5_Intro

箱ひげ図

> boxplot(pretest, posttest, names=c("Pre", "Post"), horizontal=T, ylim=c(0,100))

Page 123: HiroshimaR5_Intro

箱ひげ図

> boxplot(pretest, posttest, names=c("Pre", "Post"), horizontal=T, ylim=c(0,100), col="blue")

Page 124: HiroshimaR5_Intro

箱ひげ図

> boxplot(pretest, posttest, names=c("Pre", "Post"), horizontal=T, ylim=c(0,100), col="blue", xlab="SCORE", ylab="TEST", main="BOXPLOT")

Page 125: HiroshimaR5_Intro

作図手順のまとめ1. 変数に値を代入する

2. 作図用の関数を使って処理する

3. 形式を指定して保存

+人に伝承(twitter, FB)

Page 126: HiroshimaR5_Intro

Agenda1. R の基本操作 2. 関数と変数が命 3. 外部データの読込と作図 4. RStudio で快適操作 5. パッケージの利用

Page 127: HiroshimaR5_Intro

Agenda1. R の基本操作 2. 関数と変数が命 3. 外部データの読込と作図 4. RStudio で快適操作 5. パッケージの利用

Page 128: HiroshimaR5_Intro

RStudioとは?

• R を快適に使うための統合開発環境 (IDE)

• ひとつの画面に複数の情報を配置

• 面倒(そう)なことがすぐできて(・∀・)イイ!! • 関数等の入力支援(tab)

• 変数等の中身をすぐに確認(CLICK!)

• Markdown によるレポート作成(割愛…)

Page 129: HiroshimaR5_Intro

RStudio のインストール

(R そのものは事前のインストールが必要)

Page 130: HiroshimaR5_Intro

RStudio のインストール

Page 131: HiroshimaR5_Intro

RStudio のインストール

Page 132: HiroshimaR5_Intro

↓OS に応じてダウンロード

RStudio のインストール

Page 133: HiroshimaR5_Intro

コンソール

エディタ 変数・履歴

いろいろ

RStudio の起動直後

Page 134: HiroshimaR5_Intro

画面 できること

エディタ(左上) 関数などの入力・既存スクリプトを閲覧 Run でそのまま実行も可

コンソール(左下) 関数の実行・変数への代入など 素の R と同じ

変数・履歴(右上) 変数の中身や使用した関数を表示 ダブルクリックでコンソールに貼付

いろいろ(右下)ファイルの一覧表示 作図結果の表示 パッケージ管理

ひとつの画面ですべてのことを

Page 135: HiroshimaR5_Intro

なにはともあれ Project を作成

↓New Directory を選択

Page 136: HiroshimaR5_Intro

なにはともあれ Project を作成

↓Empty Project を選択

Page 137: HiroshimaR5_Intro

なにはともあれ Project を作成↓Directory name を入力し

その名をつけた Directoryをどこに作るか選択

Page 138: HiroshimaR5_Intro

ちなみに配置は変更可能Tools -> Global Options にある

Pane Layout で配置は変更可能

Page 139: HiroshimaR5_Intro

http://www.slideshare.net/TakashiYamane1/rstudio

時間がないので 山根さんの資料を見ましょう(HiRoshima.R #2 での資料。40K views!)

改訂版は Hijiyama.R #3 にて (11/28)

Page 140: HiroshimaR5_Intro

いろいろと手助けしてくれる

RStudio を使って

R に慣れていきましょう

Page 141: HiroshimaR5_Intro

Agenda1. R の基本操作 2. 関数と変数が命 3. 外部データの読込と作図 4. RStudio で快適操作 5. パッケージの利用

Page 142: HiroshimaR5_Intro

Agenda1. R の基本操作 2. 関数と変数が命 3. 外部データの読込と作図 4. RStudio で快適操作 5. パッケージの利用

Page 143: HiroshimaR5_Intro

Packageとは?

• ある処理・機能に特化したプログラム

• base(基本パッケージ)だけでも 1,000 以上

• 特殊な処理を行う際は,別の package を追加

• 言語処理に特化したものもある(ex. RMeCab)

Page 144: HiroshimaR5_Intro

可視化の発展編

violin plot と beeswarm

―分布をより分かりやすく捉えるために―

Page 145: HiroshimaR5_Intro

violin plot ≒ boxplot+hist

> install.packages("vioplot") > library("vioplot")

> vioplot(pretest,posttest, names=c("Pre","Post"))

Page 146: HiroshimaR5_Intro
Page 147: HiroshimaR5_Intro

boxplot + beeswarm

> install.packages("beeswarm") > library("beeswarm") > PrePost <- test[,c("Pre", “Post")] > boxplot(pretest, posttest, names=c("PRE", "POST"))

> beeswarm(PrePost, add=TRUE)

Page 148: HiroshimaR5_Intro
Page 149: HiroshimaR5_Intro

おわりに

Page 150: HiroshimaR5_Intro

のススメ

Page 151: HiroshimaR5_Intro

• すべての処理・分析が R のみで完結 • <-> Editor + Excel (+ UNIX) + R + なんやかんや

• プログラミングの基礎養成に有効

• --> Python, Perl, ... どこでも使える

• 作図が美しい(+Mac ならフォントも)

• Excel の作図は一般に (‘A`)ヴァ-

のススメ

Page 152: HiroshimaR5_Intro

参考文献

Page 153: HiroshimaR5_Intro

参考文献(続)