Data Frames and Subsetting

Question 1 (With Your TA)

(1 point) Read in the births.csv file into an object called births. Print out the first 6 rows and verify it matches the lab manual.

setwd("Stats-13")
births <- read.csv("births.csv")
head(births)

Question 2 (On Your Own)

(1 point) Print a subset of the births data that contains the third and sixths rows, and the Visits and Gained variables. Only use numeric vectors to make this subset.

births[c(3, 6), c(10, 16)]

Question 3 (On Your Own)

(1 point) Print a subset of the births data that contains the seventh through tenth rows, and the Racemom and Racedad variables. Only use numeric vectors created with the colon operator to make this subset.

births_sub <- births[7:10, 12:13]
head(births_sub)

Question 4 (On Your Own)

(1 point) Print a subset of the births data that contains the second, fourth, and seventh rows, and the Premie and weight variables. Only use a character vector to specify the columns to subset.

births_sub <- births[c(2,4,7), c(2,3)]
head(births_sub)

Model Notation and Descriptive Statistics

Question 5 (On Your Own)

(1 point) Use model notation to print the mean of the father’s age conditional on marital status.

library(mosaic)
mean(~ Fage | Marital, data = births)
##   Married Unmarried 
##  31.57728  28.40992

Question 6 (On Your Own)

(1 point) Use model notation to print the mean of the baby’s weight conditional on their gender.

mean(~ weight | Gender, data = births)
##   Female     Male 
## 113.7503 118.1986

Question 7 (On Your Own)

(1 point) Use model notation to print the variance of the baby’s weight conditional on their premature status.

var(~ weight | Premie, data = births)
##       No      Yes 
## 255.3130 652.4227

Model Notation and Data Visualization

Question 8 (On Your Own)

(1 point) Use model notation to print a histogram of the mothers’ ages (unconditional).

histogram(~ Mage, data = births)

Question 9 (On Your Own)

(1 point) Use model notation to print a histogram of the mothers’ ages conditional on marital status.

histogram(~ Mage | Marital, data = births)

Simulating Binomial Random Variables

Question 10 (On Your Own)

(1 point) Set the seed to 844, simulate \(M = 100\) coin flips, and save the result. Calculate and print the proportion.

# Parameters
M <- 100
n <- 1
pi <- 0.5
# Random Sampling
set.seed(844)
X <- rbinom(M, size=n, prob = pi)
mean(X)
## [1] 0.45

Question 11 (On Your Own)

(1 point) Set the seed to 534, simulate \(M = 1000\) coin flips, and save the result. Calculate and print the proportion.

# Parameters
M <- 1000
n <- 1
pi <- 0.5
# Random Sampling
set.seed(534)
X <- rbinom(M, size=n, prob = pi)
mean(X)
## [1] 0.515

Question 12 (On Your Own)

(1 point) Set the seed to 329, simulate \(M = 10000\) coin flips, and save the result. Calculate and print the proportion.

# Parameters
M <- 10000
n <- 1
pi <- 0.5
# Random Sampling
set.seed(329)
X <- rbinom(M, size=n, prob = pi)
mean(X)
## [1] 0.5028

Question 13 (On Your Own)

(1 point) Consider the proportion of heads that you printed from Questions 10, 11, and 12. What number is the proportion approaching as \(M\) increases? Why?

the number is approaching 0.5 because each ooutcome is either heads or tails and it has the probability for a fair coin.

Building Probability Distributions

Question 14 (With Your TA)

(1 point) State the null and alternative hypotheses. Write it in \(\LaTeX\) code (your TA will teach you).

The null hypothesis is \(H_0: \pi = 0.5\) and the alternative hypothesis is \(H_1: \pi > 0.5\). The hypothesis for this research question are:

\begin(align) H_0 &: = 0.5\ H_a &: > 0.5 \end(align)

Question 15 (With Your TA)

(1 point) Like the example shown in the lab manual above, simulate \(M = 1000\) samples from a binomial distribution, with an appropriate \(n\) and \(\pi\) for this hypothesis test. Use 325 as the seed. Store these results in a vector called X. Print the frequencies of X with the tally() function.

# Parameters
M <- 1000
n <- 8
pi <- 0.5
# Random Sampling
set.seed(325)
X <- rbinom(M, size=n, prob = pi)
tally(X)
## X
##   0   1   2   3   4   5   6   7   8 
##   5  29 119 215 284 238  89  20   1

Question 16 (With Your TA)

(1 point) Like the example shown in the lab manual, use the tally() and data.frame() functions to start a data frame of sums and frequencies. Call this data frame prob_dist. Print this data frame.

prob_dist <- data.frame(tally(X))
prob_dist

Question 17 (With Your TA)

(1 point) Create a character vector that enumerates all possible proportions that could be obtained from this experiment (as fractions) and store them as a column named p in the prob_dist data frame. Print the prob_dist data frame.

prob_dist$p <- paste0(0:8, "/", n)
prob_dist

Question 18 (With Your TA)

(1 point) Convert the frequencies of the sums into proportions and store them as a column named prob_p in the prob_dist data frame. Print the prob_dist data frame.

prob_dist$prob_p <- prob_dist$Freq/M
prob_dist

Question 19 (On Your Own)

(1 point) Calculate and print the \(p\)-value.

pvalue <- sum(prob_dist[8:9, "prob_p"])
pvalue
## [1] 0.021

Question 20 (On Your Own)

(1 point) What is the decision of this hypothesis test?

#The descision of the hypothesis test would mean we would fail to reject the hypothesis.

Question 21 (On Your Own)

(1 point) What is the conclusion of this hypothesis test?

the conclusion is the coin that is flipped is a trick coin.