From 0bf25e8e88e081ea08007fb673eb87a5b4bafc64 Mon Sep 17 00:00:00 2001 From: Sean McNally Date: Mon, 19 Sep 2016 14:35:11 -0400 Subject: [PATCH 1/5] first homework commit --- McNally_2.Rmd | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 McNally_2.Rmd diff --git a/McNally_2.Rmd b/McNally_2.Rmd new file mode 100644 index 0000000..11b5799 --- /dev/null +++ b/McNally_2.Rmd @@ -0,0 +1,30 @@ +--- +title: "McNally_2" +author: "Sean McNally" +date: "September 19, 2016" +output: html_document +--- + +```{r setup, include=FALSE} +knitr::opts_chunk$set(echo = TRUE) +``` + +## R Markdown + +This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see . + +When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this: + +```{r cars} +summary(cars) +``` + +## Including Plots + +You can also embed plots, for example: + +```{r pressure, echo=FALSE} +plot(pressure) +``` + +Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot. From f285c74739a021eb22e83789ffe0ea53b8ddace2 Mon Sep 17 00:00:00 2001 From: Sean McNally Date: Mon, 19 Sep 2016 18:25:03 -0400 Subject: [PATCH 2/5] Updated HW_2 --- McNally_2.Rmd | 90 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 77 insertions(+), 13 deletions(-) diff --git a/McNally_2.Rmd b/McNally_2.Rmd index 11b5799..a8ca33e 100644 --- a/McNally_2.Rmd +++ b/McNally_2.Rmd @@ -5,26 +5,90 @@ date: "September 19, 2016" output: html_document --- -```{r setup, include=FALSE} -knitr::opts_chunk$set(echo = TRUE) +###1. Sample Properties +#####Consider the following vasopressin levels in voles. +```{r, echo=TRUE} +library(magrittr) +library(dplyr) +library(ggplot2) +vole_vaso <- c(98,96,94,88,86,82,77,74,70,60, + 59,52,50,47,40,35,29,13,6,5) ``` -## R Markdown +#####1a. Say “Vole vasopressin” 10 times as fast as you can. How many times did you trip up? +After the first one!! -This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see . - -When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this: +#####1b. What is the mean, median, sd, and interquartile range of the sample? +```{r, echo=TRUE} +mean(vole_vaso) +median(vole_vaso) +sd(vole_vaso) +IQR(vole_vaso) +summary(vole_vaso) +``` -```{r cars} -summary(cars) +#####1c. What is the standard error of the mean (do this with a formula!)? +Standard error of the mean = standard deviation of the population/sqrt(sample size) +sd of vole_vaso = 29.75244 +sample size of vole_vaso = 20 +```{r, echo=TRUE} +se_vol <- ((29.75244)/sqrt(20)) +se_vol ``` -## Including Plots +#####1d. What does the standard error of the mean tell you about our estimate of the mean values of the population of vole vassopressin? +The standard error of the mean tell us the variance in our estimate of the mean values of our population. In the context of our vole population it tells us the variance in our estimate of the mean value of vasspressin in our population sample. It is basically the variance in our average of the population. + +###2. Sample Size for upper quartiles. +#####We can get the upper quartile value of vole vassopressin with +```{r, echo=TRUE} +quantile(vole_vaso, probs = 0.75) +``` +####Let’s assume the sample is representative of the popultion. -You can also embed plots, for example: +#####2a. Use sample() to get just one resample with a sample size of 10. What is its upper quartile? +```{r, echo=TRUE} +vol_10 <- sample(vole_vaso, size = 10, replace = TRUE) +quantile(vol_10, probs= 0.75) +``` -```{r pressure, echo=FALSE} -plot(pressure) +#####2b. Build an initial data frame for simulations with the sample sizes 5 through 20. Have 100 simulations per sample size. +```{r, echo=TRUE} +vol_vaso_sim <- data.frame(vol_vaso = rep(5:20, times=100)) ``` -Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot. +#####2c. Use this data frame to get simulated upper quartiles for each sample size. + +UpperQ <- vol_vaso_sim %>% +group_by(sim) %>% +mutate(IQR_pop = IQR(rnorm(vol_vaso, mean = 10, sd = 3))) + +*Jarret, I was having trouble with this! Were we suppose to define the upper quartile with the quantile function or the IQR function? I could only get this to work if I used rnorm and then defined the mean and the sd. Were we suppose to bootstrap this? Also how do you define the mean and sd for rnorm? Are these values also the same for a rnorm disturbtion? I just used these values from our class notes* + +#####2d. With a plot, make a guesstimate as to the best sample size for estimating the upper quartile of the population. + +plot(vol_vaso ~ IQR_pop, data = UpperQ) + +###3. SE and CI + +#####3a. With the upper quartile simulations, calculate the SD for each sample size using dplyr + +SD_UpperQ <- UpperQ %>% +mutate(SE_UpperQ = sd(rnorm(UpperQ, mean = 10, sd = 3))) + +#####3b. What does this value, the standard error of the upper quartile, mean? + +The standard error of the upper quartile + +#####3c. What is the CI of the upper quartile with a sample size of 10. What does this mean? + +CI_val <- SD_UpperQ %>% +mutate(CI_UpperQ = (2*SE_UpperQ)) + +*Is this the only way to caclute the CI or is there a function in R?* + +#####3d. Extra Credit: Instead of having each sim calculate a upper quartile, and then sifting down to the SE of that upper quartile (or the CI), have each simulation calculate a CI. Then, for just a sample size of 10, how many CI’s contain the true value of 83? + +#####3e. Extra extra credit: If you find this question by pulling from your forked repository, +1 + + From 6c990d1d0fda5e65df9acf96a8cdbb4bb6f001fd Mon Sep 17 00:00:00 2001 From: Sean McNally Date: Mon, 19 Sep 2016 18:25:50 -0400 Subject: [PATCH 3/5] updated 02 sampling HW questions --- 02_sampling.html | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 02_sampling.html diff --git a/02_sampling.html b/02_sampling.html old mode 100644 new mode 100755 From 0d3d7573b4ed1a94d7bf335bb318bbfd576a806f Mon Sep 17 00:00:00 2001 From: Sean McNally Date: Tue, 20 Sep 2016 15:07:09 -0400 Subject: [PATCH 4/5] HW_2 --- McNally_2.Rmd | 50 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/McNally_2.Rmd b/McNally_2.Rmd index a8ca33e..82442d8 100644 --- a/McNally_2.Rmd +++ b/McNally_2.Rmd @@ -16,7 +16,7 @@ vole_vaso <- c(98,96,94,88,86,82,77,74,70,60, ``` #####1a. Say “Vole vasopressin” 10 times as fast as you can. How many times did you trip up? -After the first one!! +:p #####1b. What is the mean, median, sd, and interquartile range of the sample? ```{r, echo=TRUE} @@ -37,7 +37,7 @@ se_vol ``` #####1d. What does the standard error of the mean tell you about our estimate of the mean values of the population of vole vassopressin? -The standard error of the mean tell us the variance in our estimate of the mean values of our population. In the context of our vole population it tells us the variance in our estimate of the mean value of vasspressin in our population sample. It is basically the variance in our average of the population. +The standard error of the mean tells us the variance in our estimate of the mean values of our population. In the context of our vole population it tells us the variance in our estimate of the mean value of vasspressin in our population sample. It is basically the variance in our average of the population. If we had a larger populaiton size our standard error of the mean would decrease making our estimate stronger. ###2. Sample Size for upper quartiles. #####We can get the upper quartile value of vole vassopressin with @@ -54,36 +54,56 @@ quantile(vol_10, probs= 0.75) #####2b. Build an initial data frame for simulations with the sample sizes 5 through 20. Have 100 simulations per sample size. ```{r, echo=TRUE} -vol_vaso_sim <- data.frame(vol_vaso = rep(5:20, times=100)) +vol_vaso_sim <- data.frame(sample_size = rep(5:20, times = 100)) +vol_vaso_sim$sim = 1:nrow(vol_vaso_sim) +head(vol_vaso_sim) ``` #####2c. Use this data frame to get simulated upper quartiles for each sample size. - -UpperQ <- vol_vaso_sim %>% -group_by(sim) %>% -mutate(IQR_pop = IQR(rnorm(vol_vaso, mean = 10, sd = 3))) - -*Jarret, I was having trouble with this! Were we suppose to define the upper quartile with the quantile function or the IQR function? I could only get this to work if I used rnorm and then defined the mean and the sd. Were we suppose to bootstrap this? Also how do you define the mean and sd for rnorm? Are these values also the same for a rnorm disturbtion? I just used these values from our class notes* +```{r, echo=TRUE} +vol_vaso_sim <- vol_vaso_sim %>% + group_by(sim) %>% + mutate(IQR_sample_size = IQR(rnorm(sample_size, mean = 58.05, sd = 29.75244))) %>% + ungroup() +head(vol_vaso_sim) +``` +Mean and sd were calculated from orginal vol_vaso data set from question #1 #####2d. With a plot, make a guesstimate as to the best sample size for estimating the upper quartile of the population. -plot(vol_vaso ~ IQR_pop, data = UpperQ) +```{r, echo=TRUE} +plot(IQR_sample_size ~ sample_size, data = vol_vaso_sim) +``` ###3. SE and CI #####3a. With the upper quartile simulations, calculate the SD for each sample size using dplyr +```{r, echo=TRUE} +vol_vaso_sim <- vol_vaso_sim %>% + group_by(sim) %>% + mutate(IQR_SE = sd(rnorm(IQR_sample_size, mean = 58.05, sd = 29.75244))) +head(vol_vaso_sim) +``` -SD_UpperQ <- UpperQ %>% -mutate(SE_UpperQ = sd(rnorm(UpperQ, mean = 10, sd = 3))) +*SD of sample size or the SD of the upper quaritle simulations for each sample size?* #####3b. What does this value, the standard error of the upper quartile, mean? -The standard error of the upper quartile +The standard error of the upper quartile is the standard deviation of our estimate of the upper quartile for each sample size. This estimate allows us to put a quantative value on how well our estimate of the upper quartile of our simulation was or how much we can 'trust' our calculation of the estimate of our estimate. #####3c. What is the CI of the upper quartile with a sample size of 10. What does this mean? -CI_val <- SD_UpperQ %>% -mutate(CI_UpperQ = (2*SE_UpperQ)) +95% CI = 2 * SE + +```{r, echo=TRUE} +vol_vaso_sim <- vol_vaso_sim %>% + group_by(sim) %>% + mutate(CI_IQR = (2*IQR_SE)) %>% + ungroup() +head(vol_vaso_sim) +``` + +CI of the upper quartile with a sample size of 10 is variable *Is this the only way to caclute the CI or is there a function in R?* From 39e3ef3f3ab5d43a75199f1e1e5e4a585765c446 Mon Sep 17 00:00:00 2001 From: Sean McNally Date: Tue, 20 Sep 2016 15:07:43 -0400 Subject: [PATCH 5/5] McNally_2.html --- McNally_2.html | 307 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 307 insertions(+) create mode 100644 McNally_2.html diff --git a/McNally_2.html b/McNally_2.html new file mode 100644 index 0000000..a88b434 --- /dev/null +++ b/McNally_2.html @@ -0,0 +1,307 @@ + + + + + + + + + + + + + + + +McNally_2 + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + +
+

