Benford's Law for first digit
Benford's Law states that the probability that the first digit equals d
is approximately the logarithm of (1 + 1/d)
.
By plotting the expected frequencies, it will be clear that the digits 1,…,9 do not occur with equal frequency.
This exercise is part of the course
Fraud Detection in R
Exercise instructions
- Implement Benford's law as a function
benlaw
for the first digit using log base 10 . - Calculate the expected frequency for having 5 as a first digit.
- Create a dataframe with one column
digit
holding digits from 1 to 9, and one columnprobability
holding their respective probabilities under Benford's Law. - Submit to plot the expected frequencies for the digits 1, 2, …, 9 in a barplot.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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))