# KeepNotes blog

Stay hungry, Stay Foolish.

0%

• One-Proportion Inference
• One-Mean Inference
• Two-proportion inference
• Two-mean inference

#### One-Proportion Inference

``````library(mosaic)

pi <- 0.55   # probability of success for each toss
n <- 100   # Number of times we toss the penny (sample size)
trials <- 1000   # Number of trials (number of samples)

observed <- 62  # Observed number of heads

phat = observed / n   # p-hat - the observed proportion of heads

data.sim <- do(trials) * rflip(n, prob = pi)``````

``pvalue <- sum(data.sim\$prop >= phat) / trials``

#### One-Mean Inference

``````mu <- 22

observed <- mean(~mpg, data=mtcars, na.rm=T)
paste("Observed value for sample mean: ", observed)

trials <- 1000
samples <- do(trials) * mosaic::mean(~mpg, data=resample(mtcars))``````

``````# Let's compute a 95% Confidence Interval
(ci <- quantile(samples\$mean,c(0.025,0.975)))
#     2.5%    97.5%
# 18.18406 22.23453``````

``````pvalue <- sum(samples\$mean >= mu) / trials
paste("Two-sided p-value is", 2 * pvalue)
# [1] "Two-sided p-value is 0.088"``````

#### Two-proportion inference

``````df <- rbind(
do(38) * data.frame(Group = "Men", Support = "no"),
do(62) * data.frame(Group = "Men", Support = "yes"),
do(49) * data.frame(Group = "Women", Support ="no"),
do(51) * data.frame(Group = "Women", Support = "yes")
)
(df.summary <- tally(Support ~ Group, data=df))``````

``````observed <- diffprop(Support ~ Group, data = df)
paste("Observed difference in proportions: " , round(observed,3))
# [1] "Observed difference in proportions:  0.11"``````

``````trials <- 1000
null.dist <- do(trials) * diffprop(Support ~ shuffle(Group), data=df)
histogram( ~ diffprop, data= null.dist,
xlab = "Differences in proportions",
main = "Null distribution for differences in proportions",
v= observed)``````

``````p.value <- prop(~diffprop >= observed, data= null.dist)
paste(" One-sided p-value:  ", round(p.value,3))
# [1] " One-sided p-value:   0.088"``````

#### Two-mean inference

``````df <- read.delim("http://citadel.sjfc.edu/faculty/ageraci/data/tuna.txt")
str(df)
favstats(~Mercury | Tuna, data=df)

observed <- diffmean(~Mercury | Tuna, data=df, na.rm = T)
paste("Observed difference in the means: ", round(observed, 3 ))
# [1] "Observed difference in the means:  -0.003"``````

``````trials <- 1000
null.dist <- do(trials) * diffmean(Mercury ~ shuffle(Tuna), data=df, na.rm = T)
pvalue <- prop(~ diffmean <= observed, data=null.dist)
paste("The one-sided p-value is ", round(pvalue,3))
# [1] "The one-sided p-value is  0.428"``````