Suggestion [General Sta­tis­tics]

posted by Helmut Homepage – Vienna, Austria, 2015-09-29 04:22 (3569 d 15:44 ago) – Posting: # 15489
Views: 28,615

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!

Dif-tor heh smusma 🖖🏼 Довге життя Україна! [image]
Helmut Schütz
[image]

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

Complete thread:

UA Flag
Activity
 Admin contact
23,428 posts in 4,929 threads, 1,684 registered users;
90 visitors (0 registered, 90 guests [including 13 identified bots]).
Forum time: 20:06 CEST (Europe/Vienna)

No matter what side of the argument you are on,
you always find people on your side
that you wish were on the other.    Thomas Berger

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