Updated R-code [🇷 for BE/BA]

posted by Helmut Homepage – Vienna, Austria, 2018-02-27 18:06 (2243 d 05:49 ago) – Posting: # 18471
Views: 13,886

Hi Ana,

❝ Now it's already randomized by gender as well. When I say randomize by gender, it should not generate the answer:

❝ 1 F

❝ 2 F

❝ 3 F

❝ 4 F

❝ 5 F

❝ 6 F

❝ 7 M

❝ 8 M

❝ 9 M

❝ 10 M

❝ 11 M

❝ 12 M


Did you try it? It doesn’t. But don’t use my previous codes because you might get different numbers of females and males in the sequences. :angry: In my last example:

RT, F: 6
RT, M: 2
TR, F: 2
TR, M: 6


❝ My question now, for me to do the same for 3x3 crossover studies: (TTR) (RRT)


That’s an unusual design.

❝ and 4x4: (TTRR) (TRTR) ...


And this one is strange, IMHO.

❝ how to change the formula?


I packed everything into a function with some fair degree of error handling.

random.sex <- function(n, sequences = c("TR", "RT"),
                       seed, extend = c("no", "down", "up")) {
  require(randomizeBE)
  if (missing(n)) stop("Number of subjects must be given.")
  ext <- switch(match.arg(extend, c("no", "down", "up")),
                no   = 0,
                down = -1,
                up   = +1,
                stop("invalid 'extend'."))
  sequences <- sort(sequences, decreasing = TRUE)
  no.seqs   <- length(sequences)
  if (n %% no.seqs != 0)
    stop("Number of subjects must be a multiple of sequences.")
  n.orig <- n
  if (n %% (no.seqs*2) != 0 & ext != 0) {
    if (ext == -1) {
      n <- n - no.seqs
      msg <- "decreased"
    } else {
      n <- n + no.seqs
      msg <- "increased"
    }
  }
  if (missing(seed)) seed <- runif(1, max = 1E7)
  blocksize <- n/no.seqs
  sex       <- rep(c("F", "M"), each = blocksize / no.seqs)
  if (length(sex) == 0) sex <- c("F", "M")
  periods   <- unique(nchar(sequences))
  per.name  <- paste("period", 1:periods)
  random    <- RL4(nsubj = n, seqs = sequences, seed = seed,
                   blocksize = blocksize, randctrl = TRUE)
  per       <- as.data.frame(matrix(nrow = nrow(random$rl),
                                    ncol = periods,
                                    dimnames=list(NULL, per.name)))
  for (j in 1:periods) {
    per[, j] <- paste0(substr(random$rl$sequence, j, j), "   ")
  }
  random$rl <- cbind(random$rl, per, stringsAsFactors = FALSE)
  for (j in 1:no.seqs) {
    suppressWarnings(
      random$rl$sex[random$rl$sequence == sequences[j]] <- sample(sex)
    )
  }
  random$rl <- random$rl[, c("subject", "sex", "seqno", "sequence",
                             per.name)]
  cat("\n"); print(random, sumry = TRUE)
  collect <- NULL
  for (j in no.seqs:1) {
    F <- sum(random$rl$sex[random$rl$sequence == sequences[j]] == "F")
    M <- sum(random$rl$sex[random$rl$sequence == sequences[j]] == "M")
    cat(paste0("\n", sequences[j], ", F: ", F,
               "\n", sequences[j], ", M: ", M))
    collect <- c(collect, M, F)
  }
  cat("\n")
  if (length(unique(collect)) > 1 & ext == 0)
    cat("Warning message:",
        "\nBalanced randomisation by sex not possible",
        "\nwith the given sample size.\n")
  if (ext != 0 & n != n.orig)
    cat("Sample size", msg, "from", n.orig, "to",
        "\nget balanced randomisation by sex.\n")
}


Examples:

random.sex(n = 12, sequences = c("TR", "RT")) gives

Randomization table          created: 2018-02-27 16:54:34
(seed: 666027 blocksize: 6 )

 subject sex seqno sequence period 1 period 2
       1   M     2       RT     R        T   
       2   F     2       RT     R        T   
       3   M     1       TR     T        R   
       4   F     1       TR     T        R   
       5   F     1       TR     T        R   
       6   M     2       RT     R        T   
       7   F     2       RT     R        T   
       8   F     2       RT     R        T   
       9   M     1       TR     T        R   
      10   F     1       TR     T        R   
      11   M     1       TR     T        R   
      12   M     2       RT     R        T   


