plot(c(-10,375), c(0,1.5/365), type="n", 
     xlab="Birth Time", ylab="Density")
segments(-10,0,1,0)
segments(1,1/365,365,1/365)
segments(365,0,380,0)
points(c(0,365), c(0,0))
points(c(1,365), c(1/365, 1/365), pch=16)

day <- 1:365
p.day <- rep(1/365, 365)
plot(day, p.day, main="PMF of Day of the Year Born", pch=16, cex=.1)

day <- 1:365
c.p.day <- cumsum(p.day)
plot(day, c.p.day, main="CDF of Day of the Year Born", pch=16, cex=.1)

plot(c(-10,380), c(0,1), type="n")
segments(-10,0,1,0)
segments(1,0,365,1)
segments(365,1,380,1)

#?dunif

#Draw the PDF of the birthday Uniform
points <- seq(-50,410,.01)

plot(points, dunif(points, min=1, max=365), type="l",
     main="PDF of Uniform Variable",
     xlab="Value",
     ylab="density")

plot(points, punif(points, min=1, max=365), type="l",
     main="PDF of Uniform Variable",
     xlab="Value",
     ylab="density")


runif(10,1,365)

plot(seq(-3,3,.01) ,dnorm(seq(-3,3,.01)), type="l", axes=F, main="Normal Distribution",
     ylab="",xlab="")
axis(side=1, at=c(-3,0,3), labels = c("","mu",""))

#?dnorm

values <- seq(-100,100,.001)
plot(values, dnorm(values, mean=0, sd=10), main="PDF of Normal Distribution mean 0 , sd=10",
     type="l", xlim=c(-100,100))

#?dnorm

values <- seq(-100,100,.001)
plot(values, pnorm(values, mean=0, sd=10), main="CDF of Normal Distribution mean 0 , sd=10",
     type="l", xlim=c(-100,100))


x <- seq(50,78, .01)
y <- dnorm(x, mean=64, sd=3)
plot(x, y, type="l", xlab="Height", ylab="Density", main="Distribution of Female Height in the US")
abline(v=64, lty=2)

set.seed(19104)
rbinom(1, 1000, .44)

sample.means <- rbinom(5000,1000,.44)/10

plot(density(sample.means))

plot(values, dnorm(values, mean=0, sd=10), main="PDF of Normal Distribution mean(-50,0,50) , sd=10",type="l", xlim=c(-100,100))
points(values, dnorm(values, mean=-50, sd=10), type="l")
points(values, dnorm(values, mean=50, sd=10), type="l")


plot(values, pnorm(values, mean=0, sd=10), main="CDF of Normal Distribution mean(-50,0,50) , sd=10",type="l", xlim=c(-100,100))
points(values, pnorm(values, mean=-50, sd=10), type="l")
points(values, pnorm(values, mean=50, sd=10), type="l")

X <- rnorm(100000,5,100)
Z <- X -5

plot(density(X))
points(density(Z), col="firebrick", type="l")

Z <- X/10

plot(density(X))
points(density(Z), col="firebrick", type="l")

#?dnorm

values <- seq(-3,3, .001)
plot(values, pnorm(values), main="Standard Normal CDF", 
     type="l")
abline(v=1, lty=2)

pnorm(1, mean=0, sd=1)

#OR

pnorm(1)

#For this class I wrote my own function, normal.shader(), that lets me shade in an area under a normal curve
#Feel free to make use of it!
source("https://raw.githubusercontent.com/marctrussler/IIS-Data/main/NormalShader.R")

x <- seq(-3,3, .001)
y <- dnorm(x)
plot(x, y, main="Standard Normal PDF", 
     type="l")
normal.shader(min=-3, max=3, mu=0, sd=1, value.one=1, greater=F)

dnorm(1)

x <- seq(-3,3, .001)
y <- dnorm(x)
plot(x, y, main="Standard Normal PDF", 
     type="l")
points(1, dnorm(1), pch=16, col="firebrick")

plot(x, y, main="Standard Normal PDF", 
     type="l")
normal.shader(min=-3, max=3, mu=0, sd=1, value.one=1, greater=T)

plot(x, y, main="Standard Normal PDF", 
     type="l")
normal.shader(min=-3, max=3, mu=0, sd=1, value.one=-1, greater=F)

pnorm(-1)

plot(x, y, main="Standard Normal PDF", 
     type="l")
normal.shader(min=-3, max=3, mu=0, sd=1, value.one=-1, value.two=1, between=T)

1-2*pnorm(-1)

1-2*pnorm(1)

1-2*pnorm(-2)
1-2*pnorm(-3)

plot(x, y, main="Standard Normal PDF", 
     type="l")
normal.shader(min=-3, max=3, mu=0, sd=1, value.one=-2, value.two=2, between=T)

plot(x, y, main="Standard Normal PDF", 
     type="l")
normal.shader(min=-3, max=3, mu=0, sd=1, value.one=-3, value.two=3, between=T)

x <- seq(-20,30,.001)
y <- dnorm(x, mean=5, sd=10)
plot(x,y, type="l", main="Normal with mu=5, sigma=10")


