olivier godechot

Faut-il travailler sous R pour être heureux ?

Enfin une question qui ne manque pas d'R, et qui montre par la même occasion l'intérêt des fonctions anatext et wordcount dévoilées dans un précédent billet. Voici quelques lignes de codes qui vous montreront qu'il est relativement facile sinon d'être heureux du moins de reproduire les tableaux de l'article éponyme de Christian Baudelot et Michel Gollac. Pour cela, je vous invite à télécharger les données, et ensuite de soumetre dans R l'ensemble des fonctions que l'on trouve ici . Enfin le programme ci-dessous devrait faire l'affaire.

#IMPORTATION

library("foreign")

bg<-read.xport("d:/Olivier/bw.xpt")

 

#VERIFICATION

summary(bg)

str(bg)

 

#CREATION D'UNE VARIABLE TOUT EN UN

bg$heureu<-paste(bg$HEUREU1,bg$HEUREU2,bg$HEUREU3, sep="")

 

#CREATION D'UN IDENTIFIANT

bg$ident<-row.names(bg)

 

View(bg)

#ANALYSE DES RESULTATS TEXTUELS

heu<-anatext(bg$heureu,del.punctuation=TRUE)

str(heu)

 

View(heu$text)

 

heu$wordnb

 

heu$s2nb[heu$s2nb$Freq>10,]

heu$s3nb[heu$s3nb$Freq>10,]


 

#ANALYSE FACTORIELLE

#Recupération des variables utilisées pour l'AFC

heu<-anatext(bg$heureu,keep=bg[,c("CSDET","SEXE",

"ident")])

str(heu)

 

tle<-heu$text

 

#RETRAVAIL EXPRESSION DEUX MOTS

tle$words[tle$segment2=="DU TRAVAIL"]<-"DU TRAVAIL"

tle$words[tle$segment2=="LE TRAVAIL"]<-"LE TRAVAIL"

tle$words[tle$segment2=="S ENTENDRE"]<-"S ENTENDRE"

tle$words[tle$segment2=="TEMPS LIBRE"]<-"TEMPS LIBRE"

 

#ON SE LIMITE AUX MOTS FREQUENCE >25

tle<-tle[tle$Freq>25 & tle$CSDET != "84" & tle$CSDET != "99",]

str(tle)

#Technique pour supprimer les labels vides !

tle$CSDET<-factor(tle$CSDET)

table(tle$CSDET)

 

tab<-table(tle$CSDET,tle$words)

str(tab)

 

#OPERATION : TRANSFORMATION DE MON FICHIER

#MATRICE EN BDD EN COLONNE.

# BIZARRE CERTES... MAIS LE SEUL TRUC QUE J'AI TROUVE

tab2<-as.data.frame(rbind(tab))

View(tab2)

#SI ON N'A PAS ENCORE INSTALLE FACTOMINER

install.packages("FactoMineR")

 

#ANALYSE FACTORIELLE AVEC FactoMineR

library(FactoMineR)

 

af<-CA(tab2)

 

plot.CA(af,axes=c(1,2),invisible= c("row"),cex=0.6)

plot.CA(af,axes=c(1,2),invisible= c("col"),cex=0.6)

 

##LA MÊME EN ZOOMANT LE CENTRE

plot.CA(af,axes=c(1,2),xlim=c(-0.6,1),

ylim=c(-0.6,1),invisible= c("row"),cex=0.6)

plot.CA(af,axes=c(1,2),xlim=c(-0.4,0.4),

ylim=c(-0.4,0.4),invisible= c("row"),cex=0.6)

 

help(plot.CA)

 

#VARIABLE DE COMPTAGE DES MOTS

bg$travail<-wordcount("*TRAVAIL*",bg$heu,dich=TRUE)

bg$emploi<-wordcount("*EMPLOI*",bg$heu,dich=TRUE)

