Skip to content

Instantly share code, notes, and snippets.

View jeanbaptisteb's full-sized avatar

Jean-Baptiste Bertrand jeanbaptisteb

  • CNRS
View GitHub Profile
@jeanbaptisteb
jeanbaptisteb / type.m.contingency.R
Last active July 31, 2023 15:12
Type M error for effect size (cohen's W) in contingency tables
#proof of concept for calculating a type M error (exaggeration ratio) for effect size in contingency tables
# type M error is defined as "The expectation of the absolute value of the estimate divided by the effect size,
# if statistically significantly different from zero."
# http://www.stat.columbia.edu/~gelman/research/published/retropower_final.pdf
type.m.error = function(samp.size, dof, real.w, alpha=0.05, power=0.8, n.sim=10000, seed=0) {
set.seed(seed)
chis = rchisq(n=n.sim, df=dof, ncp =samp.size*real.w^2 )
crit.value = qchisq(p=alpha, df=dof, lower.tail=T)
chis = chis[chis >= crit.value]
@jeanbaptisteb
jeanbaptisteb / plot_mca.py
Last active June 16, 2023 07:00
function to plot static representation of ACM with the output from the PRINCE library
from adjustText import adjust_text
import matplotlib.pyplot as plt
import seaborn as sns
def plot_acm(model, data, x=0, y=1, dims = (11, 8),
keep_prefix=True,
adjust_labels=True):
'''
Parameters
----------
model : prince.mca.MCA
@jeanbaptisteb
jeanbaptisteb / fei.py
Last active May 19, 2023 14:57
Python implementation of the פ (Fei) effect size for goodness-of-fit tests. Beta version. Confidence intervals may differ slightly from the R implementation.
"""
Python implementation of the פ (Fei) effect size for goodness-of-fit tests.
This is a beta version. Confidence intervals may differ from the original R implementation (see the "Returns" section below).
Reference: Ben-Shachar, M.S.; Patil, I.; Thériault, R.; Wiernik, B.M.; Lüdecke, D. Phi, Fei, Fo, Fum: Effect Sizes for Categorical Data That Use the Chi-Squared Statistic. Mathematics 2023, 11, 1982. https://doi.org/10.3390/math11091982
For the original R implementation of Fei, see the "effectsize" library https://easystats.github.io/effectsize/reference/phi.html.
"""
from scipy.stats import chisquare, ncx2
from scipy.optimize import least_squares
from collections import namedtuple
@jeanbaptisteb
jeanbaptisteb / ISCO_08_to_ISEI.py
Last active April 26, 2023 10:10
Python dictionary to convert ISCO-08 codes to International Socio-Economic Index of occupational status (ISEI) values.
'''
Python dictionaries to convert ISCO-08 occupation codes to International Socio-Economic Index of occupational status (ISEI) values.
'isco_08_to_isei' is adapted from isqoisei08.sps from H. Ganzeboom's website.
'isco_08_to_isei_PISA' is an amended version of isco_08_to_isei, based on PISA 2018 technical report,
with 3 additional pseudo ISCO codes for stay-at-home people (9701), students (9702), and retired or unemployed people (9703).
References:
* “International Stratification and Mobility File: Conversion Tools.” Amsterdam: Department of Social Research Methodology, http://www.harryganzeboom.nl/ismf/index.htm. <Last revised: 2019/10/05> <Consulted on 24th April 2023>.
* OECD. (2018). Chapter 16 : Scaling procedures and construct validation of context questionnaire data. In PISA 2018 Technical Report. OECD Publishing. https://www.oecd.org/pisa/data/pisa2018technicalreport/PISA2018_Technical-Report-Chapter-16-Background-Questionnaires.pdf
@jeanbaptisteb
jeanbaptisteb / cohenH.py
Created April 17, 2023 13:58
Cohen's h
#function to calculate Cohen's h (effect size for measuring distance between two proportions)
#https://en.wikipedia.org/wiki/Cohen's_h
import numpy as np
def cohenH(p1,p2):
phi1 = 2*np.arcsin(np.sqrt(p1))
phi2 = 2*np.arcsin(np.sqrt(p2))
h = phi1 - phi2
return h
@jeanbaptisteb
jeanbaptisteb / tirage_aleatoire_groupes.py
Created February 2, 2023 20:46
Ce script Python permet de découper aléatoirement une liste Excel en X groupes de tailles égales (ou à peu près égales), puis de les enregistrer chacun dans des fichiers Excel séparés.
import numpy as np
import pandas, os
root = r"C:\Documents\mes documents" #répertoire où se trouve le fichier. A modifier.
nom_fichier = "liste_complete.xlsx" #nom du fichier Excel contenant la liste complète que l'on souhaite découper
df = pandas.read_excel(
os.path.join(root, nom_fichier), #lire le fichier pour le traiter
header=None #Important : si les colonnes ont des intitulés (du type "nom", "prénom", etc.), supprimer « header="None" » de ce code. Sinon, si les colonnes n'ont pas d'intitulé, laisser "header=None" (autrement le script n'inclut pas la première ligne dans le tirage aléatoire).
)
df = df.sample(frac=1) #on mélange la liste aléatoirement
nb_de_groupes = 5 #changer cette valeur selon le nombre de groupes qu'on souhaite (ici 5, par exemple)
@jeanbaptisteb
jeanbaptisteb / CC0_en_sentence_sample.txt
Created August 4, 2018 09:31
list of sentences under Creative Commons Zero licence, from https://en.wikipedia.org
The following exhibition was Oggetti di poesia, dedicated to Emilio Villa.
A round table was also organized, featuring Nanni Balestrini, Paolo Fabbri, and Lamberto Pignotti.
After the transfer, Della Grazia continued to organize exhibitions and research.
In the same year the artist's books of ANS were lent to Deliberatamente.
La passione di un collezionista exhibition.
Ugo Carrega is an Italian artist and poet.
At the Centro Suolo, Carrega organized the first international exhibition of "advanced poetry".
The center closed down a few months later.
Sono importantissime in particolare le mostre Raccolta italiana di Nuova Scrittura e SCRITTURA ATTIVA.
Processi artistici di scrittura in dodici dimostrazioni ESpositive.
@jeanbaptisteb
jeanbaptisteb / cc0_fr_sentence_sample.txt
Created August 1, 2018 17:11
list of sentences under Creative Commons Zero licence, from http://fr.wikipedia.org
En mille sept cent quatre-vingt-quatorze, l'esclavage est aboli.
Olympe de Gouges est une des premières féministes françaises.
La viande de porc est marinée avec un mélange de piments, épices et ananas.
Il est alors recouvert de fromage à pàte pressée cuite préalablement rapé.
On y ajoute parfois également du jambon et/ou un œuf.
Le tout est ensuite passé au four pour cuisson.
Oignons grelot et cornichons au vinaigre constitue l'accompagnement le plus courant.
Dans le cas des yaourts, les ferments lactiques présents contribuent également à sa dégradationdeux.
Fruit d'un affinage assez court se distingue par son cerclage d'épicéaun.
Il fut suspendu en mille sept cent quatre-vingt-quinze par l'adoption du XIe amendement.