diff --git a/02_sampling.html b/02_sampling.html old mode 100644 new mode 100755 diff --git a/McNally_2.Rmd b/McNally_2.Rmd new file mode 100644 index 0000000..82442d8 --- /dev/null +++ b/McNally_2.Rmd @@ -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 + + 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 @@ + + + + +
+ + + + + + + + + + +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)
+:p
+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
+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
+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.
+quantile(vole_vaso, probs = 0.75)
+## 75%
+## 83
+vol_10 <- sample(vole_vaso, size = 10, replace = TRUE)
+quantile(vol_10, probs= 0.75)
+## 75%
+## 85
+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
+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
+plot(IQR_sample_size ~ sample_size, data = vol_vaso_sim)
+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?
+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.
+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?
+