bg$promet<-pmax(wordcount("*PROFESSION*",bg$heu,

dich=TRUE),wordcount("METIER",bg$heu,dich=TRUE))

 

bg$metier<-wordcount("*METIER*",bg$heu,dich=TRUE)

bg$boulot<-wordcount("*BOULOT*",bg$heu,dich=TRUE)

 

bg$work<-pmax(bg$travail,bg$emploi,bg$promet,

 

bg$boulot,bg$metier)

 

#Tableaux 2

 

prop.table(table(bg$SEXE,bg$travail),1)

 

prop.table(table(bg$QUESTBIS,bg$travail),1)

 

prop.table(table(bg$AGEBIS,bg$travail),1)

 

prop.table(table(bg$CSBIS,bg$travail),1)

 

#Régression. Tableau 1

 

#Situations de référence

bg$SEXE<-relevel(bg$SEXE,2)

bg$CSTERS<-relevel(bg$CSTERS,"OuvIncChômeu")

bg$AGEBIS<-relevel(bg$AGEBIS,"35-49ans")

bg$VIECOU2<-1-bg$VIECOU1

bg$NBENF2<-1-bg$NBENF1

 

summary(glm(work~SEXE+CSTERS+DIPLOBIS+QUESTBIS+CSPBIS+

AGEBIS+VIECOU2+NBENF2,data=bg[bg$QUEST %in% c("1","2","3"),],family=binomial(link = "logit")))

 

#Pour les hommes

summary(glm(work~CSTERS+DIPLOBIS+QUESTBIS+CSPBIS+

AGEBIS+VIECOU2+NBENF2,data=bg[bg$QUEST %in% c("1","2","3") & bg$SEXE==1,],family=binomial(link = "logit")))

 

#Pour les femmes

summary(glm(work~CSTERS+DIPLOBIS+QUESTBIS+CSPBIS+

AGEBIS+VIECOU2+NBENF2,data=bg[bg$QUEST %in% c("1","2","3") & bg$SEXE==2,],family=binomial(link = "logit")))

 

#Tableaux 3

prop.table(table(bg$CSBIS[bg$QUEST %in% c("1","2")],

wordcount("AVOIR",bg$heu[bg$QUEST %in% c("1","2")],dich=TRUE)),1)

 

prop.table(table(bg$CSBIS[bg$QUEST %in% c("1","2")],

wordcount("ETRE",bg$heu[bg$QUEST %in% c("1","2")],dich=TRUE)),1)

 

prop.table(table(bg$CSBIS[bg$QUEST %in% c("1","2")],

wordcount("FAIRE",bg$heu[bg$QUEST %in% c("1","2")],dich=TRUE)),1)

 

prop.table(table(bg$CSBIS[bg$QUEST %in% c("1","2")],

wordcount("SANTE",bg$heu[bg$QUEST %in% c("1","2")],dich=TRUE)),1)

 

#Autre méthode où l'on compte toutes les occurences du mot par individu

#et non la présence absence

tapply(wordcount("*AVOIR*",bg$heu[bg$QUEST %in% c("1","2")]),

bg$CSBIS[bg$QUEST %in% c("1","2")],mean)

 

tapply(wordcount("*SANTE*",bg$heu[bg$QUEST %in% c("1","2")]),

bg$CSBIS[bg$QUEST %in% c("1","2")],mean)

 

#Tests

chisq.test(table(bg$CSBIS[bg$QUEST %in% c("1","2")],

wordcount("FAIRE",bg$heu[bg$QUEST %in% c("1","2")],dich=TRUE)),correct = FALSE)

 

#OU T de student

t.test(wordcount("*FAIRE*",bg$heu[bg$QUEST %in% c("1","2")])~

bg$CSBIS[bg$QUEST %in% c("1","2")])



Tweets (rarely/rarement): @OlivierGodechot

[Webmestre]

[Fil rss]

[V. 0.93]

HOP

A CMS


000

clics / mois.