2012-10-21 84 views
1

我有齊普夫定律分佈的數據集,現在我希望它與標準zipf圖比較,這是我的R代碼:繪製齊普夫分佈

f <- read.table('/myfile.txt',sep='\t',header=T) 
attach(f) 
fs <- f[order(cnt), ] 
detach(f) 
n = 1:dim(fs)[1] 
plot(fs[,2]~n) 

現在我想將其與在zipf比較同樣的情節,我如何在R中做到這一點?

+3

如何處理類似 'N = 10; S = 0.5; y = 1:N proby = dzipf(y,N = N,s = s) lines(y,proby,type =「l」) ' 當然,您必須調整參數以適合你的情況 –

+0

是的,它必須爲我工作,但我不能在'R'運行它,我得到'錯誤:無法找到函數「dzipf」' –

+1

它是'VGAM'庫中,你必須首先附加。 –

回答

6

VGAM軟件包具有估算Zipf分佈指數的功能。您可能要策劃你對分配一個最適合的估計密度:

plot(cntdens <- table(f[['cnt']])/length(f[['cnt']]), 
     xlim=range(f[['cnt']]), ylim=c(0, 0.8) ) 

# And then plot the theoretic distribution for the VGAM fit 
# ... extending the example on ?VGAM::zipf: 

require(VGAM) 
zdata <- data.frame(y=1:max(f[['cnt']]), ofreq= table(f[['cnt']])) 
fit = vglm(y ~ 1, zipf, zdata, trace = TRUE, weight = ofreq, crit = "coef") 
proby = dzipf(1:max(f[['cnt']]), N =max(f[['cnt']]), s = Coef(fit)) 
points((1:5)+0.05, proby, col="red") 

下面是一個使用在dzipf頁的例子中,數據的程序? enter image description here

+0

我安裝'zipfR'包和'lobrary(zipfR)',然後我運行你的代碼但是我得到了'錯誤:找不到函數「dzipf」'。 –

+2

那是因爲...因爲我的代碼暗示...我的會話中的dzipf來自VGAM包,而不是zipfR包。 –