#Fetching The Dataset
library(readxl)
## Warning: package 'readxl' was built under R version 4.3.3
cybercrime <- read_excel("C:/Users/Nitin/Desktop/4th sem/ca/vasvi/data exploration/cybercrime.xlsx")
View(cybercrime)
#1. Which Indian state reports the highest number of cyber crimes overall?
max_crimes<- cybercrime[which.max(cybercrime$Total),]
print(max_crimes)
## # A tibble: 1 × 24
##   State     `Personal Revenge` Anger Fraud Extortion `Causing Disrepute` Prank
##   <chr>                  <dbl> <dbl> <dbl>     <dbl>               <dbl> <dbl>
## 1 Karnataka                 12     4 11381        43                 109     1
## # ℹ 17 more variables: `Sexual Exploitation` <dbl>, `Political Motives` <dbl>,
## #   `Terrorist Activities (Total)` <dbl>,
## #   `Terrorist Activities - Terrorist Recruitment` <dbl>,
## #   `Terrorist Activities - Terrorist Funding` <dbl>,
## #   `Terrorist Activities - Others` <dbl>,
## #   `Inciting Hate against Country` <dbl>, `Disrupt Public Service` <dbl>,
## #   `Sale Purchase Illegal Drugs` <dbl>, `Developing own Business` <dbl>, …
#2.What is the overall trend in cyber crime rates across different Indian states over the years?
cyber_data <- cybercrime[-1,]

# Convert 'Year' column to numeric
cyber_data$Year <- as.numeric(cyber_data$Year)

# Calculate total cyber crime rates for each year
crime_year <- aggregate(Total ~ Year, data = cyber_data, sum)

print(crime_year)
##   Year Total
## 1 2017 20662
## 2 2018 27004
## 3 2019 44395
## 4 2020 49708
#3.Which state has the highest incidence of cyber crimes involving personal revenge and anger?
cybercrime$Total_Revenge_Anger <- cybercrime$'Personal Revenge' + cybercrime$'Anger'

state_highest_incidence <- cybercrime$'State'[which.max(cybercrime$Total_Revenge_Anger)]

cat("State with the highest incidence of cyber crimes involving personal revenge and anger:", state_highest_incidence, "\n")
## State with the highest incidence of cyber crimes involving personal revenge and anger: Assam
#4.What is the distribution of cyber crimes involving fraud across different states?
fraud_distribution <- aggregate(cybercrime$Fraud, 
                                by = list(State = cybercrime$State, Year = cybercrime$Year), 
                                sum)
