The code below is for the time series of all states on NDPS
library(readr)
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_data <- read_csv("Crime_data.csv")
## Rows: 5 Columns: 30
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (30): Year, Andhra Pradesh, Arunachal Pradesh, Assam, Bihar, Chhattisgar...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
attach(Crime_data)
Tempdatat <- Crime_data %>% select(c(2:30))
Tempdatat
#for multiple time series in one plot vertical
ndps_2 <-ts(Tempdatat, start = 2017,
frequency = 1)
ndps_2
## Time Series:
## Start = 2017
## End = 2021
## Frequency = 1
## Andhra Pradesh Arunachal Pradesh Assam Bihar Chhattisgarh Goa Gujarat
## 2017 682 124 354 749 743 168 69
## 2018 534 122 478 615 712 222 150
## 2019 717 124 841 697 707 218 289
## 2020 866 132 983 964 875 147 308
## 2021 1635 264 2291 1469 1123 121 461
## Haryana Himachal Pradesh Jammu & Kashmir Jharkhand Karnataka Kerala
## 2017 2200 1010 991 204 1126 9244
## 2018 2587 1342 938 237 1030 8724
## 2019 2677 1439 1173 242 1652 9245
## 2020 3060 1538 NA 415 4054 4968
## 2021 2741 1537 NA 609 5787 5695
## Madhya Pradesh Maharashtra Manipur Meghalaya Mizoram Nagaland Odisha
## 2017 1286 14634 275 56 139 81 573
## 2018 1874 12195 381 81 164 66 573
## 2019 3432 14158 338 117 160 142 980
## 2020 3155 4714 304 76 97 115 1179
## 2021 4068 10087 354 69 122 154 1642
## Punjab Rajasthan Sikkim Tamil Nadu Telangana Tripura Uttar Pradesh
## 2017 12356 1596 3 3812 387 84 7439
## 2018 11654 1862 7 3717 311 431 8821
## 2019 11536 2592 20 4329 464 316 10198
## 2020 6909 2743 19 5403 509 307 10852
## 2021 9972 2989 52 6852 1346 357 10432
## Uttarakhand West Bengal
## 2017 1017 1724
## 2018 1064 1479
## 2019 1396 1479
## 2020 1282 1626
## 2021 1762 1890
#plot(ndps_2, xlab ="year", ylab ="Cases", main ="States_NDPS", col.main ="darkgreen", facets= FALSE)
The code is for doing time series using #ggplots2 #This document explains time series related plotting using ggplot2 and {ggfortify}. https://cran.r-project.org/web/packages/ggfortify/vignettes/plot_ts.html
library(ggfortify)
## Loading required package: ggplot2
library(ggplot2)
#theme_set(theme_bw())
#autoplot(ndps, ts.colour = 'red', ts.linetype = 'dashed')
autoplot(ndps_2, facets = FALSE) +
ggtitle("Time Series Plot of the `NDPS' Time-Series") +
theme(plot.title = element_text(hjust = 0.5)) #for centering the text
## Warning: Removed 2 row(s) containing missing values (geom_path).
The code is for two variables with side-by-side data
#library(lubridate)
#ndpsKar <- ts(cbind(x, y), start = 2017, frequency = 1)
#plot(ndpsKar, xlab ="year", ylab ="Cases", main ="Kar_NDPS", col.main ="darkgreen")
Below *code* is for creating the forecasting using “ARIMA” model
library(forecast)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Registered S3 methods overwritten by 'forecast':
## method from
## autoplot.Arima ggfortify
## autoplot.acf ggfortify
## autoplot.ar ggfortify
## autoplot.bats ggfortify
## autoplot.decomposed.ts ggfortify
## autoplot.ets ggfortify
## autoplot.forecast ggfortify
## autoplot.stl ggfortify
## autoplot.ts ggfortify
## fitted.ar ggfortify
## fortify.ts ggfortify
## residuals.ar ggfortify
#fit <- auto.arima(ndps_2)
#forecast for next 2 years
#forecast(fit, 2)
#plot(forecast(fit, 2), xlab ="year", ylab ="Cases", main ="Forecast_NDPS", col.main ="darkgreen")