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

FastAPI Instrumentalisation with prometheus and grafana Part1 [Counter]

welcome to this hands-on lab on API instrumentation using Prometheus and FastAPI! In the world of modern software development, real-time API monitoring is essential for understanding usage patterns, debugging issues, and ensuring optimal performance. In this lab, we’ll demonstrate how to enhance a FastAPI-based application with Prometheus metrics to monitor its behavior effectively. We’ve already set up the lab environment for you, complete with Grafana, Prometheus, and a PostgreSQL database. While FastAPI’s integration with databases is outside the scope of this lab, our focus will be entirely on instrumentation and monitoring. For those interested in exploring the database integration or testing , you can review the code in our repository: FastAPI Monitoring Repository . What You’ll Learn In this lab, we’ll walk you through: Setting up Prometheus metrics in a FastAPI application. Instrumenting API endpoints to track: Number of requests HTTP methods Request paths Using Grafana to vi...

Join Ubuntu 20.04 to Active Directory with SSSD and SSH Access

Join Ubuntu 20.04 to Active Directory with SSSD and SSH Access  Overview This guide walks you through joining an Ubuntu 20.04 machine to an Active Directory domain using SSSD, configuring PAM for AD user logins over SSH, and enabling automatic creation of home directories upon first login. We’ll also cover troubleshooting steps and verification commands. Environment Used Component Value Ubuntu Client       ubuntu-client.bazboutey.local Active Directory FQDN   bazboutey.local Realm (Kerberos)   BAZBOUTEY.LOCAL AD Admin Account   Administrator Step 1: Prerequisites and Package Installation 1.1 Update system and install required packages bash sudo apt update sudo apt install realmd sssd libnss-sss libpam-sss adcli \ samba-common-bin oddjob oddjob-mkhomedir packagekit \ libpam-modules openssh-server Step 2: Test DNS and Kerberos Configuration Ensure that the client can resolve the AD domain and discover services. 2.1 Test domain name resol...

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...