---
title: "Random Variables and Probability Models (Chapter 14)"
author: "Patrick Frenett, Vickie Ip, and Nicholas Horton (nhorton@amherst.edu)"
date: "June 20, 2018"
output:
pdf_document:
fig_height: 4
fig_width: 6
html_document:
fig_height: 3
fig_width: 5
word_document:
fig_height: 4
fig_width: 6
---
```{r, include = FALSE}
# Don't delete this chunk if you are using the mosaic package
# This loads the mosaic and dplyr packages
require(mosaic)
```
```{r, include = FALSE}
# knitr settings to control how R chunks work.
require(knitr)
opts_chunk$set(
tidy = FALSE, # display code as typed
size = "small" # slightly smaller font for code
)
```
## Introduction and background
This document is intended to help describe how to undertake analyses introduced
as examples in the Fourth Edition of *Intro Stats* (2013) by De Veaux, Velleman, and Bock.
More information about the book can be found at http://wps.aw.com/aw_deveaux_stats_series. This
file as well as the associated R Markdown reproducible analysis source file used to create it can be found at https://nhorton.people.amherst.edu/is4.
This work leverages initiatives undertaken by Project MOSAIC (http://www.mosaic-web.org), an NSF-funded effort to improve the teaching of statistics, calculus, science and computing in the undergraduate curriculum. In particular, we utilize the `mosaic` package, which was written to simplify the use of R for introductory statistics courses. A short summary of the R needed to teach introductory statistics can be found in the mosaic package vignettes (http://cran.r-project.org/web/packages/mosaic). A paper describing the mosaic approach was published in the *R Journal*: https://journal.r-project.org/archive/2017/RJ-2017-024.
Note that some of the figures in this document may differ slightly from those in the IS4 book due to small differences in datasets. However in all cases the analysis and techniques in R are accurate.
## Chapter 14: Random Variables and Probability Models
### Section 14.1: Center (the Expected Value)
We can replicate the calculation on page 363:
```{r}
library(mosaic)
library(readr)
options(digits = 3)
x <- c(10000, 5000, 0)
prob <- c(1/1000, 2/1000, 997/1000)
sum(prob) # sums to 1
expect <- sum(x*prob)
expect # expected value
```
### Section 14.2: Spread (The Standard Deviation)
We can continue with the example from page 365:
```{r}
xminmu <- x - expect
xminmu
myvar <- sum(xminmu^2*prob)
myvar
sd <- sqrt(myvar)
sd
```
### Section 14.3: Combining Random Variables
Let's replicate the values from the example on page 367-368:
```{r}
ex <- 5.83
varx <- 8.62^2
ed <- ex + 5
ed
vard <- varx
vard
sqrt(vard)
```
### Section 14.4: The Binomial Model
### Section 14.5: Modeling the Binomial with a Normal Model
### Section 14.6: The Poisson Model
### Section 14.7: Continuous Random Variables
Let's replicate the Think/Show/Tell/Conclusion on pages 381 and 382:
```{r}
sdval <- sqrt(4.50)
sdval
gf_dist("norm", params = list(18, sdval), xlab = "x", ylab = "f(x)")
xpnorm(20, mean = 18, sd = sdval) # note how exact value is different from the table!
zval <- (20-18)/sdval
zval
xpnorm(zval, mean = 0, sd = 1)
```