jeudi 1 mars 2018

export of repetead tables R

Firstly, sorry for my English, I’m French (it’s well known). I am a recent R user and I realized that I have never had to do an "automatic", repeated analysis (loop analysis?), that it can be easily exported. Let me explain, as part of my job, I have to analyze a satisfaction survey that includes many sentences. Every participants should answer with a quoting number system. Each number is between 1 and 5 (1 = I am not at all satisfied ... 5 = I am very satisfied). Each statement represents an item and each item is including in a dimension. This model of satisfaction evaluation is based on a theoretical model (there are 5 dimensions, each one including between 4 and 6 items, an each item can be only in one and only one dimension). The idea is to compare the average scores of each item and each dimension (score dimension = sum (items)) I think that my problem is multiple, but I would like roughly to export several tables of this type: Dimensions Lieu 1 Lieu 2 Lieu 3 N Score moyen N Score moyen Significativité (versus Lieu 1) N
Score moyen Significativité (versus lieu 2) Réactivité 322 4,39 181 4,24 * 170 4,24 * Le temps d'attente 338 4,46 188 4,30 * 176 4,28 * L’attention portée 325 4,35 187 4,29 ns 172 4,16 * *** différence significative au seuil 1%, ** différence significative au seuil 5%, * différence significative au seuil 10%, ns différence non significative. The right test is the Wilcoxon / Mann Whitney test (non-normality verified by the Shapiro Wilk test). My problem is that I cannot create a table with all average scores, N and significant tests I tried several things: - A tapply for the average scores but I cannot extract the "N" (or the NA) when I calculate them - A CreateTableOne to extract the most complete table possible but I still do not have the N and the Wilcoxon test does not allow to know which modalities are compared (done with kruskal.test). Moreover, when I want to use this test, it transform all averages into medians because the variables do not follow a normal distribution. - An export with write.table that seems to work

VARIABLES SUIVENT UNE LOI NORMALE ?

temps_attente_1 <- subset(enquete2, lieux == "Lieu1",temps_attente) temps_attente_2 <- subset(enquete2, lieux == "Lieu2", temps_attente) shapiro.test(temps_attente_1$temps_attente) shapiro.test(temps_attente_2$temps_attente)

TEST DE WILCOXON / MANN WHITNEY

temps_attente_1w <- as.numeric(temps_attente_1 [,1]) temps_attente_2w <- as.numeric(temps_attente_2 [,1]) wilcox.test(temps_attente_1w, temps_attente_2w)

CALCUL DES MOYENNES PAR SOUS GROUPE DE TEMPS ATENTE

m=tapply(enquete2$temps_attente, enquete2$lieux, mean, na.rm=TRUE) m Lieu 1 Lieu 2 Lieu 3 4.058894 4.131250 4.283333

CREATETABLEONE

library(tableone) library(survival) dput(names(enquete2)) vars_tot <- c("dim_reactivite", "temps_attente", "attention", "lieux") tab_1 <- CreateTableOne(vars=vars_tot, strata= "lieux", data=enquete2, testNonNormal = kruskal.test) tab_1 Stratified by lieux Lieu1 Lieu2 Lieu3 p test n 245 92 41
dim_reactivite (mean (sd)) 4.06 (0.67) 4.13 (0.43) 4.28 (0.58) 0.144
temps_attente (mean (sd)) 3.84 (1.00) 3.27 (1.00) 4.11 (0.84) <0.001
attention (mean (sd)) 4.22 (0.86) 4.45 (0.74) 4.26 (0.78) 0.074
lieux (%) <0.001
Lieu1 245 (100.0) 0 ( 0.0) 0 ( 0.0)
Lieu2 0 ( 0.0) 92 (100.0) 0 ( 0.0)
Lieu3 0 ( 0.0) 0 ( 0.0) 41 (100.0)

tab_2 <- print(tab_1$ContTable, smd=TRUE, showAllLevels=TRUE, quote=FALSE, noSpaces=TRUE) Stratified by lieux Lieu1 Lieu2 Lieu3 p test SMD n 245 92 41
dim_reactivite (mean (sd)) 4.06 (0.67) 4.13 (0.43) 4.28 (0.58) 0.144 0.263 temps_attente (mean (sd)) 3.84 (1.00) 3.27 (1.00) 4.11 (0.84) <0.001 0.588 attention (mean (sd)) 4.22 (0.86) 4.45 (0.74) 4.26 (0.78) 0.074 0.199

variante avec variables non normales

items <- c("dim_empathie", "horaires_rec", "proximite_rec", "non_jugement_rec", "tout_dire_rec") tab_2 <- print(tab_1$ContTable, nonnormal= items, smd=TRUE, showAllLevels=TRUE) Stratified by lieux Lieu1 Lieu2 Lieu3 p test SMD n 245 92 41
dim_reactivite (median [IQR]) 4.25 [3.69, 4.50] 4.25 [3.75, 4.25] 4.25 [3.81, 4.75] 0.258 nonnorm 0.263 temps_attente (median [IQR]) 4.00 [3.00, 5.00] 3.00 [3.00, 4.00] 4.00 [3.00, 5.00] <0.001 nonnorm 0.588 attention (median [IQR]) 4.00 [4.00, 5.00] 5.00 [4.00, 5.00] 4.00 [4.00, 5.00] 0.080 nonnorm 0.199

EXPORT DANS UN TABLEAU EXCEL

write.table(cbind(VARIABLES=row.names(tab_2), tab_2), file="U:/HAITI/EVENEMENTS/2018/2018_01_29_ENQUETE_SATISFACTION/test2.xls",dec=",", row.names=F, col.names=TRUE, sep ="\t")

Aucun commentaire:

Enregistrer un commentaire