library(palmerpenguins)
library(tidyverse)
set.seed(123)
Reproducing Student A’s Code
The following analysis reproduces Student A’s code in the context of the penguins
dataset from the palmerpenguins package (Horst, Hill, and Gorman 2020).
Horst, Allison Marie, Alison Presmanes Hill, and Kristen B Gorman. 2020. Palmerpenguins: Palmer Archipelago (Antarctica) Penguin Data. https://doi.org/10.5281/zenodo.3960218.
The only variable type missing from the penguins
dataset is a variable similar to Student A’s “Age” variable. The pengins
dataset contains information on adult penguins. Depending on the size of the penguin, they can take between three and eight years until they begin breeding. Most species of penguins take at least five years. Thus, when creating an age
variable in the penguins
dataset I simulated values from a Normal distribution with mean 5 and standard deviation 1.
<- penguins |>
penguins mutate(age = rnorm(n = nrow(penguins), mean = 5, sd = 1),
age = round(age, digits = 0)
)
Reproducing Student A’s Code
#upper anterior measurement Linear model
<- lm(penguins$bill_length_mm~penguins$bill_depth_mm)
linearAnterior summary(linearAnterior)
linearAnteriorwith(penguins, plot(bill_length_mm~bill_depth_mm,las = 1,col = ifelse(penguins$body_mass_g< 4050,"red","black")))
abline(linearAnterior)
plot(linearAnterior)
#Exponential function
<- lm(penguins$bill_length_mm~log(penguins$bill_depth_mm))
expAnterior summary (expAnterior)
expAnteriorwith(penguins, plot(bill_length_mm~log(bill_depth_mm), las = 1, col = ifelse(penguins$body_mass_g< 4050,"red","black")))
abline(expAnterior)
plot(expAnterior)
summary(expAnterior)
<- subset(penguins, year<2007)
early <- subset(penguins, year<2009 & year>2007)
mid <- transform(penguins, Age = as.integer(age))
penguinsSub <- subset(penguinsSub, year<2008)
Early <- subset(penguinsSub, year<2009 & year>2007)
Mid <- ddply(Early, ~Age, summarise, meanWE=mean(body_mass_g, na.rm = T))
EarlyWeightAge <- ddply(Early, ~Age, summarise, meanLE=mean(bill_length_mm, na.rm = T))
EarlyLengthAge <- ddply(Mid, ~Age, summarise, meanLM=mean(bill_length_mm, na.rm = T))
MidLengthAge <- rep(NA, 9)
WeightChange
library(plyr)
<- ddply(penguinsSub, ~Age, summarise, meanW=mean(body_mass_g, na.rm = T))
WeightAge <- ddply(penguinsSub, ~Age, summarise, meanL=mean(bill_length_mm, na.rm = T))
LengthAge plot(EarlyLengthAge$meanLE~EarlyLengthAge$Age,las = 1,ylab = "Fork Length (mm)",xlab = "Age")
lines(EarlyLengthAge$meanLE~EarlyLengthAge$Age)
points(MidLengthAge$meanLM~MidLengthAge$Age,col = "red")
lines(MidLengthAge$meanLM~MidLengthAge$Age,col = "red")
legend(6, 48, legend = c("1998-2003", "2006-2017"),col = c("black", "red"), lty = 1:1,cex = 0.8)
#Tanner's code/help
<- rep(NA, 9)
WeightChange library(plyr)
<- ddply(penguinsSub, ~Age, summarise, meanW=mean(body_mass_g, na.rm = T))
WeightAge <- ddply(penguinsSub, ~Age, summarise, meanL=mean(bill_length_mm, na.rm = T))
LengthAge plot(WeightAge$meanW~WeightAge$Age)
plot(LengthAge$mean~LengthAge$Age)
WeightChange
<- mean(penguinsSub$body_mass_g[penguinsSub$Age==2], na.rm=TRUE)
Weight1
Weight1<- mean(penguinsSub$bill_length_mm[penguinsSub$Age==2], na.rm=TRUE)
Length1 <- mean(penguinsSub$body_mass_g[penguinsSub$Age==3], na.rm=TRUE)
Weight2 <- mean(penguinsSub$bill_length_mm[penguinsSub$Age==3], na.rm=TRUE)
Length2 <- mean(penguinsSub$body_mass_g[penguinsSub$Age==4], na.rm=TRUE)
Weight3 <- mean(penguinsSub$bill_length_mm[penguinsSub$Age==4], na.rm=TRUE)
Length3 <- mean(penguinsSub$body_mass_g[penguinsSub$Age==5], na.rm=TRUE)
Weight4 <- mean(penguinsSub$bill_length_mm[penguinsSub$Age==5], na.rm=TRUE)
Length4 <- mean(penguinsSub$body_mass_g[penguinsSub$Age==6], na.rm=TRUE)
Weight5 <- mean(penguinsSub$bill_length_mm[penguinsSub$Age==6], na.rm=TRUE)
Length5 <- mean(penguinsSub$body_mass_g[penguinsSub$Age==7], na.rm=TRUE)
Weight6 <- mean(penguinsSub$bill_length_mm[penguinsSub$Age==7], na.rm=TRUE)
Length6 # Weight7 - Length9 removed
<- data.frame("Age" = 1:7, "Growth" = Weight1,Weight2,Weight3,Weight4,Weight5,Weight6) x