Detect fraudulent transactions-data preliminary exploration

Source: Internet
Author: User

# 4.2.1 Loading data to R
Library (DMWR)
# # Loading Required Package:lattice
# # Loading Required Package:grid
Data (Sales)
Head (Sales)
# # ID Prod Quant Val Insp
# 1 v1 p1 182 1665 unkn
# 2 v2 p1 3072 8780 unkn
# 3 v3 p1 20393 76990 unkn
# # 4 v4 P1 1100 unkn
# 5 v3 p1 6164 20260 unkn
# 6 v5 P2 104 1155 Unkn
# 4.2.2. Explore data sets
Summary (Sales)
# # ID Prod Quant Val
# # v431:10159 p1125:3923 Min.   : 1.00e+02 Min. : 1005
# # v54:6017 p3774:1824 1st qu.:1.07e+02 1st Qu.: 1345
# # v426:3902 p1437:1720 median:1.68e+02 median:2675
# # v1679:3016 p1917:1702 mean:8.44e+03 mean:14617
# # v1085:3001 p4089:1598 3rd qu.:7.38e+02 3rd Qu.: 8680
# # v1183:2642 p2742:1519 Max.   : 4.74e+08 Max. : 4642955
# # (Other): 372409 (Other): 388860 na ' s:13842 na ' s:1182
# # Insp
# # ok:14462
# # unkn:385414
# # FRAUD:1270
##
##
##
##
Library (HMISC)
# # Loading Required Package:survival
# # Loading Required Package:splines
# # Loading Required Package:formula
##
# # Attaching package: ' Hmisc '
##
# # The following objects are masked from ' package:base ':
##
# # Format.pval, round. Posixt, trunc. Posixt, Units
Describe (sales)
# # Sales
##
# # 5 Variables 401146 observations
## ---------------------------------------------------------------------------
# # ID
# # N Missing Unique
# 401146 0 6016
##
# # LOWEST:V1 v2 V3 v4 v5
# # highest:v6066 v6067 v6068 v6069 v6070
## ---------------------------------------------------------------------------
# # Prod
# # N Missing Unique
# 401146 0 4548
##
# # LOWEST:P1 P2 P3 P4 P5
# # highest:p4544 p4545 p4546 p4547 p4548
## ---------------------------------------------------------------------------
# # Quant
# # N Missing unique Info Mean. 05.10.25.50
# 387304 13842 20956 1 8442 100 101 107 168
# # 75.90.95
# 738 4877 12916
##
# # LOWEST:100 101 102 103 104
# # highest:56590926 164244544 173844544 194044544 473883883
## ---------------------------------------------------------------------------
# # Val
# # N Missing unique Info Mean. 05.10.25.50
# 399964 1182 21821 1 14617 1040 1085 1345 2675
# # 75.90.95
# 8680 27250 52995
##
# # LOWEST:1005 1010 1015 1020 1025
# # highest:4161740 4308620 4475360 4616735 4642955
## ---------------------------------------------------------------------------
# # Insp
# # N Missing Unique
# 401146 0 3
##
# # OK (14462, 4), Unkn (385414, 96%), fraud (1270, 0%)
## ---------------------------------------------------------------------------
# from the results, there is a lot of product and salesperson information in the data set, and you can use Nlevels () to confirm this:
Nlevels (Sales$id)
# # [1] 6016
Nlevels (Sales$prod)
# # [1] 4548
Length (Which (Is.na (sales$quant) & Is.na (Sales$val)))
# # [1] 888
# equivalent to
Sum (is.na (sales$quant) & Is.na (Sales$val))
# # [1] 888
# The percentage of fraud is generally lower:
Table (SALES$INSP)/nrow (sales) *100
##
# # OK UNKN fraud
# 3.6052 96.0782 0.3166
# equivalent to
Prop.table (table (SALES$INSP)) *100
##
# # OK UNKN fraud
# 3.6052 96.0782 0.3166
# The following code draws a graph showing the number of reports per salesperson. You can be sure that the data for all sales people is quite different.
# for each product, the same is true.
Par (MFROW=C)
Tots<-table (Sales$id)
Totp<-table (Sales$prod)
Barplot (tots,main= "Transactions per salespeople", names.arg= "",
xlab= "Salespeople", ylab= "Amount")
Barplot (totp,main= "Transactions per product", names.arg= "",
Xlab= "Products", ylab= "Amount")
Par (mfrow=c ())


