绘制Zipf分布

Nav*_*adi 1 math r

我有一个zipf法分布的数据集,现在我想将它与标准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)
Run Code Online (Sandbox Code Playgroud)

现在我想zipf在同一个情节中对它进行比较,我该怎么做R呢?

42-*_*42- 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")
Run Code Online (Sandbox Code Playgroud)

下面是使用?dzipf页面上示例中的数据的过程. 在此输入图像描述

  • 那是因为...正如我的代码所暗示的......我的会话中的dzipf来自VGAM包,而不是zipfR包. (2认同)