diff --git a/index.pdf b/index.pdf index c6bb8c6..d148663 100644 Binary files a/index.pdf and b/index.pdf differ diff --git a/index.qmd b/index.qmd index 5c869d9..93c34c2 100644 --- a/index.qmd +++ b/index.qmd @@ -1,11 +1,10 @@ --- title: "Homework 2" -author: "[Insert your name here]{style='background-color: yellow;'}" +author: "Carson Pedaci {style='background-color: yellow;'}" toc: true title-block-banner: true title-block-style: default -format: html -# format: pdf +format: pdf --- [Link to the Github repository](https://github.com/psu-stat380/hw-2) @@ -28,7 +27,7 @@ For this assignment, we will be using the [Abalone dataset](http://archive.ics.u We will be using the following libraries: -```R +```{R} library(readr) library(tidyr) library(ggplot2) @@ -51,7 +50,7 @@ EDA using `readr`, `tidyr` and `ggplot2` Load the "Abalone" dataset as a tibble called `abalone` using the URL provided below. The `abalone_col_names` variable contains a vector of the column names for this dataset (to be consistent with the R naming pattern). Make sure you read the dataset with the provided column names. -```R +```{R} library(readr) url <- "http://archive.ics.uci.edu/ml/machine-learning-databases/abalone/abalone.data" @@ -67,7 +66,8 @@ abalone_col_names <- c( "rings" ) -abalone <- ... # Insert your code here +abalone <- readr::read_csv(url, col_names = abalone_col_names) +abalone ``` --- @@ -76,11 +76,12 @@ abalone <- ... # Insert your code here Remove missing values and `NA`s from the dataset and store the cleaned data in a tibble called `df`. How many rows were dropped? -```R -df <- ... # Insert your code here +```{R} +df <- abalone[complete.cases(abalone), ] +df ``` - +No rows were dropped. --- @@ -89,8 +90,12 @@ df <- ... # Insert your code here Plot histograms of all the quantitative variables in a **single plot** [^footnote_facet_wrap] -```R -... # Insert your code here +```{R} +df %>% + pivot_longer(cols = c('length', 'diameter', 'height', 'whole_weight', 'shucked_weight', 'viscera_weight', 'shell_weight', 'rings'), names_to = 'var', values_to = 'val')%>% + ggplot + + geom_histogram(aes(x = val)) + + facet_wrap(facets = 'var') ``` @@ -100,12 +105,16 @@ Plot histograms of all the quantitative variables in a **single plot** [^footnot Create a boxplot of `length` for each `sex` and create a violin-plot of of `diameter` for each `sex`. Are there any notable differences in the physical appearences of abalones based on your analysis here? -```R -... # Insert your code for boxplot here +```{R} +df %>% + ggplot + + geom_boxplot(aes(x = sex, y = length)) ``` -```R -... # Insert your code for violinplot here +```{R} +df %>% + ggplot + + geom_violin(aes(x = sex, y = diameter)) ``` @@ -117,10 +126,12 @@ Create a scatter plot of `length` and `diameter`, and modify the shape and color -```R -... # Insert your code here +```{R} +df %>% + ggplot + + geom_point(aes(x = length, y = diameter, color = sex, size = shell_weight)) ``` - +As length increases, so does the shell weight. There is one abalone with a significantly greater diameter compared to other abalones of its length. --- ###### 1.6 (5 points) @@ -128,8 +139,12 @@ Create a scatter plot of `length` and `diameter`, and modify the shape and color For each `sex`, create separate scatter plots of `length` and `diameter`. For each plot, also add a **linear** trendline to illustrate the relationship between the variables. Use the `facet_wrap()` function in R for this, and ensure that the plots are vertically stacked **not** horizontally. You should end up with a plot that looks like this: [^footnote_plot_facet] -```R -... # Insert your code here +```{R} +df %>% + ggplot + + geom_point(aes(x = length, y = diameter)) + + facet_grid(facets = 'sex', as.table = TRUE) + + geom_smooth(aes(x = length, y = diameter)) ``` @@ -154,8 +169,11 @@ More advanced analyses using `dplyr`, `purrrr` and `ggplot2` Filter the data to only include abalone with a length of at least $0.5$ meters. Group the data by `sex` and calculate the mean of each variable for each group. Create a bar plot to visualize the mean values for each variable by `sex`. -```R -df %>% ... # Insert your code here +```{R} +df %>% + filter(length >= 0.5) %>% + group_by(sex)%>% + summarise(mean(length), mean(diameter), mean(height), mean(whole_weight), mean(shucked_weight), mean(viscera_weight), mean(shell_weight), mean(rings)) ``` @@ -176,8 +194,16 @@ Implement the following in a **single command**: 3. Use the `geom_tile()` function to create a tile plot of `num_rings` vs `sex` with the color indicating of each tile indicating the `avg_weight` value. -```R -df %>% ... # Insert your code here +```{R} +num_df <- + df %>% + mutate('num_rings' = case_when(rings < 10 ~ 'low', rings > 20 ~ 'high', 10 <= rings | rings <= 20 ~ 'med')) + +num_df %>% + group_by(num_rings, sex) %>% + summarise('avg_weight' = mean(whole_weight + shucked_weight + viscera_weight + shell_weight))%>% + ggplot + + geom_tile(aes(x = sex, y = num_rings, fill = avg_weight)) ``` @@ -189,8 +215,11 @@ df %>% ... # Insert your code here Make a table of the pairwise correlations between all the numeric variables rounded to 2 decimal points. Your final answer should look like this [^footnote_table] -```R -df %>% ... # Insert your code here +```{R} +R <- df %>% +keep(is.numeric) %>% +cor() +R ``` @@ -206,8 +235,12 @@ Use the `map2()` function from the `purrr` package to create a scatter plot for ::: -```R -... # Insert your code here +```{R} +df %>% + pivot_longer(cols = c('length', 'diameter', 'height', 'whole_weight', 'shucked_weight', 'viscera_weight', 'shell_weight'), names_to = 'var', values_to = 'val')%>% + ggplot + + geom_point(aes(x = rings, y = val, color = sex)) + + facet_grid(facets = 'var') ``` @@ -230,8 +263,10 @@ Linear regression using `lm` Perform a simple linear regression with `diameter` as the covariate and `height` as the response. Interpret the model coefficients and their significance values. -```R -... # Insert your code here +```{R} +model <- lm(df$height ~ df$diameter) + +summary(model) ``` @@ -243,10 +278,12 @@ Perform a simple linear regression with `diameter` as the covariate and `height` Make a scatterplot of `height` vs `diameter` and plot the regression line in `color="red"`. You can use the base `plot()` function in R for this. Is the linear model an appropriate fit for this relationship? Explain. -```R -... # Insert your code here +```{R} +x = df$diameter +plot(df$diameter, df$height) +lines(df$diameter, fitted(lm(df$height~df$diameter)), col='red') ``` - +The linear model is an appropriate fit. The data present no visible patterns of a correlation other than linear. --- @@ -255,8 +292,8 @@ Make a scatterplot of `height` vs `diameter` and plot the regression line in `co Suppose we have collected observations for "new" abalones with `new_diameter` values given below. What is the expected value of their `height` based on your model above? Plot these new observations along with your predictions in your plot from earlier using `color="violet"` -```R - +```{R} +df3 <- df %>% select(diameter, height) new_diameters <- c( 0.15218946, 0.48361548, @@ -268,10 +305,14 @@ new_diameters <- c( 0.92906875, 0.94245437, 0.01209518 -) - - -... # Insert your code here. +) + +nd_frame = data.frame(new_diameters)%>% + rename('diameter' = 'new_diameters') +model <- lm(height ~ diameter, data = df3) +predict_model <- predict(model, nd_frame) +plot(nd_frame%>% unlist(), predict_model, col = 'violet') +points(df3$diameter, df3$height, col = 'grey') ```