This is HW 6 for Programming with Data in R. I am taking this class with Professor Lee as a Data Science student at the University of Florida.

# I am taking this class so that I can get an introduction on how to use R.
#Question 1
#1a
x <- 0:4
dbinom(x, size = 4, prob = 0.5)
## [1] 0.0625 0.2500 0.3750 0.2500 0.0625
#1b
pbinom(x, 4, 0.5)
## [1] 0.0625 0.3125 0.6875 0.9375 1.0000
#1c
par(mfrow = c(1,2))
plot(c(0,1,2,3,4), dbinom(x,4,0.5),type="h",ylim=c(0,0.4),ylab="f(x)",xlab="x",main = "PMF of Bin(4, 0.5)", font.main = 4)
points(c(0,1,2,3,4), dbinom(x,4,0.5), pch = 19)
plot(c(-1,0,1,2,3,4,5), c(0, pbinom(x, 4, 0.5),1), type = "s", ylim=c(0,1), ylab="F(x)", xlab = "x", main = "CDF of Bin(4, 0.5)", font.main = 4)
points(c(0,1,2,3,4), pbinom(x, 4, 0.5), pch = 16)
points(c(0,1,2,3,4), c(0, pbinom(x, 4, 0.5)[-5]), )

Learning R code is essential for several reasons, especially in the realms of data analysis, statistical modeling, and academic research. R is a powerful programming language specifically designed for statistical computing and graphics, making it an indispensable tool for anyone involved in data-driven decision making, research, or analytics. Its extensive package ecosystem allows users to perform a wide range of statistical techniques from basic to advanced levels. Furthermore, R’s open-source nature encourages collaboration and sharing of code, enhancing the reproducibility of research. By learning R, individuals gain the ability to analyze complex datasets, create high-quality visualizations, and stay at the forefront of statistical methodologies, making it a valuable skill in many professional and academic fields.

# Question 2
#2a
n <- 50
X <- rnorm(n,5,sqrt(3))
qqnorm(X)
qqline(X)
#The Q-Q plot appears to be linear

#2b
z <- seq(from = 1/(2*n), to = 1 - 1/(2*n), length.out = n)
z <- qnorm(z)

#2c
points(sort(z), sort(X), pch = 21, bg = "green")
points(qnorm(c(0.25,0.75)), quantile(X, c(0.25,0.75)), type = "l", col = "red")

#2d
abline(5, sqrt(3), col = "blue")

#2e
Y <- runif(n, min = 2, max = 8)
mean(Y)
## [1] 4.777397
var(Y)
## [1] 2.962898
mean(X)
## [1] 4.642832
var(X)
## [1] 2.344822
#2f
qqplot(X,Y)

# The Q-Q plot appears to be linear 

#2g
plot(sort(X), sort(Y))
points(sort(X), sort(Y), pch = 21, bg = "green")
points(quantile(X, c(0.25,0.75)), qunif(c(0.25,0.75),2,8), type = "l", col = "red")

#Question 3 
#3a is done by hand 

