A random variable numerically summarizes the possible outcomes of a random experiment. Formally, a random variable assigns numerical values to the outcomes of a random phenomenon. This lab introduces random variables by exploring the clinical trial example described at the beginning of Chapter 3 and by discussing the binomial distribution.

The material in this lab corresponds to Sections 3.1 and 3.2 of OpenIntro Biostatistics.

Clinical Trial Simulation

  1. Suppose that a clinical trial to test a new drug will be conducted on 8 patients, in which the probability of a good response to the drug is thought to be 0.15. The following code simulates the trial with 500 replicates.
#define parameters
number.patients = 8
response.prob = 0.15
number.replicates = 500

#create empty vectors to store results
number.responses.replicate = vector("numeric", number.patients)
number.responses = vector("numeric", number.replicates)

#set the seed for a pseudo-random sample
set.seed(1979)

#simulate the trials
for(k in 1:number.replicates){
  
  number.responses.replicate = sample(c(0,1), size = number.patients,prob = c(1 - response.prob, response.prob), replace = TRUE)
  
  number.responses[k] = sum(number.responses.replicate)
  
}
table(number.responses)
## number.responses
##   0   1   2   3   4 
## 142 181 127  41   9
barplot(table(number.responses))

#define parameters
number.patients = 8
response.prob = 0.5
number.replicates = 500

#create empty vectors to store results
number.responses.replicate = vector("numeric", number.patients)
number.responses = vector("numeric", number.replicates)

#set the seed for a pseudo-random sample
set.seed(1979)

#simulate the trials
for(k in 1:number.replicates){
  
  number.responses.replicate = sample(c(0,1), size = number.patients,prob = c(1 - response.prob, response.prob), replace = TRUE)
  
  number.responses[k] = sum(number.responses.replicate)
  
}
table(number.responses)
## number.responses
##   1   2   3   4   5   6   7 
##  16  54 109 148 105  53  15
barplot(table(number.responses))

#define parameters
number.patients = 8
response.prob = 0.8
number.replicates = 500

#create empty vectors to store results
number.responses.replicate = vector("numeric", number.patients)
number.responses = vector("numeric", number.replicates)

#set the seed for a pseudo-random sample
set.seed(1979)

#simulate the trials
for(k in 1:number.replicates){
  
  number.responses.replicate = sample(c(0,1), size = number.patients,prob = c(1 - response.prob, response.prob), replace = TRUE)
  
  number.responses[k] = sum(number.responses.replicate)
  
}
table(number.responses)
## number.responses
##   3   4   5   6   7   8 
##   4  25  69 153 164  85
barplot(table(number.responses))

  1. Run the simulation and view the results (Hint: make a plot). Describe the distribution of the number of good responses to the drug.

table(number.responses)
## number.responses
##   3   4   5   6   7   8 
##   4  25  69 153 164  85
barplot(table(number.responses))

  1. What value(s) for response probability would produce a left-skewed distribution? What value(s) would produce a symmetric distribution?

  1. Based on the results of the simulation, estimate the probability that 0 patients respond well to the new drug.

sum(number.responses == 0)/number.replicates
## [1] 0
  1. Let \(X\) be a random variable defined as the number of patients who respond well to the experimental drug. Based on the results of the simulation, \(\dots\)
  1. Construct a probability distribution for \(X\).

  1. Calculate \(E(X)\), where \[E(X) = x_1 P(X=x_1) + \cdots + x_k P(X=x_k) = \sum_{i=1}^{k}x_iP(X=x_i)\]

\[\begin{align*} E(X) &= \sum_{i=1}^{k}x_iP(X=x_i) \notag \\ &= (x_1)P(X = x_1) + (x_2)P(X = x_2) + \cdots + (x_k)P(X = x_k) \\ &= (0)(0.266) + (1)(0.418) + (2)(0.250) + (3)(0.052) \\ & \qquad + (4)(0.012) + (5)(0.002) + (6)(0) + (7)(0) + (8)(0) \\ &= 1.132 \end{align*}\]

  1. Calculate \(\text{Var}(X)\), where \[\text{Var}(X) = (x_1-\mu)^2 P(X=x_1) + \cdots + (x_k-\mu)^2 P(X=x_k) = \sum_{j=1}^{k} (x_j - \mu)^2 P(X=x_j) \]

\[\begin{align*} \text{Var}(X) &= \sum_{j=1}^{k} (x_j - \mu)^2 P(X=x_j) \\ &= (x_1-\mu)^2 P(X=x_1) + \cdots+ (x_k-\mu)^2 P(X=x_k) \\ &= (0 - 1.132)^{2}(0.266) + (1 - 1.132)^{2}(0.418) + (2 - 1.132)^{2}(0.250) + (3 - 1.132)^{2}(0.052) \\ & \qquad + (4 - 1.132)^{2}(0.012) + (5 - 1.132)^{2}(0.002) + (6 - 1.132)^{2}(0) + (7 - 1.132)^{2}(0) \\ & \qquad (8 - 1.132)^{2}(0) \\ &= 0.847 \end{align*}\]

#using r as a calculator
prop.table(table(number.responses))  #print probability distribution table
## number.responses
##     3     4     5     6     7     8 
## 0.008 0.050 0.138 0.306 0.328 0.170
#calculate E(X)
x.i = 0:8
prob.x.i = prop.table(table(number.responses))
e.x = sum(x.i[1:6]*prob.x.i)
e.x
## [1] 3.406

