source("getRelativeFreqMtx.R")
freqMtx<-getRelativeFreqMtx("osaka-u.txt")
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)
library(manipulate)
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)
)
\[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))
\[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
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)
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)
配置:“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"))
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)
)
スライダーのステップは0.05に設定
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
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")