Summary of randomisation

12 subjects randomized into 2 sequence groups.
Number of subjects in sequence groups:
RT TR
 6  6
Runs test of randomness: p.value=0.2259

RT, F: 3
RT, M: 3
TR, F: 3
TR, M: 3


random.sex(n = 12, sequences = c("TRT", "RTR")) gives

Randomization table          created: 2018-02-27 16:55:15
(seed: 6305806 blocksize: 6 )

 subject sex seqno sequence period 1 period 2 period 3
       1   F     2      RTR     R        T        R   
       2   F     1      TRT     T        R        T   
       3   M     2      RTR     R        T        R   
       4   M     1      TRT     T        R        T   
       5   M     2      RTR     R        T        R   
       6   M     1      TRT     T        R        T   
       7   F     2      RTR     R        T        R   
       8   F     1      TRT     T        R        T   
       9   M     2      RTR     R        T        R   
      10   F     2      RTR     R        T        R   
      11   F     1      TRT     T        R        T   
      12   M     1      TRT     T        R        T   


Summary of randomisation

12 subjects randomized into 2 sequence groups.
Number of subjects in sequence groups:
RTR TRT
  6   6
Runs test of randomness: p.value=0.0693

RTR, F: 3
RTR, M: 3
TRT, F: 3
TRT, M: 3


random.sex(n = 12, sequences = c("TRTR", "RTRT")) gives

Randomization table          created: 2018-02-27 16:56:30
(seed: 3391479 blocksize: 6 )

 subject sex seqno sequence period 1 period 2 period 3 period 4
       1   F     2     RTRT     R        T        R        T   
       2   F     1     TRTR     T        R        T        R   
       3   M     1     TRTR     T        R        T        R   
       4   F     2     RTRT     R        T        R        T   
       5   F     1     TRTR     T        R        T        R   
       6   M     2     RTRT     R        T        R        T   
       7   M     2     RTRT     R        T        R        T   
       8   M     1     TRTR     T        R        T        R   
       9   M     2     RTRT     R        T        R        T   
      10   F     1     TRTR     T        R        T        R   
      11   M     1     TRTR     T        R        T        R   
      12   F     2     RTRT     R        T        R        T   


Summary of randomisation

12 subjects randomized into 2 sequence groups.
Number of subjects in sequence groups:
RTRT TRTR
   6    6
Runs test of randomness: p.value=0.2259

RTRT, F: 3
RTRT, M: 3
TRTR, F: 3
TRTR, M: 3


random.sex(n = 12, sequences = c("TRR", "RTR", "RRT")) gives

Randomization table          created: 2018-02-27 16:57:49
(seed: 1819956 blocksize: 3 )

 subject sex seqno sequence period 1 period 2 period 3
       1   M     1      TRR     T        R        R   
       2   F     2      RTR     R        T        R   
       3   M     3      RRT     R        R        T   
       4   M     2      RTR     R        T        R   
       5   F     1      TRR     T        R        R   
       6   F     3      RRT     R        R        T   
       7   M     1      TRR     T        R        R   
       8   F     2      RTR     R        T        R   
       9   M     3      RRT     R        R        T   
      10   F     1      TRR     T        R        R   
      11   F     3      RRT     R        R        T   
      12   M     2      RTR     R        T        R   


Summary of randomisation

12 subjects randomized into 3 sequence groups.
Number of subjects in sequence groups:
RRT RTR TRR
  4   4   4
Runs test of randomness: p.value=0.2502

RRT, F: 2
RRT, M: 2
RTR, F: 2
RTR, M: 2
TRR, F: 2
TRR, M: 2
Warning message:
Blocksize is not a multiple of sequences! Blocksize adapted to 3.


random.sex(n = 12, sequences = williams(ntmt = 3)) gives

Randomization table          created: 2018-02-27 21:13:16
(seed: 2807886 blocksize: 6 )

 subject sex seqno sequence period 1 period 2 period 3
       1   M     3      BCA     B        C        A   
       2   F     2      CAB     C        A        B   
       3   M     4      BAC     B        A        C   
       4   F     6      ABC     A        B        C   
       5   M     5      ACB     A        C        B   
       6   M     1      CBA     C        B        A   
       7   F     3      BCA     B        C        A   
       8   F     4      BAC     B        A        C   
       9   F     5      ACB     A        C        B   
      10   F     1      CBA     C        B        A   
      11   M     6      ABC     A        B        C   
      12   M     2      CAB     C        A        B   


