Example of Benford Tests in R:

Resources:

library(benford.analysis)
## Warning: package 'benford.analysis' was built under R version 3.2.2
example(benford.analysis)
## 
## bnfrd.> data(corporate.payment) #gets data
## 
## bnfrd.> cp <- benford(corporate.payment$Amount, 2, sign="both") #generates benford object
## 
## bnfrd.> cp #prints
## 
## Benford object:
##  
## Data: corporate.payment$Amount 
## Number of observations used = 189347 
## Number of obs. for second order = 65853 
## First digits analysed = 2
## 
## Mantissa: 
## 
##    Statistic    Value
##         Mean  0.49573
##          Var  0.09191
##  Ex.Kurtosis -1.25840
##     Skewness -0.00035
## 
## 
## The 5 largest deviations: 
## 
##   digits absolute.diff
## 1     50       5929.58
## 2     11       3496.85
## 3     10       2835.42
## 4     14       1110.45
## 5     98        885.15
## 
## Stats:
## 
##  Pearson's Chi-squared test
## 
## data:  corporate.payment$Amount
## X-squared = 31890, df = 89, p-value < 2.2e-16
## 
## 
##  Mantissa Arc Test
## 
## data:  corporate.payment$Amount
## L2 = 0.0041627, df = 2, p-value < 2.2e-16
## 
## Mean Absolute Deviation: 0.002336108
## Distortion Factor: 0.6338313
## 
## Remember: Real data will never conform perfectly to Benford's Law. You should not focus on p-values!
## bnfrd.> plot(cp) #plots

## 
## bnfrd.> head(suspectsTable(cp),10) #prints the digits by decreasing order of discrepancies
##     digits absolute.diff
##  1:     50     5929.5833
##  2:     11     3496.8494
##  3:     10     2835.4192
##  4:     14     1110.4465
##  5:     98      885.1466
##  6:     99      775.5372
##  7:     90      729.3459
##  8:     92      702.9929
##  9:     25      651.7883
## 10:     41      649.5985
## 
## bnfrd.> #gets observations of the 2 most suspicious groups
## bnfrd.> suspects <- getSuspects(cp, corporate.payment, how.many=2)
## 
## bnfrd.> duplicatesTable(cp) #prints the duplicates by decreasing order
##         number duplicates
##     1:   50.00       6030
##     2: 1153.35       2279
##     3: 1083.45       1190
##     4:  150.00       1116
##     5:  988.35       1025
##    ---                   
## 65850:   38.22          1
## 65851:    5.36          1
## 65852:   28.49          1
## 65853:  424.28          1
## 65854:  108.97          1
## 
## bnfrd.> #gets the observations of the 2 values with most duplicates
## bnfrd.> duplicates <- getDuplicates(cp, corporate.payment,how.many=2)
## 
## bnfrd.> MAD(cp) #gets the Mean Absolute Deviation
## [1] 0.002336108
## 
## bnfrd.> chisq(cp) #gets the Chi-squared test
## 
##  Pearson's Chi-squared test
## 
## data:  corporate.payment$Amount
## X-squared = 31890, df = 89, p-value < 2.2e-16
## 
## 
## bnfrd.> #gets observations starting with 50 or 99
## bnfrd.> digits_50_and_99 <- getDigits(cp, corporate.payment, digits=c(50, 99))