Binomial Distribution

Let \(X\) represent the number of successes in a series of independent Bernoulli trials. Suppose the probability of a single trial being a success is \(p\). The probability of observing exactly \(k\) successes in \(n\) independent trials is given by \[P(X = k) = { n \choose k} p^k (1-p)^{n-k} = \frac{n!}{k!(n-k)!} p^k (1-p)^{n-k}. \]

For a binomial random variable, \(E(X) = np\) and \(\text{Var}(X) = np(1-p)\).

Binomial probabilities are calculated in with the use of and .

The following code shows how to calculate \(P(X = 5)\), \(P(X \leq 5)\), and \(P(X > 5)\) for \(X \sim \text{Bin}(10, 0.35)\).

#probability X equals 5
dbinom(5, 10, 0.35)
## [1] 0.1535704
#probability X is less than or equal to 5
pbinom(5, 10, 0.35)
## [1] 0.9050659
#probability X is greater than 5
pbinom(5, 10, 0.35, lower.tail = FALSE) 
## [1] 0.09493408
  1. Why is the value returned by greater than the value from ?

  1. The hypothetical clinical trial discussed in Question 1 can be modeled with a binomial distribution.
  1. Confirm that the clinical trial satisfies the conditions for a binomial experiment.

  1. Calculate \(E(X)\) and \(\text{Var}(X)\) using the formulas specific to the binomial distribution; compare the results to the answers from part d) of Question 1. Explain any observed discrepancies.

  1. Approximately 12,500 stocks of Drosophila melanogaster flies are kept at the Bloomingdale Drosophila Stock Center for research purposes. A 2006 study examined how many stocks were infected with Wolbachia, an intracellular microbe that can manipulate host reproduction for its own benefit. About 30% of stocks were identified as infected. Researchers working with infected stocks should be cautious of the potential confounding effects that Wolbachia infection may have on experiments.

Consider a random sample of 250 stocks. Let \(X\) represent the number of infected stocks in the sample.

  1. Calculate the probability that exactly 60 stocks are infected.
dbinom(60, 250, 0.30)
## [1] 0.006301219
  1. Calculate the probability that at most 60 stocks are infected.

pbinom(60, 250, 0.30)
## [1] 0.02103864
  1. Calculate the probability that at least 80 stocks are infected.

pbinom(79, 250, 0.30, lower.tail = FALSE)
## [1] 0.2654606
  1. Assume that a researcher will use all of the stocks sampled for an experiment. If the researcher wants to be sure that no more than 40% of the stocks used for an experiment are infected, does it seem reasonable to take a random sample of 250 stocks?

pbinom(100, 250, 0.30)
## [1] 0.9997024
  1. Demonstrate how parts a) through d) can be approached through simulation.

  2. the difference between the probability that exactly 60 stocks are infected and the probability that at most 60 stocks are infected is 0.0147.

#define parameters
sample.size = 250
prob.infection = 0.30
number.replicates = 1000

#create empty vectors to store results
number.infected.replicate = vector("numeric", sample.size)
number.infected = vector("numeric", number.replicates)

#set the seed for a pseudo-random sample
set.seed(1979)

#simulate the samples
for(k in 1:number.replicates){
  
  number.infected.replicate = sample(c(0,1), size = sample.size,
                                     prob = c(1 - prob.infection, prob.infection),
                                     replace = TRUE)
  
  number.infected[k] = sum(number.infected.replicate)
  
}

#part a)
sum(number.infected == 60)/number.replicates
## [1] 0.008
#part b)
sum(number.infected <= 60)/number.replicates
## [1] 0.026
#part c)
sum(number.infected >= 80)/number.replicates
## [1] 0.282
#part d)
sum(number.infected <= 100)/number.replicates
## [1] 1
  1. The US Centers for Disease Control and Prevention (CDC) estimates that 90% of Americans have had chickenpox by the time they reach adulthood.
  1. Calculate the probability that exactly 97 out of 100 randomly sampled American adults had chickenpox during childhood.

dbinom(97, size = 100, prob = 0.90)
## [1] 0.005891602
  1. Calculate the probability that exactly 3 out of a new sample of 100 American adults have not had chickenpox in their childhood.

The probability is 0.006 based on calculations in section a). The event that precisely 3 out of 100 adults did not have chickenpox during childhood is similar to the event that exactly 97 out of 100 did have chickenpox during childhood.

  1. What is the probability that at least 1 out of 10 randomly sampled American adult has had chickenpox?

P(X >= 1)=P(X >0); this means that there is practically a 1 in the likelihood that at least 1 out of 10 randomly selected adult Americans has experienced chickenpox.

1 - dbinom(0, size = 10, prob = 0.90)
## [1] 1
pbinom(0, size = 10, prob = 0.90, lower.tail = FALSE)
## [1] 1
  1. What is the probability that at most 3 out of 10 randomly sampled American adults have not had chickenpox?

pbinom(3, size = 10, prob = 0.10)
## [1] 0.9872048
  1. Suppose that the adults were sampled from the same city. Would it be appropriate to use the binomial distribution to calculate the probabilities from parts a) through d)?

The binomial distribution might not be the best option. There may be some correlation between each person’s childhood chickenpox infection if the city is small enough that 10 or 100 people may have come into touch with one another. On the other hand, if a sample of 10 or 100 persons is taken from the whole US, it seems sense to presume independence.