# Load tidyverse
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# Define plot_binom as a function that plots the binomial pmf using ggplot
plot_binom <- function(n, p_values) {
# x represents possible number of successes
x <- 0:n
# Generate the binomial pmf for each p and store the results in a data frame
plot_data <- expand.grid(x = x, p = p_values) %>%
mutate(PMF = dbinom(x, size = n, prob = p),
ExpectedValue = n * p) # Calculate expected values for vertical lines
# Plot the pmf using ggplot
ggplot(data = plot_data, aes(x = x, y = PMF, color = as.factor(p))) +
geom_segment(aes(xend = x, yend = 0), linewidth = 1.5) +
geom_vline(aes(xintercept = ExpectedValue, color = as.factor(p)),
# Add vertical lines to represent mean
linetype = "dashed", size = 1) +
# Add title and legend
labs(title = paste("Binomial PMF for Bin(n =", n, ")"),
x = "Number of successes (k)",
y = "P(X = k)",
color = "p") +
theme_minimal() +
theme(legend.position = "right",
legend.justification = "center",
legend.box.margin = margin(0, 30, 0, 0))
}
# Set n = 40 and p equal to the desired values
p_values <- c(0.05, 0.1, 0.4, 0.6, 0.9, 0.95)
plot_binom(n = 40, p_values = p_values)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
YOUR ANSWER: The shape of the plots differs depending on the p value.
Plots with a p value closer to 0 are skewed to the left and are less
symmetric. Plots with a p value closer to 1 are skewed to the right and
are also less symmetric. Plots with a p value closer to 0.5 tend to have
a symmetric distribution.
# Load tidyverse
library(tidyverse)
# Let plot_binom_cdf be a function that plots the binomial cdf
plot_binom_cdf <- function(n = 40, p_values) {
x <- 0:n # Values for x-axis
# Generate cdf for each p_value and store the results in plot_data
plot_data <- expand.grid(x = x, p = p_values) %>%
mutate(CDF = pbinom(x, size = n, prob = p)) # Calculate CDF values
# Plot cdf using ggplot
ggplot(data = plot_data, aes(x = x, y = CDF, color = as.factor(p))) +
geom_step(size = 1.5) +
labs(title = "CDF of Binomial Distribution for Different p Values",
x = "Number of successes (k)",
y = "Cumulative Probability",
color = "p") + # Label the legend
# Add horizontal line at y = 0.8
geom_hline(yintercept = 0.8, linetype = "dashed", color = "black", size = 1) +
theme_minimal() +
# Create legend
theme(legend.position = "right",
legend.justification = "center",
legend.box.margin = margin(0, 30, 0, 0))
}
# Setting p_values to the desired values
p_values <- c(0.05, 0.1, 0.4, 0.6, 0.9, 0.95)
plot_binom_cdf(n = 40, p_values)
x <- seq(-10, 20, length.out = 1000)
#plot(x, pbinom(x, size = 15, prob = 0.8))
par(mfrow = c(2,1))
plot(x, pbinom(x, size = 15, prob = 0.99),
main = "CDF of Binomial with p = 0.99",
xlab = "x_values",
ylab = "cdf of Binnomial")
plot(x, pbinom(x, size = 15, prob = 0.6))
plot(x, pbinom(x, size = 15, prob = 0.4))
plot(x, pbinom(x, size = 15, prob = 0.05))
YOUR ANSWER: The points on the graph where the line meets the height of
0.8 show how many successes you need to have an 80% chance of reaching
that number. This depends on p, which represents how likely success is
in each attempt. By ooking at the graph, we can see that as p increases
(meaning the chance of success gets higher), the number of successes
needed to get an 80% chance of achieving that number usually goes down.
This makes sense because if you’re more likely to succeed in each
attempt, you don’t need as many successes to reach a certain total.
# Load tidyverse
library(tidyverse)
# Set number of trials set to o100
n <- 100
# Have plot_pois_pmf calculate the poisson pmf for a range of mu_values
plot_pois_pmf <- function(mu_values) {
# Have plot_data store the pmf values
plot_data <- data.frame(x = integer(), mu = numeric(), PMF = numeric())
# Loop over mu_values and calculate pmf
for (mu in mu_values) {
x <- 0:n
pois <- dpois(x, lambda = mu)
temp_data <- data.frame(x = x, mu = mu, PMF = pois)
plot_data <- rbind(plot_data, temp_data)
}
# Plot using ggplot
ggplot(data = plot_data, aes(x = x, y = PMF, color = as.factor(mu))) +
geom_segment(aes(xend = x, yend = 0), size = 1.5) +
# Set a vertical line to represent the mean
geom_vline(aes(xintercept = mu), linetype = "dashed", color = "black", size = 1) +
labs(title = "Poisson PMF for Different Mean Values",
x = "x",
y = "P(X=x)",
color = "Mean (μ)") +
theme_minimal() +
# Add legend
theme(legend.position = "right",
legend.justification = "center",
legend.box.margin = margin(0, 30, 0, 0))
}
# Mu_values represents the expected values that were provided
mu_values <- c(0.5, 1, 5, 8, 20, 50)
# Call the function
plot_pois_pmf(mu_values)
# Load tidyverse
library(tidyverse)
# Set the number of trials to 100
n <- 100
# Create a function that calculates the poisson cdf for provided mu values
plot_pois_cdf <- function(mu_values) {
# Have plot_data store cdf values
plot_data <- data.frame(x = integer(), mu = numeric(), CDF = numeric())
# Loop over mu_values and calculate cdf
for (mu in mu_values) {
x <- 0:n
pois_cdf <- ppois(x, lambda = mu)
# Combine results into a data frame
temp_data <- data.frame(x = x, mu = mu, CDF = pois_cdf)
plot_data <- rbind(plot_data, temp_data)
}
# Plot using ggplot
ggplot(data = plot_data, aes(x = x, y = CDF, color = as.factor(mu))) +
geom_step(size = 1.5) + # Step function for CDF
# Vertical line for mean
geom_vline(aes(xintercept = mu), linetype = "dashed", color = "black", size = 1) +
labs(title = "Poisson CDF for Different Mean Values",
x = "x",
y = "P(X <= x)",
color = "Mean (μ)") +
theme_minimal() +
# Create legend
theme(legend.position = "right",
legend.justification = "center",
legend.box.margin = margin(0, 30, 0, 0))
}
# Mu_values represents the expected values that were provided
mu_values <- c(0.5, 1, 5, 8, 20, 50)
# Call the function
plot_pois_cdf(mu_values)
Plot the graphs of the cdf of \(\text{Binom}(n=10, p=0.5)\) and \(\text{Pois}(\mu= 5)\) on the same plot.
# Set parameters equivalent to the provided binomial and poisson parameters
n1 <- 10
p1 <- 0.5
mu <- 5
# Create a sequence for x values ranging from 0 to 10
x1 <- 0:n1
# Calculate cdf for binomial and poisson cdfs using pbinom() and ppois()
binom_cdf1 <- pbinom(x1, size = n1, prob = p1)
pois_cdf1 <- ppois(x1, lambda = mu) # For the same x1
# Create a plot for binomial and poisson cdfs
plot(x1, binom_cdf1, type = "s", col = "purple",
lwd = 2, main = "CDF of Binomial(n=10, p=0.5) and Poisson(mu=5)",
xlab = "x", ylab = "Cumulative Probability",
ylim = c(0, 1))
# Make it pretty and add a legend
lines(x1, pois_cdf1, col = "orange", lwd = 2)
legend("topleft", legend = c("Binomial", "Poisson"),
col = c("purple", "orange"), lwd = 2)
Do the graphs overlap? Why/Why not?
YOUR ANSWER: The graphs do not overlap because the number of trials is very small, at 10. The Poisson distribution is used to predict the probability of rare events in a large population. Here, we have a small population of 10 with a probability of 0.5, which is not low. So, it makes sense that these graphs do not overlap.
Now plot the graphs of the cdf of \(\text{Binom}(n= 1000, p= 0.005)\) and \(\text{Pois}(\mu= 5)\).
# Set parameters equivalent to the provided binomial and poisson parameters
n2 <- 1000
p2 <- 0.005
mu <- 5
# Create a sequence for x values ranging from 0 to 1000
x2 <- 0:n2
# Calculate cdf for binomial and poisson cdfs using pbinom() and ppois()
binom_cdf2 <- pbinom(x2, size = n2, prob = p2)
pois_cdf2 <- ppois(x2, lambda = mu) # For the same x2
# Create a plot for binomial and poisson cdfs
plot(x2, binom_cdf2, type = "s", col = "purple",
lwd = 2, main = "CDF of Binomial(n=1000, p=0.005) and Poisson(mu=5)",
xlab = "x", ylab = "Cumulative Probability",
ylim = c(0, 1))
# Make it pretty and add a legend
lines(x2, pois_cdf2, col = "orange", lwd = 2)
legend("bottomright", legend = c("Binomial", "Poisson"),
col = c("purple", "orange"), lwd = 2)
Do the graphs overlap? Why/Why not?
YOUR ANSWER: The graphs overlap significantly because the number of trials is large and the probability of success is small. In this graph with 1000 trials, even though the probability of success on each trial is low, we still see around 5 successes on average, which aligns well with the average of the Poisson distribution.
In this section we will explore the normal distribution.
Set \(\mu = 5\). For values of \(\sigma\) given by \(0.2, 0.4, 0.8, 1, 1.3, 1.8, 2\), plot the densities of \(N(\mu, \sigma)\) in the same plot. It might help if (1) you have the densities of \(N(\mu = 5, \sigma = 0.2)\) and \(N(\mu = 5, \sigma = 2)\) to be blue in color and the rest to be red. (2) choose appropriate limits for the x-axis (use x_lim parameter in the plot funtion) and y-axis (use y_lim).
library(tidyverse)
x <- seq(-6, 6, length.out = 1000)
x <- 5+x
y_0.2 <- dnorm(x, mean= 5, sd = 0.2)
y_0.4 <- dnorm(x, mean= 5, sd = 0.4)
y_0.8 <- dnorm(x, mean= 5, sd = 0.8)
y_1 <- dnorm(x, mean= 5, sd = 1)
y_1.3 <- dnorm(x, mean= 5, sd = 1.3)
y_1.8 <- dnorm(x, mean = 5, sd = 1.8)
y_2 <- dnorm(x, mean= 5, sd = 2)
plot_data_pdf <- data.frame(x,y_0.2,y_0.4,y_0.8,y_1,y_1.3, y_1.8, y_2)
plot_data_pdf_new <- pivot_longer(plot_data_pdf,
cols = c(y_0.2,y_0.4,y_0.8,y_1,y_1.3, y_1.8, y_2),
names_to = "std_devs"
)
ggplot(data = plot_data_pdf_new,
aes(x = x,
y = value,
group = std_devs,
color = std_devs))+
geom_line()+
labs(title = "Normal Density Plots: Varying Std Devs",
subtitle = "Jonathan Fernandes",
x = "x-axis",
y = "density")
What do you notice about the plot? Comment about how the width changes. YOUR ANSWER: All the plots have a peak at 5, as this is what the mean is. The red distribution is narrow and has a high peak/density because the standard deviation is small (SD=0.2), so the graph is less “spread out” and more tightly concentrated around the mean. The magenta distribution is wider and a low peak/density because the standard deviation is large (SD=2), so the graph is more “spread out” and less tightly concentrated around the mean. In conclusion, the lower the SD value, the less spread out the graph is and the more dense it is. The higher the SD value, the more spread out the graph is and the less dense it is.
Set \(\mu = 5\). For values of \(\sigma\) given by \(0.2, 0.4, 0.8, 1, 1.3, 1.8, 2\), plot the cummulative distribution function of \(N(\mu, \sigma)\) in the same plot. It might help if (1) you have the cdf of \(N(\mu = 5, \sigma = 0.2)\) and \(N(\mu = 5, \sigma = 2)\) to be blue in color and the rest to be red. (2) choose appropriate limits for the x-axis (use x_lim parameter in the plot funtion) and y-axis (use y_lim).
z_0.2 <- pnorm(x, mean= 5, sd = 0.2)
z_0.4 <- pnorm(x, mean= 5, sd = 0.4)
z_0.8 <- pnorm(x, mean= 5, sd = 0.8)
z_1 <- pnorm(x, mean= 5, sd = 1)
z_1.3 <- pnorm(x, mean= 5, sd = 1.3)
z_1.8 <- pnorm(x, mean = 5, sd = 1.8)
z_2 <- pnorm(x, mean= 5, sd = 2)
plot_data_cdf <- data.frame(x,z_0.2,z_0.4,z_0.8,z_1,z_1.3, z_1.8, z_2)
plot_data_cdf_new <- pivot_longer(plot_data_cdf,
cols = c(z_0.2,z_0.4,z_0.8,z_1,z_1.3, z_1.8, z_2),
names_to = "std_devs"
)
ggplot(data = plot_data_cdf_new,
aes(x = x,
y = value,
group = std_devs,
color = std_devs))+
geom_line()+
labs(title = "Normal Density Plots: Varying Std Devs",
x = "x-axis",
y = "density")
What information does the point of intersection of two cdfs give us? YOUR ANSWER: The intersection point of all the cdfs tells us that all of the distributions have the same cumulative probability up to that specific value of x. This graph shows us that regardless of the SD, all of the distributions have the same cumulative probability up to 5, which is the mean value for this graph.
Set \(\sigma = 0.4\). For values of \(\mu\) given by \(-1, -0.5, 0, 0.4, 0.9, 2.5, 4\) plot the densities of \(N(\mu, \sigma)\) in the same plot. You might need to choose appropriate limits for the x-axis.
# Set the standard deviation equal to 0.4
sigma <- 0.4
# Set the means_array equal to all of the means we want
means_array <- c(-1, -0.5, 0, 0.4, 0.9, 2.5, 4)
# Adjust limit for the x values
x <- seq(-4, 8, length.out = 1000)
# Densities stores the densities for each mean
densities <- sapply(means_array, function(mu) dnorm(x, mean = mu, sd = sigma))
# Use data.frame() for plotting
plot_data_density <- data.frame(x, densities)
colnames(plot_data_density)[-1] <- means_array
# Reshape data for ggplot
plot_data_density_long <- pivot_longer(plot_data_density, cols = -x,
names_to = "mean_values", values_to = "density")
# Plot densities using ggplot()
ggplot(data = plot_data_density_long, aes(x = x, y = density, group = mean_values, color = mean_values)) +
geom_line() +
labs(title = "Normal Density Plots: Varying Means, Fixed SD = 0.4",
x = "x-axis",
y = "Density",
color = "Mean") +
theme_minimal()
Set \(\sigma = 0.4\). For values of \(\mu\) given by \(-1, -0.5, 0, 0.4, 0.9, 2.5, 4\) plot the cumulative distribution functions of \(N(\mu, \sigma)\) in the same plot. You might need to choose appropriate limits for the x-axis.
# Set the standard deviation equal to 0.4
sigma <- 0.4
# Set the means_array equal to all of the means we want
means_array <- c(-1, -0.5, 0, 0.4, 0.9, 2.5, 4)
# Adjust limit for the x values
x <- seq(-4, 8, length.out = 1000)
# Have cdf_vals store the CDF for each mean value
cdf_vals <- sapply(means_array, function(mu) pnorm(x, mean = mu, sd = sigma))
# Use data.frame() for plotting
plot_data_cdf <- data.frame(x, cdf_vals)
colnames(plot_data_cdf)[-1] <- means_array
# Reshape data for ggplot
plot_data_cdf_long <- pivot_longer(plot_data_cdf, cols = -x,
names_to = "mean_values",
values_to = "cdf")
# Plot CDFs using ggplot
ggplot(data = plot_data_cdf_long, aes(x = x, y = cdf, group = mean_values, color = mean_values)) +
geom_line() +
labs(title = "Normal CDF Plots with a SD = 0.4",
x = "x-axis",
y = "Cumulative Probability",
color = "Mean") +
theme_minimal()
For values of \(\lambda\) in \((0.2, 0.5, 1, 2, 8, 10)\) plot the graphs of the densities of \(\text{Exp}(\lambda)\) in the same plot.
# Set lamba_arr equal to an array of the desired lamba values
lambda_arr <- c(0.2, 0.5, 1, 2, 8, 10)
# Make the x-axis range from 0 to 5
x <- seq(0, 5, length.out = 1000)
# Calculate densities for each lambda value
pdf_values <- sapply(lambda_arr, function(lambda) dexp(x, rate = lambda))
# Use data.frame() to get plot_data_pdf values, which will be used in the plot
plot_data_pdf <- data.frame(x, pdf_values)
plot_data_pdf_long <- pivot_longer(plot_data_pdf, cols = -x,
names_to = "lambda_values", values_to = "pdf")
# Use ggplot() to plot the densities
ggplot(data = plot_data_pdf_long, aes(x = x, y = pdf, group = lambda_values, color = lambda_values)) +
geom_line() +
labs(title = "Exponential Density Plots for Different Lambda Values",
x = "x-axis",
y = "Density") +
scale_color_discrete(name = "Lambda", labels = lambda_arr) +
theme_minimal()
For values of \(\lambda\) in \((0.2, 0.5, 1, 2, 8, 10)\) plot the graphs of the cumulative distribution function of \(\text{Exp}(\lambda)\) in the same plot. Draw a horizontal line at \(y=0.8\).
# Define the lambda values
lambda_arr <- c(0.5, 1, 2, 5, 10)
# Adjust x values
x <- seq(0, 5, length.out = 1000)
# Calculate cdf for each lambda value and store it in cdf_arr
cdf_arr <- sapply(lambda_arr, function(lambda) pexp(x, rate = lambda))
# Use data.frame() to get plot_data_cdf values, which will be used in the plot
plot_data_cdf <- data.frame(x, cdf_arr)
# Assign the column names using the lambda values
colnames(plot_data_cdf)[-1] <- lambda_arr
# Reshape the data for ggplot
plot_data_cdf_long <- pivot_longer(plot_data_cdf,
cols = -x,
names_to = "lambda_values",
values_to = "cdf")
# Plot the CDFs using ggplot
ggplot(data = plot_data_cdf_long, aes(x = x, y = cdf, group = lambda_values, color = lambda_values)) +
geom_line() +
# Add horizontal line at y = 0.8
geom_hline(yintercept = 0.8, linetype = "dashed", color = "black") +
labs(title = "Exponential CDF Plots for Different Lambda Values",
x = "x-axis",
y = "Cumulative Probability",
color = "Lambda") +
theme_minimal()
Interpret the values of \(x\) where the line \(y=0.8\) intersects with the graphs of the cdfs. YOUR ANSWER: The value of x at which the cumulative probability is 0.8 represents the point where there’s an 80% chance that the observed value is less than or equal to x. Smaller lambda values (like 0.2 or 0.5) need a larger x value for the cdf to reach 0.8 because smaller lambda means a slower rate of decay, so it takes longer to accumulate 80% of the distribution. In comparison, higher lambda values (like 8 or 10) reach 0.8 significantly faster because a larger lambda means a larger rate of decay, so it takes less time to accumulate 80% of the distribution.
We will plot the Gamma distribution for different shapes and scales. You might need to adjust the limits of x and y axes appropriately.
For values of \(\alpha \in (0.3,0.7, 1, 1.5, 2, 2.5, 10)\), plot the Plot the densities of \(\text{Gamma}(\alpha, \beta = 1)\) in a single plot.
# Load necessary libraries
library(ggplot2)
library(tidyr)
# Values of x ranging from 0 to 10
x <- seq(0, 10, length.out = 1000)
# Gamma density functions for each specified alpha
alpha_values <- c(0.3, 0.7, 1, 1.5, 2, 2.5, 10)
density_list <- lapply(alpha_values, function(alpha) dgamma(x, shape = alpha, scale = 1))
# Create a data frame with the gamma densities
plot_data_pdf <- data.frame(x)
for (i in 1:length(alpha_values)) {
plot_data_pdf[[paste0("y_", alpha_values[i])]] <- density_list[[i]]
}
# Reshape the data frame for ggplot
plot_data_pdf_long <- pivot_longer(plot_data_pdf,
cols = -x,
names_to = "shapes",
values_to = "density"
)
# Create a vector with the alpha values for labeling
alpha_labels <- as.character(alpha_values)
# Plot the gamma densities using ggplot
ggplot(data = plot_data_pdf_long,
aes(x = x, y = density, group = shapes, color = shapes)) +
geom_line() +
labs(title = "Gamma Density Plots: Varying Shape Parameters (α)",
x = "x-axis",
y = "Density") +
scale_color_discrete(name = "Alpha",
labels = alpha_labels) +
theme_minimal()
For each of the shapes, identify a feature that distinguishes one shape from the other. This to consider a: does it have a peak, concavity, presence of inflection points, etc. YOUR RESPONSE: alpha = 0.3 Peak: The distribution is heavily skewed to the right and has a very high peak when x is 0. Concavity: It is concave up for most of its range. It has rapid growth initially, but it falls down fast Inflection Points: The curve does not have inflection points. alpha = 0.7 Peak: The peak is very similar to the peak when alpha is 0.3 because it is very high and it peaks when x is 0. Concavity: Similar to alpha=0.3, it is concave up, but it is different because it is slightly less concave then when alpha is 0.3. Inflection Points: The curve does not have inflection points. alpha = 1 Peak: This distribution has a moderate peak at 1 when x is 0 and it looks more like the exponential distribution (a special case of the gamma distribution.) Concavity: The curve is still concave up, but not as concave as when alpha is lower. Inflection Points: The curve has no inflection points. alpha = 1.5: Peak: The peak is lower than when alpha is lower. It’s not when x=0 anymore, and the peak is a little under 0.5 Concavity: The curve is concave down and very close together, suggesting a lower standard deviation. The graph is also skewed to the right. Inflection Points: The curve does not have inflection points. alpha = 2: Peak: The peak is on the right side of the graph, which none of the other functions have. Its density starts at 0 and then the peak occurs at around x=9. It is the lowest peak value out of all of the alpha values. Concavity: The curve starts off concave up and then become concave down at around x=5.5. Inflection Points: The curve contains one inflection point where the concavity shifts, around x=5.5. alpha = 2.5: Peak: The distribution has a slightly lower peak compared to lower alpha values and it has a broader spread. It is also skewed to the right. Concavity: It is concave down. Inflection Points: The curve does not have inflection points. alpha = 10: Peak: This graph has the lowest peak. Concavity: The curve is the least skewed and it is concave down. Inflection Points: The curve does not have inflection points.
Set \(\alpha = 1\), vary \(\beta\) over \(0.2, 0.6, 1, 1.5, 2\). Plot the densities of \(\text{Gamma}(\alpha, \beta)\) in a single plot.
# Set alpha and beta values
alpha <- 1
beta_values <- c(0.2, 0.6, 1, 1.5, 2)
# Create a sequence of x values
x <- seq(0, 10, length.out = 1000)
# Initialize an empty plot
plot(0, 0, type = "n", xlim = c(0, 10), ylim = c(0, 3),
main = expression(paste("Gamma Density with ", alpha, " = 1")),
xlab = "x", ylab = "Density")
# Define a color palette
colors <- rainbow(length(beta_values))
# Loop over beta values and plot the densities
for (i in seq_along(beta_values)) {
beta <- beta_values[i]
y <- dgamma(x, shape = alpha, scale = beta)
lines(x, y, lwd = 2, col = colors[i])
}
legend("topright", legend = beta_values, title = "Beta", col = colors, lwd = 2)
Set \(\alpha = 0.6\), vary \(\beta\) over \(0.2, 0.6, 1, 1.5, 2\). Plot the densities of \(\text{Gamma}(\alpha, \beta)\) in a single plot.
# Set alpha and beta values
alpha <- 0.6
beta_values <- c(0.2, 0.6, 1, 1.5, 2)
# Create a sequence of x values
x <- seq(0, 10, length.out = 1000)
# Initialize an empty plot
plot(0, 0, type = "n", xlim = c(0, 10), ylim = c(0, 3),
main = expression(paste("Gamma Density with ", alpha, " = 0.6")),
xlab = "x", ylab = "Density")
# Create a color palette
colors <- rainbow(length(beta_values))
# Loop over beta values and plot the densities
for (i in seq_along(beta_values)) {
beta <- beta_values[i]
y <- dgamma(x, shape = alpha, scale = beta)
lines(x, y, lwd = 2, col = colors[i])
}
legend("topright", legend = beta_values, title = "Beta", col = colors, lwd = 2)
Set \(\alpha = 2\), vary \(\beta\) over \(0.2, 0.6, 1, 1.5, 2\). Plot the densities of \(\text{Gamma}(\alpha, \beta)\) in a single plot.
# Set alpha and beta values
alpha <- 2
beta_values <- c(0.2, 0.6, 1, 1.5, 2)
# Create a sequence of x values
x <- seq(0, 10, length.out = 1000)
# Initialize an empty plot
plot(0, 0, type = "n", xlim = c(0, 10), ylim = c(0, 3),
main = expression(paste("Gamma Density with ", alpha, " = 2")),
xlab = "x", ylab = "Density")
# Define a color palette
colors <- rainbow(length(beta_values))
# Loop over beta values and plot the densities
for (i in seq_along(beta_values)) {
beta <- beta_values[i]
y <- dgamma(x, shape = alpha, scale = beta)
lines(x, y, lwd = 2, col = colors[i])
}
legend("topright", legend = beta_values, title = "Beta", col = colors, lwd = 2)
Set \(\alpha = 5\), vary \(\beta\) over \(0.2, 0.6, 1, 1.5, 2\). Plot the densities of \(\text{Gamma}(\alpha, \beta)\) in a single plot.
# Set alpha and beta values
alpha <- 5
beta_values <- c(0.2, 0.6, 1, 1.5, 2)
# Create a sequence of x values
x <- seq(0, 10, length.out = 1000)
# Initialize an empty plot
plot(0, 0, type = "n", xlim = c(0, 10), ylim = c(0, 3),
main = expression(paste("Gamma Density with ", alpha, " = 5")),
xlab = "x", ylab = "Density")
# Select a color palette
colors <- rainbow(length(beta_values))
# Loop over beta values and plot the densities
for (i in seq_along(beta_values)) {
beta <- beta_values[i]
y <- dgamma(x, shape = alpha, scale = beta)
lines(x, y, lwd = 2, col = colors[i])
}
legend("topright", legend = beta_values, title = "Beta", col = colors, lwd = 2)