x <- seq(-20,30,.001)
y <- dnorm(x, mean=5, sd=10)
plot(x,y, type="l", main="Normal with mu=5, sigma=10")
normal.shader(min=-20, max=30, mu=5, sd=10, value.one=-5, value.two=15, between=T)


x <- seq(50,78, .001)
y <- dnorm(x, mean=64, sd=3)
plot(x,y, main="Distribution of Female Height", type="l")
normal.shader(min=50,max=78, mu=64, sd=3, value.one=58, value.two=70, between=T)

pnorm(3)

pnorm(73, mean=64, sd=3)

#Generate some fake data
x <- runif(100)
y <- 2 + x*3  + rnorm(100)
#Regress x on y
m <- lm(y ~ x)
summary(m)


plot(0:45,dbinom(0:45, 45, .1), xlab="Number of Left-Handed Presidents", ylab="Prob This Many Left Handed",
     main="Binomial with n=45, p=.1", pch=16)

dbinom(8, 45, .1)

plot(0:45,pbinom(0:45, 45, .1), xlab="Number of Left-Handed Presidents", ylab="Cumlative Prob This Many Left Handed",
     main="Binomial with n=45, p=.1", pch=16)

cbind(0:3,dbinom(0:3, 3, .5),pbinom(0:3,3,.5))

1-pbinom(7,45,.1)


plot(0:14,dbinom(0:14, 14, .1), xlab="Number of Left-Handed Presidents", ylab="Prob This Many Left Handed",
     main="Binomial with n=14, p=.1", pch=16)
1-pbinom(5,14,.1)

dunif(345, min=0, max=365)

punif(300,0,365)

1- punif(350,0,365)

1- 2*punif(182.5-105.36, 0 , 365)

x <- seq(50,78, .01)
y <- dnorm(x, mean=64, sd=3)
plot(x, y, type="l", xlab="Height", ylab="Density", main="Distribution of Female Height in the US")

plot(x, y, type="l", xlab="Height", ylab="Density", main="Distribution of Female Height in the US", lwd=2)
normal.shader(min=50, max=78, mu=64, sd=3, value.one = 60, greater = F, col="dodgerblue")

pnorm(60, mean=64, sd=3)

plot(x, y, type="l", xlab="Height", ylab="Density", main="Distribution of Female Height in the US", lwd=2)
normal.shader(min=50, max=78, mu=64, sd=3, value.one = 60, greater = T, col="firebrick")

1-pnorm(60, mean=64, sd=3)

pnorm(60, mean=64, sd=3, lower.tail=F)

plot(x, y, type="l", xlab="Height", ylab="Density", main="Distribution of Female Height in the US", lwd=2)
normal.shader(min=50, max=78, mu=64, sd=3, value.one = 60, value.two=68, between=T, col="purple")

plot(x, y, type="l", xlab="Height", ylab="Density", main="Distribution of Female Height in the US", lwd=2)
normal.shader(min=50, max=78, mu=64, sd=3, value.one = 68,  greater=F, col="firebrick")

plot(x, y, type="l", xlab="Height", ylab="Density", main="Distribution of Female Height in the US", lwd=2)
normal.shader(min=50, max=78, mu=64, sd=3, value.one = 60,  greater=F, col="dodgerblue")

pnorm(68, mean=64, sd=3, lower.tail=T) - pnorm(60, mean=64, sd=3, lower.tail=T)

plot(x, y, type="l", xlab="Height", ylab="Density", main="Distribution of Female Height in the US", lwd=2)
normal.shader(min=50, max=78, mu=64, sd=3, value.one = 60,  greater=F, col="dodgerblue")
normal.shader(min=50, max=78, mu=64, sd=3, value.one = 68,  greater=T, col="dodgerblue")

1-2*pnorm(60, mean=64, sd=3, lower.tail=T)

pnorm(67, mean=64, sd=3, lower.tail=T) - pnorm(61, mean=64, sd=3, lower.tail=T)

1-2*pnorm(61, mean=64, sd=3, lower.tail=T)

qnorm(.8, mean=64, sd=3, lower.tail=T)

plot(x, y, type="l", xlab="Height", ylab="Density", main="Distribution of Female Height in the US", lwd=2)
normal.shader(min=50, max=78, mu=64, sd=3, value.one = 66.5,  greater=F, col="dodgerblue")

qnorm(.8, mean=64, sd=3, lower.tail=F)

plot(x, y, type="l", xlab="Height", ylab="Density", main="Distribution of Female Height in the US", lwd=2)
normal.shader(min=50, max=78, mu=64, sd=3, value.one = 61.5,  greater=T, col="firebrick")


qnorm(.025, mean=64, sd=3, lower.tail=F)
qnorm(.025, mean=64, sd=3, lower.tail=T)

plot(x, y, type="l", xlab="Height", ylab="Density", main="Distribution of Female Height in the US", lwd=2)
normal.shader(min=50, max=78, mu=64, sd=3, value.one = 58.12, value.two=69.87, between=T, col="purple")
