Skip to main content

Spacial analysis of commited crime in montreal With R

R est un logiciel statistique très puissant qui permet entre autres de réaliser des graphes et de faire de l'analyse statistique. aujourd'hui nous allons utiliseé R pour placer les postes de quartier a Montréal. POur cela 2 packages vont etre necessaires, on parle de ggplot2 et ggmmap, bien sur nous devons installer ggplot2 avant ggmap sinon ca ne marchera pas. Les données proviennent du site portail données ouvertes de la ville de Montréal.

 Pour cela nous allons utilisé Rstudio qui est un IDE qui permet d'utiliser R. pour afficher la carte et les données dessus, nous devons d'abord télécharger les diffèrent packages qui nous permettront de manipuler nos données.

 -ggmap
 -ggplot2
-sp
-RgoogleMaps

certains package nous seront utiles pour la suite, donc on les télécharge des maintenant. Nous pouvons soit télécharge les données directement sur notre ordinateur ou y avoir accès du site de la ville via ce lien.

http://donnees.ville.montreal.qc.ca/dataset/91f66001-b461-4f63-aff4-cddc0fe30ffe/resource/c9f296dd-596e-48ed-9c76-37230b2c916d/download/spvmpdq.csv spvmpdq

le code ci-dessous nous permet de lire les données a partir du lien et d'afficher une carte avec les différents poste de police dessus

 spvmpdq <- read.csv("C:\\Users\\emmanuel\\AppData\\Local\\Temp\\RtmpAplCdo\\data2e3871a6f9e")

   View(spvmpdq)
qmplot(longitude , latitude , data = spvmpdq, colour = I('red'), size = I(3), darken = .3)


et voici le résultat, bien sur on va vouloir avoir une plus belle carte avec beaucoup plus d'informations


par exemple on peut vouloir un autre type de carte, dans ce cas il suffit de changer source, Normalement la fonction qmplot vient avec la source = "stamen" qui a produit la carte ci dessus, mais nous pouvons changer cela, Par exemple avec source = "osm" voici le resultat


qmplot(longitude , latitude , data = spvmpdq, colour = I('red'), size = I(3), darken = .3,source = "osm", zoom = 12)


ou encore on peu s'amuser avec le style de carte et le type aussi avec la commande maptype, par exemple.
qmplot(longitude , latitude , data = spvmpdq, colour = I('red'), size = I(3), darken = .3,source = "google", zoom = 11, maptype = "roadmap")



On peut même faire apparaitre l'intensité d'un évènement sur une carte avec r, 

Nous allons donc utiliser un autre source de donne, On va essayer de voir les endroits ou il y a le plus d'entre par infraction a montreal pour  2015-2016. 
Premièrement, nous allons telecharge les données avec notre bout de code

 montreal <- read.csv("C:\\Users\\emmanuel\\AppData\\Local\\Temp\\RtmpUDMEdM\\data24cc7d24e28")

apres avoir lu les donnees il nous suffit d'utiliser le package ggmap et ses fnctions  nous aurons

montreal <- get_map(location = "montreal", zoom = 11, color = "color",
                   source = "google" )
montrealMap <- ggmap(montreal, base_layer = ggplot(aes(x = LONG, y = LAT),
                                                 data = infractions))
montrealMap +
  stat_density2d(aes(x = LONG, y = LAT, fill = ..level.., alpha = ..level..),
                 bins = 5, geom = "polygon",
                 data = infractions) +
  scale_fill_gradient(low = "black", high = "red")


On voit que les entrées par effraction sont majoritairement concentres dans les zones comme Hochelaga,le plateau et maison neuve selon les données de la ville, 

et encore il y a moins d'entreés par infraction la nuit que le jour et ggmap peut encore nous fournir une carte qui pourra nous aider, en représentant sur les carte les endroits les plus a risque selon ou on est a Montréal, pour cela on n'a qu'a faire la commande 

montreal <- get_map(location = "montreal", zoom = 11, color = "color",
                   source = "google" )
montrealMap <- ggmap(montreal, base_layer = ggplot(aes(x = LONG, y = LAT),
                                                 data = infractions))
montrealMap +
  stat_density2d(aes(x = LONG, y = LAT, fill = ..level.., alpha = ..level..),
                 bins = 5, geom = "polygon",
                 data = infractions) +
  scale_fill_gradient(low = "black", high = "red") +
  facet_wrap(~ QUART)


voila la carte produite selon les moments de la journée, cela permet de voir les zones les plus a risque et on voit que le territoire n'a pas trop change. 

Comments

  1. This comment has been removed by a blog administrator.

    ReplyDelete

Post a Comment

Popular posts from this blog

Observability with grafana and prometheus (SSO configutation with active directory)

How to Set Up Grafana Single Sign-On (SSO) with Active Directory (AD) Grafana is a powerful tool for monitoring and visualizing data. Integrating it with Active Directory (AD) for Single Sign-On (SSO) can streamline access and enhance security. This tutorial will guide you through the process of configuring Grafana with AD for SSO. Prerequisites Active Directory Domain : Ensure you have an AD domain set up. Domain: bazboutey.local AD Server IP: 192.168.170.212 Users: grafana (for binding AD) user1 (to demonstrate SSO) we will end up with a pattern like this below Grafana Installed : Install Grafana on your server. Grafana Server IP: 192.168.179.185 Administrator Privileges : Access to modify AD settings and Grafana configurations. Step 1: Configure AD for LDAP Integration Create a Service Account in AD: Open Active Directory Users and Computers. Create a user (e.g., grafana ). Assign this user a strong password (e.g., Grafana 123$ ) and ensure it doesn’t expire. Gather Required AD D...

Deploying a Scalable Monitoring Stack Lab on AWS using Terraform and Ansible

Deploying a Scalable Monitoring Stack Lab on AWS using Terraform and Ansible Introduction Effective monitoring is a cornerstone of cloud infrastructure management, ensuring high availability and performance. This guide provides a professional walkthrough on deploying Prometheus , Grafana , and Node Exporter on AWS using Terraform for infrastructure provisioning and Ansible for configuration management. This lab will create a prometheus server and a grafana server, It will install node exporter on both server. You should be able to see the metrics in grafana, we already install a node exporter dashboard for the user. The diagram below will give you an idea of what the architecture will look like If you want to replicate this lab, you can find the complete code repository here: GitHub - MireCloud Terraform Infra .  Infrastructure Setup with Terraform 1. Creating a Dedicated VPC To ensure isolation, we define a VPC named Monitoring with a CIDR block of 10.0.0.0/16 . reso...

Building a Static Website on AWS with Terraform

The Journey to a Fully Automated Website Deployment A few weeks ago, I found myself needing to deploy a simple static website . Manually setting up an S3 bucket, configuring permissions, and linking it to a CloudFront distribution seemed like a tedious process. As someone who loves automation, I decided to leverage Terraform to simplify the entire process. Why Terraform? Infrastructure as Code (IaC) is a game-changer. With Terraform, I could:  Avoid manual setup errors  Easily reproduce and  Automate security best practices Instead of clicking through AWS settings, I wrote a few Terraform scripts and deployed everything in minutes. Let me walk you through how I did it!  Architecture Overview The architecture consists of three main components: User:  The end user accesses the website via a CloudFront URL.  CloudFront Distribution:  Acts as a content delivery network (CDN) to distribute content efficiently, reduce latency, and enhance security. It ...