-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.html
247 lines (229 loc) · 18.5 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<!DOCTYPE html>
<!-- Generated by pkgdown: do not edit by hand --><html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Benford Analysis for Data Validation and Forensic Analytics • benford.analysis</title>
<!-- jquery --><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script><!-- Bootstrap --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha256-916EbMg70RQy9LHiGkXzG8hSg9EdNy97GazNG/aiY1w=" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha256-U5ZEeKfGNOja007MMD3YBI0A3OSZOQbeG6z2f2Y0hu8=" crossorigin="anonymous"></script><!-- Font Awesome icons --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" integrity="sha256-eZrrJcwDc/3uDhsdt61sL2oOBY362qM3lon1gyExkL0=" crossorigin="anonymous">
<!-- clipboard.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.4/clipboard.min.js" integrity="sha256-FiZwavyI2V6+EXO1U+xzLG3IKldpiTFf3153ea9zikQ=" crossorigin="anonymous"></script><!-- sticky kit --><script src="https://cdnjs.cloudflare.com/ajax/libs/sticky-kit/1.1.3/sticky-kit.min.js" integrity="sha256-c4Rlo1ZozqTPE2RLuvbusY3+SU1pQaJC0TjuhygMipw=" crossorigin="anonymous"></script><!-- pkgdown --><link href="pkgdown.css" rel="stylesheet">
<script src="pkgdown.js"></script><meta property="og:title" content="Benford Analysis for Data Validation and Forensic Analytics">
<meta property="og:description" content="Provides tools that make it easier to validate data using Benford's Law.">
<meta name="twitter:card" content="summary">
<!-- mathjax --><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js" integrity="sha256-84DKXVJXs0/F8OTMzX4UR909+jtl4G7SPypPavF+GfA=" crossorigin="anonymous"></script><!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container template-article">
<header><div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<span class="navbar-brand">
<a class="navbar-link" href="index.html">benford.analysis</a>
<span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Released version">0.1.5</span>
</span>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="index.html">
<span class="fa fa-home fa-lg"></span>
</a>
</li>
<li>
<a href="reference/index.html">Reference</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="https://github.com/carloscinelli/benford.analysis">
<span class="fa fa-github fa-lg"></span>
</a>
</li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
<!--/.container -->
</div>
<!--/.navbar -->
</header><div class="row">
<div class="col-md-9 contents">
<div id="benford-analysis" class="section level2">
<h2 class="hasAnchor">
<a href="#benford-analysis" class="anchor"></a>benford.analysis <img src="tools/benford.analysis.png" height="140" width="120" align="right">
</h2>
<!-- README.md is generated from README.Rmd. Please edit that file -->
<p><a href="https://travis-ci.org/carloscinelli/benford.analysis"><img src="https://travis-ci.org/carloscinelli/benford.analysis.svg?branch=master" alt="Travis-CI Build Status"></a> <a href="https://ci.appveyor.com/project/carloscinelli/benford-analysis/branch/master"><img src="https://ci.appveyor.com/api/projects/status/igyn1737s67jqqnb/branch/master?svg=true" alt="Build status"></a> <a href="https://codecov.io/github/carloscinelli/benford.analysis?branch=master"><img src="https://img.shields.io/codecov/c/github/carloscinelli/benford.analysis/master.svg" alt="Coverage Status"></a> <a href="https://cran.r-project.org/package=benford.analysis"><embed src="http://www.r-pkg.org/badges/version/benford.analysis"></embed></a> <embed src="http://cranlogs.r-pkg.org/badges/benford.analysis"></embed></p>
<p>The Benford Analysis (<code>benford.analysis</code>) package provides tools that make it easier to validate data using Benford’s Law. The main purpose of the package is to identify suspicious data that need further verification.</p>
</div>
<div id="cran" class="section level2">
<h2 class="hasAnchor">
<a href="#cran" class="anchor"></a>CRAN</h2>
<p>You can install the package from CRAN by running:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb1-1" title="1"><span class="kw"><a href="https://www.rdocumentation.org/packages/utils/topics/install.packages">install.packages</a></span>(<span class="st">"benford.analysis"</span>)</a></code></pre></div>
</div>
<div id="how-to-install-the-development-version-from-github" class="section level2">
<h2 class="hasAnchor">
<a href="#how-to-install-the-development-version-from-github" class="anchor"></a>How to install the development version from GitHub</h2>
<p>To install the GitHub version you need to have the package <code>devtools</code> installed. Make sure to set the option <code>build_vignettes = TRUE</code> to compile the package vignette.</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb2-1" title="1"><span class="co"># install.packages("devtools") # run this to install the devtools package</span></a>
<a class="sourceLine" id="cb2-2" title="2">devtools<span class="op">::</span><span class="kw"><a href="https://www.rdocumentation.org/packages/devtools/topics/reexports">install_github</a></span>(<span class="st">"carloscinelli/benford.analysis"</span>, <span class="dt">build_vignettes =</span> <span class="ot">TRUE</span>)</a></code></pre></div>
</div>
<div id="example-usage" class="section level2">
<h2 class="hasAnchor">
<a href="#example-usage" class="anchor"></a>Example usage</h2>
<p>The <code>benford.analysis</code> package comes with 6 real datasets from Mark Nigrini’s book <a href="http://www.amazon.com/gp/product/B007KG9ZAI/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=B007KG9ZAI&linkCode=as2&tag=analreal-20">Benford’s Law: Applications for Forensic Accounting, Auditing, and Fraud Detection</a>.</p>
<p>Here we will give an example using 189.470 records from the corporate payments data. First we need to load the package and the data:</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb3-1" title="1"><span class="kw"><a href="https://www.rdocumentation.org/packages/base/topics/library">library</a></span>(benford.analysis) <span class="co"># loads package</span></a>
<a class="sourceLine" id="cb3-2" title="2"><span class="kw"><a href="https://www.rdocumentation.org/packages/utils/topics/data">data</a></span>(corporate.payment) <span class="co"># loads data</span></a></code></pre></div>
<p>Then to validade the data against Benford’s law you simply use the function <code>benford</code> in the appropriate column:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb4-1" title="1">bfd.cp <-<span class="st"> </span><span class="kw"><a href="reference/benford.html">benford</a></span>(corporate.payment<span class="op">$</span>Amount)</a></code></pre></div>
<p>The command above created an object of class “Benford” with the results for the analysis using the first two significant digits. You can choose a different number of digits changing the <code>number.of.digits</code> paramater. For more information and parameters see <code><a href="reference/benford.html">?benford</a></code>:</p>
<p>Let’s check the main plots of the analysis:</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb5-1" title="1"><span class="kw"><a href="https://www.rdocumentation.org/packages/graphics/topics/plot">plot</a></span>(bfd.cp)</a></code></pre></div>
<p><img src="tools/unnamed-chunk-6-1.png" width="1152"></p>
<p>The original data is in blue and the expected frequency according to Benford’s law is in red. For instance, in our example, the first plot shows that the data do have a tendency to follow Benford’s law, but also that there is a clear discrepancy at 50.</p>
<p>You can print the main results of the analysis:</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb6-1" title="1">bfd.cp</a>
<a class="sourceLine" id="cb6-2" title="2"><span class="co">#> </span></a>
<a class="sourceLine" id="cb6-3" title="3"><span class="co">#> Benford object:</span></a>
<a class="sourceLine" id="cb6-4" title="4"><span class="co">#> </span></a>
<a class="sourceLine" id="cb6-5" title="5"><span class="co">#> Data: corporate.payment$Amount </span></a>
<a class="sourceLine" id="cb6-6" title="6"><span class="co">#> Number of observations used = 185083 </span></a>
<a class="sourceLine" id="cb6-7" title="7"><span class="co">#> Number of obs. for second order = 65504 </span></a>
<a class="sourceLine" id="cb6-8" title="8"><span class="co">#> First digits analysed = 2</span></a>
<a class="sourceLine" id="cb6-9" title="9"><span class="co">#> </span></a>
<a class="sourceLine" id="cb6-10" title="10"><span class="co">#> Mantissa: </span></a>
<a class="sourceLine" id="cb6-11" title="11"><span class="co">#> </span></a>
<a class="sourceLine" id="cb6-12" title="12"><span class="co">#> Statistic Value</span></a>
<a class="sourceLine" id="cb6-13" title="13"><span class="co">#> Mean 0.496</span></a>
<a class="sourceLine" id="cb6-14" title="14"><span class="co">#> Var 0.092</span></a>
<a class="sourceLine" id="cb6-15" title="15"><span class="co">#> Ex.Kurtosis -1.257</span></a>
<a class="sourceLine" id="cb6-16" title="16"><span class="co">#> Skewness -0.002</span></a>
<a class="sourceLine" id="cb6-17" title="17"><span class="co">#> </span></a>
<a class="sourceLine" id="cb6-18" title="18"><span class="co">#> </span></a>
<a class="sourceLine" id="cb6-19" title="19"><span class="co">#> The 5 largest deviations: </span></a>
<a class="sourceLine" id="cb6-20" title="20"><span class="co">#> </span></a>
<a class="sourceLine" id="cb6-21" title="21"><span class="co">#> digits absolute.diff</span></a>
<a class="sourceLine" id="cb6-22" title="22"><span class="co">#> 1 50 5938.25</span></a>
<a class="sourceLine" id="cb6-23" title="23"><span class="co">#> 2 11 3331.98</span></a>
<a class="sourceLine" id="cb6-24" title="24"><span class="co">#> 3 10 2811.92</span></a>
<a class="sourceLine" id="cb6-25" title="25"><span class="co">#> 4 14 1043.68</span></a>
<a class="sourceLine" id="cb6-26" title="26"><span class="co">#> 5 98 889.95</span></a>
<a class="sourceLine" id="cb6-27" title="27"><span class="co">#> </span></a>
<a class="sourceLine" id="cb6-28" title="28"><span class="co">#> Stats:</span></a>
<a class="sourceLine" id="cb6-29" title="29"><span class="co">#> </span></a>
<a class="sourceLine" id="cb6-30" title="30"><span class="co">#> Pearson's Chi-squared test</span></a>
<a class="sourceLine" id="cb6-31" title="31"><span class="co">#> </span></a>
<a class="sourceLine" id="cb6-32" title="32"><span class="co">#> data: corporate.payment$Amount</span></a>
<a class="sourceLine" id="cb6-33" title="33"><span class="co">#> X-squared = 32094, df = 89, p-value < 2.2e-16</span></a>
<a class="sourceLine" id="cb6-34" title="34"><span class="co">#> </span></a>
<a class="sourceLine" id="cb6-35" title="35"><span class="co">#> </span></a>
<a class="sourceLine" id="cb6-36" title="36"><span class="co">#> Mantissa Arc Test</span></a>
<a class="sourceLine" id="cb6-37" title="37"><span class="co">#> </span></a>
<a class="sourceLine" id="cb6-38" title="38"><span class="co">#> data: corporate.payment$Amount</span></a>
<a class="sourceLine" id="cb6-39" title="39"><span class="co">#> L2 = 0.0039958, df = 2, p-value < 2.2e-16</span></a>
<a class="sourceLine" id="cb6-40" title="40"><span class="co">#> </span></a>
<a class="sourceLine" id="cb6-41" title="41"><span class="co">#> </span></a>
<a class="sourceLine" id="cb6-42" title="42"><span class="co">#> Kolmogorov-Smirnov test</span></a>
<a class="sourceLine" id="cb6-43" title="43"><span class="co">#> </span></a>
<a class="sourceLine" id="cb6-44" title="44"><span class="co">#> data: corporate.payment$Amount</span></a>
<a class="sourceLine" id="cb6-45" title="45"><span class="co">#> D = 0.033195, critical value = 0.0031612, alpha = 0.0500000</span></a>
<a class="sourceLine" id="cb6-46" title="46"><span class="co">#> </span></a>
<a class="sourceLine" id="cb6-47" title="47"><span class="co">#> Mean Absolute Deviation (MAD): 0.002336614</span></a>
<a class="sourceLine" id="cb6-48" title="48"><span class="co">#> MAD Conformity - Nigrini (2012): Nonconformity</span></a>
<a class="sourceLine" id="cb6-49" title="49"><span class="co">#> Distortion Factor: -1.065467</span></a>
<a class="sourceLine" id="cb6-50" title="50"><span class="co">#> </span></a>
<a class="sourceLine" id="cb6-51" title="51"><span class="co">#> Remember: Real data will never conform perfectly to Benford's Law. You should not focus on p-values!</span></a></code></pre></div>
<p>The print method first shows the general information of the analysis, like the name of the data used, the number of observations used and how many significant digits were analyzed.</p>
<p>After that you have the main statistics of the log mantissa of the data. If the data follows Benford’s Law, the numbers should be close to:</p>
<table class="table table">
<thead><tr class="header">
<th>Statistic</th>
<th>Value</th>
</tr></thead>
<tbody>
<tr class="odd">
<td>Mean</td>
<td>0.5</td>
</tr>
<tr class="even">
<td>Variance</td>
<td>1/12 (0.08333…)</td>
</tr>
<tr class="odd">
<td>Ex. Kurtosis</td>
<td>-1.2</td>
</tr>
<tr class="even">
<td>Skewness</td>
<td>0</td>
</tr>
</tbody>
</table>
<p>Printing also shows the 5 largest discrepancies. Notice that, as we had seen on the plot, the highest deviation is 50. These deviations are good candidates for closer inspections. It also shows the results of statistical tests like the Chi-squared test and the Mantissa Arc test.</p>
<p>The package provides some helper functions to further investigate the data. For example, you can easily extract the observations with the largest discrepancies by using the <code>getSuspects</code> function.</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb7-1" title="1">suspects <-<span class="st"> </span><span class="kw"><a href="reference/getSuspects.html">getSuspects</a></span>(bfd.cp, corporate.payment)</a>
<a class="sourceLine" id="cb7-2" title="2">suspects</a>
<a class="sourceLine" id="cb7-3" title="3"><span class="co">#> VendorNum Date InvNum Amount</span></a>
<a class="sourceLine" id="cb7-4" title="4"><span class="co">#> 1: 2001 2010-01-02 3822J10 50.38</span></a>
<a class="sourceLine" id="cb7-5" title="5"><span class="co">#> 2: 2001 2010-01-07 100107-2 1166.29</span></a>
<a class="sourceLine" id="cb7-6" title="6"><span class="co">#> 3: 2001 2010-01-08 11210084007 1171.45</span></a>
<a class="sourceLine" id="cb7-7" title="7"><span class="co">#> 4: 2001 2010-01-08 1585J10 50.42</span></a>
<a class="sourceLine" id="cb7-8" title="8"><span class="co">#> 5: 2001 2010-01-08 4733J10 113.34</span></a>
<a class="sourceLine" id="cb7-9" title="9"><span class="co">#> --- </span></a>
<a class="sourceLine" id="cb7-10" title="10"><span class="co">#> 17852: 52867 2010-07-01 270358343233 11.58</span></a>
<a class="sourceLine" id="cb7-11" title="11"><span class="co">#> 17853: 52870 2010-02-01 270682253025 11.20</span></a>
<a class="sourceLine" id="cb7-12" title="12"><span class="co">#> 17854: 52904 2010-06-01 271866383919 50.15</span></a>
<a class="sourceLine" id="cb7-13" title="13"><span class="co">#> 17855: 52911 2010-02-01 270957401515 11.20</span></a>
<a class="sourceLine" id="cb7-14" title="14"><span class="co">#> 17856: 52934 2010-02-01 271745237617 11.88</span></a></code></pre></div>
<p>More information can be found on the help documentation and examples. The vignette will be ready soon.</p>
</div>
</div>
<div class="col-md-3 hidden-xs hidden-sm" id="sidebar">
<div class="links">
<h2>Links</h2>
<ul class="list-unstyled">
<li>Download from CRAN at <br><a href="https://cloud.r-project.org/package=benford.analysis">https://cloud.r-project.org/package=benford.analysis</a>
</li>
<li>Browse source code at <br><a href="https://github.com/carloscinelli/benford.analysis">https://github.com/carloscinelli/benford.analysis</a>
</li>
<li>Report a bug at <br><a href="http://github.com/carloscinelli/benford.analysis/issues">http://github.com/carloscinelli/benford.analysis/issues</a>
</li>
</ul>
</div>
<div class="license">
<h2>License</h2>
<ul class="list-unstyled">
<li><a href="https://www.r-project.org/Licenses/GPL-3">GPL-3</a></li>
</ul>
</div>
<div class="developers">
<h2>Developers</h2>
<ul class="list-unstyled">
<li>Carlos Cinelli <br><small class="roles"> Maintainer </small> </li>
</ul>
</div>
</div>
</div>
<footer><div class="copyright">
<p>Developed by Carlos Cinelli.</p>
</div>
<div class="pkgdown">
<p>Site built with <a href="https://pkgdown.r-lib.org/">pkgdown</a> 1.3.0.</p>
</div>
</footer>
</div>
</body>
</html>