print(fraud_distribution)
##                 State Year     x
## 1      Andhra Pradesh 2017   537
## 2   Arunachal Pradesh 2017     0
## 3               Assam 2017    48
## 4               Bihar 2017   397
## 5        Chhattisgarh 2017    47
## 6                 Goa 2017     8
## 7             Gujarat 2017   305
## 8             Haryana 2017    43
## 9    Himachal Pradesh 2017     8
## 10    Jammu & Kashmir 2017    15
## 11          Jharkhand 2017   460
## 12          Karnataka 2017  2764
## 13             Kerala 2017    77
## 14     Madhya Pradesh 2017   155
## 15        Maharashtra 2017  2171
## 16            Manipur 2017    35
## 17          Meghalaya 2017    15
## 18            Mizoram 2017     7
## 19           Nagaland 2017     0
## 20             Odisha 2017   531
## 21             Punjab 2017    53
## 22          Rajasthan 2017   331
## 23             Sikkim 2017     0
## 24         Tamil Nadu 2017    51
## 25          Telangana 2017   529
## 26            Tripura 2017     3
## 27      Uttar Pradesh 2017  3450
## 28        Uttarakhand 2017    54
## 29        West Bengal 2017    45
## 30     Andhra Pradesh 2018   733
## 31  Arunachal Pradesh 2018     2
## 32              Assam 2018   389
## 33              Bihar 2018   351
## 34       Chhattisgarh 2018    23
## 35                Goa 2018    11
## 36            Gujarat 2018   401
## 37            Haryana 2018   137
## 38   Himachal Pradesh 2018    18
## 39    Jammu & Kashmir 2018    20
## 40          Jharkhand 2018   783
## 41          Karnataka 2018  5441
## 42             Kerala 2018    93
## 43     Madhya Pradesh 2018   230
## 44        Maharashtra 2018  1998
## 45            Manipur 2018    14
## 46          Meghalaya 2018    35
## 47            Mizoram 2018     0
## 48           Nagaland 2018     0
## 49             Odisha 2018   506
## 50             Punjab 2018    48
## 51          Rajasthan 2018   499
## 52             Sikkim 2018     0
## 53         Tamil Nadu 2018    55
## 54          Telangana 2018   732
## 55            Tripura 2018     8
## 56      Uttar Pradesh 2018  2351
## 57        Uttarakhand 2018    46
## 58        West Bengal 2018    68
## 59     Andhra Pradesh 2019  1211
## 60  Arunachal Pradesh 2019     0
## 61              Assam 2019   243
## 62              Bihar 2019   844
## 63       Chhattisgarh 2019    47
## 64                Goa 2019     9
## 65            Gujarat 2019   363
## 66            Haryana 2019   151
## 67   Himachal Pradesh 2019    15
## 68    Jammu & Kashmir 2019    27
## 69          Jharkhand 2019   964
## 70          Karnataka 2019 11381
## 71             Kerala 2019    67
## 72     Madhya Pradesh 2019   178
## 73        Maharashtra 2019  3551
## 74            Manipur 2019     0
## 75          Meghalaya 2019    50
## 76            Mizoram 2019     0
## 77           Nagaland 2019     0
## 78             Odisha 2019  1000
## 79             Punjab 2019    81
## 80          Rajasthan 2019   938
## 81             Sikkim 2019     0
## 82         Tamil Nadu 2019    73
## 83          Telangana 2019  2013
## 84            Tripura 2019     2
## 85      Uttar Pradesh 2019  3549
## 86        Uttarakhand 2019    28
## 87        West Bengal 2019    68
## 88     Andhra Pradesh 2020  1149
## 89  Arunachal Pradesh 2020    26
## 90              Assam 2020   242
## 91              Bihar 2020  1218
## 92       Chhattisgarh 2020    75
## 93                Goa 2020    25
## 94            Gujarat 2020   875
## 95            Haryana 2020   157
## 96   Himachal Pradesh 2020    19
## 97          Jharkhand 2020  1069
## 98          Karnataka 2020  9680
## 99             Kerala 2020    96
## 100    Madhya Pradesh 2020   292
## 101       Maharashtra 2020  3413
## 102           Manipur 2020    40
## 103         Meghalaya 2020    81
## 104           Mizoram 2020     3
## 105          Nagaland 2020     5
## 106            Odisha 2020  1380
## 107            Punjab 2020   164
## 108         Rajasthan 2020   641
## 109            Sikkim 2020     0
## 110        Tamil Nadu 2020   134
## 111         Telangana 2020  4436
## 112           Tripura 2020    11
## 113     Uttar Pradesh 2020  4674
## 114       Uttarakhand 2020    98
## 115       West Bengal 2020    72
#5.Which states report the most cases of cyber crimes involving extortion?

extortion_cases <- aggregate(Extortion ~ State, data = cybercrime,sum)

max_extortion_cases <- extortion_cases[which.max(extortion_cases$Extortion), ]