# Add the unit price as a new column to the data frame, the code is as follows:
Sales$uprice<-sales$val/sales$quant
# We can use the following code to check the distribution of product unit Price:
Summary (Sales$uprice)
# # Min. 1st Qu.    Median Mean 3rd Qu.    Max. NA ' s
# 0 8 12 20 19 26500 14136
Describe (Sales$uprice)
# # Sales$uprice
# # N Missing unique Info Mean. 05.10.25.50
# 387010 14136 167071 1 20.3 1.557 3.269 8.460 11.887
# # 75.90.95
# 19.112 34.068 50.779
##
# # lowest:2.448e-06 4.203e-04 4.283e-04 4.741e-04 5.274e-04
# # highest:1.334e+04 1.388e+04 1.615e+04 2.117e+04 2.646e+04
# Once again we see the obvious variability.
# It may be interesting to check the most expensive and cheapest products. We use the median of the unit price to represent the standard price of the sold product. The following code is used to get the information we need:
Attach (Sales)
Upp<-aggregate (Uprice,list (Prod), median,na.rm=t)
Topp<-sapply (c (t,f), function (o)
Upp[order (Upp[,2],decreasing=o) [1:5],1])
Colnames (TopP) <-c ("Expensive", "Cheap")
TopP
# # expensive Cheap
# # [1,] "p3689" "p560"
# # [2,] "p2453" "p559"
# # [3,] "p2452" "p4195"
# # [4,] "p2456" "P601"
# # [5,] "p2459" "p563"
# We can use the box chart of the unit price of these 5 products to confirm their completely different price distributions:
Tops<-sales[prod%in% topp[1,],c ("Prod", "Uprice")]
Tops$prod<-factor (Tops$prod)
BoxPlot (uprice~prod,data=tops,ylab= "Uprice", log= "Y") # take logarithm of Y axis




# A similar analysis can be performed to identify those salespeople who bring more (less) money to the company:
Vs<-aggregate (Val,list (ID), sum,na.rm=t)
Scoresss<-sapply (c (t,f), function (o)
Vs[order (Vs$x,decreasing=o) [1:5],1])
Colnames (SCORESSS) <-c ("Most", "Least")
Scoresss
# # Most Least
# # [1,] "v431" "v3355"
# # [2,] "v54" "v6069"
# # [3,] "v19" "v5876"
# # [4,] "v4520" "v6058"
# # [5,] "v955" "v4515"
# The top 100 sales staff accounted for almost 40% of the company's capital income, while the bottom 2000 of the 6,016 sales staff had less than 2% of the company's total income.
Sum (Vs[order (vs$x,decreasing=t) [1:100],2])/sum (val,na.rm=t) *100
# # [1] 38.33
Sum (Vs[order (VS$X,DECREASING=F) [1:2000],2])/sum (val,na.rm=t) *100
# # [1] 1.989
# If we conduct a similar analysis of the quantity sold for each product, the results are more unbalanced:
Qs<-aggregate (Quant,list (Prod), sum,na.rm=t)
Scoresps<-sapply (c (t,f), function (o)
Qs[order (Qs$x,decreasing=o) [1:5],1])
Colnames (Scoresps) <-c ("Most", "Least")
Scoresps
# # Most Least
# # [1,] "p2516" "p2442"
# # [2,] "p3599" "p2443"
# # [3,] "p314" "p1653"
# # [4,] "p569" "p4101"
# # [5,] "p319" "p3678"
Sum (as.double (Qs[order (qs$x,decreasing=t) [1:100],2])/sum (as.double (Quant), na.rm=t) *100
# # [1] 74.63
Sum (as.double (Qs[order (QS$X,DECREASING=F) [1:4000],2])/sum (as.double (Quant), na.rm=t) *100
# # [1] 8.945
# of the 4,548 products, 4,000 of them represent less than 10% of sales, while the highest sales of 100 products account for nearly 75% of sales.


# Determine the number of outliers per product:
Out<-tapply (Uprice,list (Prod=prod),
function (x) Length (boxplot.stats (x) $out))
The # function boxplot.stats () can get some statistics for drawing a box plot.
Out[order (out,decreasing=t) [1:10]]
# # Prod
# # p1125 p1437 p2273 p1917 p1918 p4089 p538 p3774 p2742 p3338
# 376 181 165 156 156 137 129 125 120 117
# Use this simple method to find 29,446 transactions that are not considered outliers, which is equivalent to 7% of the total number of trades.
SUM (out)
# # [1] 29446
SUM (out)/nrow (sales) *100
# # [1] 7.34

Detect fraudulent transactions-data preliminary exploration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.