From 7fe6580b074ee2d2b48b4267de42cad1547e0589 Mon Sep 17 00:00:00 2001 From: jessicakarch Date: Wed, 21 Sep 2016 23:44:17 -0400 Subject: [PATCH] Add files via upload --- karch_02.Rmd | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 karch_02.Rmd diff --git a/karch_02.Rmd b/karch_02.Rmd new file mode 100644 index 0000000..1868f77 --- /dev/null +++ b/karch_02.Rmd @@ -0,0 +1,94 @@ +--- +title: "Homework 2: Sampling and Simulation" +author: "Jessica Karch" +date: "September 15, 2016" +output: html_document +--- + +```{r setup, include=FALSE} +knitr::opts_chunk$set(echo = TRUE) +``` + +```{r 0} +library(dplyr) +library(magrittr) +``` +## 1. Sample Properties +Consider the following vasopressin levels in voles. + +``` {r 1} +vole_vaso <- c(98,96,94,88,86,82,77,74,70,60, + 59,52,50,47,40,35,29,13,6,5) +``` +**1a. Say "Vole vasopressin" 10 times as fast as you can. How many times did you trip up?** +Three. But to be fair, I'm not sure I pronounced it correctly at all. + +**1b. What is the mean, median, sd, and interquartile range of the sample?** +```{r 1b} +mean(vole_vaso) +median(vole_vaso) +sd(vole_vaso) +IQR(vole_vaso) +``` +The mean is 58.05, median is 59.5, standard deviation is 29.75 and interquartile range is 44.25. + +**1c. What is the standard error of the mean (do this with a formula!)?** +```{r 1c} +sd(vole_vaso) / sqrt(20) +``` +The standard error of the mean is 6.65. + +**1d. What does the standard error of the mean tell you about our estimate of the mean values of the population of vole vassopressin?** +According to Cumming et al, the standard error of the mean measures how variable the mean will be if you repeat the whole study multiple times; it also gives us a sense of how much we can trust our estimate. Our SE is fairly high (only 1 order of magnitude smaller than our values), so our estimate of the mean is probably not very precise. + +##2. Sample Size for upper quartiles. +We can get the upper quartile value of vole vassopressin with ```quantile(probs = 0.75)``` + +Let's assume the sample is representative of the popultion. + +**2a. Use sample() to get just one resample with a sample size of 10. What is its upper quartile?** +```{r 2a} +set.seed(42) +sample(vole_vaso, size = 10, replace = TRUE) %>% + quantile(probs = 0.75) +``` +Its upper quartile is 56.75. + +**2b. Build an initial data frame for simulations with the sample sizes 5 through 20. Have 100 simulations per sample size.** +**2c. Use this data frame to get simulated upper quartiles for each sample size.** +```{r 2b} +#2b +set.seed(42) +df_sim <- data.frame(samp_size = rep(5:20, times = 100)) +df_sim$sim_number = 1:nrow(df_sim) +#2c +df_sim2 <- df_sim %>% + group_by(sim_number) %>% +#get simulated upper quartiles + mutate(upquart = quantile(sample(vole_vaso, samp_size, replace = TRUE), prob = (0.75))) %>% + ungroup() +#2d +plot(upquart ~ samp_size, data = df_sim2) +``` +**2d. With a plot, make a guesstimate as to the best sample size for estimating the upper quartile of the population.** +The best sample size is 14 or 19-- they have the least amount of variation of the estimated upper quartile values. + +## 3. SE and CI +**3a. With the upper quartile simulations, calculate the SD for each sample size using dplyr** +```{r = 3a} +df_sim_sd <- df_sim2 %>% + group_by(samp_size) %>% + summarize(sd_pop = sd(upquart)) %>% + ungroup() +print.data.frame(df_sim_sd) +plot(sd_pop ~ samp_size, data = df_sim_sd) +``` + +**3b. What does this value, the standard error of the upper quartile, mean?** +The standard error of the upper quartile is the the standard deviation of the estimate of the upper quartile. Because we simulated the upper quartile 100 times for each sample size, the SE is the SD of those 100 estimates at each sample size. + +**3c. What is the CI of the upper quartile with a sample size of 10. What does this mean?** +``` {r = 3c} +11.2748 * 1.96 +``` +The 95% confidence interval is 22.1. This means we 95% confident that the true value of the upper quartile lies somewhere in the range of the estimated upper quartile value +/- 22.1.