1. Sample Properties

+
+
Consider the following vasopressin levels in voles.
+
library(magrittr) 
+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(ggplot2) 
+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?
+

:p

+
+
+
1b. What is the mean, median, sd, and interquartile range of the sample?
+
mean(vole_vaso)
+
## [1] 58.05
+
median(vole_vaso)
+
## [1] 59.5
+
sd(vole_vaso)
+
## [1] 29.75244
+
IQR(vole_vaso)
+
## [1] 44.25
+
summary(vole_vaso)
+
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
+##    5.00   38.75   59.50   58.05   83.00   98.00
+
+
+
1c. What is the standard error of the mean (do this with a formula!)?
+

Standard error of the mean = standard deviation of the population/sqrt(sample size) sd of vole_vaso = 29.75244 sample size of vole_vaso = 20

+
se_vol <- ((29.75244)/sqrt(20))
+se_vol
+
## [1] 6.652848
+
+
+
1d. What does the standard error of the mean tell you about our estimate of the mean values of the population of vole vassopressin?
+

The standard error of the mean tells us the variance in our estimate of the mean values of our population. In the context of our vole population it tells us the variance in our estimate of the mean value of vasspressin in our population sample. It is basically the variance in our average of the population. If we had a larger populaiton size our standard error of the mean would decrease making our estimate stronger.

