In this assignment, You’ll learn how to do homework using RMarkdown and Git repository. Follow the submission guideline (How to submit homework) to submit the html-preview link of your homework.
Play with the Class01-Example-1 data. Create RMarkdown report using this template. Read and answer each question carefully. The grading rubric is provided below.
ggplot2,
nutshell). Load and print the dimension of data
births2006.smpl. (Note: If a package is
not available from the official repository, you can install the package
from the archived source. See the template RMarkdown note for a hint in
the code block about how to install the package from its archived
source.)frequency on the y-axis and
day of week on the x-axis). Use facet_grid in
ggplot to break down the frequencies according to the three
delivery methods.
DOB_WK is the day of week, and
DMETH_REC is the delivery method.DPLURAL is the multiple-birth status.library('lattice')
library('ggplot2')
install.packages('https://cran.r-project.org/src/contrib/Archive/nutshell/nutshell_1.02.tar.gz',repo=NULL, type="source")
YOUR ANSWER for Problem 1.
births2006.smpl)Grading note (10%): need to load and print the dimension (5%), and explicitly answer the dimension of the dataset (5%)
# Load necessary packages
library(nutshell)
# Load the birth data
data(births2006.smpl)
# Print the dimensions of the dataset
dim(births2006.smpl)
## [1] 427323 13
YOUR ANSWER for Problem 2.
Grading note (40%): need to generate two plots as requested (30%) and explicitly answer the question (What is the most popular day of the week for babies being born?) (10%)
## plot by ggplot
ggplot(data = births2006.smpl, aes(x=factor(DOB_WK),fill=factor(DMETH_REC) )) +
geom_histogram(stat="count") +
scale_x_discrete("Day of Week") + scale_y_continuous("Freq") +
scale_fill_discrete(name="method of\ndelivery") +
coord_flip()
What is the most popular day of the week for babies being born? Wed
# Load necessary packages
if (!requireNamespace("ggplot2", quietly = TRUE)) {
install.packages("ggplot2", repos = "http://cran.us.r-project.org")
}
library(ggplot2)
# Load the data
library(nutshell)
data(births2006.smpl)
# Convert DOB_WK to a factor with ordered levels
births2006.smpl$DOB_WK <- factor(births2006.smpl$DOB_WK, levels = c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"), ordered = TRUE)
# Generate the histogram plot using ggplot2
ggplot(births2006.smpl, aes(x = DOB_WK)) +
geom_bar(aes(fill = DMETH_REC), position = "dodge") +
labs(title = "Number of Births by Day of Week and Delivery Method",
x = "Day of Week",
y = "Frequency") +
scale_fill_manual(values = c("1" = "blue", "2" = "orange", "3" = "green")) +
facet_grid(. ~ DMETH_REC)
What is the most popular day of the week for babies being born? Sunday (Sun)
YOUR ANSWER for Problem 3.
Grading note (50%): need to filter the right categories, and generate a plot (with only three categories) (40%); need to explicitly answer the question (Are triplets more likely to weigh less than singleton and twins?) with reasoning (could be based on density plot or hypothesis testing) (10%)
# Load necessary packages
if (!requireNamespace("ggplot2", quietly = TRUE)) {
install.packages("ggplot2", repos = "http://cran.us.r-project.org")
}
library(ggplot2)
# Load the data
library(nutshell)
data(births2006.smpl)
# Filter data for singletons, twins, and triplets
singleton_data <- subset(births2006.smpl, DPLURAL == 1)
twin_data <- subset(births2006.smpl, DPLURAL == 2)
triplet_data <- subset(births2006.smpl, DPLURAL == 3)
# Generate the density plot using ggplot2
ggplot() +
geom_density(aes(x = WTGAIN, fill = factor(DPLURAL)), data = births2006.smpl, alpha = 0.3) +
labs(title = "Density Plot of Baby Weights for Singletons, Twins, and Triplets",
x = "Baby Weight",
y = "Density") +
scale_fill_manual(values = c("1" = "blue", "2" = "orange", "3" = "green"),
labels = c("Singletons", "Twins", "Triplets")) +
theme_minimal()
Are triplets more likely to weigh less than singleton and twins? Yes, I think that triplets are more likely to weigh less than singletons and twins because the graph consistently indicates significant differences in baby weights among the groups. The graph show that triplets have significant lower weights compared to both singletons and twins.