ComeçarComece de graça

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.

Este exercício faz parte do curso

Fraud Detection in R

Ver curso

Instruções do exercício

  • 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 column probability holding their respective probabilities under Benford's Law.
  • Submit to plot the expected frequencies for the digits 1, 2, …, 9 in a barplot.

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

# 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))
Editar e executar o código