# install.packages(c("dplyr", "janitor", "skimr", "lubridate", "tidyr", "readr", "readxl", "tibble"))
# install.packages("ggplot2")
# install.packages("broom")Midterm_Ryan_Giffee
Midterm
Ryan Giffee
2023 November 17
packages
Libraries
library(tidyr)Warning: package 'tidyr' was built under R version 4.3.2
library(dplyr) Warning: package 'dplyr' was built under R version 4.3.2
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
library(readr)Warning: package 'readr' was built under R version 4.3.2
library(lubridate) Warning: package 'lubridate' was built under R version 4.3.2
Attaching package: 'lubridate'
The following objects are masked from 'package:base':
date, intersect, setdiff, union
library(skimr)Warning: package 'skimr' was built under R version 4.3.2
library(tidyverse) Warning: package 'ggplot2' was built under R version 4.3.2
Warning: package 'tibble' was built under R version 4.3.2
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ forcats 1.0.0 ✔ stringr 1.5.0
✔ ggplot2 3.4.4 ✔ tibble 3.2.1
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(visdat)
library(broom) Warning: package 'broom' was built under R version 4.3.2
library(modelr)
Attaching package: 'modelr'
The following object is masked from 'package:broom':
bootstrap
theme_set(theme_bw(base_size=12))1) Sampling my system
My system is Nantucket Island, and the variable I am looking at is microplastics. I am trying to get a sense of their variability in nature. Samples were collected over a period of almost 4 years. On each of the 10 sampling dates, we collected intertidal sand and water, from 3 differentlocations on Nantucket Island. The locations were chosen because of their different geomorphologies and human land usage. We collected triplicates on every sampling date at every location. We collected at low tide, and always collected at 3 ft deep (water), and between the tide and rack (sand). We used glass jars, and aluminum containers to avoid contamination. With this information, we can compare concentrations by season and location.
2) Data Reshaping and Visualization
a) Load in Data
covid_data <- read.csv("data/time_series_covid19_confirmed_US.csv")b) It’s big and wide!
Filter by state, I chose MA to facilitate the next question
plus I would have chosen it anyway
MA_data <- covid_data |>
filter(Province_State == "Massachusetts")Pivot, select, group, and summarize
MA_data_long <- MA_data |>
pivot_longer(cols = starts_with("X"),
names_to = "date_collected",
values_to = "cumulative_cases") |>
select(date_collected, cumulative_cases, Province_State, Admin2) |>
group_by(date_collected) |>
summarise(cumulative_cases = sum(cumulative_cases)) Mutate into true date form
MA_data_long <- MA_data_long |>
mutate(date_collected = mdy(substring(date_collected, 2)))calculate new daily cases
MA_data_long <- MA_data_long |>
arrange(date_collected) |>
mutate(new_daily_cases = c(0, diff(cumulative_cases)))c) Visualize it!
ggplot(MA_data_long, aes(x = date_collected)) +
geom_line(aes(y = cumulative_cases, color = "Cumulative Cases"), size = 1.5) +
geom_smooth(aes(y = cumulative_cases, color = "Trend Line"), method = "loess", size = 1.2) +
theme_classic() +
labs(
title = "COVID-19 Time Series in Massachusetts",
subtitle = "Cumulative Cases with Trend Line",
x = "Date",
y = "Number of Cases",
color = "Case Type"
) +
theme(legend.position = c(0.8, 0.3))Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
`geom_smooth()` using formula = 'y ~ x'
Originally, I had new daily cases included in the graph as well, but the scale was so different that I decided not to include it. I wanted to include another line to show some comparison, so I made a trend line to show how cumulative cases interacted with it over time.
3. Fit and Evaluate a linear model!
a. Download it
quail_data <- read.csv("data/Morphology data.csv")If you’re looking for the steps for fitting a linear model, they are:
Load the data, Look at the data, Visualize it, Fit the model, Check assumptions, Evalute the fit model, Viz the fit model
b. Look at the data
summary(quail_data) Bird.. Sex Age..days. Exp..Temp...degree.C.
Min. : 1.00 Length:880 Min. : 5 Min. :15.0
1st Qu.:10.75 Class :character 1st Qu.: 15 1st Qu.:15.0
Median :20.50 Mode :character Median : 26 Median :22.5
Mean :20.50 Mean : 37 Mean :22.5
3rd Qu.:30.25 3rd Qu.: 49 3rd Qu.:30.0
Max. :40.00 Max. :123 Max. :30.0
Mass..g. Tarsus..mm. Culmen..mm. Depth..mm.
Min. : 12.51 Min. :16.19 Min. : 4.23 Min. :3.300
1st Qu.: 50.06 1st Qu.:26.19 1st Qu.: 9.31 1st Qu.:5.058
Median :127.58 Median :33.81 Median :11.98 Median :5.600
Mean :138.75 Mean :31.73 Mean :11.73 Mean :5.593
3rd Qu.:219.59 3rd Qu.:37.17 3rd Qu.:14.03 3rd Qu.:6.110
Max. :375.00 Max. :49.42 Max. :18.14 Max. :9.520
NA's :76 NA's :112 NA's :114 NA's :112
Width..mm. NOTES
Min. :3.290 Length:880
1st Qu.:4.447 Class :character
Median :4.840 Mode :character
Mean :4.823
3rd Qu.:5.170
Max. :6.920
NA's :112
head(quail_data) Bird.. Sex Age..days. Exp..Temp...degree.C. Mass..g. Tarsus..mm. Culmen..mm.
1 1 5 15 16.09 19.38 7.64
2 2 m 5 15 19.22 20.38 7.49
3 3 f 5 15 17.51 19.04 7.31
4 4 m 5 15 14.36 20.11 7.34
5 5 f 5 15 17.43 21.82 8.24
6 6 m 5 15 15.66 19.83 6.82
Depth..mm. Width..mm. NOTES
1 4.23 4.49
2 4.46 4.44
3 3.92 4.01
4 3.85 4.22
5 4.42 4.56
6 3.65 3.73
str(quail_data)'data.frame': 880 obs. of 10 variables:
$ Bird.. : int 1 2 3 4 5 6 7 8 9 10 ...
$ Sex : chr "" "m" "f" "m" ...
$ Age..days. : int 5 5 5 5 5 5 5 5 5 5 ...
$ Exp..Temp...degree.C.: int 15 15 15 15 15 15 15 15 15 15 ...
$ Mass..g. : num 16.1 19.2 17.5 14.4 17.4 ...
$ Tarsus..mm. : num 19.4 20.4 19 20.1 21.8 ...
$ Culmen..mm. : num 7.64 7.49 7.31 7.34 8.24 6.82 7.84 7.39 7.4 7.81 ...
$ Depth..mm. : num 4.23 4.46 3.92 3.85 4.42 3.65 3.94 3.72 4.5 4.09 ...
$ Width..mm. : num 4.49 4.44 4.01 4.22 4.56 3.73 4.6 4.66 3.83 3.89 ...
$ NOTES : chr "" "" "" "" ...
With these we can get a sense of structure, look for missing values, outliers, etc. They make sure things meet expectations. For now, it looks ok.
change bird # and age to categorical variables
quail_data$Bird.. <- as.factor(quail_data$Bird)
quail_data$Age..days. <- as.factor(quail_data$Age)c. Visualize it
ggplot(quail_data, aes(x = Age..days., y = Tarsus..mm., color = factor(Exp..Temp...degree.C.))) +
geom_point(size = 3, shape = 21, fill = "white") +
labs(
title = "Development of Tarsus Length Over Time by Temperature Treatment",
x = "Age",
y = "Tarsus Length",
color = "Temperature Treatment"
) +
scale_color_manual(values = c("15" = "blue", "30" = "orange")) +
theme_classic()Warning: Removed 112 rows containing missing values (`geom_point()`).
d. Fit the model
quail_lm <- lm(Tarsus..mm. ~ Age..days. * factor(Exp..Temp...degree.C.) + as.factor(Bird..), data = quail_data)