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.
#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))
table(number.responses)
## number.responses
## 3 4 5 6 7 8
## 4 25 69 153 164 85
barplot(table(number.responses))
sum(number.responses == 0)/number.replicates
## [1] 0
\[\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*}\]
\[\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
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
Consider a random sample of 250 stocks. Let \(X\) represent the number of infected stocks in the sample.
dbinom(60, 250, 0.30)
## [1] 0.006301219
pbinom(60, 250, 0.30)
## [1] 0.02103864
pbinom(79, 250, 0.30, lower.tail = FALSE)
## [1] 0.2654606
pbinom(100, 250, 0.30)
## [1] 0.9997024
Demonstrate how parts a) through d) can be approached through simulation.
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
dbinom(97, size = 100, prob = 0.90)
## [1] 0.005891602
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.
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
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
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.