Lecture5: Manipulate関数(その3)

準備

getRelativeFreqMtx.Rを読み込む

source("getRelativeFreqMtx.R")
freqMtx<-getRelativeFreqMtx("osaka-u.txt")

lapply():listへの関数の適用

tmp <- c("MINO", "KURODA", "TAGI")
paste(tmp, "*")
## [1] "MINO *"   "KURODA *" "TAGI *"
lapply(tmp,paste,"*")
## [[1]]
## [1] "MINO *"
## 
## [[2]]
## [1] "KURODA *"
## 
## [[3]]
## [1] "TAGI *"
plot(0,0, xlim=c(-1,1),ylim=c(-1,1))
text(0,0.5,tmp[1],col="red",cex=1.5)

実習

“KURODA”をx=0.5, y=-0.5の位置に配置

“TAGI”をx=-0.5, y=-0.5の位置に配置

manipulate package

library(manipulate)

実習

manilupateで“MINO”のx位置を-1.0<=x<=1.0まで、0.1刻みで移動できるようにしてください

manipulate({
  plot(0,0, xlim=c(-1,1),ylim=c(-1,1))
text(x,0.5,tmp[1],col="red",cex=1.5)
text(0.5,-0.5,tmp[2],col="blue",cex=1.5)
text(-0.5,-0.5,tmp[3],col="green",cex=1.5)
},
  x=slider(-1,1,initial=0, step=0.1)
)

Zipf’sの法則

\[Frequency=\frac{K}{Rank^A} \] K,A: 定数

K=freqMtx[1,2]
A=0.75

rank <- seq(1:dim(freqMtx)[1])
zipf <- unlist(lapply(rank, function(r) K/r^A))

補足: “unlist(lapply(rank, function(r) K/r^A))”

\[Frequency(r1)=\frac{K}{r1^A} \]

freqMtx[1,]
##     term raw   relative
## 211  the  33 0.06470588
K=freqMtx[1,2]
A=0.75
r1=rank[1]
K/r1^A
## [1] 33

リスト内の要素すべてに適用

res<-lapply(rank, function(r) K/r^A)
head(res)
## [[1]]
## [1] 33
## 
## [[2]]
## [1] 19.62192
## 
## [[3]]
## [1] 14.47681
## 
## [[4]]
## [1] 11.66726
## 
## [[5]]
## [1] 9.869302
## 
## [[6]]
## [1] 8.607965

アンリスト

unlist(res)

もっと完結な表現

zipf <- K/rank^A

グラフ図:Zipf’sの理論式

title="Zipf's Law"
xlabel="Rank"
ylabel="Frequency"
plot(zipf, log="xy", type="l",col="red" ,
xlim=c(1,nrow(freqMtx)),ylim=c(1,50),main=title, xlab=xlabel, ylab=ylabel)

頻度散布図&Zipf’sの理論式の重ね書き

par(new=T)
plot(rank,freqMtx[,2], xlim=c(1,nrow(freqMtx)), ylim=c(1,50),log="xy",pch=8, col="darkgreen", main=title, xlab=xlabel, ylab=ylabel)

凡例をつける: legend

配置:“bottomright”, “bottom”, “bottomleft”, “left”, “topleft”, “top”, “topright”, “right”, “center” ラベル lty: 線の種類 pch: プロットの種類

legend("topright",c("Frequency","Zipf's law"),lty=c(NA,1),pch=c(8,NA),col=c("darkgreen","red"))

グラフ図

練習1

Kの値を変化させる

title="Zipf's Law"
xlabel="Rank"
ylabel="Frequency"

A=0.75
rank <- seq(1:dim(freqMtx)[1])

manipulate(
  {
    zipf <- constK/rank^A
    plot(zipf, log="xy", type="l",col="red" ,
xlim=c(1,nrow(freqMtx)),ylim=c(1,50),main=title, xlab=xlabel, ylab=ylabel)
    par(new=T)
    plot(rank,freqMtx[,2], xlim=c(1,nrow(freqMtx)), ylim=c(1,50),log="xy",pch=8, col="darkgreen", main=title, xlab=xlabel, ylab=ylabel)
    legend("topright",c("Frequency","Zipf's law"),lty=c(NA,1),pch=c(8,NA),col=c(col="darkgreen",col="red"))
    text(20, 50, "Frequency=K/Rank^A")
    text(20, 40, paste("K=", constK))
    text(20, 30, paste("A=", A))
  }
, constK=slider(10,100, initial=freqMtx[1,2],step=2)
)

実習:練習1に追加して、定数Aの値を変化させる

スライダーのステップは0.05に設定

alt text

alt text

同一ディレクトリの複数フォルダから出現単語行列を作成する

ディレクトリ名

dirName <- "testData"

指定ディレクトリのファイル一覧を取得

files <- list.files(dirName)
files
## [1] "test1.txt" "test2.txt" "test3.txt"

作業ディレクトリからのファイルの相対参照パスを作成

filesDir <- unlist(lapply(dirName, paste, files, sep = "/"))
filesDir
## [1] "testData/test1.txt" "testData/test2.txt" "testData/test3.txt"

ファイルリストからの素頻度表作成

freqLst <- lapply(filesDir, getRelativeFreqMtx)
freqLst
## [[1]]
##   term raw  relative
## 3    c  13 0.4814815
## 4    e   7 0.2592593
## 2    b   4 0.1481481
## 1    a   3 0.1111111
## 
## [[2]]
##   term raw   relative
## 5    f  11 0.40740741
## 6    g   7 0.25925926
## 2    b   4 0.14814815
## 1    a   2 0.07407407
## 3    c   2 0.07407407
## 4    e   1 0.03703704
## 
## [[3]]
##   term raw   relative
## 5    f   9 0.33333333
## 6    g   7 0.25925926
## 7    h   4 0.14814815
## 2    c   3 0.11111111
## 1    a   2 0.07407407
## 3    d   1 0.03703704
## 4    e   1 0.03703704

条件文: if

getName <- function(co){
  tmp <- c("MINO", "KURODA", "TAGI")
  ans=""
  if(co=="red"){ans<-tmp[1]
  }else if(co=="blue"){ans<-tmp[2]
  }else ans<-tmp[3]
  ans
}

こたえは?

getName("blue")

こたえは?

getName("purple")

実習(時間があまれば…)

getRelativeFreqMtxの関数を改造して、引数にRaw Freq./Relative Freq.のどちらかを選択するオプションを追加し、Raw/Relativeだけの結果を表示させる。