#3b
N <- 100000
n <- 48
Y <- runif(N, 2, 8)
mean(4.7 < Y & Y < 5.3)
## [1] 0.09936
#3c
# The central limit theorem states that if you have a large enough sample size with a mean and variance, 
# then the sample means will follow a normal distribution no matter what. Therefore, we can approximate
# Y to be of normal distribution.
pnorm(5.3, 5, sqrt(3/n)) - pnorm(4.7, 5, sqrt(3/n))
## [1] 0.7698607
#3d
startTime <- proc.time()
means <- c()
for (i in 1:N) {
  Y <- runif(n,2,8)
  Yi <- mean(Y)
  means <- c(means, Yi)
}
mean(4.7 < means & means < 5.3)
## [1] 0.76935
proc.time() - startTime
##    user  system elapsed 
##    4.44    0.88   14.55
#3e
startTime <- proc.time()
Y <- matrix(runif(n*N,2,8), nrow = n)
Ybar <- colMeans(Y)
mean(4.7 < Ybar & Ybar < 5.3)
## [1] 0.76731
proc.time() - startTime
##    user  system elapsed 
##    0.05    0.00    0.14
#3f
startTime <- proc.time()
Y <- matrix(rep(0,n*N), nrow = n)
for (i in 1:n) {
  for (j in 1:N) {
    Y[i,j] <- runif(1, 2, 8)
  }
}
Ybar <- colMeans(Y)
mean(4.7 < Ybar & Ybar < 5.3)
## [1] 0.77012
proc.time() - startTime
##    user  system elapsed 
##    2.28    0.14    7.73
#3g
# The most efficient approach is method two 
# as it executes the fastest 
#Question 4
riffle <- function(x, y) {
  len <- min(length(x), length(y))
  
  result <- vector(length = len * 2)
  
  result[seq(0, len*2-1, 2) + 1] <- x[1:len]
  result[seq(0, len*2-1, 2) + 2] <- y[1:len]
  
  if (length(x) > length(y)) {
    result <- c(result, x[(len+1):length(x)])
  } else if (length(y) > length(x)) {
    result <- c(result, y[(len+1):length(y)])
  }
    return(result)
}
a <- 1:10
b <- 50:55
riffle(a,b)
##  [1]  1 50  2 51  3 52  4 53  5 54  6 55  7  8  9 10
#Question 5
insert <- function(x, where, what) {
  if (where > length(x)) {
    warning(where," exceeds the dimension of the vector")
    n <- where - length(x)
    x <- c(x, rep(NA, n-1), what)
  } else {
    x <- c(x[1:(where-1)], what, x[where:length(x)])
  }
  x
}
a <- 1:10
insert(a,5,0)
##  [1]  1  2  3  4  0  5  6  7  8  9 10
insert(a,11,0)
## Warning in insert(a, 11, 0): 11 exceeds the dimension of the vector
##  [1]  1  2  3  4  5  6  7  8  9 10  0
insert(a,15,0)
## Warning in insert(a, 15, 0): 15 exceeds the dimension of the vector
##  [1]  1  2  3  4  5  6  7  8  9 10 NA NA NA NA  0
#Question 6
# 6a - 6e are all by hand
#6f
ttest <- function(y1,y2, alt = "two.sided", lev = 0.95) {
  x1bar <- mean(y1)
  x2bar <- mean(y2)
  s1 <- sd(y1)
  s2 <- sd(y2)
  n1 <- length(y1)
  n2 <- length(y2)
  df <- n1 + n2 - 2
  sp <- sqrt(((n1-1)*s1^2+(n2-1)*s2^2)/df)
  t <- (x1bar - x2bar) / (sp*sqrt(1/n1 + 1/n2))
  if (alt == "two.sided"){
    pvalue <- 2 * pt(-abs(t),df)
    CInterval <- x1bar - x2bar + c(-1,1) * qt(1-lev/2, df) * sp * sqrt(1/n1 + 1/n2)
  }
  else if (alt == "less") {
    pvalue <- pt(t,df)
    CInterval <- c(-Inf, x1bar - x2bar + qt(lev,df)*sp*sqrt(1/n1+1/n2))
  }
  else if (alt == "greater") {
    pvalue <- 1 - pt(t, df)
    CInterval <- c(x1bar - x2bar + qt(1-lev, df)*sp*sqrt(1/n1+1/n2), Inf)
  }
  else {
    stop("Not a valid alternative hypothesis")
  }
  result <- list(t = t, pvalue = pvalue, CInterval = CInterval)
  return(result)
}

#6g
library(MASS)
data(Cars93)
us_mpg <- Cars93$MPG.city[Cars93$Origin == "USA"]
non_us_mpg <- Cars93$MPG.city[Cars93$Origin != "USA"]
result <- ttest(us_mpg, non_us_mpg)
result$pvalue
## [1] 0.01183163
result$CInterval
## [1] -2.979524 -2.837143
# The p-value for the two-sample t-test is < 0.05. Thus, we reject the null hypothesis
# and conclude that there is a difference in mean city MPG between American and non-American cars.
# The 95% confidence interval for the difference in mean city MPG is (-2.9795, -2.8371). 
# This means we are 95% confident that the difference in mean city MPG between American 
# and non-American cars falls between -2.9795 and -2.8371.