Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 157 additions & 0 deletions LaBella_03.rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
---
title: "LaBella_03"
author: "Rachel LaBella"
date: "September 22, 2016"
output: html_document
---
```{r, echo = TRUE}
library(readr)
library(dplyr)
library(ggplot2)
library(RColorBrewer)
library(forcats)
library(tibble)
```
###1) Complete problems 10 and 17-18 on pg. 109-111. Use R where possible. Data sets (so you don’t have to type things in) are available at http://www.zoology.ubc.ca/~whitlock/ABD/teaching/datasets.html.

###Problem 10
####a)
``` {r, echo = TRUE}
gene_reg <- read_csv("../04q11NumberGenesRegulated.csv")
gene_reg
str(gene_reg)
gene_reg %>%
summarize(avg_ngenes = mean(ngenes))
std.dev <- (14.5)/sqrt(26)
std. error <- 1.96*2.843683
```
####b)
I am 95% confident the true mean will fall between 20.073 and 8.927. (14.5 +/- 5.573) 20.072 is the upper limit, 8.927 is the lower limit of the confidence interval. 14.5 is the true mean of ngenes.

###Problem 17
Instead of saying that there is a 95% chance that it will fall within those ranges, they should have said we are 95% confident that the true mean will fall within the upper and lower confidence limits. There is no probability involved in calculating confidence intervals, the true mean either is or is not between the upper and lower limits. The upper and lower limits (0.9 and 3.1) are constants.

###Problem 18
```{r, echo = TRUE}
#a)
corpse_flower <- c(51, 45, 61, 76, 11, 117, 7, 132, 52, 149)
mean(corpse_flower)
sd(corpse_flower)

#b)
std.error.flwr <- (70.1)/sqrt(10)

#c)
conf_int <- 1.96*22.1675663977803
#Upper limit:
48.50074 + 43.448430

#Lower limit:
48.50074 - 43.448430
```
####d)
I would say that the mean would be about the same, the range of the numbers would be different but the average would be about the same. Unless all of the random points were on either end of the mean and it would push the mean higher or lower.

####e)
The standard deviation would be less than the original sample because the more data points you have, the more accurate the mean and more narrow the standard deviation.

####f)
The standard errors would be less as well. With the same idea as the standard deviation, the more points you have in your sample, the more accurate the standard errors.

####2.1) Load the data using readr and make the Month_Names column into a factor whose levels are in order of month.
``` {r, echo = TRUE}
sea_ice <- read_csv("../NH_seaice_extent_monthly_1978_2016.csv")
sea_ice
sea_ice$Month
sea_ice <- sea_ice %>%
arrange(Month) %>%
mutate(Month_Name = fct_inorder(Month_Name))
levels(sea_ice$Month_Name)
```

####2.2) Make a boxplot showing the variability in sea ice extent every month.
``` {r, echo = TRUE}
sea_ice_plot <- ggplot(sea_ice, mapping = aes(x = Month_Name, y = Extent))
sea_ice_plot +
geom_boxplot()
```


####2.3) Use dplyr to get the annual minimum sea ice. Plot minimum ice by year, and add a trendline (either a smooth spline or a straight line).
``` {r, echo = TRUE}
sea_ice_min <- sea_ice %>%
group_by(Year) %>%
slice(which.min(Extent))
list(sea_ice_min)

sea_ice_min_plot <- ggplot(data = sea_ice_min, mapping = aes(x = Year, y = Extent))
sea_ice_min_plot +
geom_jitter() +
theme_bw(base_size = 10) +
stat_smooth(method = "lm", color = "red")

```


####2.4) Plot sea ice by year, with different lines for different months. Then, add a new column (mutate!) using the ggplot2 function cut_interval(Month, n=4) - this will create four even bins. Seasons, if you will. Use facet_wrap on the same plot and split the plot into seasons.
``` {r, echo = TRUE}
library(viridis)
library(ggthemes)

ggplot(data = sea_ice) +
geom_line(mapping = aes(Year, Extent, color = Month_Name)) +
scale_color_hue() +
theme_bw()


#############
season_names <- list(
'(1,3.75)'='Winter',
'(3.75,6.5)'='Spring',
'(6.5,9.25)'='Summer',
'(9.25,12)'='Fall')

season_labeller <- function(variable,value){
return(season_names[value])}

sea_ice_season <- sea_ice %>%
mutate(season = cut_interval(Month, n=4))

sea_ice_season
ggplot(sea_ice_season) +
geom_line(mapping = aes(Year, Extent, color = Month_Name)) +
facet_wrap(~season, labeller=season_labeller) +
theme_bw()

```


####2.5) Last, make a line plot of sea ice by month. Gussy it up with colors by year, a different theme, and whatever other annotations, changes to axes, etc., you think best show the story of this data.
``` {r, echo = TRUE}
ggplot(data = sea_ice) +
geom_line(mapping = aes(Month_Name, Extent, group = Year, color = Year)) +
scale_color_viridis() +
theme_bw(base_size = 12)
```

####Extra Credit Animation
```{r, echo = TRUE}
library(animation)
library(viridis)

saveGIF({
for(i in 1979:2016){
print(ggplot(sea_ice %>% filter(Year <= i),
mapping = aes(x = Month_Name, y = Extent, group = Year, color = Year)) +
geom_line() +
scale_color_viridis() +
theme_bw(base_size = 12) +
labs(title = "Changes in Arctic Sea Ice Over Time", x = "Month", y = "Extent in 10^6 sq km") +
annotate(x=10, y=2, geom="text", label = "Chart by Rachel LaBella", size = 3))}},
interval=0.3, ani.width = 600, ani.height = 600)

```




Binary file added animation.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.