# File:   mit18_05_s22_RQuiz-grader.r 
# Authors: Jeremy Orloff and Jennifer French
#
# MIT OpenCourseWare: https://ocw.mit.edu
# 18.05 Introduction to Probability and Statistics
# Spring 2022
# For information about citing these materials or our Terms of Use, visit:
# https://ocw.mit.edu/terms.
#
# R Quiz grading script
# Expected output in RQuiz-grader.html
# If this file changes --need to rebuild RQuiz-grader.html

# Use 'File > Compile report...' to create an R Markdown report from this.
# Because this opens a new session, it doesn't see the environment.
# So we need the following line, which should be commented out when using the grading script for grading.

 source('mit18_05_s22_RQuiz-solutions.r')  ### COMMENT OUT FOR GRADING
 cat("WARNING: make sure source('mit18_05_s22_RQuiz-solutions.r') is commented out before grading\n")
## WARNING: make sure source('mit18_05_s22_RQuiz-solutions.r') is commented out before grading
# For grading, open this file and set working directory to source file location
# Problem 1
rquiz_problem_1a(-1, 1.5, 1.5, 2, -5, 7)
## ----------------------------------
## Problem 1a (5 points): Graphs

## See plots
rquiz_problem_1b(12, 8, 17)
## -----
## Problem 1b (5 points): Combinations and factorials
## choose(12, 8) = 495
## 17! = 3.556874e+14
## lchoose(12, 8) = 6.204558
rquiz_problem_1b(100000, 1001, 67)
## -----
## Problem 1b (5 points): Combinations and factorials
## choose(1e+05, 1001) = Inf
## 67! = 3.647111e+94
## lchoose(1e+05, 1001) = 5600.38
theta_values = seq(0, 1, 0.1)
rquiz_problem_1c(theta_values, 100, 33)
## -----
## Problem 1c (10 points): Bayesian coins 
## 1c(i) The MLE is theta = 0.3 
## 1c(ii) Prior predictive probability of success = 0.5 
## 1c(iii) Posterior probabilities for theta
##       theta_values    posterior
##  [1,]          0.0 0.000000e+00
##  [2,]          0.1 2.549852e-09
##  [3,]          0.2 8.190037e-03
##  [4,]          0.3 6.899807e-01
##  [5,]          0.4 2.994886e-01
##  [6,]          0.5 2.340277e-03
##  [7,]          0.6 3.085159e-07
##  [8,]          0.7 2.126308e-13
##  [9,]          0.8 2.774892e-23
## [10,]          0.9 9.167896e-42
## [11,]          1.0 0.000000e+00
theta_values = c(0.4, 0.6, 0.8, 1.0)
rquiz_problem_1c(theta_values, 10, 7)
## -----
## Problem 1c (10 points): Bayesian coins 
## 1c(i) The MLE is theta = 0.6 
## 1c(ii) Prior predictive probability of success = 0.7 
## 1c(iii) Posterior probabilities for theta
##      theta_values  posterior
## [1,]          0.4 0.09256482
## [2,]          0.6 0.46860939
## [3,]          0.8 0.43882580
## [4,]          1.0 0.00000000
# Problem 2
rquiz_problem_2a(500, 20, 1.0)
## ----------------------------------
## Problem 2a (10 points): Density histograms

## See plots
rquiz_problem_2a(1000, 40, 1.0)
## ----------------------------------
## Problem 2a (10 points): Density histograms

## See plots
rquiz_problem_2b(10000, 500, 20, 0.2)
## -----
## Problem 2b (10 points): Density histograms

## See plots
# Problem 3
set.seed(2)
rquiz_problem_3(rnorm(300, 2, 3), 0.05)
## ----------------------------------
## Problem 3 (10 points): 
## The null hypothesis H0 is that the data is from a normal distribution. 
## p_value = 0.3861367 
## Do not reject H0.
rquiz_problem_3(runif(300, 2, 3), 0.2)
## ----------------------------------
## Problem 3 (10 points): 
## The null hypothesis H0 is that the data is from a normal distribution. 
## p_value = 2.484962e-08 
## Reject H0 in favor of HA that the data is not normal
rquiz_problem_3(rt(200, 1), 0.1)
## ----------------------------------
## Problem 3 (10 points): 
## The null hypothesis H0 is that the data is from a normal distribution. 
## p_value = 1.752288e-29 
## Reject H0 in favor of HA that the data is not normal
rquiz_problem_3(rt(30, 4), 0.1)
## ----------------------------------
## Problem 3 (10 points): 
## The null hypothesis H0 is that the data is from a normal distribution. 
## p_value = 0.8653541 
## Do not reject H0.
# Problem 4 (Extra credit)
# Make sure the WORKING DIRECTORY is set to the source file
# Problem 4 (Extra credit)
# Make sure the WORKING DIRECTORY is set to the source file
rquiz_problem_4('mit18_05_s22_RQuiz_data_prob4_test.txt', 0.05)
## ----------------------------------
## Problem 4 (Extra credit: 5 points): 
## p value = 0.7195056 
## Do not reject H0 that the means are the same
rquiz_problem_4('mit18_05_s22_RQuiz_data_grader1.txt', 0.05)
## ----------------------------------
## Problem 4 (Extra credit: 5 points): 
## p value = 2.616381e-13 
## Reject H0 in favor of HA that the means are different
rquiz_problem_4('mit18_05_s22_RQuiz_data_grader2.txt', 0.05)
## ----------------------------------
## Problem 4 (Extra credit: 5 points): 
## p value = 0.04735002 
## Reject H0 in favor of HA that the means are different
rquiz_problem_4('mit18_05_s22_RQuiz_data_grader2.txt', 0.01)
## ----------------------------------
## Problem 4 (Extra credit: 5 points): 
## p value = 0.04735002 
## Do not reject H0 that the means are the same