1 - (5/6) * (5/6) * (5/6)
roll_sample <- sample(6, 1000, replace=TRUE)
roll_sample
roll_sample <- sample(6, 1000, replace=TRUE)
mean(roll_sample)
roll_sample <- sample(6, 100, replace=TRUE)
mean(roll_sample)
die <- c(1, 2, 3, 4, 5, 6)
mean(die)
die <- c(1, 2, 3, 4, 5, 6)
mean(die)
# calculated variance from the random variable
die_var <- mean((mean(die) - die)^2)
die_var
sqrt(die_var)   # standard deviation of the random variable
roll_sample <- sample(6, 100, replace=TRUE)
mean(roll_sample)
sd(roll_sample)
sd(roll_sample)^2   # variance from the sample
# return the population variance of a vector
var_pop <- function(col) {
m <- mean(col)
sqr_diffs <- (m - col)^2
mean(sqr_diffs)
}
box <- c(1, 1, 2, 2, 3, 9)
mean(box)
var_pop(box)
sqrt(var_pop(box))  # standard deviation of the box
# simulate random variable from two boxes
box_sample <- sample(box, 100, replace=TRUE) + sample(box, 100, replace=TRUE)
mean(box_sample)
sd(box_sample)
# return the population variance of a vector
var_pop <- function(col) {
m <- mean(col)
sqr_diffs <- (m - col)^2
mean(sqr_diffs)
}
box <- c(1, 1, 2, 2, 3, 9)
mean(box)
var_pop(box)
sqrt(var_pop(box))  # standard deviation of the box
box_sample <- sample(box, 100, replace=TRUE)  # obtains 100 sample from box
mean(box_sample)
sd(box_sample)
p <- 0.75
N <- 36
expected <- p * N
expected
exp_sd <- sqrt(p * (1-p))
exp_sd
std_error_counts <- sqrt(N) * exp_sd
std_error_counts
std_error_counts / N    # std error for proportion
z <- (22  - 27) / std_error_counts
z
# proportion / probability that shoots at 22 for a sample of 36 at 75%
pnorm(z)
z2 <- (22/36 - 0.75) / (std_error_counts / N)
z2
setwd(dirname(rstudioapi::getSourceEditorContext()$path))  # sets working directory
df <- read.csv(file="movies_data_clean.csv", header=TRUE)
runtimes <- df$Runtime
runtimes
summary(runtimes)
sd(runtimes)
hist(runtimes)
# obtain samples of size 100 from the movie runtimes
num_samples <- 200
sample_means <- rep(0, num_samples)
for (i in 1:num_samples) {
sample_means[i] <- mean(sample(runtimes, 100, replace=FALSE))
}
summary(sample_means)
sd(sample_means)
sample_means
hist(sample_means)
# obtain samples of size 100 from the movie runtimes
num_samples <- 200
sample_means <- rep(0, num_samples)
for (i in 1:num_samples) {
sample_means[i] <- mean(sample(runtimes, 100, replace=FALSE))
}
sample_means
summary(sample_means)
sd(sample_means)
hist(sample_means)
# theoretical standard error of sample N=100
std_error <- sd(runtimes) / sqrt(100)
std_error
# sampling in practice
one_sample <- sample(runtimes, 100, replace=FALSE)
est_sd <- sd(one_sample)
est_std_error <- est_sd / 100
est_std_error
setwd(dirname(rstudioapi::getSourceEditorContext()$path))  # sets working directory
df <- read.csv(file="movies_data_clean.csv", header=TRUE)
runtimes <- df$Runtime
runtimes
summary(runtimes)
sd(runtimes)
hist(runtimes)
# obtain samples of size 100 from the movie runtimes
num_samples <- 200
sample_means <- rep(0, num_samples)
for (i in 1:num_samples) {
sample_means[i] <- mean(sample(runtimes, 100, replace=FALSE))
}
sample_means
summary(sample_means)
sd(sample_means)
hist(sample_means)
# theoretical standard error of sample N=100
std_error <- sd(runtimes) / sqrt(100)
std_error   # compare to SD of means of 200 samples
# sampling in practice
one_sample <- sample(runtimes, 100, replace=FALSE)
est_sd <- sd(one_sample)
est_std_error <- est_sd / 100
est_std_error
mean(one_sample) - est_std_error
mean(one_sample) + est_std_error
est_std_error <- est_sd / sqrt(100)
est_std_error
mean(one_sample) - est_std_error
mean(one_sample) + est_std_error
# 95% confidence interval
mean(one_sample) - est_std_error * 2
mean(one_sample) + est_std_error *2
