Professional Documents
Culture Documents
Michael King
Fall 2019
Question 1
X is a binomial random variable with parameters p = 0.5 and n = 30. Plot the probability function of X
# Define the distribution parameters
n <- 30
p <- .5
G <-
ggplot(
data = M) +
geom_col(
mapping = aes(x = x, y = y, fill = y),
color = 'black',
size = 0.2,
width = 1) +
scale_fill_gradientn(
colors = rev(heat.colors(6)))+
scale_x_continuous(
breaks = seq(from = 0, to = n, by = floor(n / 5))) +
labs(
x = 'random variable',
y = NULL,
title = 'A Binomial Probability Function / n = 10, p = 0.5',
subtitle = NULL,
caption = 'Michael King / Statistics I / Test #2') +
theme_bw() +
theme(
legend.position = 'none',
axis.title.x = element_text(vjust = -3),
axis.title.y = element_text(vjust = +3),
plot.margin = margin(unit ='cm', b = 1, l = 1),
plot.title = element_text(hjust = 0.5),
panel.border = element_rect(fill = NA),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
axis.ticks = element_blank(),
text = element_text(
size = 18,
color = 'black',
face = 'italic',
family = 'serif'))
G
A Binomial Probability Function / n = 10, p = 0.5
0.15
0.10
0.05
0.00
0 6 12 18 24 30
random variable Michael King / Statistics I / Test #2
Question 2
Generate a random sample of size 1,000,000 from a binomial distribution with parameters n = 30 and p = 0.439 and graph the
cumulative empirical distribution.
# Set the distribution parameters
n <- 30
p = .439
# Create a table
tb <- table(rb)
# Create a data_frame
M <- data_frame(nb, tb, cb)
G <-
ggplot(
data = M) +
geom_col(
mapping = aes(x = nb, y = cb, fill = cb),
color = 'black',
width = 1,
size = 0.2) +
scale_fill_gradientn(
colors = rev(heat.colors(6))) +
scale_x_continuous(
breaks = seq(from = min(rb), to = max(rb), by = 2)) +
labs(
x = NULL,
y = 'Relative Cumulative Frequency',
title = 'Empirical Cumulative Distribution Function',
subtitle = NULL,
caption = 'Michael King / Statistics I / Fall 2019') +
theme_bw() +
theme(
legend.position = 'none',
axis.title.x = element_text(vjust = -3),
axis.title.y = element_text(vjust = +3),
plot.margin = margin(unit ='cm', b = 1, l = 1),
plot.title = element_text(hjust = 0.5),
panel.border = element_rect(fill = NA),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
axis.ticks = element_blank(),
text = element_text(
size = 18,
color = 'black',
face = 'italic',
family = 'serif'))
G
Empirical Cumulative Distribution Function
1.00
0.75
Relative Cumulative Frequency
0.50
0.25
0.00
1 3 5 7 9 11 13 15 17 19 21 23 25
Michael King / Statistics I / Fall 2019
Question 3
• Let X be a geometric random variable with parameter p = 0.25.
# Generate a random sample of size 1000 from X and plot the histogram
rn <- rgeom(n = n, prob = p)
# Create a data_frame
M <- data_frame(x = rn)
G
Relative Frequency Histogram of Geometric Data
0.2
0.1
0.0
0 10 20
Michael King / Statistics I / Fall 2019
Question 4
• Let Y be a negative binomial random variable with parameters r = 6 and p = 0.5
G <-
ggplot(
data = M) +
geom_col(
mapping = aes(x = x, y = y, fill = y),
color = 'black',
size = 0.2,
width = 1) +
scale_fill_gradientn(
colors = rev(heat.colors(10))) +
labs(
x = NULL,
y = NULL,
title = 'A Negative Binomial Probability Function',
subtitle = NULL,
caption = 'Michael King / Statistics I / Fall 2019') +
theme_bw() +
theme(
legend.position = 'none',
axis.title.x = element_text(vjust = -3),
axis.title.y = element_text(vjust = +3),
plot.margin = margin(unit ='cm', b = 1, l = 1),
panel.grid.major.x = element_blank(),
panel.grid.minor.x= element_blank(),
plot.title = element_text(hjust = 0.5),
panel.border = element_rect(fill = NA),
axis.ticks = element_blank(),
text = element_text(
size = 18,
color = 'black',
face = 'italic',
family = 'serif'))
G
A Negative Binomial Probability Function
0.125
0.100
0.075
0.050
0.025
0.000
0 5 10 15 20
Michael King / Statistics I / Fall 2019
Question 5
• Let X be Poisson random variable with mean 5,
• Plot the probability function of X for 0 ≤ X ≤ 20
# Create the data
M <-
data_frame(
x = seq(from = 0, to = 20, by = 1),
y = dpois(x, lambda = 5))
G
Poisson Probability Function
Michael King
0.15
0.10
0.05
0.00
0 5 10 15 20
Random Variable
Question 6
• Let X be a Poisson random variable with mean 4
G
The Survival Function of a Poisson Random Variable
1.00
0.75
Survival Probability
0.50
0.25
0.00
0 5 10 15
x−values
Question 7
# Define parameters
shape <- 2
scale <- 3
a <- 2
b <- 7
[1] 0.5324553
# Plot the area
M <-
data_frame(
x = seq(from = a, to = b, length = 1e3),
y = dgamma(x, shape = shape, scale = scale),
xp = seq(from = 0, to = 15, length = 1e3),
yp = dgamma(xp, shape = shape, scale = scale))
G <-
ggplot(
data = M) +
geom_area(
mapping = aes(x = x, y = y),
fill = 'darkorange',
alpha = 0.3) +
geom_line(
mapping = aes(x = xp, y = yp),
color = 'darkorange',
size = 1.2,
linetype = 'solid') +
geom_text(
mapping = aes(x = 4.50, y = 0.05),
label = paste('P[2 < X < 7] = ', round(prob, digits = 2)),
size = 5,
color = 'black',
family = 'serif',
fontface = 'italic',
angle = 0) +
labs(
title = 'A Gamma Area Plot',
subtitle = NULL,
x = 'x-values',
y = NULL,
caption = 'Michael King / Statistics I / Fall 2019') +
theme_bw() +
theme(
axis.ticks = element_blank(),
text = element_text(
size = 18,
face = 'italic',
family = 'serif',
color = 'black'),
axis.title.x = element_text(
vjust = 6))
G
A Gamma Area Plot
0.125
0.100
0.075
0.025
0.000
0 5 x−values 10 15
G
Relative Frequency Histogram / Exponential Numbers
0.15
0.10
0.05
0.00
0 10 20 30
Random Exponential Numbers
Michael King / Statistics I / Section 4.8
Question 9
M <-
expand.grid(
x = seq(from = 0, to = 1, by = 0.01),
alpha = 3,
beta = c(1, 2, 3)) %>%
mutate(y = dbeta(x = x, shape1 = alpha, shape2 = beta))
G <-
ggplot(
data = M) +
geom_line(
mapping = aes(x = x, y = y, col = as.factor(beta)),
size = 1.2,
linetype = 'solid') +
scale_color_manual(
name = 'Beta',
values = topo.colors(3)) +
labs(
title = 'Beta Density Functions, alpha = 3',
subtitle = NULL,
x = 'x-values',
y = NULL,
caption = 'Michael King / Statistics I / Fall 2019') +
theme(
legend.position = c(0.75, 0.95),
plot.title = element_text(hjust = 0.5),
legend.direction = 'horizontal',
axis.ticks = element_blank(),
text = element_text(
size = 18,
color = 'black',
family = 'serif',
face = 'italic'))
G
Beta Density Functions, alpha = 3
3 Beta 1 2 3
G <-
ggplot(
data = M) +
geom_line(
mapping = aes(x = x, y = y, color = as.factor(sigma)),
size = 1.3,
linetype = 'solid') +
scale_color_manual(
labels = c('1.0', '1.5', '2.0'),
values = topo.colors(3),
name = 'Standard Deviation') +
labs(
title = 'Normal Density Functions',
caption = 'Michael King / Statistics I / Section 4.10',
x = NULL,
y = NULL) +
theme(
plot.title = element_text(hjust = 0.5),
legend.position = c(0.8, 0.9),
legend.key.width = unit(1, units = 'inches'),
axis.ticks = element_blank(),
text = element_text(
size = 16,
color = 'black',
face = 'italic',
family = 'serif'))
G
Normal Density Functions
0.4 Standard Deviation
1.0
1.5
2.0
0.3
0.2
0.1
0.0