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
Empty file modified 02_sampling.html
100644 → 100755
Empty file.
114 changes: 114 additions & 0 deletions McNally_2.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
---
title: "McNally_2"
author: "Sean McNally"
date: "September 19, 2016"
output: html_document
---

###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)
```

#####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?
```{r, echo=TRUE}
mean(vole_vaso)
median(vole_vaso)
sd(vole_vaso)
IQR(vole_vaso)
summary(vole_vaso)
```

#####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
```

#####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
```{r, echo=TRUE}
quantile(vole_vaso, 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, echo=TRUE}
vol_10 <- sample(vole_vaso, size = 10, replace = TRUE)
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(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.
```{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.

```{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 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

```{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?*

#####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


307 changes: 307 additions & 0 deletions McNally_2.html

Large diffs are not rendered by default.