randomization [R for BE/BA]

posted by ElMaestro  – Denmark, 2018-02-23 22:24  – Posting: # 18454
Views: 5,108

Hi A.C.,


Let us say you have 16 subjects.
8 will be female, 8 will be male. As you enroll them into the study you assign subjects numbers sequentially so that the females are 1..8, and the males are 9..16.

The follwing code will provide a randomizer:

## we have N subjects, N is divisible by 4
## N/2 will be males, N/2 will be females
## N/4 will be males in TR
## N/4 will be males in RT
## N/4 will be females in TR
## N/4 will be females in RT

Ana.Cristinas.Randomizer=function(Seed, N)
{
  if (N %% 4 != 0) {cat("N not divisible by four.!\n");return("Error.")}
  Gender=c(rep("F", N/2), rep("M", N/2))
  Subject=c(1:N)
  ##We need to assign sequences to both genders
  ##N/4 males are assigned to TR and N/4 to RT
  Seqx=c(rep("TR", N/4), rep("RT", N/4))
  ##Seqx is just an 'unrandomised' block that applies to both genders. 
  ##Now we permute the sequences(=resample without replacement) for M and F
  Seq=c(sample(Seqx, replace=F), sample(Seqx, replace=F))
  TrtP1=substr(Seq,1,1)
  TrtP2=substr(Seq,2,2)
  Rslt=data.frame(Subject, Gender, Seq, TrtP1, TrtP2)
  return(Rslt)
}

##can be run with e.g.
Ana.Cristinas.Randomizer(12312351, 16)


It is a good idea to use a new seed every time and to record the one you are actually using.
Note also that this algo assigns unique subjects numbers to every participant. Then we do not need to worry about Subject%in%Sequence and we are compliant with ICH E6 §1.58 (unique identifier). :-D

if (3) 4

x=c("Foo", "Bar")
b=data.frame(x)
typeof(b[,1]) ##aha, integer?
b[,1]+1 ##then let me add 1


Best regards,
ElMaestro

“(...) targeted cancer therapies will benefit fewer than 2 percent of the cancer patients they’re aimed at. That reality is often lost on consumers, who are being fed a steady diet of winning anecdotes about miracle cures.” New York Times (ed.), June 9, 2018.

Complete thread:

Activity
 Mix view
Bioequivalence and Bioavailability Forum |  Admin contact
19,437 posts in 4,125 threads, 1,325 registered users;
online 10 (1 registered, 9 guests [including 5 identified bots]).
Forum time (Europe/Vienna): 02:39 CEST

On two occasions I have been asked,—“Pray, Mr. Babbage,
if you put into the machine wrong figures,…
will the right answers come out?”

I am not able rightly to apprehend the kind of confusion of ideas
that could provoke such a question.    Charles Babbage

The BIOEQUIVALENCE / BIOAVAILABILITY FORUM is hosted by
BEBAC Ing. Helmut Schütz
HTML5