pnorm(375,400,70,lower.tail = T) #slide 90
## [1] 0.3604924
qnorm(0.36,mean=400,sd=70)
## [1] 374.9079
pnorm(375,400,70/10,lower.tail = T) #slide 91
## [1] 0.0001775197
sample(100,10,F)
## [1] 63 74 52 66 82 67 98 32 48 46
#Simulating a coin flip in R
#Andrew Ellis
#http://rstudio-pubs-static.s3.amazonaws.com/8492_b817c712a5f6456fb4c5932e3d957135.html#/1
sample.space <- c(0,1)
theta <- 0.5 # this is a fair coin
N <- 20 # we want to flip a coin 20 times
flips <- sample(sample.space,
size = N,
replace = TRUE,
prob = c(theta, 1 - theta))
flips
## [1] 1 1 0 0 0 0 1 1 1 0 1 0 0 0 1 1 0 0 0 0
table(flips)
## flips
## 0 1
## 12 8
barplot(table(flips))

hist(flips,col = rainbow(6))

sample.space2 <- c(1:6)
theta2 <- 1/6 # this is a fair coin
N <- 200 # we want to flip a coin 20 times
rolls <- sample(sample.space2,
size = N,
replace = TRUE,
prob = rep(theta2,6))
rolls
## [1] 1 6 3 4 2 2 4 6 3 3 1 5 6 4 2 6 1 3 2 6 2 4 4 5 1 4 5 1 6 6 5 5 4 4 4
## [36] 6 3 6 3 2 4 1 3 2 2 6 3 5 3 6 1 1 2 4 4 1 6 1 1 1 5 4 4 1 2 3 3 5 3 6
## [71] 1 2 5 6 3 1 6 3 6 3 3 2 5 1 2 5 4 2 4 2 6 4 5 5 2 3 1 1 6 1 3 2 5 2 6
## [106] 5 3 1 1 1 2 6 3 4 4 6 6 6 2 5 4 4 3 6 3 3 5 6 6 6 4 5 6 3 3 4 3 2 2 1
## [141] 6 2 6 2 6 2 3 1 1 5 2 5 1 4 2 3 6 6 6 6 4 4 3 3 3 4 6 2 4 1 3 3 2 3 4
## [176] 3 5 4 1 4 4 5 3 6 6 1 3 6 3 6 2 6 3 4 5 5 3 3 4 3
table(rolls)
## rolls
## 1 2 3 4 5 6
## 29 30 42 34 24 41
barplot(table(rolls))

hist(rolls,col = rainbow(6))

sample(50,15,F) #slide120
## [1] 36 42 49 43 11 8 10 37 39 3 26 31 30 40 46
#slide 52
library(MASS)
height.survey=survey$Height
mean(height.survey,na.rm = T)
## [1] 172.3809
height.response=na.omit(survey$Height)
n=length(height.response)
n
## [1] 209
sigma=9.48
SEmean=sigma/sqrt(n)
SEmean
## [1] 0.6557453
qnorm(0.975)
## [1] 1.959964
E=qnorm(0.975)*SEmean
E
## [1] 1.285237
mean(height.response)
## [1] 172.3809
xbar=mean(height.response)
xbar+c(-E,E)
## [1] 171.0956 173.6661
s=sd(height.response)
SE=s/sqrt(n)
SE
## [1] 0.6811677
Et=qt(0.975,n-1)
Et
## [1] 1.971435
xbar+c(-Et,Et)
## [1] 170.4094 174.3523
sample(c("H","T"),10,T)
## [1] "T" "T" "H" "T" "T" "H" "H" "H" "H" "T"
dice=as.vector(outer(1:6,1:6,paste))
dice
## [1] "1 1" "2 1" "3 1" "4 1" "5 1" "6 1" "1 2" "2 2" "3 2" "4 2" "5 2"
## [12] "6 2" "1 3" "2 3" "3 3" "4 3" "5 3" "6 3" "1 4" "2 4" "3 4" "4 4"
## [23] "5 4" "6 4" "1 5" "2 5" "3 5" "4 5" "5 5" "6 5" "1 6" "2 6" "3 6"
## [34] "4 6" "5 6" "6 6"
sample(dice,20,T)
## [1] "6 6" "5 3" "1 2" "2 1" "5 2" "4 6" "2 3" "1 1" "3 4" "4 2" "5 5"
## [12] "4 1" "4 6" "3 4" "3 5" "2 5" "6 3" "4 1" "2 6" "6 1"