Published
Edited
Jun 22, 2021
Importers
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import { sondages, regions, desc } from '@taniki/regionales2021-donnees-nsppolls'
Insert cell
import { resultats, desc as descBrice } from '@taniki/regionales2021-resultats-premiertour'
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
regions
Insert cell
Insert cell
regionSondages = sondages.filter(i => i.region == regionSelect)
Insert cell
Insert cell
Insert cell
premierTour = tourSondages(regionSondages, 'Premier tour')
Insert cell
Insert cell
Inputs.table(secondTour)
Insert cell
secondTour = tourSondages(regionSondages, 'Deuxième tour')
Insert cell
tourSondages = (sondages, tour) => {
return sondages
.map(s => [s, s.tours.find(t => t.tour == tour)])
.map(([s, x]) => x?.hypotheses.map(h => ({sondage: s.id, date: s.fin_enquete, n: h.sous_echantillon, ...h})))
.filter(r => r != undefined)
.flat()
}
Insert cell
Insert cell
Insert cell
t1TetesListe = tetesListeRegion('Premier tour')
Insert cell
tetesListeRegion = (tour) => {
let data = [ ...new Set(
tourSondages(regionSondages, tour)
.map(x => x.tetes_liste)
.flat()
.map(t => t.tete_liste)
.filter(t => t != null)
)]
.map(t => [t, candidates.find(c => t.toLowerCase() == `${c['Prénom candidat']} ${c['Nom candidat']}`.toLowerCase() && c['Tête de Liste'] == 'Oui')])
.filter(t => t[1] != undefined )
.map(t => ({ 'name': t[0], info: t[1] }))

return data
}
Insert cell
Insert cell
t2TetesListe = tetesListeRegion('Deuxième tour')
Insert cell
Insert cell
// intentions = (sondages) =>
Insert cell
Insert cell
t1Intentions = premierTour
.map(t => t.tetes_liste.map(tl => ({sondage: t.sondage, date: t.date, n: t.sous_echantillon, hypothese: t.hypothese, ...tl})))
.flat()
.filter(t => t1TetesListe.map(t => t.name).includes(t.tete_liste))
.sort((a,b) => b.date.localeCompare(a.date))
Insert cell
Insert cell
t1Moyennes = [ ...d3.group(t1Intentions, i => i['tete_liste'])]
.map(t => {
let n = d3.sum(t[1], i => i.n)
let p = d3.sum(t[1], i => i.intentions * i.n) / n
let Z = jStat.normal.inv((1+0.95)/2, 0, 1)
let e = Z * Math.sqrt(((p/100) * (1 - (p/100))) / n)

return {
name: t[0],
mean: p,
n,
inf: d3.sum(t[1], i => i.erreur_inf * i.n) / d3.sum(t[1], i => i.n),
sup: d3.sum(t[1], i => i.erreur_sup * i.n) / d3.sum(t[1], i => i.n),
inf2: p - e*100,
sup2: p + e*100,
}
})
Insert cell
Inputs.table(t2Intentions)
Insert cell
t2Intentions = secondTour
.map(t => t.tetes_liste.map(tl => ({sondage: t.sondage, date: t.date, n: t.sous_echantillon, hypothese: t.hypothese, ...tl})))
.flat()
.filter(t => t2TetesListe.map(t => t.name).includes(t.tete_liste))
.sort((a,b) => b.date.localeCompare(a.date))
Insert cell
Inputs.table(t2Moyennes)
Insert cell
t2Moyennes = [ ...d3.group(t2Intentions, i => i['tete_liste'])]
.map(t => {
let n = d3.sum(t[1], i => i.n)
let p = d3.sum(t[1], i => i.intentions * i.n) / n
let Z = jStat.normal.inv((1+0.95)/2, 0, 1)
let e = Z * Math.sqrt(((p/100) * (1 - (p/100))) / n)

return {
name: t[0],
mean: p,
n,
inf: d3.sum(t[1], i => i.erreur_inf * i.n) / d3.sum(t[1], i => i.n),
sup: d3.sum(t[1], i => i.erreur_sup * i.n) / d3.sum(t[1], i => i.n),
inf2: p - e*100,
sup2: p + e*100,
}
})
Insert cell
import { raw as candidates, desc as descMinInt } from '@taniki/regionales2021-donnees-minint'
Insert cell
nuance = (c, name = 'tete_liste') => {
let found = candidates.find(x => `${x['Prénom candidat']} ${x['Nom candidat']}`.toLowerCase() == c[name].toLowerCase())
if (found) return found['Nuance candidat']
else return '???'
}
Insert cell
import {candidateColors, desc as descColors } from '@taniki/regionales2021-colors'
Insert cell
jStat = require('jstat')
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more