gpt4 book ai didi

r - 为什么 'curve' 与 R 中的 'lines' 和 'points' 如此不同?

转载 作者:行者123 更新时间:2023-12-02 01:54:49 32 4
gpt4 key购买 nike

我想用离散广义贝塔分布(DGBD)来拟合频率数据。

数据如下:

freq = c(1116, 2067, 137 ,  124, 643,  2042, 55  ,47186,  7504, 1488, 211,   1608,   
3517 , 7 , 896 , 378, 17 ,3098, 164977 , 601 , 196, 637, 149 , 44,2 , 1801, 882 , 636,5184, 1851, 776 , 343 , 851, 33 ,4011, 209, 715 ,
937 , 20, 6922, 2028 , 23, 3045 , 16 , 334, 31 , 2)

Rank = rank(-freq, ties.method = c("first") )
p = freq/sum(freq)

获取日志表单

log.f = log(freq)
log.p = log(p)
log.rank = log(Rank)
log.inverse.rank = log(length(Rank)+1-Rank)

离散广义 beta 分布的线性回归

co=coef(lm(log.p~log.inverse.rank + log.rank))
zmf = function(x) exp(co[[1]]+ co[[2]]*log(length(x)+1-x) + co[[3]]*log(x))

绘图

plot(p~Rank, xlim = c(1, 80), log = "xy",xlab = "Rank (log)", ylab = "Probability (log)")
curve(zmf, col="blue", add = T)
xx=c(1:length(Rank))
lines(zmf(xx)~xx, col = "red")
points(zmf(xx)~xx, col = "purple")

enter image description here

图 1. 情节如下

我的问题是演示结果的正确方法是什么?直线(点)还是曲线?

更新:

虽然我还没有弄清楚底层逻辑,但是找到了解决方案:

@Frank 提醒我注意在曲线中设置 n 长度的技巧。它解决了问题。因此,当我们尝试拟合原始数据时,曲线中的 n 是必要的。尽管在很多情况下,n 会被忽略。

plot(p~Rank, log = "xy",xlab = "Rank (log)", ylab = "Probability (log)")
curve(zmf, col="blue", add = T, n = length(Rank)) # set the the number of x values at which to evaluate.

enter image description here

​图2曲线的正确使用方法:指定'n'

最佳答案

您需要在此处指定n的原因是您的函数依赖于length(x)!

zmf = function(x) exp(co[[1]]+ co[[2]]*log(length(x)+1-x) + co[[3]]*log(x))
^^^^^^^^^

这里,curve 提供给函数的 x 的长度是 n!

如果您坚持使用默认的 n=101 但使用向量 xx 提供您的 linepoint,那么这就是您的绘图长度为 101 的 :

plot(p~Rank, xlim = c(1,80), log = "xy",xlab = "Rank (log)", ylab = "Probability (log)")
curve(zmf, col="blue", add = T)
xx=seq(1,length(Rank),length.out=101)
lines(zmf(xx)~xx, col = "red")
points(zmf(xx)~xx, col = "purple")

enter image description here

既不是巫术也不是 bug ! :)

关于r - 为什么 'curve' 与 R 中的 'lines' 和 'points' 如此不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22446006/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com