Goals for Review:

1) [1.5 pts] Setting up

1a. [0.5 pts] Rename this assignment as “Lab 12 Assignment [Last, First].Rmd” and fill out the author and date lines.

1b. [0.5 pts] Check and set (if needed) your working directory. If you’d like to load packages with library(), please do so in this block.

library(palmerpenguins)
## 
## Attaching package: 'palmerpenguins'
## The following objects are masked from 'package:datasets':
## 
##     penguins, penguins_raw
getwd()
## [1] "/Users/owner/Downloads"
setwd("/Users/owner/Desktop/Lab_Assignment_10")
getwd()
## [1] "/Users/owner/Desktop/Lab_Assignment_10"

1c. [0.5 pts] Download the cake_data.csv dataset from Canvas. Relocate it to your folder for this class and import it into R. Please display the first few rows. Additionally, please use two other functions to get a sense of the data. This data contains the breakage angle (angle) for cakes made with different recipes (recipe).

data <- read.csv("/Users/owner/Desktop/Lab_Assignment_10/cake_data.csv")
head(data)
##   cakeid recipe    angle
## 1      1      A 42.44988
## 2      2      A 42.69705
## 3      3      A 33.50886
## 4      4      A 43.60376
## 5      5      A 54.89232
## 6      6      A 37.27297
str(data)
## 'data.frame':    100 obs. of  3 variables:
##  $ cakeid: int  1 2 3 4 5 6 7 8 9 10 ...
##  $ recipe: chr  "A" "A" "A" "A" ...
##  $ angle : num  42.4 42.7 33.5 43.6 54.9 ...
summary(data)
##      cakeid             recipe        angle      
##  Min.   :  1.00   Length   :100   Min.   :18.87  
##  1st Qu.: 25.75   N.unique :  4   1st Qu.:32.25  
##  Median : 50.50   N.blank  :  0   Median :37.43  
##  Mean   : 50.50   Min.nchar:  1   Mean   :37.83  
##  3rd Qu.: 75.25   Max.nchar:  1   3rd Qu.:43.86  
##  Max.   :100.00                   Max.   :57.97

^this worked!

2) [4 pts] Analysis of Variance (ANOVA): cake data

2a. [1 pt] You’d like to test to see if there are differences in the breakage angle for cakes made with these different recipes with a One-Way ANOVA. What are your null and alternative hypotheses for this test?

Your answer: ## Null hypothesis: The mean breakage angle is equal for cake recipes. ## Alternative hypothesis: At least one cake recipe has a different mean breakage angle.

2b. [1.5 pts] Perform this ANOVA and display the results with summary().

ANOVA:

cake_anova <- aov(angle ~ recipe, data = data)
summary(cake_anova)
##             Df Sum Sq Mean Sq F value Pr(>F)    
## recipe       3   3310  1103.4    39.1 <2e-16 ***
## Residuals   96   2709    28.2                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

2c. [1.5 pts] Briefly describe your results. Report F(df between, df within) and the p-value.

My results:

Your answer: ## The resulting f-statistic is 39.1, the df between is 3, the df within is 96, and the p-value is less than 0.001.

The results are statistically significant. The one-way ANOVA indicated a statistically significant difference in mean breakage angle among the four cake recipes, F(3,96) = 39.1, p < 0.001. I reject the null hypothesis and conclude that at least one recipe has a different mean breakage angle.

3) [4.5 pts] ANOVA: penguin data

Load the palmerpenguins package (install it first with install.packages(“palmerpenguins”) if needed). Does penguin body mass (body_mass_g) differ by species?

3a. [1.5 pts] Run a One-Way ANOVA of body_mass_g by species and display the results with summary(). Report F(df between, df within) and the p-value. Why is df within 339 and not 341?

Running the one-way ANOVA:

library(palmerpenguins)

penguin_anova <- aov(body_mass_g ~ species, data = penguins)
summary(penguin_anova)
##              Df    Sum Sq  Mean Sq F value Pr(>F)    
## species       2 146864214 73432107   343.6 <2e-16 ***
## Residuals   339  72443483   213698                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 2 observations deleted due to missingness

Your answer: ## The within df ar 339, not 341, because two of the observations have missing body mass measurements. These observations were excluded, leaving 342 penguins in the analysis. With 3 species, the df within = 342-3 = 339.

The one-way ANOVA results in a statistically significant difference in mean body mass among penguin species, F(2,339) = 343.6, p < 0.001. I reject the null hypothesis and conclude that at least one penguin species has a significantly different mean body mass.

3b. [1.5 pts] Using the Sum Sq and Df columns from your output in 3a, compute MS_B, MS_W, and F by hand in R. Does your F match the output?

anova_results <- summary(penguin_anova)[[1]]

MS_B <- anova_results["species", "Sum Sq"] / anova_results["species", "Df"]

MS_W <- anova_results["Residuals", "Sum Sq"] / anova_results["Residuals", "Df"]

F_value <- MS_B / MS_W

MS_B
## [1] 73432107
MS_W
## [1] 213697.6
F_value
## [1] 343.6263

Your answer: ## Based on my results, the MS_B = 73,432,107, the MS_W = 213,697.6, and the F_value = 343.6263. The calculated F-value matches the ANOVA output of 343.6. The slight decimal differences are likely due to rounding.

3c. [1.5 pts] Make a plot of body_mass_g by species (e.g., a boxplot or violin plot). Based on the plot, which species seems to differ from the others? Why can’t the ANOVA alone tell you this?

boxplot(body_mass_g ~ species, data = penguins,
        xlab = "Species",
        ylab = "Body Mass (g)",
        main = "Penguin Body Mass by Species")

Your answer: ## The Gentoo penguins appear to have higher body mass in comparison to the Adelie and Chinstrap penguins. The ANOVA alone does not tell us which specific species differ from one another because it only tell us whether or not at least one group mean is significantly different. A post-hoc test, such as Tukey’s HSD, may help to identify which specific groups differ.