print(max_extortion_cases)
##            State Extortion
## 27 Uttar Pradesh      2217
#6.Which states report the most cases of cyber crimes involving causing disrepute?
disrepute_cases <- aggregate(`Causing Disrepute` ~ State, data = cybercrime,sum)
max_disrepute_cases <- disrepute_cases[which.max(disrepute_cases$`Causing Disrepute`), ]
print(max_disrepute_cases)
##            State Causing Disrepute
## 27 Uttar Pradesh              2209
#7.Which state has the highest incidence of cyber crimes involving sexual exploitation?
sexual_exploitation <- aggregate(`Sexual Exploitation` ~ State, data = cybercrime,sum)
max_sexual_exploitation <- sexual_exploitation[which.max(sexual_exploitation$`Sexual Exploitation`), ]
print(max_sexual_exploitation)
##          State Sexual Exploitation
## 15 Maharashtra                2355
#8. What is the distribution of cyber crimes involving the sale and purchase of illegal drugs across different states?
illegal_drugs <- aggregate(`Sale Purchase Illegal Drugs` ~ State, data = cybercrime,sum)
print(illegal_drugs)
##                State Sale Purchase Illegal Drugs
## 1     Andhra Pradesh                           0
## 2  Arunachal Pradesh                           0
## 3              Assam                           0
## 4              Bihar                           2
## 5       Chhattisgarh                           0
## 6                Goa                           0
## 7            Gujarat                           2
## 8            Haryana                           2
## 9   Himachal Pradesh                           0
## 10   Jammu & Kashmir                           0
## 11         Jharkhand                          26
## 12         Karnataka                           2
## 13            Kerala                           0
## 14    Madhya Pradesh                           0
## 15       Maharashtra                           2
## 16           Manipur                           0
## 17         Meghalaya                           2
## 18           Mizoram                           0
## 19          Nagaland                           0
## 20            Odisha                           0
## 21            Punjab                           3
## 22         Rajasthan                           0
## 23            Sikkim                           0
## 24        Tamil Nadu                           0
## 25         Telangana                           0
## 26           Tripura                           0
## 27     Uttar Pradesh                           0
## 28       Uttarakhand                           4
## 29       West Bengal                           0
#9.How prevalent is cyber crime involving disrupting public services in each state?
public_service_disruption_cases <- aggregate(`Disrupt Public Service` ~ State, data = cybercrime,sum)
print(public_service_disruption_cases)
##                State Disrupt Public Service
## 1     Andhra Pradesh                      4
## 2  Arunachal Pradesh                      0
## 3              Assam                     15
## 4              Bihar                      1
## 5       Chhattisgarh                      1
## 6                Goa                      0
## 7            Gujarat                     14
## 8            Haryana                      3
## 9   Himachal Pradesh                      1
## 10   Jammu & Kashmir                     13
## 11         Jharkhand                     30
## 12         Karnataka                      6
## 13            Kerala                      1
## 14    Madhya Pradesh                      2
## 15       Maharashtra                      6
## 16           Manipur                      0
## 17         Meghalaya                      1
## 18           Mizoram                      1
## 19          Nagaland                      0
## 20            Odisha                      0
## 21            Punjab                      4
## 22         Rajasthan                      0
## 23            Sikkim                      0
## 24        Tamil Nadu                     28
## 25         Telangana                      2
## 26           Tripura                      0
## 27     Uttar Pradesh                     58
## 28       Uttarakhand                      0
## 29       West Bengal                      0
#10. In which states are cyber crimes involving the development of one's own business most common?
business_development <- aggregate(`Developing own Business` ~ State, data = cybercrime,sum)
max_business_development <- business_development[which.max(business_development$`Developing own Business`), ]
print(max_business_development)
##            State Developing own Business
## 27 Uttar Pradesh                     247
#11.Which states report the highest cases of cyber crimes involving spreading piracy?
max_piracy_state <- cybercrime[which.max(cybercrime$`Spreading Piracy`), "State"]
print(max_piracy_state)
## # A tibble: 1 × 1
##   State        
##   <chr>        
## 1 Uttar Pradesh
#12.How does the distribution of a specific type of cybercrime (e.g., fraud, extortion, political motives, etc.) 
#vary across different states? Visualize this using a box plot
#to compare the median, quartiles, and potential outliers of the reported cybercrime counts in each state.
# Specify the specific type of cybercrime you want to analyze (e.g., fraud, extortion, political motives)
specific_cybercrime <- "Fraud"
filtered_data <- subset(cybercrime, select = c("State", specific_cybercrime))
boxplot(filtered_data[[specific_cybercrime]] ~ filtered_data$State,
        xlab = "State", ylab = paste("Reported Instances of", specific_cybercrime),
        main = paste("Distribution of", specific_cybercrime, "across Different States"),
        col = "skyblue", border = "black")

