r - 如何绘制R中的度分布

标签 r plot distribution power-law

我想知道绘制度数分布的脚本输出是否正确。

所以脚本是(我所有顶点的度数的向量存储在 x 中):

x 是

x
 [1] 7 9 8 5 6 2 8 9 7 5 2 4 6 9 2 6 10 8 

x 是某个网络顶点的度数 - 比如顶点 1 的度数为 7,顶点 2 的度数为 9 等等 x <- v2 总结(x)

library(igraph)
split.screen(c(1,2))
screen(1)
plot (tabulate(x), log = "xy", ylab = "Frequency (log scale)", xlab = "Degree (log scale)", main = "Log-log plot of degree distribution")
screen(2)
y <- (length(x) - rank(x, ties.method = "first"))/length(x)
plot(x, y, log = "xy", ylab = "Fraction with min. degree k (log scale)", xlab = "Degree (k) (log scale)", main = "Cumulative log-log plot of degree distribution")
close.screen(all = TRUE)
power.law.fit(x, xmin = 50)

我的问题是对数-对数图似乎不正确 - 例如,我总体上有 8 次“7”度数,所以对数-对数图上的这一点不应该变成 0.845 (log 7)/0.903 (log(8) 如 (x/y)?

此外,有人能告诉我如何将线(对数对数尺度上的幂律)拟合到屏幕 2 中的绘图吗?

最佳答案

我不熟悉 igraph 包,所以你能帮忙解决那个特定的包吗?但是,这里有一些用于在对数对数图上绘制分布的代码。首先是一些数据:

set.seed(1)
x = ceiling(rlnorm(1000, 4))

然后我们需要重新排列 以获得逆 CDF:

occur = as.vector(table(x))
occur = occur/sum(occur)
p = occur/sum(occur)
y = rev(cumsum(rev(p)))
x = as.numeric(names(table(x)))
plot(x, y, log="xy", type="l")

给予

enter image description here

关于你的拟合问题,我认为出现差异是因为 igraph 使用 MLE 而你正在做简单的线性回归(不推荐)。


作为一个插件,我已经开始研究 package用于拟合和绘制幂律。所以,使用这个包你会得到:

library(poweRlaw)

##Create a displ object
m = displ$new(x)
##Estimate the cut-off
estimate_xmin(m)
m$setXmin(105); m$setPars(2.644)

##Plot the data and the PL line
plot(m)
lines(m, col=2)

enter image description here

关于r - 如何绘制R中的度分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14476426/

相关文章:

c++ - 高效的 boost 分配使用

iphone - 将 iPhone 存档交给经销商

matlab - 生成2个随机数

r - 在 data.frame 结构中绑定(bind)和匹配

r - 在 R 中绘制多条线(数据系列),每条线具有唯一的颜色

r - 将列表的每个元素与字符串连接起来

algorithm - 二分图中的最佳匹配(例如,将标签与图上的点相关联)

python - 在matplotlib中更改虚线中的破折号间距

r - 条件下的数据分组

r - 函数总是给我一个尾随的 `NULL`