Kopiuj
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE nazwa (@threshold float = 0.1)
as
begin
-- Use Benford law to get the potential fraud vendors.
exec sp_execute_external_script
@language = N'R',
@script = N'
library(reshape2);
dd = dcast(InputDataSet, VendorNumber ~ Digits, value.var="Freq");
OutputDataSet = cbind(
dd,
apply(dd[,-1], 1, function(xx) chisq.test(xx, p=(log(1+1/(1:9))/log(10)))$p.value)); ## Equation using Benford law
colnames(OutputDataSet) <- c("VendorNumber", "Digit1", "Digit2", "Digit3", "Digit4",
"Digit5", "Digit6", "Digit7", "Digit8", "Digit9", "Pvalue");
OutputDataSet <- subset(OutputDataSet, Pvalue < threshold);
',
@input_data_1 = N'
select VendorNumber, Digits, Freq
from [BenfordFraud].VendorInvoiceDigits(default)
order by VendorNumber asc, Digits asc;
',
@params = N'@threshold float',
@threshold = @threshold
with result sets (( VendorNumber varchar(10),
Digit1 int, Digit2 int, Digit3 int, Digit4 int, Digit5 int, Digit6 int, Digit7 int, Digit8 int, Digit9 int,
Pvalue float));
end;