+
+
+
+

2. Sample Size for upper quartiles.

+
+
We can get the upper quartile value of vole vassopressin with
+
quantile(vole_vaso, probs = 0.75)
+
## 75% 
+##  83
+
+
+

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?
+
vol_10 <- sample(vole_vaso, size = 10, replace = TRUE)
+quantile(vol_10, probs= 0.75)
+
## 75% 
+##  85
+
+
+
2b. Build an initial data frame for simulations with the sample sizes 5 through 20. Have 100 simulations per sample size.
+
vol_vaso_sim <- data.frame(sample_size = rep(5:20, times = 100))
+vol_vaso_sim$sim = 1:nrow(vol_vaso_sim)
+head(vol_vaso_sim)
+
##   sample_size sim
+## 1           5   1
+## 2           6   2
+## 3           7   3
+## 4           8   4
+## 5           9   5
+## 6          10   6
+
+
+
2c. Use this data frame to get simulated upper quartiles for each sample size.
+
vol_vaso_sim <- vol_vaso_sim %>%
+  group_by(sim) %>%
+  mutate(IQR_sample_size = IQR(rnorm(sample_size, mean = 58.05, sd = 29.75244))) %>%
+  ungroup()
+head(vol_vaso_sim)
+
## # A tibble: 6 × 3
+##   sample_size   sim IQR_sample_size
+##         <int> <int>           <dbl>
+## 1           5     1        38.46881
+## 2           6     2        11.95286
+## 3           7     3        32.96158
+## 4           8     4        39.52702
+## 5           9     5        62.40937
+## 6          10     6        30.69045
+

