0% found this document useful (0 votes)
46 views3 pages

Sampling & Sampling Distribution Part - (A), (B)

The document discusses random sampling from vectors with and without replacement using the sample() function in R. It provides examples of sampling with replacement, such as tossing 10 coins or rolling 10 dice. Sampling without replacement is demonstrated through a lottery example of selecting 6 random numbers from 1 to 50. The document also discusses an error that occurs when trying to sample with size greater than the population size without replacement. Density plots are generated to visualize the central limit theorem for different sample sizes.

Uploaded by

Call Duty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views3 pages

Sampling & Sampling Distribution Part - (A), (B)

The document discusses random sampling from vectors with and without replacement using the sample() function in R. It provides examples of sampling with replacement, such as tossing 10 coins or rolling 10 dice. Sampling without replacement is demonstrated through a lottery example of selecting 6 random numbers from 1 to 50. The document also discusses an error that occurs when trying to sample with size greater than the population size without replacement. Density plots are generated to visualize the central limit theorem for different sample sizes.

Uploaded by

Call Duty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

(a) Random sampling with or without replacement using sample() function.

(Mandatory)
 Introduction:-
The sample(x, n, replace = FALSE, prob = NULL) function takes a sample from a
vector x of size n. This sample can be with or without replacement and the
probabilities of selecting each element to the sample can be either the same for each
element or a vector informed by the user.
 Tossing 10 coins

sample(0:1, 10, replace = TRUE)


Output:- 0 0 1 0 0 1 1 0 0 1

 Roll 10 dice

sample(1:6, 10, replace = TRUE)


Output:- 1 4 3 6 1 2 5 5 2 5

 Play lottery (6 random numbers out of 50 without replacement)

sample(1:50, 6, replace = FALSE)


Output:- 31 15 25 20 22 48

 Coin example:-
sample(x, size = 5)
Output:- 1 2 0 0 3
Now, let’s perform our coin-flipping experiment just once.
coin = c("Heads", "Tails")
sample(coin, size = 1)
Output:- "Tails"
And now, let’s try it 100 times
sample(coin, size = 100)
Error in sample(coin, size = 100) :
cannot take a sample larger than the population when 'replace = FALSE'

Oops, we can’t take a sample of size 100 from a vector of size 2, unless we set
the replace argument to TRUE.
table(sample(coin, size = 100, replace = TRUE))
Heads Tails
53 47
(b) Generate n random samples (take n = 10, 50, 100, 200, 500, 1000 as an example),
create a vector of Sample Means. Draw the Density Plot of Sample Means to visualize
Central Limit Theorem. (Mandatory).
Solution:-
p <- 0.05
n <- 6
sims <- 4000
m <- c(10, 50, 100, 200, 500, 1000)
E.of.X <- n*p
V.of.X <- n*p*(1-p)
Z <- matrix(NA, nrow = sims, ncol = length(m))
for (i in 1:sims)
{
for (j in 1:length(m))
{
samp <- rbinom(n = m[j], size = n, prob = p)
sample.mean <- mean(samp)
Z[i,j] <- (sample.mean - E.of.X) / sqrt(V.of.X/m[j])
}
}
par(mfrow = c(3,2))
for (j in 1:6)
{
hist(Z[,j], xlim = c(-5, 5), freq = FALSE, ylim = c(0, 0.5), ylab = "Probability",
xlab = "", main = paste("Sample Size =", m[j]))
x <- seq(-4, 4, by = 0.01)
y <- dnorm(x)
lines(x, y, col = "blue")
}
Output:-

You might also like