我试图估计Heaps定律的常数。
我有以下数据集novels_colection
:
Number of novels DistinctWords WordOccurrences
1 1 13575 117795
2 1 34224 947652
3 1 40353 1146953
4 1 55392 1661664
5 1 60656 1968274
然后我构建下一个函数:
# Function for Heaps law
heaps <- function(K, n, B){
K*n^B
}
heaps(2,117795,.7) #Just to test it works
所以n = Word Occurrences
,K
和B
是应该是常量的值,以便找到我对不同词汇的预测。
我尝试了这个,但它给了我一个错误:
fitHeaps <- nls(DistinctWords ~ heaps(K,WordOccurrences,B),
data = novels_collection[,2:3],
start = list(K = .1, B = .1), trace = T)
错误= Error in numericDeriv(form[[3L]], names(ind), env) :
Missing value or an infinity produced when evaluating the model
我是如何解决此问题或方法以适应函数并获取K
和B
的值?
答案 0 :(得分:2)
如果您在y = K * n ^ B
的两边进行日志转换,则会获得log(y) = log(K) + B * log(n)
。这是log(y)
和log(n)
之间的线性关系,因此您可以使用线性回归模型来查找log(K)
和B
。
logy <- log(DistinctWords)
logn <- log(WordOccurrences)
fit <- lm(logy ~ logn)
para <- coef(fit) ## log(K) and B
para[1] <- exp(para[1]) ## K and B
答案 1 :(得分:1)
使用minpack.lm我们可以拟合非线性模型,但我想它会比对数变换变量上的线性模型更容易过度拟合(如哲源所做),但我们可以比较残差对某些数据集进行线性/非线性模型得到实证结果,这将是有趣的。
library(minpack.lm)
fitHeaps = nlsLM(DistinctWords ~ heaps(K, WordOccurrences, B),
data = novels_collection[,2:3],
start = list(K = .01, B = .01))
coef(fitHeaps)
# K B
# 5.0452566 0.6472176
plot(novels_collection$WordOccurrences, novels_collection$DistinctWords, pch=19)
lines(novels_collection$WordOccurrences, predict(fitHeaps, newdata = novels_collection[,2:3]), col='red')