#13.Is there a relationship between the year and the total number of cybercrimes reported? 
#Can we use regression to predict the expected number of cybercrimes in future years?
str(cybercrime)
## tibble [115 × 25] (S3: tbl_df/tbl/data.frame)
##  $ State                                       : chr [1:115] "Andhra Pradesh" "Arunachal Pradesh" "Assam" "Bihar" ...
##  $ Personal Revenge                            : num [1:115] 24 0 246 12 8 0 6 7 5 0 ...
##  $ Anger                                       : num [1:115] 5 0 83 5 3 0 5 6 4 1 ...
##  $ Fraud                                       : num [1:115] 537 0 48 397 47 8 305 43 8 15 ...
##  $ Extortion                                   : num [1:115] 65 0 65 3 4 0 12 4 6 10 ...
##  $ Causing Disrepute                           : num [1:115] 0 0 40 0 29 5 79 9 3 0 ...
##  $ Prank                                       : num [1:115] 0 0 40 0 18 0 3 2 3 3 ...
##  $ Sexual Exploitation                         : num [1:115] 61 0 217 16 16 0 24 36 10 7 ...
##  $ Political Motives                           : num [1:115] 12 0 10 0 3 0 4 0 3 0 ...
##  $ Terrorist Activities (Total)                : num [1:115] 8 0 44 0 0 0 0 0 0 3 ...
##  $ Terrorist Activities - Terrorist Recruitment: num [1:115] 0 0 0 0 0 0 0 0 0 0 ...
##  $ Terrorist Activities - Terrorist Funding    : num [1:115] 0 0 0 0 0 0 0 0 0 0 ...
##  $ Terrorist Activities - Others               : num [1:115] 8 0 44 0 0 0 0 0 0 3 ...
##  $ Inciting Hate against Country               : num [1:115] 0 0 2 0 1 0 6 0 0 0 ...
##  $ Disrupt Public Service                      : num [1:115] 1 0 0 0 0 0 2 1 0 10 ...
##  $ Sale Purchase Illegal Drugs                 : num [1:115] 0 0 0 0 0 0 0 2 0 0 ...
##  $ Developing own Business                     : num [1:115] 0 0 0 0 0 0 1 7 4 0 ...
##  $ Spreading Piracy                            : num [1:115] 1 0 0 0 0 0 0 4 1 0 ...
##  $ Psycho or Pervert                           : num [1:115] 0 0 0 0 0 0 0 0 0 0 ...
##  $ Steal Information                           : num [1:115] 0 0 3 0 0 0 0 1 0 0 ...
##  $ Abetment to Suicide                         : num [1:115] 0 0 0 0 0 0 0 0 0 0 ...
##  $ Others                                      : num [1:115] 217 1 322 0 42 0 11 382 9 14 ...
##  $ Total                                       : num [1:115] 931 1 1120 433 171 13 458 504 56 63 ...
##  $ Year                                        : num [1:115] 2017 2017 2017 2017 2017 ...
##  $ Total_Revenge_Anger                         : num [1:115] 29 0 329 17 11 0 11 13 9 1 ...
total_crimes_per_year <- aggregate(Total ~ Year, data = cybercrime,sum)
model <- lm(Total ~ Year, data = total_crimes_per_year)
summary(model)
## 
## Call:
## lm(formula = Total ~ Year, data = total_crimes_per_year)
## 
## Residuals:
##     1     2     3     4 
##  1178 -3584  3633 -1227 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)  
## (Intercept) -20499737    3433929   -5.97   0.0269 *
## Year            10174       1701    5.98   0.0268 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3804 on 2 degrees of freedom
## Multiple R-squared:  0.947,  Adjusted R-squared:  0.9206 
## F-statistic: 35.76 on 1 and 2 DF,  p-value: 0.02684
future_years <- data.frame(Year = c(2025, 2026, 2027))  
predicted_crimes <- predict(model, newdata = future_years)
print(predicted_crimes)
##        1        2        3 
## 101803.4 111977.0 122150.6
#14.Can we identify any correlations between different types of cybercrimes
#(e.g., fraud and theft of information)?
str(cybercrime)
## tibble [115 × 25] (S3: tbl_df/tbl/data.frame)
##  $ State                                       : chr [1:115] "Andhra Pradesh" "Arunachal Pradesh" "Assam" "Bihar" ...
##  $ Personal Revenge                            : num [1:115] 24 0 246 12 8 0 6 7 5 0 ...
##  $ Anger                                       : num [1:115] 5 0 83 5 3 0 5 6 4 1 ...
##  $ Fraud                                       : num [1:115] 537 0 48 397 47 8 305 43 8 15 ...
##  $ Extortion                                   : num [1:115] 65 0 65 3 4 0 12 4 6 10 ...
##  $ Causing Disrepute                           : num [1:115] 0 0 40 0 29 5 79 9 3 0 ...
##  $ Prank                                       : num [1:115] 0 0 40 0 18 0 3 2 3 3 ...
##  $ Sexual Exploitation                         : num [1:115] 61 0 217 16 16 0 24 36 10 7 ...
##  $ Political Motives                           : num [1:115] 12 0 10 0 3 0 4 0 3 0 ...
##  $ Terrorist Activities (Total)                : num [1:115] 8 0 44 0 0 0 0 0 0 3 ...
##  $ Terrorist Activities - Terrorist Recruitment: num [1:115] 0 0 0 0 0 0 0 0 0 0 ...
##  $ Terrorist Activities - Terrorist Funding    : num [1:115] 0 0 0 0 0 0 0 0 0 0 ...
##  $ Terrorist Activities - Others               : num [1:115] 8 0 44 0 0 0 0 0 0 3 ...
##  $ Inciting Hate against Country               : num [1:115] 0 0 2 0 1 0 6 0 0 0 ...
##  $ Disrupt Public Service                      : num [1:115] 1 0 0 0 0 0 2 1 0 10 ...
##  $ Sale Purchase Illegal Drugs                 : num [1:115] 0 0 0 0 0 0 0 2 0 0 ...
##  $ Developing own Business                     : num [1:115] 0 0 0 0 0 0 1 7 4 0 ...
##  $ Spreading Piracy                            : num [1:115] 1 0 0 0 0 0 0 4 1 0 ...
##  $ Psycho or Pervert                           : num [1:115] 0 0 0 0 0 0 0 0 0 0 ...
##  $ Steal Information                           : num [1:115] 0 0 3 0 0 0 0 1 0 0 ...
##  $ Abetment to Suicide                         : num [1:115] 0 0 0 0 0 0 0 0 0 0 ...
##  $ Others                                      : num [1:115] 217 1 322 0 42 0 11 382 9 14 ...
##  $ Total                                       : num [1:115] 931 1 1120 433 171 13 458 504 56 63 ...
##  $ Year                                        : num [1:115] 2017 2017 2017 2017 2017 ...
##  $ Total_Revenge_Anger                         : num [1:115] 29 0 329 17 11 0 11 13 9 1 ...
correlation_matrix <- cor(cybercrime[, c("Fraud", "Steal Information")])
print(correlation_matrix)
##                        Fraud Steal Information
## Fraud             1.00000000        0.04347427
## Steal Information 0.04347427        1.00000000
#15.Are there noticeable trends in cyber crime types in specific states over time?
target_states <- c("Tamil Nadu", "Uttar Pradesh")  
filtered_data <- cybercrime[cybercrime$State %in% target_states, ]
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
crime_trends <- filtered_data %>%
  group_by(Year) %>%
  summarise(
    `Personal Revenge` = sum(`Personal Revenge`),
    Anger = sum(Anger),
    Fraud = sum(Fraud),
    Extortion = sum(Extortion)
  )

 library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