Mean and sd were calculated from orginal vol_vaso data set from question #1

+
+
+
2d. With a plot, make a guesstimate as to the best sample size for estimating the upper quartile of the population.
+
plot(IQR_sample_size ~ sample_size, data = vol_vaso_sim)
+

+
+
+
+
+

3. SE and CI

+
+
3a. With the upper quartile simulations, calculate the SD for each sample size using dplyr
+
vol_vaso_sim <- vol_vaso_sim %>%
+  group_by(sim) %>%
+  mutate(IQR_SE = sd(rnorm(IQR_sample_size, mean = 58.05, sd = 29.75244))) 
+head(vol_vaso_sim)
+
## Source: local data frame [6 x 4]
+## Groups: sim [6]
+## 
+##   sample_size   sim IQR_sample_size   IQR_SE
+##         <int> <int>           <dbl>    <dbl>
+## 1           5     1        38.46881 29.48252
+## 2           6     2        11.95286 26.83514
+## 3           7     3        32.96158 32.67714
+## 4           8     4        39.52702 34.60823
+## 5           9     5        62.40937 28.18701
+## 6          10     6        30.69045 35.08836
+

SD of sample size or the SD of the upper quaritle simulations for each sample size?

+
+
+
3b. What does this value, the standard error of the upper quartile, mean?
+

The standard error of the upper quartile is the standard deviation of our estimate of the upper quartile for each sample size. This estimate allows us to put a quantative value on how well our estimate of the upper quartile of our simulation was or how much we can ‘trust’ our calculation of the estimate of our estimate.

+
+
+
3c. What is the CI of the upper quartile with a sample size of 10. What does this mean?
+

95% CI = 2 * SE

+
vol_vaso_sim <- vol_vaso_sim %>%
+  group_by(sim) %>%
+  mutate(CI_IQR = (2*IQR_SE)) %>%
+  ungroup()
+head(vol_vaso_sim)
+
## # A tibble: 6 × 5
+##   sample_size   sim IQR_sample_size   IQR_SE   CI_IQR
+##         <int> <int>           <dbl>    <dbl>    <dbl>
+## 1           5     1        38.46881 29.48252 58.96503
+## 2           6     2        11.95286 26.83514 53.67028
+## 3           7     3        32.96158 32.67714 65.35429
+## 4           8     4        39.52702 34.60823 69.21645
+## 5           9     5        62.40937 28.18701 56.37402
+## 6          10     6        30.69045 35.08836 70.17672
+

CI of the upper quartile with a sample size of 10 is variable

+

Is this the only way to caclute the CI or is there a function in R?

+
+
+
3d. Extra Credit: Instead of having each sim calculate a upper quartile, and then sifting down to the SE of that upper quartile (or the CI), have each simulation calculate a CI. Then, for just a sample size of 10, how many CI’s contain the true value of 83?
+
+
+
3e. Extra extra credit: If you find this question by pulling from your forked repository, +1
+
+
+ + + + +
+ + + + + + + +