1 #Author: Pierre Ratinaud
2 #Copyright (c) 2008-2009 Pierre Ratinaud
6 #fonction pour la double classification
7 #cette fonction doit etre splitter en 4 ou 5 fonctions
9 AssignClasseToUce <- function(listuce, chd) {
10 print('assigne classe -> uce')
14 fille<-function(classe,classeuce) {
15 listfm<-unique(unlist(classeuce[classeuce[,classe%/%2]==classe,]))
16 listf<-listfm[listfm>=classe]
22 croiseeff <- function(croise, classeuce1, classeuce2) {
25 for (i in 1:ncol(classeuce1)) {
30 for (j in 1:ncol(classeuce2)) {
33 croise[cl1 - 1, clj1 -1] <- length(which(classeuce1[,i] == cl1 & classeuce2[,j] == clj1))
34 croise[cl1 - 1, clj2 -1] <- length(which(classeuce1[,i] == cl1 & classeuce2[,j] == clj2))
35 croise[cl2 - 1, clj1 -1] <- length(which(classeuce1[,i] == cl2 & classeuce2[,j] == clj1))
36 croise[cl2 - 1, clj2 -1] <- length(which(classeuce1[,i] == cl2 & classeuce2[,j] == clj2))
42 addallfille <- function(lf) {
44 for (i in 1:length(lf)) {
45 if (! is.null(lf[[i]])) {
50 if (f1 > length(lf)) {
51 for (j in (length(lf) + 1):f2) {
62 getfille <- function(nlf, classe, pf) {
63 if (length(nlf[[classe]]) == 1) {
66 pf <- c(pf, nlf[[classe]])
67 c1 <- nlf[[classe]][1]
68 c2 <- nlf[[classe]][2]
69 pf <- getfille(nlf, c1, pf)
70 pf <- getfille(nlf, c2, pf)
75 getmere <- function(list_mere, classe) {
79 pf <- c(pf, list_mere[[i]])
85 getfillemere <- function(list_fille, list_mere, classe) {
86 return(c(getfille(list_fille, classe, NULL), getmere(list_mere, classe)))
89 #nbt nbcl = nbt+1 tcl=((nbt+1) *2) - 2 n1[,ncol(n1)], nchd1[,ncol(nchd1)]
90 Rchdtxt<-function(uceout, chd1, chd2 = NULL, mincl=0, classif_mode=0, nbt = 9) {
91 #FIXME: le nombre de classe peut etre inferieur
93 tcl <- ((nbt+1) * 2) - 2
94 #Assignation des classes
95 classeuce1<-AssignClasseToUce(listuce1,chd1$n1)
96 if (classif_mode==0) {
97 classeuce2<-AssignClasseToUce(listuce2,chd2$n1)
99 classeuce2<-classeuce1
102 #calcul des poids (effectifs)
104 makepoids <- function(classeuce, poids) {
110 poids[cl1 - 1] <- length(which(classeuce[,i] == cl1))
111 poids[cl2 - 1] <- length(which(classeuce[,i] == cl2))
116 # makepoids<-function(classeuce,poids) {
117 # for (classes in 2:(tcl + 1)){
118 # for (i in 1:ncol(classeuce)) {
119 # if (poids[(classes-1)]<length(classeuce[,i][classeuce[,i]==classes])) {
120 # poids[(classes-1)]<-length(classeuce[,i][classeuce[,i]==classes])
127 poids1<-vector(mode='integer',length = tcl)
128 poids1<-makepoids(classeuce1,poids1)
129 if (classif_mode==0) {
130 poids2<-vector(mode='integer',length = tcl)
131 poids2<-makepoids(classeuce2,poids2)
136 print('croisement classif')
138 # croise=matrix(ncol=tcl,nrow=tcl)
140 # docroise <- function(croise, classeuce1, classeuce2) {
141 # #production du tableau de contingence
142 # for (i in 1:ncol(classeuce1)) {
143 # #poids[i]<-length(classeuce1[,i][x==classes])
144 # for (j in 1:ncol(classeuce2)) {
145 # tablecroise<-table(classeuce1[,i],classeuce2[,j])
146 # tabcolnames<-as.numeric(colnames(tablecroise))
147 # #tabcolnames<-c(tabcolnames[(length(tabcolnames)-1)],tabcolnames[length(tabcolnames)])
148 # tabrownames<-as.numeric(rownames(tablecroise))
149 # #tabrownames<-c(tabrownames[(length(tabrownames)-1)],tabrownames[length(tabrownames)])
150 # for (k in (ncol(tablecroise)-1):ncol(tablecroise)) {
151 # for (l in (nrow(tablecroise)-1):nrow(tablecroise)) {
152 # croise[(tabrownames[l]-1),(tabcolnames[k]-1)]<-tablecroise[l,k]
159 croise <- croiseeff( matrix(ncol=tcl,nrow=tcl), classeuce1, classeuce2)
160 if (classif_mode == 0) {ind <- (nbcl * 2)} else {ind <- nbcl}
162 mincl<-round(nrow(classeuce1)/ind)
170 #tableau des chi2 signes
174 # nr <- nrow(classeuce1)
175 # newchicroise <- function(croise, mincl, nr, poids1, poids2) {
176 # chicroise <- croise
177 # chicroise[which(croise < mincl)] <- 0
178 # tocompute <- which(chicroise > 0, arr.ind = TRUE)
179 # for (i in 1:nrow(tocompute)) {
180 # chitable <- matrix(ncol=2,nrow=2)
181 # chitable[1,1] <- croise[tocompute[i,1], tocompute[i,2]]
182 # chitable[1,2] <- poids1[tocompute[i,1]] - chitable[1,1]
183 # chitable[2,1] <- poids2[tocompute[i,2]] - chitable[1,1]
184 # chitable[2,2] <- nr - poids2[tocompute[i,2]] - chitable[1,2]
185 # chitest<-chisq.test(chitable,correct=FALSE)
186 # chicroise[tocompute[i,1], tocompute[i,2]] <- ifelse(chitable[1,1] > chitest$expected[1,1], round(chitest$statistic,digits=7), -round(chitest$statistic,digits=7))
193 dochicroise <- function(croise, mincl) {
195 for (i in 1:nrow(croise)) {
196 for (j in 1:ncol(croise)) {
197 if (croise[i,j]==0) {
199 } else if (croise[i,j]<mincl) {
202 chitable<-matrix(ncol=2,nrow=2)
203 chitable[1,1]<-croise[i,j]
204 chitable[1,2]<-poids1[i]-chitable[1,1]
205 chitable[2,1]<-poids2[j]-chitable[1,1]
206 chitable[2,2]<-nrow(classeuce1)-poids2[j]-chitable[1,2]
207 chitest<-chisq.test(chitable,correct=FALSE)
208 if ((chitable[1,1]-chitest$expected[1,1])<0) {
209 chicroise[i,j]<--round(chitest$statistic,digits=7)
211 chicroise[i,j]<-round(chitest$statistic,digits=7)
219 chicroise <- dochicroise(croise, mincl)
222 #determination des chi2 les plus fort
223 chicroiseori<-chicroise
225 doxy <- function(chicroise) {
228 listxy <- which(chicroise > 3.84, arr.ind = TRUE)
230 val <- chicroise[which(chicroise > 3.84)]
231 ord <- order(val, decreasing = TRUE)
232 listxy <- listxy[ord,]
233 #for (i in 1:nrow(listxy)) {
234 # if ((!listxy[,2][i] %in% listx) & (!listxy[,1][i] %in% listy)) {
235 # listx <- c(listx, listxy[,2][i])
236 # listy <- c(listy, listxy[,1][i])
239 xy <- list(x = listxy[,2], y = listxy[,1])
242 xy <- doxy(chicroise)
250 # maxi[i]<-which.max(chicroise)
251 # chimax[i]<-chicroise[maxi[i]]
252 # chicroise[maxi[i]]<-0
254 # testpres<-function(x,listcoord) {
255 # for (i in 1:length(listcoord)) {
256 # if (x==listcoord[i]) {
264 # c.len=nrow(chicroise)
265 # r.len=ncol(chicroise)
270 # #on garde une valeur par ligne / colonne
271 # for (i in 1:length(maxi)) {
272 # #coordonnées de chi2 max
273 # #coord <- arrayInd(maxi[i], dim(chicroise))
274 # #x.co <- coord[1,2]
275 # #y.co <- coord[1,1]
276 # x.co<-ceiling(maxi[i]/c.len)
277 # y.co<-maxi[i]-(x.co-1)*c.len
280 # #print(arrayInd(maxi[i], dim(chicroise)))
281 # a<-testpres(x.co,listx)
282 # b<-testpres(y.co,listy)
294 #pour ecrire les resultats
295 for (i in 1:length(listx)) {
296 txt<-paste(listx[i]+1,listy[i]+1,sep=' ')
297 txt<-paste(txt,croise[listy[i],listx[i]],sep=' ')
298 txt<-paste(txt,chicroiseori[listy[i],listx[i]],sep=' ')
302 #colonne de la classe
303 #trouver les filles et les meres
304 trouvefillemere<-function(classe,chd) {
305 unique(unlist(chd[chd[,classe%/%2]==classe,]))
309 #----------------------------------------------------------------------
310 findbestcoord <- function(classeuce1, classeuce2) {
314 #fillemere1 <- unique(classeuce1)
315 #if (classif_mode == 0) {
316 # fillemere2 <- unique(classeuce2)
318 # fillemere2 <- fillemere1
322 listcoordok <- list()
325 lf1 <- addallfille(chd1$list_fille)
326 if (classif_mode == 0) {
327 lf2 <- addallfille(chd2$list_fille)
331 lme1 <- chd1$list_mere
332 if (classif_mode == 0) {
333 lme2 <- chd2$list_mere
337 for (first in 1:length(listx)) {
344 #listxp<-listx[first:length(listx)]
345 #listxp<-c(listxp,listx[1:(first-1)])
346 #listyp<-listy[first:length(listy)]
347 #listyp<-c(listyp,listy[1:(first-1)])
348 listxp <- listxp[order(listx, decreasing = TRUE)]
349 listyp <- listyp[order(listx, decreasing = TRUE)]
350 #listxp<-c(listxp[first:length(listx)], listx[1:(first-1)])
351 #listyp<-c(listyp[first:length(listy)], listy[1:(first-1)])
352 for (i in 1:length(listx)) {
353 if( (!(listxp[i]+1) %in% f1) & (!(listyp[i]+1) %in% f2) ) {
357 coordok <- rbind(coordok, c(listyp[i] + 1,listxp[i] + 1))
358 #print(c(listyp[i] + 1,listxp[i] + 1))
359 un1 <- getfillemere(lf2, chd2$list_mere, listxp[i] + 1)
361 f1 <- c(f1, listxp[i] + 1)
362 un2 <- getfillemere(lf1, chd1$list_mere, listyp[i] + 1)
364 f2 <- c(f2, listyp[i] + 1)
368 #if (nrow(coordok) > maxcl) {
370 # listcoordok <- list()
371 listcoordok[[nb]] <- coordok
372 # maxcl <- nrow(coordok)
373 #} else if (nrow(coordok) == maxcl) {
375 # listcoordok[[nb]] <- coordok
378 listcoordok <- unique(listcoordok)
381 if (length(listcoordok) > 1) {
383 for (i in 1:length(listcoordok)) {
386 for (j in 1:nrow(listcoordok[[i]])) {
387 chi<-c(chi,croise[(listcoordok[[i]][j,1]-1),(listcoordok[[i]][j,2]-1)])
388 uce<-c(uce,chicroiseori[(listcoordok[[i]][j,1]-1),(listcoordok[[i]][j,2]-1)])
390 if (maxchi < sum(chi)) {
396 print(suce/nrow(classeuce1))
400 #---------------------------------------------------------------------------------
401 #pour trouver une valeur dans une liste
402 #is.element(elem, list)
404 oldfindbestcoord <- function(listx, listy) {
406 trouvecoordok<-function(first) {
411 listxp<-listx[first:length(listx)]
412 listxp<-c(listxp,listx[1:(first-1)])
413 listyp<-listy[first:length(listy)]
414 listyp<-c(listyp,listy[1:(first-1)])
415 for (i in 1:length(listxp)) {
416 if (!(listxp[i]+1)%in%fillemere1) {
417 if (!(listyp[i]+1)%in%fillemere2) {
418 coordok<-rbind(coordok,c(listyp[i]+1,listxp[i]+1))
419 fillemere1<-c(fillemere1,trouvefillemere(listxp[i]+1,chd2$n1))
420 fillemere2<-c(fillemere2,trouvefillemere(listyp[i]+1,chd1$n1))
426 #fonction pour trouver le nombre maximum de classes
427 findmaxclasse<-function(listx,listy) {
431 for (i in 1:length(listy)) {
432 coordok<-trouvecoordok(i)
433 if (maxcl <= nrow(coordok)) {
435 listcoordok[[nb]]<-coordok
439 listcoordok<-unique(listcoordok)
441 #si plusieurs ensemble avec le meme nombre de classe, on conserve
442 #la liste avec le plus fort chi2
443 if (length(listcoordok)>1) {
446 for (i in 1:length(listcoordok)) {
449 if (nrow(listcoordok[[i]])==maxcl) {
450 for (j in 1:nrow(listcoordok[[i]])) {
451 chi<-c(chi,croise[(listcoordok[[i]][j,1]-1),(listcoordok[[i]][j,2]-1)])
452 uce<-c(uce,chicroiseori[(listcoordok[[i]][j,1]-1),(listcoordok[[i]][j,2]-1)])
454 if (maxchi < sum(chi)) {
462 print((maxchi/nrow(classeuce1)*100))
466 coordok<-findmaxclasse(listx,listy)
469 #findmaxclasse(listx,listy)
470 #coordok<-trouvecoordok(1)
471 #coordok <- oldfindbestcoord(listx, listy)
472 coordok <- findbestcoord(listx, listy)
475 lfilletot<-function(classeuce,x) {
477 for (classe in 1:nrow(coordok)) {
478 listfille<-unique(c(listfille,fille(coordok[classe,x],classeuce)))
483 listfille1<-lfilletot(classeuce1,1)
484 listfille2<-lfilletot(classeuce2,2)
486 #utiliser rownames comme coordonnees dans un tableau de 0
487 Assignclasse<-function(classeuce,x) {
488 nchd<-matrix(0,ncol=ncol(classeuce),nrow=nrow(classeuce))
489 for (classe in 1:nrow(coordok)) {
490 clnb<-coordok[classe,x]
492 nchd[which(classeuce[,colnb]==clnb), colnb:ncol(nchd)] <- classe
496 print('commence assigne new classe')
497 nchd1<-Assignclasse(classeuce1,1)
498 if (classif_mode==0) {
499 nchd2<-Assignclasse(classeuce2,2)
503 print('fini assign new classe')
504 #croisep<-matrix(ncol=nrow(coordok),nrow=nrow(coordok))
505 nchd2[which(nchd1[,ncol(nchd1)]==0),] <- 0
506 nchd2[which(nchd1[,ncol(nchd1)]!=nchd2[,ncol(nchd2)]),] <- 0
507 nchd1[which(nchd2[,ncol(nchd2)]==0),] <- 0
510 elim<-which(nchd1[,ncol(nchd1)]==0)
511 keep<-which(nchd1[,ncol(nchd1)]!=0)
512 n1<-nchd1[nchd1[,ncol(nchd1)]!=0,]
513 n2<-nchd2[nchd2[,ncol(nchd2)]!=0,]
516 write.csv2(nchd1[,ncol(nchd1)],uceout)
517 res <- list(n1 = nchd1, coord_ok = coordok, cuce1 = classeuce1, cuce2 = classeuce2)