adding more stringent defenses to the plottinng fn
adding more stringent defenses to the plottinng fn
# MSC1090, Fall 2018 - UofT/SciNet %%% Marcelo Ponce
# this script is used to visually explore the results of your
# assignment #5
# this function receives as arguments two vectors
# containing the sample sizes and probabilities values
# function to generate a semi-log plot of two vectors with connecting lines
plotMC <- function(NS,probs, wHist=T) {
# DEFENSE ... DEFENSE ...
# this internal fn takes care of protecting the fn for receiving the proper arguments
defense <- function(input1,input2) {
# check that the arguments are vectors, ie. not a list!
if ( (class(input1) == "list") | (class(input2) == "list") ) {
stop("The fn requires two numeric vectors!")
}
# check that the arguments are numbers!
if ( !is.numeric(input1) | !is.numeric(input2) ) {
stop("The fn requires two numeric vectors!")
}
# check that the arguments have matching dimensions
if (length(input1) != length(input2)) {
stop("This function requires two vectors with the same number of elements!")
}
}
# execute defensive programming first!
defense(NS,probs)
# obtain lenght of vectors
lNS <- length(NS)
lprobs <- length(probs)
# set up figure canvas
par(fig = c(0,1,0,1))
# semi-log plot of probs vs NS with connecting lines
plot(log(NS),probs,'b')
# determine values to consider for computing convergent prob.
i0 <- 2*lprobs/3
i1 <- lprobs
convP <- mean(probs[i0:i1])
# add line to display convergent value of P
abline(v=0,h=convP, col='red', lw=2, lt=2)
# add inset figure with a histogram
if (wHist) {
# figure out where to place inset
if ( (convP-mean(probs[1:2])) < 0 ) {
# mean below 1st value, inset above
par(fig = c(0.47,1, 0.5, 1), new = T)
} else {
# mean above 1st value, inset below
par(fig = c(0.47,1, 0.07, 0.5), new = T)
}
hist(probs, col='grey')
}
}