diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5b6a065 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.Rproj.user +.Rhistory +.RData +.Ruserdata diff --git a/animation.gif b/animation.gif new file mode 100644 index 0000000..aba2fd5 Binary files /dev/null and b/animation.gif differ diff --git a/homework-3.Rproj b/homework-3.Rproj new file mode 100644 index 0000000..8e3c2eb --- /dev/null +++ b/homework-3.Rproj @@ -0,0 +1,13 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX diff --git a/karch_03.html b/karch_03.html new file mode 100644 index 0000000..779f647 --- /dev/null +++ b/karch_03.html @@ -0,0 +1,333 @@ + + + + +
+ + + + + + + + + + +library(ggplot2)
+library(dplyr)
+##
+## 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)
+library(animation)
+library(RColorBrewer)
+library(ggthemes)
+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
+# load data set
+reggenes <- read.csv("2016_homework_03_ggplot2-master/data/04q11NumberGenesRegulated.csv")
+10a) Using an approximate method, provide a rough 95% confidence interval for the population mean.
+# expand values by frequency
+reggenes2 <- rep(reggenes$ngenes, reggenes$frequency)
+# calculate sd
+sd <- sd(reggenes2)
+# calculate se using formula
+se <- sd / sqrt(109)
+# calculate 95% CI
+se * 1.96
+## [1] 1.417751
+mean <- mean(reggenes2)
+mean + 1.42
+## [1] 9.731927
+mean - 1.42
+## [1] 6.891927
+The mean (8.31) has a 95% confidence interval from 6.9 to 9.7 genes.
+10b) Provide an interpretation of the interval you calculated in part (a).
+We are 95% confident that the calculated mean of the population (8.31) lies within +/- 1.42 of the mean of the real population. If we take some number of random samples, 95% of these samples will have the true value of the mean within the 95% confidence interval.
17) No, this is not a correct interpretation of a confidence interval.A 95% confidence interval does not mean there is a 95% chance that the true value lies within that interval, it means that we are 95% confident that the true mean of the population lies in the interval we calculated. If we take 100 random samples, 95 of these will have will have the true population mean within the range of their 95% confidence interval.
+18a) What is the mean and standard deviation of beetles per flower?
+flower <- c(51, 45, 61, 76, 11, 117, 7, 132, 52, 149)
+mean_flower <- mean(flower)
+mean_flower
+## [1] 70.1
+sd(flower)
+## [1] 48.50074
+The mean is 70.1 beetles with a standard deviation of 48.5 beetles.
+18b) What is the standard error of this estimate of the mean?
+se_flower <- sd(flower) / sqrt(10)
+se_flower
+## [1] 15.33728
+15.34.
+18c) Give an approximate 95% confidence interval of the mean. Provide lower and upper limits.
+ci_flower <- se_flower * 1.96
+mean_flower + ci_flower
+## [1] 100.1611
+mean_flower - ci_flower
+## [1] 40.03893
+The 95% confidence interval of the mean is 40.04 to 100.16 beetles.
+18d) If you had been given 25 data points instead of 10, would you expect the mean to be greater, less than, or about the same as the mean of this sample?
+Assuming a normal distribution, I’d expect the mean to be about the same.
18e) If you had been given 25 data points instead of 10, would you expect the standard deviation to be greater, less than, or about the same as the standard deviation of this sample?
+I’d expect the standard deviation to be less than that of this sample. Standard deviation is inversely proportional to sqrt(n-1), where n is the size of the sample. Therefore a larger sample size should yield a smaller standard deviation.
+18f) If you had been given 25 data points instead of 10, would you expect the standard error to be greater, less than, or about the same as the standard error of this sample?
+I’d expect it to be smaller. Standard error is the standard deviation divided by the square root of the sample size. Because the numerator will be smaller (see 18e) and the denominator will be larger, se will be smaller than that of this sample.
2) The other day, Dave Curran using some of the code from our lab made a wonderful animation of change in arctic sea ice.
+2.1) Load the data using readr and make the Month_Names column into a factor whose levels are in order of month.
+# Save rmarkdown file in homework master folder downloaded from github
+# Set working directory to source file location
+# Load data
+seaice <- read.csv("2016_homework_03_ggplot2-master/data/NH_seaice_extent_monthly_1978_2016.csv")
+# Factorize Month_Names column
+seaice_month <- seaice %>%
+ mutate(Months_Name = factor(Month_Name, levels = month.abb))
+sort(seaice_month$Months_Name)
+## [1] Jan Jan Jan Jan Jan Jan Jan Jan Jan Jan Jan Jan Jan Jan Jan Jan Jan
+## [18] Jan Jan Jan Jan Jan Jan Jan Jan Jan Jan Jan Jan Jan Jan Jan Jan Jan
+## [35] Jan Jan Jan Feb Feb Feb Feb Feb Feb Feb Feb Feb Feb Feb Feb Feb Feb
+## [52] Feb Feb Feb Feb Feb Feb Feb Feb Feb Feb Feb Feb Feb Feb Feb Feb Feb
+## [69] Feb Feb Feb Feb Feb Feb Feb Mar Mar Mar Mar Mar Mar Mar Mar Mar Mar
+## [86] Mar Mar Mar Mar Mar Mar Mar Mar Mar Mar Mar Mar Mar Mar Mar Mar Mar
+## [103] Mar Mar Mar Mar Mar Mar Mar Mar Mar Mar Mar Apr Apr Apr Apr Apr Apr
+## [120] Apr Apr Apr Apr Apr Apr Apr Apr Apr Apr Apr Apr Apr Apr Apr Apr Apr
+## [137] Apr Apr Apr Apr Apr Apr Apr Apr Apr Apr Apr Apr Apr Apr Apr May May
+## [154] May May May May May May May May May May May May May May May May May
+## [171] May May May May May May May May May May May May May May May May May
+## [188] May May Jun Jun Jun Jun Jun Jun Jun Jun Jun Jun Jun Jun Jun Jun Jun
+## [205] Jun Jun Jun Jun Jun Jun Jun Jun Jun Jun Jun Jun Jun Jun Jun Jun Jun
+## [222] Jun Jun Jun Jun Jun Jun Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul
+## [239] Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul
+## [256] Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul Aug Aug Aug Aug Aug Aug Aug
+## [273] Aug Aug Aug Aug Aug Aug Aug Aug Aug Aug Aug Aug Aug Aug Aug Aug Aug
+## [290] Aug Aug Aug Aug Aug Aug Aug Aug Aug Aug Aug Aug Aug Aug Sep Sep Sep
+## [307] Sep Sep Sep Sep Sep Sep Sep Sep Sep Sep Sep Sep Sep Sep Sep Sep Sep
+## [324] Sep Sep Sep Sep Sep Sep Sep Sep Sep Sep Sep Sep Sep Sep Sep Sep Sep
+## [341] Sep Sep Oct Oct Oct Oct Oct Oct Oct Oct Oct Oct Oct Oct Oct Oct Oct
+## [358] Oct Oct Oct Oct Oct Oct Oct Oct Oct Oct Oct Oct Oct Oct Oct Oct Oct
+## [375] Oct Oct Oct Oct Oct Oct Nov Nov Nov Nov Nov Nov Nov Nov Nov Nov Nov
+## [392] Nov Nov Nov Nov Nov Nov Nov Nov Nov Nov Nov Nov Nov Nov Nov Nov Nov
+## [409] Nov Nov Nov Nov Nov Nov Nov Nov Nov Nov Nov Dec Dec Dec Dec Dec Dec
+## [426] Dec Dec Dec Dec Dec Dec Dec Dec Dec Dec Dec Dec Dec Dec Dec Dec Dec
+## [443] Dec Dec Dec Dec Dec Dec Dec Dec Dec Dec Dec Dec Dec Dec Dec Dec
+## Levels: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
+levels(seaice_month$Months_Name)
+## [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov"
+## [12] "Dec"
+2.2 Make a boxplot showing the variability in sea ice extent every month.
+boxplot(Extent ~ Months_Name, data = seaice_month)
+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).
+# get annual minimum sea ice, use filter
+seaice_min <- seaice_month %>%
+ group_by(Year) %>%
+ filter(Extent==min(Extent)) %>%
+ ungroup()
+seaice_min_plot <- ggplot(data = seaice_min,
+ mapping = aes(x = Year, y = Extent))
+seaice_min_plot +
+ geom_point() +
+ geom_smooth(method = "lm") +
+ labs(title = "Change in the Annual Minimum Arctic Sea Ice Levels from 1979 to 2016", x = "Year",
+ y = "Extent in 10^6 sq km")
+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.
seaice_plot <-ggplot(data = seaice_month,
+ mapping = aes(x = Year, y = Extent, group = Months_Name,
+ color = Months_Name))
+seaice_plot +
+ geom_line()
+seaice_season <- seaice_month %>%
+ mutate(Season = cut_interval(Month, n = 4))
+seaice_season_plot <- ggplot(data = seaice_season,
+ mapping = aes(x = Year, y = Extent,
+ group = Months_Name,
+ color = Months_Name))
+seaice_season_plot +
+ geom_line() + facet_wrap(~Season)
+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.
+seaice_month_plot <-ggplot(data = seaice_month,
+ mapping = aes(x = Months_Name, y = Extent,
+ group = Year,
+ color = Year))
+seaice_month_plot +
+ geom_line() +
+ scale_color_gradientn(colors = brewer.pal(9, "PuBu")) +
+ labs(title = "Change in Arctic Sea Ice Levels over Time", x = "Month",
+ y = "Extent in 10^6 sq km") +
+ theme_few() +
+ ylim(c(2, 17)) +
+ annotate(x=9, y=2.5, geom="text",
+ label = "Chart by Jessica Karch | Data source: nsidc.org", size = 3)
+2.6 Extra Credit) Make it animated.
+saveGIF({
+ for(i in 1979:2016){
+
+ print(ggplot(seaice_month %>% filter(Year <= i),
+ mapping = aes(x = Months_Name, y = Extent,
+ group = Year,
+ color = Year)) +
+ geom_line() +
+ scale_color_gradientn(colors = brewer.pal(9, "PuBu")) +
+ labs(title = "Change in Arctic Sea Ice Levels over Time", x = "Month",
+ y = "Extent in 10^6 sq km") +
+ theme_few() +
+ ylim(c(2, 17)) +
+ annotate(x=9, y=2.5, geom="text", label = "Chart by Jessica Karch | Data source: nsidc.org", size = 3)
+ )}
+}, interval=0.3,ani.width = 600, ani.height = 600)
+## Executing:
+## ""convert" -loop 0 -delay 30 Rplot1.png Rplot2.png Rplot3.png
+## Rplot4.png Rplot5.png Rplot6.png Rplot7.png Rplot8.png
+## Rplot9.png Rplot10.png Rplot11.png Rplot12.png Rplot13.png
+## Rplot14.png Rplot15.png Rplot16.png Rplot17.png Rplot18.png
+## Rplot19.png Rplot20.png Rplot21.png Rplot22.png Rplot23.png
+## Rplot24.png Rplot25.png Rplot26.png Rplot27.png Rplot28.png
+## Rplot29.png Rplot30.png Rplot31.png Rplot32.png Rplot33.png
+## Rplot34.png Rplot35.png Rplot36.png Rplot37.png Rplot38.png
+## "animation.gif""
+## Output at: animation.gif
+## [1] TRUE
+
+
+
+
+