首位数字的 Benford 定律
Benford 定律指出,首位数字等于 d 的概率近似为 (1 + 1/d) 的对数。
通过绘制期望频率,您将看到数字 1,…,9 的出现频率并不相同。
本练习是课程的一部分
R 中的欺诈检测
练习说明
- 使用以 10 为底的对数,将 Benford 定律实现为首位数字的函数
benlaw。 - 计算首位数字为 5 的期望频率。
- 创建一个包含两列的数据框:
digit列为 1 到 9 的数字,probability列为这些数字在 Benford 定律下对应的概率。 - 提交以在条形图中绘制数字 1、2、…、9 的期望频率。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Implement Benford's Law for first digit
benlaw <- function(d) log10(___ + ___ / ___)
# Calculate expected frequency for d=5
benlaw(___)
# Create a dataframe of the 9 digits and their Benford's Law probabilities
df <- data.frame(digit = ___:___, probability = ___)
# Create barplot with expected frequencies
ggplot(df, aes(x = digit, y = probability)) +
geom_bar(stat = "identity", fill = "dodgerblue") +
xlab("First digit") + ylab("Expected frequency") +
scale_x_continuous(breaks = 1:9, labels = 1:9) +
ylim(0, 0.33) + theme(text = element_text(size = 25))