Suggestion [General Sta­tis­tics]

posted by Helmut Homepage – Vienna, Austria, 2015-09-29 02:22  – Posting: # 15489
Views: 18,788

Hi Shuanghe,

» # remove subjects who has imcomplete periods. More elegant solution?
» library(dplyr)

Didn’t know this package. The syntax within some functions (i.e., the %>%-operator) is – well – un­con­ven­tio­nal.

Code to keep only subjects completing all four periods:

summary(ema[2:4])                # show structure
per.4  <- summary(ema$subj) == 4 # subjs have 4 levels?
incomp <- per.4[per.4 == FALSE]  # incomplete ones
print(sort(names(incomp)))
comp   <- per.4[per.4 == TRUE]   # complete ones
print(sort(names(comp)))
d      <- subset(ema, subj %in% names(comp))
print(d)
summary(d[2:4])
seq1   <- as.numeric(summary(d[, 3])[names(summary(d[, 3]))[1]])
seq2   <- as.numeric(summary(d[, 3])[names(summary(d[, 3]))[2]])
if(seq1 == seq2) {
 txt <- "Balanced: "
 } else {
 txt <- "Unbalanced: "
}
cat(paste0(txt, names(summary(d[, 3]))[1], "=", seq1, ", ",
 names(summary(d[, 3]))[2], "=", seq2, "\n"))

Lines in red can be dropped (checking results only).

Which subjects are incomplete? print(sort(names(incomp))) gives

[1] "11" "20" "24" "31" "42" "67" "69" "71" [image]

summary(d[2:4]) gives

 per      seq      treat
 1:69   RTRT:144   R:138
 2:69   TRTR:132   T:138
 3:69
 4:69

and the goody

Unbalanced: RTRT=144, TRTR=132

The code works for fancy non-numeric subject-IDs and other sequence-codings as well. Try

ema$subj <- as.factor(paste0("Sub #", ema$subj))
levels(ema$seq)[levels(ema$seq)=="RTRT"]  <- 1
levels(ema$seq)[levels(ema$seq)=="TRTR"]  <- 2
levels(ema$treat)[levels(ema$treat)=="T"] <- "A"
levels(ema$treat)[levels(ema$treat)=="R"] <- "B"

and run the code. I got for print(sort(names(incomp)))

[1] "Sub #11" "Sub #20" "Sub #24" "Sub #31" "Sub #42" "Sub #67" "Sub #69"
[8] "Sub #71

and finally for summary(d[2:4])

 per    seq     treat
 1:69   1:144   B:138
 2:69   2:132   A:138
 3:69       
 4:69

Same as above (apart from differing sequences and treatments). :-D


Edit: Once I saw Detlew’s code below, I can only say about my snippet: Forget it!

Cheers,
Helmut Schütz
[image]

The quality of responses received is directly proportional to the quality of the question asked. 🚮
Science Quotes

Complete thread:

Activity
 Admin contact
20,346 posts in 4,274 threads, 1,405 registered users;
online 8 (0 registered, 8 guests [including 6 identified bots]).
Forum time (Europe/Vienna): 08:54 CET

The belief that there is only one truth and
that oneself is in possession of it,
seems to me the deepest root of all
that is evil in the world.    Max Born

The Bioequivalence and Bioavailability Forum is hosted by
BEBAC Ing. Helmut Schütz
HTML5