setwd <- "C:/Users/drobb/Desktop/Linear Regression"
set.seed(123)
# Q1 ------------------------------------------------------------------------------
lambda <- 3
x <- rpois(100, lambda)
y <- x^2
hist(y, main="Histogram Q1", breaks = 20)

#histogram is skewed to the left
# Q2 ------------------------------------------------------------------------------
lambda2 <- 1
Y <- replicate(1000,rpois(100, lambda2)^2)
mu.hat <- mean(colMeans(Y))
hist(colMeans(Y), main="Histogram Q2", breaks = 20)

# Q3 ------------------------------------------------------------------------------
z.bar <- apply(Y, 2, function(x) (mean(x)-mu.hat)/sd(x)/10)
hist(z.bar, main="Histogram Q3", probability=T)
lines(density(z.bar), col="magenta", lwd=2)

# Q4 ------------------------------------------------------------------------------
Y <- replicate(1000, rexp(100, rate=1)^2)
mu.hat <- mean(colMeans(Y))
z.bar <- apply(Y,2,function(x) (mean(x)-mu.hat)/(sd(x)/10))
hist(z.bar, main="Histogram Q4", probability = TRUE)
curve(dnorm, add= T)

#Using the exponential, the distribution is more skewed to the right and has a better line of fit.