# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Lab 2, part B: Statistical Inference                                                       ----
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# This script includes R commands for random sampling and 
# statistical inference basics: estimation, confidence intervals,
# and hypotheses tests for the normal mean.


# Random sample generation
n=10
sample(n)    # select a random permutation from 1 to n
x=c(8,3,5,11,9,5,4,2)
sample(x)           # randomly permute x
sample(x,replace=T) # random sampling with replacement
m=3
sample(x,m)           # sample m items from x without replacement
sample(x,m,replace=T) # sample m items from x with replacement
p=c(0.1,0.2,0.05,0.05,0.3,0.1,0.05,0.15)
sample(x,m,replace=T,prob=p) # sample m items from x with probabilities given by p

# Simulate n draws from the Normal distribution with mean equal to mu
# and standard deviation equal to sd
n=100
mu=0
sd=1
x=rnorm(n,mu,sd)
mean(x)     # sample mean
var(x)      # sample variance

n=10000
mu=10
sd=3
x=rnorm(n,mu,sd)
hist(x)     # histogram
mean(x)     # sample mean
var(x)      # sample variance

# density function, distribution function 
# and quantile function of the normal distribution
y=3
mu=4
sd=1
dnorm(y,mu,sd)    # density
pnorm(y,mu,sd)    # distribution function
qnorm(0.95,mu,sd) # quantile function
qnorm(0.975,0,1)

# Statistical Inference
n=100
mu=5
sd=2
x=rnorm(n,mu,sd)
x_bar=mean(x)
x_var=var(x)
xbar_se=sqrt(x_var/n)
# Confidence Interval
CI=c(x_bar-xbar_se*qt(0.975,n-1),x_bar+xbar_se*qt(0.975,n-1))
CI
# H0: mu=4.8, H1: mu>4.8
mu=4.8
T=(x_bar-mu)/xbar_se  # test statistic
t0=qt(0.95,n-1)       # critical value
T>t0
pvalue=1-pt(T,n-1) 
pvalue 
