-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhepatite.R
More file actions
executable file
·294 lines (237 loc) · 8.59 KB
/
hepatite.R
File metadata and controls
executable file
·294 lines (237 loc) · 8.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
###############################################################
"Khaled Ben Abdallah - Data analysis of the Hepatitis dataset"
###############################################################
votre_dossier = "YOUR_FOLDER"
setwd(votre_dossier)
hepatite = read.csv("hepatite.csv", header = TRUE, stringsAsFactors=FALSE)
#Les points interro correspondent aux valeurs manquantes
#On remplace les ? par des valeurs manquantes sous format R
hepatite[hepatite=='?'] <- NA
#recoder le fichier
hepatite$SURVIE[hepatite$SURVIE==1] <- "D"
hepatite$SURVIE[hepatite$SURVIE==2] <- "V"
hepatite$SEXE[hepatite$SEXE==1] <- "M"
hepatite$SEXE[hepatite$SEXE==2] <- "F"
hepatite[4:14][hepatite[4:14]==1] <- "N"
hepatite[4:14][hepatite[4:14]==2] <- "O"
hepatite[20][hepatite[20]==1] <- "N"
hepatite[20][hepatite[20]==2] <- "O"
#le sauvegarder
write.csv(hepatite, file = "hepatite_recode.csv")
hepatite = read.csv("hepatite_recode.csv", header = TRUE, stringsAsFactors=FALSE)
#utilise pour affichage sous excel uniquement
hepatite[, 1] <- NULL
#hepatite$id_patient<- NULL
hepatite[, 15] <- as.numeric(hepatite[, 15])
hepatite[, 16] <- as.numeric(hepatite[, 16])
hepatite[, 17] <- as.numeric(hepatite[, 17])
hepatite[, 18] <- as.numeric(hepatite[, 18])
hepatite[, 19] <- as.numeric(hepatite[, 19])
##############################
"FIN TRAITEMENT VALEURS NA"
##############################
#% valeur manquantes
val_manquantes <- colMeans(is.na(hepatite))
val_manquantes
#grid.table(colMeans(is.na(hepatite)))
#si NA > 15/20% : on choisit de ne pas retenir la variable
#on supprime les variables PHOSPHATASE.ALCALINE et TAUX.DE.PROTHROMBIE du dataset
hepatite$PHOSPHATASE.ALCALINE<- NULL
hepatite$TAUX.DE.PROTHROMBIE<- NULL
#idem en ligne sur patients
ligne_val_NA = apply(hepatite, 1, function(x) sum(is.na(x))) / ncol(hepatite) * 100
id <- rownames(hepatite)
hepatite <- cbind(id=id, hepatite)
for(i in 1:nrow(hepatite)){
if(ligne_val_NA[i] > 16.7){
cat("Suppression de \n",i,"\n")
hepatite<-hepatite[!hepatite$id==i,]
}
}
hepatite$id<- NULL
#les noms de colonnes
col_names <- names(hepatite)
#variable SURVIE
hepatite$SURVIE <- as.factor(hepatite$SURVIE)
#variables de 3 à 14 en factor
hepatite[,col_names[3:14]] <- lapply(hepatite[col_names[3:14]] , factor)
#AGE n'était pas en numérique
hepatite$AGE <- as.numeric(hepatite$AGE)
#variable 20
hepatite$HISTOLOGIE <- as.factor(hepatite$HISTOLOGIE)
#verifier type : factor ou numérique ?
sapply(hepatite, class)
##############################
"DISCRETISATION"
##############################
#vue d'ensemble
summary(hepatite)
#decoupage age
q<-quantile(hepatite$AGE,probs=seq(0,1,by=.3))
AGE<-cut(hepatite$AGE,q)
tab<-table(AGE,hepatite$SURVIE)
tab
prop.table(tab,1)
#graphique
barplot(t(prop.table(tab,1) [,1]), las=3, ylim=c(0,1), main="AGE", ylab="DECES", density=0 )
abline(h=.2, lty=2)
hepatite$AGE_discret[hepatite$AGE <= 41] <- "AG <= 41"
hepatite$AGE_discret[hepatite$AGE > 41] <- "AG > 41"
#code pour envoyer ctrl + L : nettoyer la console
cat("\014")
#croisement entre deciles BILIRUBIN et variable DIE/LIVE
q<-quantile(hepatite$BILIRUBINE,na.rm = TRUE)
BILIRUBINE<-cut(hepatite$BILIRUBINE,q)
tab<-table(BILIRUBINE,hepatite$SURVIE)
tab
prop.table(tab,1)
#graphique
barplot(t(prop.table(tab,1) [,1]), ylim=c(0,1), las=3, main="BILIRUBINE", ylab="DECES", density=0 )
abline(h=.2, lty=2)
hepatite$BILIRUBINE_discret[hepatite$BILIRUBINE <= 1] <- "BI <= 1"
hepatite$BILIRUBINE_discret[hepatite$BILIRUBINE > 1] <- "BI > 1"
#decoupage SGOT
q<-quantile(hepatite$SGOT,probs=seq(0,1,by=.1),na.rm = TRUE)
SGOT<-cut(hepatite$SGOT,q)
tab<-table(SGOT,hepatite$SURVIE)
tab
prop.table(tab,1)
#graphique
barplot(t(prop.table(tab,1) [,1]), ylim=c(0,1), las=3, main="SGOT", ylab="DECES", density=0 )
abline(h=.2, lty=2)
hepatite$SGOT_discret[hepatite$SGOT <= 30] <- "SG <= 30"
hepatite$SGOT_discret[hepatite$SGOT > 30 & hepatite$SGOT <= 48] <- "SG >30 <=48"
hepatite$SGOT_discret[hepatite$SGOT > 48] <- "SG > 48"
#decoupage ALBUMIN
q<-quantile(hepatite$ALBUMINE,na.rm = TRUE)
ALBUMINE<-cut(hepatite$ALBUMINE,q)
tab<-table(ALBUMINE,hepatite$SURVIE )
prop.table(tab,1)
#graphique
barplot(t(prop.table(tab,1) [,1]), ylim=c(0,1), las=3, main="ALBUMINE", ylab="DECES", density=0 )
abline(h=.2, lty=2)
hepatite$ALBUMINE_discret[hepatite$ALBUMINE <= 3.45] <- "AL <= 3.45"
hepatite$ALBUMINE_discret[hepatite$ALBUMINE > 3.45] <- "AL > 3.45"
#on supprime de l'analyse les variable non discretisées
hepatite$AGE<- NULL
hepatite$BILIRUBINE<- NULL
hepatite$SGOT<- NULL
hepatite$ALBUMINE<- NULL
#bien remettre en factor
col_names <- names(hepatite)
hepatite[,col_names] <- lapply(hepatite[,col_names] , factor)
#sauvegarder le dataset
save(hepatite,file="hepatite.Rda")
##############################
"VCramer"
##############################
cols <- -grep(('SURVIE'),names(hepatite))
hepatite_tmp <- hepatite[,cols]
#on construit une matric vide ou on mettra les resultats
cramer <- matrix(NA,ncol(hepatite_tmp), 2)
for(i in (1 : ncol(hepatite_tmp)))
#le nom des variables
{cramer[i, 1] <- names(hepatite_tmp[i])
#le calcul
cramer[i, 2] <- sqrt(chisq.test(table(hepatite_tmp[,i],
hepatite$SURVIE))$statistic / length(hepatite_tmp[,i]))
}
#nommer les variables
colnames(cramer) <- c("variable", "V Cramer")
vcramer <- cramer [order (cramer[, 2], decreasing='T'),]
install.packages("questionr")
library("questionr")
cramer <- matrix(NA,ncol(hepatite_tmp), ncol(hepatite_tmp) )
#chaque colonne boucle sur chacune des colonnes
for(i in (1 : ncol(hepatite_tmp))){
for(j in (1 : ncol(hepatite_tmp))){
cramer[i,j] <-cramer.v(table(hepatite_tmp[,i], hepatite_tmp[,j]))
}
}
colnames(cramer) <- colnames(hepatite_tmp)
rownames(cramer) <- colnames(hepatite_tmp)
install.packages("corrplot")
library(corrplot)
corrplot(cramer, type="upper", tl.srt = 90, tl.col = "black", diag=F, addCoef.col = "black",
addCoefasPercent = T)
##############################
"ACM : PREMIERE ITERATION"
##############################
#la var illustrative : la variable a expliquer
#le tableau disjonctif est cree automatiquement par la fonction MCA, voir code ici
#https://github.com/cran/FactoMineR/blob/master/R/MCA.R
install.packages("FactoMineR")
library(FactoMineR)
install.packages("missMDA")
library(missMDA)
nb <- estim_ncpMCA(hepatite)
#on renomme les champs en prenant les trois premieres lettres
names(hepatite) <- sapply(names(hepatite),function(z) {
sub("",substring(z,0,3),"")
})
names(hepatite)[8] <- "FOIG"
names(hepatite)[9] <- "FOIF"
#fonction pour remplacer les valeurs manquantes par le mode
Mode <- function (x, na.rm) {
xtab <- table(x)
xmode <- names(which(xtab == max(xtab)))
if (length(xmode) > 1) xmode <- ">1 mode"
return(xmode)
}
#imputation par la valeur modale
for (var in 1:ncol(hepatite)) {
hepatite[is.na(hepatite[,var]),var] <- Mode(hepatite[,var], na.rm = TRUE)
}
complete <- hepatite
names(complete)
##############################
"CAH"
##############################
res.mca <- MCA (complete, quali.sup = 1, graph = TRUE)
res.hcpc <- HCPC(res.mca)
##############################
"PATIENTS ATYPIQUES"
##############################
##############################
"DEUXIEME ITERATION"
##############################
#obtenir la liste des indications disponibles
res.mca
#val propres et cos2
res.mca$eig
res.mca$var$cos2
res.mca$var$v.test
res.mca$var$contrib
res.mca$quali.sup
#graph des valeurs propres
plot(res.mca$eig[,1],type="b",main="Valeurs propres",xlab="Composante", ylab="Valeurs propres")
#patients
plot(res.mca,invisible=c("var","quali.sup"), title="Hepatite - patients",habillage=1)
#modalites
plot(res.mca,invisible="ind", title="Hepatite - modalités",autoLab="yes", cex=0.6)
#aide a l'interpretation
abline(v=.48, lty=1, col = "blue")
abline(v=-.48, lty=1, col = "blue")
abline(h=.32, lty=1, col = "green")
abline(h=-.32, lty=1, col = "green")
#variables
plot(res.mca,choix="var", title="Hepatite - variables", cex=0.7)
summary(hepatite)
#classification
res.mca <- MCA(complete,ncp=4, quali.sup = 1, graph = FALSE)
res.hcpc <- HCPC(res.mca)
load("hepatite.Rda")
#recoder la variable SURVIE
hepatite$SURVIE <- ifelse(hepatite$SURVIE == "D", 0, ifelse(hepatite$SURVIE == "V", 1, 99))
#ajouter la variable à expliquer
MCA_individus_SURVIE = res.mca$ind$coord;
MCA_individus_SURVIE = cbind(MCA_individus_SURVIE, SURVIE = hepatite$SURVIE);
#mettre variable à epliquer en premier
MCA_individus_SURVIE = MCA_individus_SURVIE[,c(5,1,2,3,4)];
install.packages("R.matlab")
library(R.matlab)
#exporter le dataset res.mca et poursuivre sous MATLAB
#un csv aurait été suffisant mais il était facile de générer un fichier
#sous format MATLAB avec R
writeMat("hepatite.mat", hepatite = MCA_individus_SURVIE)
#fin analyse des données "classique"