Summary of randomisation

12 subjects randomized into 6 sequence groups.
Number of subjects in sequence groups:
ABC ACB BAC BCA CAB CBA
  2   2   2   2   2   2
Runs test of randomness: p.value=1.0000

ABC, F: 1
ABC, M: 1
ACB, F: 1
ACB, M: 1
BAC, F: 1
BAC, M: 1
BCA, F: 1
BCA, M: 1
CAB, F: 1
CAB, M: 1
CBA, F: 1
CBA, M: 1


random.sex(n = 12, sequences = williams(ntmt = 4)) gives

Randomization table          created: 2018-02-27 21:40:29
(seed: 8600620 blocksize: 4 )

 subject sex seqno sequence period 1 period 2 period 3 period 4
       1   M     3     BCAD     B        C        A        D   
       2   F     2     CDBA     C        D        B        A   
       3   M     4     ABDC     A        B        D        C   
       4   F     1     DACB     D        A        C        B   
       5   F     3     BCAD     B        C        A        D   
       6   M     2     CDBA     C        D        B        A   
       7   M     1     DACB     D        A        C        B   
       8   F     4     ABDC     A        B        D        C   
       9   F     1     DACB     D        A        C        B   
      10   F     2     CDBA     C        D        B        A   
      11   M     3     BCAD     B        C        A        D   
      12   M     4     ABDC     A        B        D        C   


Summary of randomisation

12 subjects randomized into 4 sequence groups.
Number of subjects in sequence groups:
ABDC BCAD CDBA DACB
   3    3    3    3
Runs test of randomness: p.value=0.2259

ABDC, F: 1
ABDC, M: 2
BCAD, F: 1
BCAD, M: 2
CDBA, F: 2
CDBA, M: 1
DACB, F: 2
DACB, M: 1
Warning message:
Balanced randomisation by sex not possible
with the given sample size.


Note the warning. You would need 8 or 16 subjects if you want sex balanced within the 4 sequences. If you insist in this, you can used the optional argument extend="up". Decreasing the sample size by extend="down" is also supported but doesn’t make much sense.
random.sex(n = 12, sequences = williams(ntmt = 4), extend = "up") gives

Randomization table          created: 2018-02-28 00:29:38
(seed: 2223505 blocksize: 4 )

 subject sex seqno sequence period 1 period 2 period 3 period 4
       1   F     1     DACB     D        A        C        B   
       2   F     2     CDBA     C        D        B        A   
       3   M     3     BCAD     B        C        A        D   
       4   F     4     ABDC     A        B        D        C   
       5   M     2     CDBA     C        D        B        A   
       6   M     4     ABDC     A        B        D        C   
       7   F     3     BCAD     B        C        A        D   
       8   M     1     DACB     D        A        C        B   
       9   F     1     DACB     D        A        C        B   
      10   M     3     BCAD     B        C        A        D   
      11   F     2     CDBA     C        D        B        A   
      12   F     4     ABDC     A        B        D        C   
      13   M     1     DACB     D        A        C        B   
      14   M     4     ABDC     A        B        D        C   
      15   M     2     CDBA     C        D        B        A   
      16   F     3     BCAD     B        C        A        D   


Summary of randomisation

16 subjects randomized into 4 sequence groups.
Number of subjects in sequence groups:
ABDC BCAD CDBA DACB
   4    4    4    4
Runs test of randomness: p.value=0.1205

ABDC, F: 2
ABDC, M: 2
BCAD, F: 2
BCAD, M: 2
CDBA, F: 2
CDBA, M: 2
DACB, F: 2
DACB, M: 2
Sample size increased from 12 to
get balanced randomisation by sex.


If you want to reproduce a run, add the argument seed=X to the function call (where X is the seed given in the output of the previous run).

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
22,988 posts in 4,825 threads, 1,654 registered users;
91 visitors (0 registered, 91 guests [including 3 identified bots]).
Forum time: 00:55 CEST (Europe/Vienna)

The only way to comprehend what mathematicians mean by Infinity
is to contemplate the extent of human stupidity.    Voltaire

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