You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

265 lines
9.0KB

  1. #' Comparatif par groupe
  2. #'
  3. #' Produit un tableau comparatif par groupe
  4. #'
  5. #' Permet de produire un tableau comparatif des variables contenues dans la table passee en parametre selon les modalites d'une d'entre elles.
  6. #' Si les labels et formats sont definis et charges ils seront utilises pour peupler le tableau.
  7. #' Le fichier de sortie est place dans ../HTML Output
  8. #' @encoding UTF-8
  9. #' @param table Table a utiliser
  10. #' @param groupe Nom de la variable qualitative a utiliser pour la comparaison
  11. #' @param param Vecteur de noms de variables considerees comme parametriques
  12. #' @param html Nom du fichier html, par defaut "desc_groupe_nomdelavariable.html"
  13. #' @param titre Titre du tableau, par defaut "Comparaison selon nom_de_la_variable"
  14. #' @param variables Vecteur de noms de variables a comparer, par defaut toutes les variables contenues dans la table moins celle servant de comparateur
  15. #' @param variables_neg Vecteur de noms de variables a exclure de la comparaison
  16. #' @param stats Vecteur de valeurs a calculer, parmi N, \%, \%/moy, moy, \%/med, med, et, ic95, Q1, med, Q3, min et max. Par defaut c("N","\%/moy","et")
  17. #' @param note Note de bas de page, par defaut vide
  18. #' @param nbdec Nombre de decimales apres la virgule, par defaut 1
  19. #' @param pourcent Pourcentages pour les variables qualitatives, en colonnes ("col") ou en lignes ("row"), par defaut sur le total
  20. #' @param paired Use a paired test for quantitative values. Must have equal number of observations in the two groups. Only works for two groups.
  21. #' @examples
  22. #' \dontrun{
  23. #' Ma_table <- charger("donnees.xls")
  24. #'
  25. #' desc_groupe(Ma_table, "sexe")
  26. #'
  27. #' para <- diagnostic(Ma_table)
  28. #' desc_groupe(Ma_table, "sexe", param = para, titre="Comparatif selon le sexe", pourcent="row")
  29. #' }
  30. #' @export
  31. desc_groupe <- function(table, groupe, param = character(0), html=NULL, titre=NULL, variables=NULL, variables_neg=NULL,stats=c("N","%/moy","et"), note=NULL, nbdec=1, pourcent="total", paired=F)
  32. {
  33. if (nbdec<2)
  34. nbdecp<-2
  35. else
  36. nbdecp<-nbdec
  37. if (missing(table))
  38. {
  39. warning("Pas de table donnee !")
  40. return
  41. }
  42. if (missing(groupe) | (!groupe %in% names(table)))
  43. {
  44. warning("Pas de comparateur !")
  45. return
  46. }
  47. modulo <- table[[groupe]]
  48. if (nlevels(modulo)<2)
  49. {
  50. warning("La variable ", groupe, " n'a qu'un niveau !")
  51. return
  52. }
  53. etiq <- label(modulo)
  54. if (etiq == "modulo") etiq <- groupe
  55. # Creation du titre
  56. if (is.null(titre))
  57. titre<-paste("Comparaison selon", etiq)
  58. # Creation de la table temporaire contenant les variables demandees
  59. if (!is.null(variables))
  60. {
  61. badvar <- variables[!variables %in% names(table)]
  62. if (length(badvar) != 0)
  63. warning("Les variables suivantes n'existent pas dans la table : ",paste(badvar,collapse=", "))
  64. table <- table[variables[variables %in% names(table)]]
  65. }
  66. if (!is.null(variables_neg))
  67. {
  68. badvar <- variables_neg[!variables_neg %in% names(table)]
  69. if (length(badvar) != 0)
  70. warning("Les variables_neg suivantes n'existent pas dans la table : ",paste(badvar,collapse=", "))
  71. table[variables_neg]<-list(NULL)
  72. }
  73. table[groupe]<-NULL
  74. # Creation du nom de fichier html
  75. if (is.null(html))
  76. html <- paste0("desc_groupe_",groupe)
  77. # Header de la table
  78. HTMLInit(file=paste0(html,".html"), title=titre, CSSfile="desc.css")
  79. HTML("<div class='desc'>")
  80. inc()
  81. HTML("<table class='desc'>")
  82. inc()
  83. HTML("<caption>", titre, "</caption>")
  84. HTML("<thead>")
  85. inc()
  86. HTML("<tr><th></th>", paste0("<th colspan='",length(stats),"'>",levels(modulo),"</th>", collapse=""), "<th>p</th></tr>")
  87. HTML("<tr><td></td>", sep="")
  88. for (i in levels(modulo))
  89. HTML("<td colspan='",length(stats),"'>N=", length(na.omit(modulo[modulo==i])), "</td>", sep="")
  90. HTML("<td></td></tr>")
  91. HTML("<tr><td></td>", paste0(rep(paste("<td>",stats,"</td>",sep="", collapse=""),nlevels(modulo)),collapse=""), "<td></td></tr>")
  92. dec()
  93. HTML("</thead>")
  94. # Footer de la table
  95. if (!is.null(note))
  96. {
  97. HTML("<tfoot>")
  98. inc()
  99. HTML("<tr><td colspan='", nlevels(modulo)*length(stats)+2, "'>", note,"</td></tr>")
  100. dec()
  101. HTML("</tfoot>")
  102. }
  103. # Corps de la table
  104. HTML("<tbody>")
  105. inc()
  106. for (var in names(table))
  107. {
  108. # Creation du label de la variable
  109. etiq <- label(table[[var]])
  110. if (etiq == "table[[var]]") etiq <- var
  111. # Variable quantitative
  112. if (is.numeric(table[[var]]))
  113. {
  114. if (var %in% param)
  115. {
  116. if (nlevels(modulo) == 2)
  117. {
  118. p <- tryCatch(t.test(table[[var]] ~ modulo, paired=paired,na.action=na.pass)$p.value, error=function(e){erreur(e,var,"dans le calcul du test t")})
  119. test <- "t"
  120. }
  121. else if (nlevels(modulo) > 2)
  122. {
  123. p <- tryCatch(summary(aov(table[[var]] ~ modulo))[[1]][["Pr(>F)"]][1], error=function(e){erreur(e,var,"dans le calcul de l'ANOVA")})
  124. test <- "ANOVA"
  125. }
  126. }
  127. else
  128. {
  129. if (nlevels(modulo) == 2)
  130. {
  131. p <- tryCatch(wilcox.test(table[[var]] ~ modulo, paired=paired, na.action=na.pass)$p.value, error=function(e){erreur(e,var,"dans le calcul du test de Mann-Whitney")})
  132. test <- "M-W"
  133. }
  134. else if (nlevels(modulo) > 2)
  135. {
  136. p <- tryCatch(kruskal.test(table[[var]] ~ modulo)$p.value, error=function(e){erreur(e,var,"dans le calcul du test de Kruskal-Wallis")})
  137. test <- "K-W"
  138. }
  139. }
  140. HTML("<tr><td class='var'>", etiq, "</td>", sep="")
  141. for (level in levels(modulo))
  142. {
  143. for (stat in stats)
  144. {
  145. HTML("<td>",sep="")
  146. HTML(tryCatch(
  147. {
  148. format(switch(stat,
  149. N = length(na.omit(table[[var]][modulo==level])),
  150. "%/moy" = ,
  151. moy = mean(table[[var]][modulo==level],na.rm=T),
  152. "%/med" = ,
  153. med = median(table[[var]][modulo==level],na.rm=T),
  154. Q1 = quantile(table[[var]][modulo==level], probs=.25, na.rm=T,names=F,type=3),
  155. Q3 = quantile(table[[var]][modulo==level], probs=.75, na.rm=T,names=F,type=3),
  156. min = min(table[[var]][modulo==level],na.rm=T),
  157. max = max(table[[var]][modulo==level],na.rm=T),
  158. et = sd(table[[var]][modulo==level],na.rm=T),
  159. ic95 = paste0("&plusmn;",format(mean(table[[var]][modulo==level],na.rm=T) - t.test(table[[var]][modulo==level])$conf.int[1], digits=nbdec,nsmall=nbdec)),
  160. ... = "ERR"
  161. ),digits=nbdec,nsmall=nbdec)
  162. },error = function(e){erreur(e,var,"dans le calcul de ",stat)}), sep="")
  163. HTML("</td>", sep="")
  164. }
  165. }
  166. HTML("<td>", format(p,digits=nbdecp,nsmall=nbdecp), sep="")
  167. if (!is.na(p))
  168. if (p<.05)
  169. HTML("*", sep="")
  170. HTML(" (",test,")</td>", sep="")
  171. HTML("</tr>")
  172. }
  173. # Variable qualitative
  174. else if (is.factor(table[[var]]))
  175. {
  176. test <- "X"
  177. p <- tryCatch(
  178. {
  179. chisq.test(table[[var]],modulo)$p.value
  180. },
  181. warning=function(w)
  182. {
  183. test <<- "f"
  184. tryCatch(
  185. {
  186. fisher.test(table[[var]],modulo,workspace=1e+08)$p.value
  187. },
  188. error=function(e)
  189. {
  190. #test <<- "X(f failed)"
  191. #chisq.test(table[[var]],modulo)$p.value
  192. fisher.test(table[[var]],modulo,simulate.p.value = T,B = 1e+06)$p.value
  193. })
  194. },
  195. error=function(e)
  196. {
  197. "NA"
  198. })
  199. HTML("<tr><td class='var' colspan='", nlevels(modulo)*length(stats)+1, "'>", etiq, "</td>", sep="")
  200. HTML("<td>", format(p,digits=nbdecp,nsmall=nbdecp), sep="")
  201. if (!is.na(p))
  202. if (p<.05)
  203. HTML("*", sep="")
  204. HTML(" (",test,")</td>", sep="")
  205. HTML("</tr>")
  206. # Levels de la variable qualitative
  207. for (level in levels(table[[var]]))
  208. {
  209. HTML("<tr><td class='level'>", level, "</td>",sep="")
  210. for (level_m in levels(modulo))
  211. {
  212. for (stat in stats)
  213. {
  214. HTML("<td>",sep="")
  215. if (stat == "N")
  216. HTML(table(table[[var]],modulo)[level,level_m],sep="")
  217. else if ((stat == "%") | (stat == "%/moy") | (stat == "%/med"))
  218. switch(pourcent,
  219. row = HTML(format(100*table(table[[var]],modulo)[level,level_m]/rowSums(table(table[[var]],modulo))[level], digits=nbdec, nsmall=nbdec), sep=""),
  220. col = HTML(format(100*table(table[[var]],modulo)[level,level_m]/colSums(table(table[[var]],modulo))[level_m], digits=nbdec, nsmall=nbdec), sep=""),
  221. total = HTML(format(100*table(table[[var]],modulo)[level,level_m]/sum(table(table[[var]],modulo)), digits=nbdec, nsmall=nbdec), sep=""))
  222. HTML("</td>",sep="")
  223. }
  224. }
  225. HTML("<td></td></tr>")
  226. }
  227. }
  228. else next
  229. }
  230. dec()
  231. HTML("</tbody>")
  232. dec()
  233. HTML("</table>")
  234. HTML("</div>")
  235. HTMLEnd()
  236. }