×
Services Samples Blogs Make Payment About us Reviews 4.9/5 Order Now

Program in R to Analyse the Estimated Crimes between 1979 and 2019 Assignment Solution

November 22, 2022
Connor Cruz
Connor Cruz
🇦🇹 Austria
R Programming
Manuel Hill is a R Programming Assignment Tutor with 7 years of experience and has completed over 1800 assignments. He is from Austria and holds a Master’s in Statistics from the University of Vienna. Manuel provides expert guidance in R programming, helping students excel in their assignments with his extensive knowledge.
Key Topics
  • Instructions
  • Assignment Solution
Tip of the day
When tackling a statistics problem, always start by visualizing the data! A simple graph or chart can help reveal trends, outliers, and patterns that aren’t immediately obvious from raw numbers. Understanding your data visually can make the analysis much clearer!
News
The rise of AI and big data is reshaping the field of statistics. Recent trends highlight the growing need for expertise in statistical modeling, machine learning, and data visualization, with applications in healthcare, finance, and technology.

Instructions

Make an R programming project to analyze the data on the estimated_crimes_1979_2019.csv file. This dataset contains estimated data at the state and national level and was derived from the Summary Reporting System (SRS). The sample of the data is shown in the screenshots below.

yearstate abbstate npopulatioviolent crhomiciderape_lega rape_revisrobberyaggravatepropertyburglarylarcenymotor_vecaveats
19792.2E+081208030214607639048070062948011041500332770066010001112800
1979AKAlaska4060001994542924451203231935616150762501
1979ALAlabama376900015578496103741279918144372485178379112064
1979ARArkansas21800007984198595162655657094921457452674225
1979AZArizona2450000145282191120430588841779774891611697612085
1979CACalifornia2269600018408729521223975767931291511021496310847148167563
1979COColorado2772000144721611472435384861809844974111789813345
1979CTConnectic31150001290213175260215998167131482299699721905
1979DCDistrict of65600010553180489692029644587713452288193606
1979DEDelaware5820003127331627532179348538890230812882
1979FLFlorida88600007388110844576220974612460728119088437809938298
1979GAGeorgia511800028594877221610939145622486418157914575821304
1979HIHawaii91500026516629616886016366416538405806546
1979IAlowa29030005259653201457341711962026768850237829
1979IDIdaho9050002613491863921986357669729235772460
1979ILIllinois112300008354012033702360564257959375016177635606275912
1979INIndiana5400000182544481681716789582302236317614366623381
1979KSKansas236900083761306262423519710760531504696226479
1979KYKentucky352700087483357193247444710354832082624319035
1979LALouisiana40260002722968215548832161611885145623711585616421
1979MAMassachu576900030650212142811724172863107569257015213566051
1979MDMaryland414900033007406163613740172252281596263014529720232
1979MEMaine109700022213113134917104503012592298282610
1979MIMichigan9208000565588344100202183140650945713880631521155440
1979MNMinnesota4060000897393871375442551693764518311082713366
1979MOMissouri486800025662543163810267132142148097042312439819988
1979MTMontana7860001762331622601307332986314245372447
1979NCNorth Car56060002500960011374327189452201137268713490312523
1979NDNorth Dak657000403105465274177033029136071067
1979NENebraska1574000355665338115719965929512804421954296
1979NHNew Ham8870001241211522548143937310635258562882
1979NJNew Jerse7332000367474842037183321589439001811742122155251045
1979NMNew Mexi12410007272154582150250346456318385417454433
1979NVNevada7020005866123418286124645613219799313884945
1979NYNew York17649000161906209253949347160949933234308302500589124343
1979OHOhio10731000490928653409209092390950138913812831616247099
1979OKOklahoma28920001171928195329667519124293426466929212355
1979OROregon25270001378110711213299925414726440682968239759
1979PAPennsylva11731000391337242533178551802137091410966821556745679

Assignment Solution

library(forecast)

library(ggplot2)

#preparing time series object for analysis

ts<- ts(data,start = 1985,end = 2019,frequency = 12)

seasonplot(ts, s = 12,col=rainbow(16), year.labels=TRUE,main = "Homicide rate by year and month")

ggseasonplot(ts, polar=TRUE)

autoplot.ts(ts)

ggAcf(ts)

ARIMA

fit.arima<- auto.arima(ts)

with(fit.arima,plot(fitted,residuals))

checkresiduals(fit.arima)

plot(forecast(fit.arima,h = 24))

For the Exploratory Data Analysis

library(tidyverse) ## For data wrangling and visualization

library(lubridate) ## To work with dates

library(ggpubr) ## Extra visualizations and themes

library(patchwork) ## Patch visualizations together

library(hrbrthemes)## extra themes and formatting

library(scales) ## For formatting numeric variables

library(tidytext) ## Reordering within facets in ggplot2

library(pier) ## Make interactive piecharts in R

library(ggalt) ## Extra visualizations

crimes <- read_csv(Crime.csv)

mutate(year = dmy(Date_Column))

crimes %>%

group_by(Year = floor_date(Date_Column,unit = "year")) %>%

summarise(Incidents=sum(total_incidents,na.rm = TRUE)) %>%

ungroup() %>% mutate(pct_change= (Incidents-lag(Incidents))/lag(Incidents),

pct_change=replace_na(pct_change,0)) %>%

ggplot(aes(Year,Incidents))+

geom_bar(stat="identity",fill="firebrick",color="black")+

geom_line(color="steelblue",size=1.5,linetype="dashed")+

  geom_text(aes(label=percent(pct_change)),vjust=-1,color="black",face="bold")+ geom_text(aes(label=comma(Incidents)),vjust=1,fontface="bold",color="white")+

scale_y_comma(expand = c(0,0),limits = c(0,800000))+

scale_x_date(breaks = "year",date_labels ="%Y")+

theme_classic()+

labs(title = "Total Incidents over the years")

Let's visualize a simple barplot and see total incidents

crimes %>%

group_by(state¬_name) %>%

summarise(Incidents=sum(total_incidents)) %>%

ungroup() %>%

ggplot(aes(reorder(borough,Incidents),Incidents))+

geom_bar(stat = "identity",aes(fill=borough),color="black")+

coord_flip()+

scale_y_comma()+

geom_text(aes(label=comma(Incidents)),hjust=1)+

theme_classic()+

theme(legend.position = "none")+

labs(x=" ",y=" ",title = "Total Incidents for boroughs from 2008-2016 ")

Similar Samples