In this exploration, you are asked to

  1. First,
    1. Type your name into the author field in the YAML parameters above
    2. Also save this file while replacing “template” with your full name (be sure to keep the *.rmd file format).
library("ggplot2")

Uniform Distribution

  1. Demonstrate your understanding about the example code block below by adding at least 5 comments in the code block in your own words.
N <- 1000
a <- 1
b <- 32
data <- runif(N, a, b) #generate 1000 random variables from 1 to 32 and labels it 'data'
df <- data.frame(data) #stores the data as a data table 

#creates a box plot 
ggplot(df, aes(y = data)) + 
  coord_flip() +
  geom_boxplot() +
  labs(title = paste0("Uniform Distribution U(", a, ",", b, ")"),
       subtitle = "boxplot",
       caption = "Humberto Flores") +
  theme(axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  ylim(0,50) #defines the horizontal axis range from 0 to 50 for the box plot

#creates uniform distribution graph
ggplot(df, aes(x = data)) +
  geom_density(kernel = "gaussian") +
  labs(title = paste0("Uniform Distribution U(", a, ",", b, ")"),
       subtitle = "density plot",
       caption = "Humberto Flores") +
  theme(panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  xlim(0,50)

  1. Repeat step 2 in a new code block, but with different values of \(a\) and \(b\) in \(0 < a < b < 50\)
N <- 1000
a <- 3
b <- 30
data <- runif(N, a, b) #generate 1000 random variables from 0 to 50 and labels it 'data'
df <- data.frame(data) #stores the data as a data table 

#creates a box plot 
ggplot(df, aes(y = data)) +
  coord_flip() +
  geom_boxplot() +
  labs(title = paste0("Uniform Distribution U(", a, ",", b, ")"),
       subtitle = "boxplot",
       caption = "Humberto Flores") +
  theme(axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  ylim(0,50) #defines the horizontal axis range from 0 to 50 for the box plot

#creates uniform distribution graph
ggplot(df, aes(x = data)) +
  geom_density(kernel = "gaussian") +
  labs(title = paste0("Uniform Distribution U(", a, ",", b, ")"),
       subtitle = "density plot",
       caption = "Humberto Flores") +
  theme(panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  xlim(0,50)

  1. Make at least two observations about the results (the graphs) in an unordered list.

Normal Distribution

  1. Make adjustments to the code below to repeat what was done above with the uniform distribution, but this time looking at the normal distribution (hint: use the rnorm command).
N <- 1000
mu <- 25
sd <- 3
data <- rnorm(N, mu, sd)
df <- data.frame(data)

ggplot(df, aes(y = data)) +
  coord_flip() +
  geom_boxplot() +
  labs(title = paste0("Normal Distribution N(", mu, ",", sd*sd, ")"),
       subtitle = "boxplot",
       caption = "Humberto Flores") +
  theme(axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  ylim(0,50)

ggplot(df, aes(x = data)) +
  geom_density(kernel = "gaussian") +
  labs(title = paste0("Normal Distribution N(", mu, ",", sd*sd, ")"),
       subtitle = "density plot",
       caption = "Humberto Flores") +
  theme(panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  xlim(0,50)

  1. Repeat step 5 in a new code block, but with different values of of \(\mu\) and \(\sigma\) in \(0 < \sigma < \mu < 50\)
N <- 1000
mu <- 25
sd <- 2
data <- rnorm(N, mu, sd)
df <- data.frame(data)

ggplot(df, aes(y = data)) +
  coord_flip() +
  geom_boxplot() +
  labs(title = paste0("Normal Distribution N(", mu, ",", sd*sd, ")"),
       subtitle = "boxplot",
       caption = "Math 32") +
  theme(axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  ylim(0,50)

ggplot(df, aes(x = data)) +
  geom_density(kernel = "gaussian") +
  labs(title = paste0("Normal Distribution N(", mu, ",", sd*sd, ")"),
       subtitle = "density plot",
       caption = "Humberto Flores") +
  theme(panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  xlim(0,50)

  1. Make at least two observations about the results (the graphs) in an unordered list.

Exponential Distribution

  1. Make adjustments to the code below to repeat what was done above with the uniform distribution, but this time looking at the exponential distribution with \(\lambda = \frac{1}{\mu}\) (hint: use the rexp command).
N <- 1000
mu <- 32
m <- 1/32
data <- rexp(N, m)
df <- data.frame(data)

ggplot(df, aes(y = data)) +
  coord_flip() +
  geom_boxplot() +
  labs(title = paste0("Exponential Distribution E(1/", mu, ")"),
       subtitle = "boxplot",
       caption = "Humberto Flores") +
  theme(axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  ylim(0,250)

ggplot(df, aes(x = data)) +
  geom_density(kernel = "gaussian") +
  labs(title = paste0("Exponential Distribution E(1/", mu, ")"),
       subtitle = "density plot",
       caption = "Humberto Flores") +
  theme(panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  xlim(0,250)

  1. Repeat step 7 in a new code block, but with different a value of \(\mu\) in \(0 < \mu < 50\)
N <- 1000
mu <- 30
m <- 1/mu
data <- rexp(N, m)
df <- data.frame(data)

ggplot(df, aes(y = data)) +
  coord_flip() +
  geom_boxplot() +
  labs(title = paste0("Exponential Distribution E(1/", mu, ")"),
       subtitle = "boxplot",
       caption = "Humberto Flores") +
  theme(axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  ylim(0,250)
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

ggplot(df, aes(x = data)) +
  geom_density(kernel = "gaussian") +
  labs(title = paste0("Exponential Distribution E(1/", mu, ")"),
       subtitle = "density plot",
       caption = "Humberto Flores") +
  theme(panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  xlim(0,250)
## Warning: Removed 1 rows containing non-finite values (stat_density).

  1. Make at least two observations about the results (the graphs) in an unordered list.
  1. Verify that you have 12 graphs in total (6 boxplots and 6 density plots), and replace “Math 32” with your full name in all of the graphs.

  2. Finally, knit your work as either an HTML or PDF document, and upload that document back into the CatCourses assignment.