r @ linguistics - fc2jcorpus.web.fc2.com/tutorial/r.pdfr @ linguistics 淺尾仁彦...
TRANSCRIPT
R @ linguistics
淺尾仁彦
京都大学大学院文学研究科言語学専修
2007/08/24
淺尾仁彦 (京都大学) R @ linguistics 2007/08/24 1 / 24
もくじ
もくじ
1 イントロダクション2 Rの基礎
計算ベクトル,行列
3 Rで簡単な検定をするデータの読込グラフ作成検定
4 コーパスから生産性を計算するテキスト処理関数
5 おわりに
淺尾仁彦 (京都大学) R @ linguistics 2007/08/24 2 / 24
イントロ
Rの特徴
R:フリーの統計解析ソフト数値計算・統計機能が充実グラフィックも充実テキスト処理もできる無料コマンド操作が基本
淺尾仁彦 (京都大学) R @ linguistics 2007/08/24 3 / 24
イントロ
Rの特徴
—Life is short. Use the command line.(Crawley 2002: 11)
マウスで操作できる R Commanderというのもあります
淺尾仁彦 (京都大学) R @ linguistics 2007/08/24 4 / 24
イントロ
Rと言語研究
パッケージStefan Evertのページ
テキストStefan Gries (to appear). Quantitative corpus linguistics with R: apractical introduction. New York: Routledge.
Harald Baayen (to appear). Analyzing Linguistic Data. A PracticalIntroduction to Statistics. Cambridge: Cambridge University Press.(今だけ電子版あり)
淺尾仁彦 (京都大学) R @ linguistics 2007/08/24 5 / 24
Rの基礎
とりあえず計算してみる
Rを起動!> ←これが入力待ちのサイン
> 3+5
[1] 8
> 22/7
[1] 3.142857
> 1+2*3
[1] 7
> sqrt(49)
[1] 7
淺尾仁彦 (京都大学) R @ linguistics 2007/08/24 6 / 24
Rの基礎
変数
変数に代入> x <- 33
> y <- 6*7
変数の中身を見る> y
[1] 42
変数を使って計算する> x+y
[1] 75
淺尾仁彦 (京都大学) R @ linguistics 2007/08/24 7 / 24
Rの基礎
ベクトル
ベクトルをつくる関数: c> myvector <- c(1,5,4,3,8,6)
ベクトルの中身を見る> myvector
[1] 1 5 4 3 8 6
平均> mean(myvector)
[1] 4.5
標準偏差> sd(myvector)
[1] 2.428992
淺尾仁彦 (京都大学) R @ linguistics 2007/08/24 8 / 24
Rの基礎
関数いろいろ
関数の使い方がわからないとき> help(sd)
あるいは> ?sd
ベクトル向けの関数sum() max() rev() length() sort() summary()
淺尾仁彦 (京都大学) R @ linguistics 2007/08/24 9 / 24
Rの基礎
行列
行列をつくる> mymatrix <- matrix(1:9, nrow=3)
※ 1:9は c(1,2,3,4,5,6,7,8,9)と同じ
行列の中身を見る> mymatrix
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
行列を Excel風に編集> fix(mymatrix)
淺尾仁彦 (京都大学) R @ linguistics 2007/08/24 10 / 24
簡単な検定
課題:関連性を確かめる
例:英語の与格交替I give that to her.
I give her that.
「Recipientが有生のときは二重目的語構文が好まれる」と主張したいどうする?
作業順序1 コーパスの用例ごとにタグ付けする2 有生性と構文選択のクロス集計表をつくる3 検定する
淺尾仁彦 (京都大学) R @ linguistics 2007/08/24 11 / 24
簡単な検定
データのファイルからの読込
read.delim() タブ区切りデータの読込read.csv() カンマ区切りデータ (csv)の読込
Excelからのコピペも可能:1 Excelで verbs.txtを開く2 表全体を選択してコピー3 Rで次を入力> verbs <- read.delim("clipboard")
または,ファイルから直接読込> verbs <- read.delim(choose.files())
淺尾仁彦 (京都大学) R @ linguistics 2007/08/24 12 / 24
簡単な検定
クロス集計表をつくる
データを「ちょっと」見てみる> head(verbs)
クロス集計する関数: xtabs> mydata <- xtabs (˜ 実現形 + Recipient, data=verbs)
中身を見てみる> mydata
無生 有生NP 34 521PP 47 301
淺尾仁彦 (京都大学) R @ linguistics 2007/08/24 13 / 24
簡単な検定
グラフを描く
無生 有生NP 34 521PP 47 301
グラフを描いてみる> barplot(mydata)
> barplot(mydata, beside=T, legend=T)
グラフの保存:1 右クリック→メタファイルにコピー2 Microsoft Wordなどに貼り付け
淺尾仁彦 (京都大学) R @ linguistics 2007/08/24 14 / 24
簡単な検定
検定をする
無生 有生NP 34 521PP 47 301
カイ二乗検定> chisq.test(mydata)
フィッシャーの正確確率検定> fisher.test(mydata)
淺尾仁彦 (京都大学) R @ linguistics 2007/08/24 15 / 24
簡単な検定
検定をする
> fisher.test(mydata)
Fisher’s Exact Test for Count Data
data: y
p-value = 0.0002826
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
0.2547492 0.6809272
sample estimates:
odds ratio
0.4183587
p値 < 0.001→有生性と構文選択は有意に関連している
淺尾仁彦 (京都大学) R @ linguistics 2007/08/24 16 / 24
生産性
Rでのテキスト処理
文字列も普通に扱える> mysentence <- "This is a pen"
> mysentence
[1] "This is a pen"
スペースで区切って単語のリストにする> mywords <- unlist(strsplit(mysentence, " "))
[1] "This" "is" "a" "pen"
検索> grep("is", mywords)
[1] 1 2
> grep("is", mywords, value=T)
[1] "This" "is"
淺尾仁彦 (京都大学) R @ linguistics 2007/08/24 17 / 24
生産性
課題:コーパスから接辞の生産性を計算する
生産性の計算式(たとえば -ness の場合):
Pness =n1
N
ただしn1: -ness で終わる語のうち,ただ 1度だけ現れたものN: -ness をもつ語の総トークン数
作業順序:1 コーパスを読み込んで単語のリストにする2 調べたい接辞を含む語の頻度表をつくる3 生産性を計算
淺尾仁彦 (京都大学) R @ linguistics 2007/08/24 18 / 24
生産性
コーパスの読込
テキストファイルから一行ずつ読み込む> alice.text <- scan(choose.files(), what="char", sep="\n")
ダイアログ画面で alice.txtを選択
小文字で統一> alice.text <- tolower(alice.text)
「文字以外」で区切って単語のリストにする> alice.words <- unlist(strsplit(alice.text, "\\W"))
-ingで終わるものを検索して取り出す> ing.words <- grep("ing$", alice.words, value=T)
淺尾仁彦 (京都大学) R @ linguistics 2007/08/24 19 / 24
生産性
生産性の計算
頻度表にする> ing.freqlist <- table(ing.words)
降順でソート> ing.freqlist.sorted <- sort(ing.freqlist, decreasing=T)
総トークン数 (N)を数える> ing.token.freq <- sum(ing.freqlist.sorted)
ただ一度だけ現れた語の数 (n1)を数える> ing.hapax <-
length(ing.freqlist.sorted[ing.freqlist.sorted==1])
生産性を計算> ing.hapax / ing.token.freq
淺尾仁彦 (京都大学) R @ linguistics 2007/08/24 20 / 24
生産性
生産性の計算
…などということを毎回やる必要はない!
関数の読込> source(choose.files())
ダイアログ画面で productivity.Rを選択
データの読込> alice.text <- scan(choose.files(), what="char", sep="\n")
ダイアログ画面で alice.txtを選択
淺尾仁彦 (京都大学) R @ linguistics 2007/08/24 21 / 24
生産性
生産性の計算
使える関数:freq.list(), type.freq(), token.freq(), hapax(), productivity()
試してみてください> freq.list("oo", alice.text)
> productivity("ity$", alice.text)
淺尾仁彦 (京都大学) R @ linguistics 2007/08/24 22 / 24
おわりに
おわりに
おすすめのテキスト舟尾暢男・高浪洋平『データ解析環境「R」』工学社.
おすすめウェブサイトhttp://cwoweb2.bai.ne.jp/%7Ejgb11101/
舟尾氏のページ。http://aoki2.si.gunma-u.ac.jp/R/
青木先生のページ。http://www.okada.jp.org/RWiki/
Rについての wiki。
淺尾仁彦 (京都大学) R @ linguistics 2007/08/24 23 / 24
おわりに
ありがとうございました
淺尾仁彦 (京都大学) R @ linguistics 2007/08/24 24 / 24