Lecture4: Manipulate関数(その2)

getRelativeFreqMtx.Rを読み込む

source("getRelativeFreqMtx.R")
freqMtx<-getRelativeFreqMtx("osaka-u.txt")
subMtx <-as.table(freqMtx[1:20,2])
names(subMtx) <-freqMtx[1:20,]$term

前回の復習

manipulate package

library(manipulate)

プロットサイズの選択

slider関数: 最小=1,最大=10, 初期値3

manipulate({
  plot(0,0,type="n")
  text(0,0, "MINO",cex=mySize,col=myColors)
}, 
  myColors=picker("red", "yellow", "green", "violet", "orange", "blue", "pink", "cyan"),
mySize=slider(1,10,initial=3))

実践編: 単語頻度数分布

実践編1(barplot): 色の選択(初期値の色=“pink”)

title="Word Frequency Distribution"
xlabel="Word"
ylabel="Frequency"
manipulate(barplot(subMtx,col=myColors,main=title, xlab=xlabel, ylab=ylabel,las=3), myColors=picker("red", "yellow", "green", "violet", "orange", "blue", "pink", "cyan" ,initial="pink") )

実践編2(barplot): スライダーの追加

スライダー:最小1, 最大20, 初期値10

manipulate(barplot(subMtx,col=myColors,main=title, xlab=xlabel, ylab=ylabel, xlim=c(0,x.max),las=3), myColors=picker("red", "yellow", "green", "violet", "orange", "blue", "pink", "cyan",initial="pink") , x.max=slider(1,20, initial=10))

実践編2(おまけ): スライダー

スライダー:最小1, 最大20, 初期値10

manipulate(barplot(subMtx,col=myColors,main=title, xlab=xlabel, ylab=ylabel, xlim=c(x.min,x.max),las=3), myColors=picker("red", "yellow", "green", "violet", "orange", "blue", "pink", "cyan",initial="pink") , x.min=slider(1,10, initial=1),x.max=slider(11,20, initial=20))

単語長分布

単語の文字数を調べる

freqMtx$term[1]
## [1] the
## 246 Levels: 18th 1931 2003 2007 a ability academic accepted ... your
as.character(freqMtx$term[1])
## [1] "the"
nchar(as.character(freqMtx$term[1]))
## [1] 3

単語長度数集計:table

ncharAll <- nchar(as.character(freqMtx$term))

実習:単語長度数を集計してください

実行例

charlenF
## ncharAll
##  1  2  3  4  5  6  7  8  9 10 11 12 13 14 
##  4 14 19 34 33 25 30 27 28 13 11  3  4  1

単語長分布(Types)

title="Word Length Frequency Distribution (Types)"
xlabel="Word Length"
ylabel="Frequency"
xmax=length(charlenF)
ymax=max(charlenF)
plot(charlenF, type="b",pch=8,col="orange",xlim=c(1,xmax),ylim=c(1,ymax),main=title, xlab=xlabel, ylab=ylabel)

単語出現頻度分布

単語出現頻度分布

title="Word Frequency Distribution"
xlabel="Rank"
ylabel="Frequency"

rank=seq(1:dim(freqMtx)[1])
plot(rank,freqMtx[,2], pch=8, col="darkgreen", main=title, xlab=xlabel, ylab=ylabel)

両軸対数表示:log=“xy”

plot(rank,freqMtx$raw, xlim=c(1,nrow(freqMtx)), ylim=c(1,50),log="xy",pch=8, col="darkgreen", main=title, xlab=xlabel, ylab=ylabel)

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))

zipfの結果抜粋

##  [1] 33.000000 19.621917 14.476814 11.667262  9.869302  8.607965  7.668147
##  [8]  6.937395  6.350853  5.868322

グラフ図: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

Zipfs理論値の線と実際の値(*プロット)の色を選べるように変えてください。 色の種類は5種類程度 Zipfs理論:初期値の色は“red”を指定 Zipfs理論:初期値の色は“darkgreen”を指定

picker()関数

alt text

alt text

練習1

Kの値を変化させる

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

K=res[1,2]
A=0.75
K
rank <- seq(1:dim(freqMtx)[1])

manipulate(
  {
    zipf <- unlist(lapply(rank, function(r) constK/r^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)
)

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

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

alt text

alt text