1. Generate 100 normally distributed random variable with \(\mu=0.5\) and \(\sigma^2 =9\).

n = 100; sample = rnorm(n, .5, 3)
head(sample) # preview the sample
## [1]  2.8681786 -2.3334662  0.9074395 -0.6560089 -3.4837762  5.3465830


2. Compute the sample mean and sample variance.

First, let us calculate the mean of the sample, ie \(\bar{x}\).

# create a function to find mean of a list of observations 
# note: this list must have length strictly greater than 0
my_mean = function(set){
  total = 0
  set_length = 0
  for (i in set) {
    total = i + total
    set_length = set_length + 1
  }
  result = total / set_length
  return(result)
}

x_bar = my_mean(set = sample)
x_bar # print sample mean 
## [1] 0.8682293

Next, we shall calculate the sample variance, ie \(s^2\).

# create a function to find sample variance of a list of observations 
# note: this list must have length strictly greater than 1
my_samp_var = function(set){
  set_mean = my_mean(set)
  dist = 0
  set_length = 0
  for (i in set) {
    dist = dist + (i - set_mean)**2
    set_length = set_length + 1
  }
  result = dist / (set_length - 1) 
  return(result)
}

s_squared  = my_samp_var(set = sample)
s_squared # print sample variance
## [1] 8.140405


3. Compute the test statistics.

t = (x_bar - 0) / sqrt(s_squared / n)
t # print t statistic 
## [1] 3.043067


4. Compute the p-value.

p_value = pt(t, n-1, lower.tail = FALSE)*2
p_value # print p-value
## [1] 0.002998891


5. Use the existing function or procedure to test the hypothesis. Compare the two results.

# compare both my_mean and built in mean function to nine digits
#   and print boolean based on evalutation
round(x_bar, 9) == round(mean(sample), 9)
## [1] TRUE
# compare both my_samp_var and built in var function to nine digits
#   and print boolean based on evalutation
round(s_squared, 9) == round(var(sample), 9)
## [1] TRUE
# compare both my p_value and t.test's p_value to nine digits
#   and print boolean based on evalutation
built_in_results = t.test(sample, mu=0)
round(p_value, 9) == round(built_in_results$p.value, 9) 
## [1] TRUE