ggplot(crime_trends, aes(x = Year)) +
  geom_line(aes(y = `Personal Revenge`, color = "Personal Revenge")) +
  geom_line(aes(y = Anger, color = "Anger")) +
  geom_line(aes(y = Fraud, color = "Fraud")) +
  geom_line(aes(y = Extortion, color = "Extortion")) +
  labs(title = "Trends in Cyber Crime Types Over Time",
       x = "Year", y = "Total Cases") +
  scale_color_manual(values = c("red", "blue", "green", "orange"),
                     breaks = c("Personal Revenge", "Anger", "Fraud", "Extortion"),
                     labels = c("Personal Revenge", "Anger", "Fraud", "Extortion"))

# Comparative Analysis:
#16.How do different states compare in terms of the number and types of cyber crimes?
  
library(dplyr)
state_crime_summary <- cybercrime %>%
  group_by(State) %>%
  summarise_all(sum)

library(ggplot2)
crime_distribution_long <- state_crime_summary[order(state_crime_summary$Total), ]
ggplot(state_crime_summary, aes(x = reorder(State, Total), y = Total, fill = State)) +
  geom_col() +
  labs(title = "Total Number of Cyber Crime Cases by State",
       x = "State", y = "Total Cases") +
  theme(axis.text.x = element_text(angle = 90))

#17.How is the distribution of different types of cyber crimes across Indian states (e.g., extortion, fraud, sexual harassment)

library(dplyr)

crime_distribution <- cybercrime %>%
  group_by(State) %>%
  summarise(
    Extortion = sum(Extortion),
    Fraud = sum(Fraud),
    Sexual_Harassment = sum(`Sexual Exploitation`)
  )

library(ggplot2)

ggplot(crime_distribution_long, aes(x = State, y = Total)) +
  geom_col(fill = "blue") +
  labs(title = "Cyber Crime Distribution Across Indian States",
       x = "State", y = "Total Cases") +
  theme(axis.text.x = element_text(angle = 90))

#18.What is the total number of cyber crime incidents reported in each Indian state? 
library(dplyr)

total_incidents_by_state <- cybercrime %>%
  group_by(State) %>%
  summarise(Total_Incidents = sum(Total))
print(total_incidents_by_state)
## # A tibble: 29 × 2
##    State             Total_Incidents
##    <chr>                       <dbl>
##  1 Andhra Pradesh               5923
##  2 Arunachal Pradesh              46
##  3 Assam                        8903
##  4 Bihar                        3369
##  5 Chhattisgarh                  782
##  6 Goa                            97
##  7 Gujarat                      3227
##  8 Haryana                      2142
##  9 Himachal Pradesh              299
## 10 Jammu & Kashmir               209
## # ℹ 19 more rows