0% found this document useful (0 votes)
15 views4 pages

Fraud Detection Data Analysis Guide

The document outlines a data analysis process for fraud detection using R, including data cleaning, visualization, and modeling techniques. It covers steps such as checking for missing values and duplicates, creating histograms and bar plots for various factors, and applying logistic regression and decision tree models to understand the predictors of fraud. The analysis aims to identify significant factors influencing fraud occurrences based on the dataset provided.

Uploaded by

Deni wijaya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Fraud Detection Data Analysis Guide

The document outlines a data analysis process for fraud detection using R, including data cleaning, visualization, and modeling techniques. It covers steps such as checking for missing values and duplicates, creating histograms and bar plots for various factors, and applying logistic regression and decision tree models to understand the predictors of fraud. The analysis aims to identify significant factors influencing fraud occurrences based on the dataset provided.

Uploaded by

Deni wijaya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

library(ggplot2)

library(forcats)
library(dplyr)
library(rpart)
library([Link])
library(ROSE)
library(caret)

setwd("D:\\Data Files\\Desktop\\Data Analytic\\BA3- Analitika Data Oktober


2024\\BA3- Analitika Data")
Fraud = [Link]("data_soal 1_ Fraud [Link]")

#1A. Sebelum melakukan pengolahan data, pastikan terlebih dahulu


#bahwa tidak terdapat masalah pada data, misalnya nilai data yang tidak valid,
#duplikasi data, dan penanganan sebagian informasi yang hilang pada data jika
#ada. Identifikasi apakah ada masalah pada data,
#dan lakukan langkah penanganan yang menurut kalian sesuai.

Fraud$Make = [Link](Fraud$Make)
Fraud$AccidentArea = [Link](Fraud$AccidentArea)
Fraud$Sex = [Link](Fraud$Sex)
Fraud$DriverRating = [Link](Fraud$DriverRating)
Fraud$MonthClaimed = [Link](Fraud$MonthClaimed)
Fraud$Age = [Link](Fraud$Age)
Fraud$Month = factor(Fraud$Month,
levels=c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","De
c"))
Fraud$PolicyType = [Link](Fraud$PolicyType)
Fraud$FraudFound_P =[Link](Fraud$FraudFound_P)
Fraud$DayOfWeek = [Link](Fraud$DayOfWeek)
Fraud$Month = [Link](Fraud$Month)
Fraud$ClaimSize = [Link](Fraud$ClaimSize)

str(Fraud)
summary(Fraud)

colSums([Link](Fraud)) #cek nilai nill


sum(duplicated(Fraud)) #cek baris duplikat
summary(Fraud[, sapply(Fraud, [Link])]) #memeriksa summary, tapi hanya
kolom yang berupa angka

#1B Berikan deskripsi singkat dari data (berupa ringkasan numerik dan/
#atau grafik), untuk memudahkan klien memahami data dan pengukuran yang ada.

summary(Fraud)

plot_hist = function(fill_value){ggplot(Fraud, aes(x=ClaimSize)) +


geom_histogram(aes(fill = .data[[fill_value ]]), colour="Black") +
facet_grid(Year~FraudFound_P, scales ="free")}

plot_hist("Fault")
plot_hist("Sex")
plot_hist("AccidentArea")
plot_hist("PolicyType")
plot_hist("VehicleCat")
plot_hist("DriverRating")
plot_hist("Age")
plot_hist("Make")

plot_hist_make = function(fill_make){ggplot(Fraud, aes(x= fct_lump(Make,


n=5),fill = .data[[fill_make]])) +
geom_bar()} + xlab("Make") +facet_grid(Year~FraudFound_P,scale ="free")

plot_hist_make("Fault")

Fraud_only <- Fraud[Fraud$FraudFound_P==1,]


Fraud_only$Month = factor(Fraud_only$Month,
levels=c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","De
c"))

ggplot(data=Fraud_only)+ geom_bar(aes(x=Month, fill=Sex)) +


facet_grid(Year~Sex, scales="free")

ggplot(data=Fraud_only)+ geom_bar(aes(x=Month, fill=Sex)) +


facet_grid(AccidentArea~., scales="free")

ggplot(data=Fraud)+ geom_bar(aes(x=Month, fill=Sex)) +


facet_grid(AccidentArea~., scales="free")

ggplot(data=Fraud)+ geom_bar(aes(x=Age)) #+ facet_grid(AccidentArea~.,


scales="free")
ggplot(data=Fraud_only)+ geom_bar(aes(x=Age)) #+ facet_grid(AccidentArea~.,
scales="free")

# No 2. Visualisasi = Klien Anda ingin mengetahui apakah faktor-faktor


berikut:
# bulan, hari, awal/akhir bulan, wilayah atau area, jenis kelamin pengemudi,
# usia pengemudi, dan merek kendaraan tertentu merupakan faktor-faktor
# yang potensial menjelaskan terjadinya fraud/tidak. Secara grafis,
# bagaimana Anda menyajikan dan menjelaskan hal ini?

Fraud$Group <- "All Data"


Fraud_only$Group <- "Fraud Only"
Fraud_combined = rbind(Fraud,Fraud_only)

Grouping_Bar = function(Measurement){ggplot(data=Fraud_combined) +
geom_bar(aes(x=.data[[Measurement]],fill=Group),position="fill") +
scale_y_continuous(labels=scales::percent)}

Grouping_Bar("DayOfWeek")
Grouping_Bar("Month")
Grouping_Bar("WeekOfMonth")
Grouping_Bar("AccidentArea")
Grouping_Bar("Sex")
Grouping_Bar("Age")

ggplot(data=Fraud_combined) + geom_bar(aes(x=fct_lump(Make, n=5), fill=Group),


position ="fill")
#------- Tugas 4 = GLM

table(Fraud$Sex , Fraud$FraudFound_P )

model = glm(formula=FraudFound_P ~ [Link](Sex) +


Age + AgeOfVehicle, family = "binomial", data = Fraud)
summary(model)

new_data = [Link](Sex="Female",Age = 35, AgeOfVehicle = 1)

prob_fraud <- predict(model, newdata = new_data, type = "response")


prob_fraud

#Tugas 5 (15 poin): DT


#Ingin diketahui faktor apa saja yang paling bisa menjelaskan
#terjadinya fraud atau tidak, dan untuk memudahkan mengambil keputusan,
#digunakan Decision Tree. Ada masalah ketidaksetimbangan pada data,
#sebagai berikut:

#undersampling

table(Fraud$FraudFound_P)

[Link](123)
Fraud_undersampling <- [Link](FraudFound_P ~ .,
data=Fraud, method="under")$data

table(Fraud_undersampling$FraudFound_P)
Fraud_undersampling
str(Fraud_undersampling)

#visualisasi Decision Tree

model = rpart(FraudFound_P ~ VehicleCat + MonthClaimed +


Month + Make + DriverRating,
data = Fraud_undersampling,
method = "class",
control = [Link](cp=0.0001,
minsplit = 2,
minbucket = 1,
maxdepth = 4))

[Link](model,
type=2,
extra= 104,
[Link] = TRUE,
faclen = 0,
cex = 0.7,
main = "Decision Tree Fraud Detection")

prediksi <- predict(model, Fraud_undersampling, type = "class")


confusionMatrix(prediksi, Fraud_undersampling$FraudFound_P)

#regresi linear
str(Fraud_undersampling)

df5 = subset(Fraud, select = c(Sex,MaritalStatus,[Link],


VehicleCat,Age,AgeOfVehicle,
Month,ClaimSize))

df5 <- df5 %>%


mutate(Month = case_when(
Month == "Jan" ~ 1,
Month == "Feb" ~ 2,
Month == "Mar" ~ 3,
Month == "Apr" ~ 4,
Month == "May" ~ 5,
Month == "Jun" ~ 6,
Month == "Jul" ~ 7,
Month == "Aug" ~ 8,
Month == "Sep" ~ 9,
Month == "Oct" ~ 10,
Month == "Nov" ~ 11,
Month == "Dec" ~ 12,
TRUE ~ NA_real_
))

df5

sapply(df5, function(x) length(unique(x)))

fitting_regression <- lm(formula = ClaimSize ~. , data = df5)


summary(fitting_regression)

#perbaikan regresi linear

#otomatis
fitting_regression2 <- step(fitting_regression, direction = "both")
summary(fitting_regression2)

#manual, utamakan pilih yang manual


fitting_regression3 <- lm(log(ClaimSize) ~ Age + AgeOfVehicle + Make +
Month + MonthClaimed + VehicleCat, data = Fraud)
summary(fitting_regression3)

You might also like