Exercise 1

set.seed(150)
data <- rnorm(1000, mean = 30, sd = 2.5)
results <- numeric(50)
for(i in 1:50) {
  sample_data <- sample(data, 1000, replace = TRUE)
  results[i] <- mean(sample_data)
}
results
##  [1] 29.91217 29.95111 29.88946 29.83869 29.83717 29.96443 29.87997 29.98104
##  [9] 29.88365 29.88761 29.92220 29.88761 29.92020 29.88494 29.90298 29.91531
## [17] 29.87105 29.87878 30.02194 29.94590 29.95923 29.74643 29.92373 29.94112
## [25] 29.83525 30.01097 29.99350 30.06145 29.97009 29.92336 29.84687 29.92258
## [33] 29.91140 29.99878 29.96903 29.90596 29.86557 29.97388 29.96784 29.97746
## [41] 29.87797 30.01480 30.10231 29.91795 29.89808 29.99120 29.93832 29.85962
## [49] 29.90181 29.95929

Exercise 2

par(mfrow = c(1, 2))
hist(results,
     main = "Distribution of Sample Means",
     xlab = "Sample Mean",
     col = "lightblue")
hist(data,
     main = "Distribution of Original Data",
     xlab = "Values",
     col = "lightgreen")

PART 2: applying the above to real sample data

Download and bring in the “trees.csv” file from Canvas, call it “trees”. This dataset contains tree measurements from 31 individual trees. We are going to apply bootstrapping to repeatedly sample from this sample!

Perform the following tasks: 1. Code: determine the sample size, the SD of height, and the median of height 2. Code: copy/paste all of the code from section 2.1 in th link above, but you will need to replace certain values: a. you do not need the set.seed() code b. sample.size should be the sample size from our trees data c. any mention of “myData” should be replaced with “trees$Height”

trees <- read.csv("~/Desktop/BIN510-assignments/trees.csv")
length(trees$Height)
## [1] 31
sd(trees$Height)
## [1] 6.371813
median(trees$Height)
## [1] 76
sample.size <- 31
n.samples <- 1000
bootstrap.results <- c()
for (i in 1:n.samples)
{
    obs <- sample(1:sample.size, replace=TRUE)
    bootstrap.results[i] <- mean(trees$Height[obs])
}

length(bootstrap.results)
## [1] 1000
sd(bootstrap.results)
## [1] 1.096471
par(mfrow=c(2,1), pin=c(5.8,0.98))

hist(bootstrap.results,
     col="green",
     xlab="Mean",
     main=paste("Means of 1000 bootstrap samples from trees"))

hist(trees$Height,
     col="#37aad8",
     xlab="Height",
     main=paste("Distribution of trees$Height"))