Skip to content

Commit

Permalink
visualize - v3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
coatless committed Apr 27, 2017
1 parent 3df7b2c commit d7f656d
Show file tree
Hide file tree
Showing 17 changed files with 159 additions and 95 deletions.
12 changes: 7 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ Package: visualize
Type: Package
Title: Graph Probability Distributions with User Supplied Parameters
and Stats.
Version: 2.2
Date: 2013-01-15
Version: 3.0
Date: 2013-02-15
Author: James Balamuta
Maintainer: James Balamuta <james.balamuta@gmail.com>
Description: Graphs the pdf or pmf and highlights what area or
probability is present in defined locations. Supports lower,
bounded, and upper calculations. Also provided on the graph is
the mean and variance of the distribution.
probability is present in user defined locations. Visualize is
able to provide lower tail, bounded, and upper tail
calculations. Supports strict and equal to inequalities. Also
provided on the graph is the mean and variance of the
distribution.
License: MIT License
4 changes: 2 additions & 2 deletions R/visualize.binom.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
visualize.binom <-
function(stat = 1, size = 3, prob = .5, section = "lower") {
visualize.it('binom', stat = stat, list(size = size, prob = prob), section = section)
function(stat = 1, size = 3, prob = .5, section = "lower", strict = FALSE) {
visualize.it('binom', stat = stat, list(size = size, prob = prob), section = section, strict=strict)
}
66 changes: 41 additions & 25 deletions R/visualize.discrete.R
Original file line number Diff line number Diff line change
@@ -1,77 +1,93 @@
visualize.discrete <-
function(dist, stat = c(0,1), params, section = "lower"){
function(dist, stat = c(0,1), params, section = "lower", strict){

stat = round(stat)

#Perform the approriate scales to center the distribution.
mean = dist$init(params)[1];var = dist$init(params)[2]
lower_bound = max(0,round(-3*sqrt(var) + mean));
upper_bound = round(3*sqrt(var) + mean) #axis length
upper_bound = round(3*sqrt(var) + mean)

#Builds data for graph
x = seq(lower_bound,upper_bound,by=1)
y = dist$density(x,params)
ymax = max(y)+0.05

#Creates Graph Title
graphmain = paste(dist$name," \n")
for(i in 1:length(params)){
graphmain = paste(graphmain, dist$varsymbols[i]," = ",params[[i]], " ")
}

#evaluate based on sections.
if(section == "lower"){
if(stat >= lower_bound) region = length(seq(lower_bound,stat[[1]],1))
if(stat >= lower_bound) region = stat[[1]]-lower_bound+1
else region = 0

#Build lower tail shade if stat is within bounds.
i = region
j = abs(region - upper_bound)+1
i = region-1*(strict[[1]] == 1 & stat >= lower_bound)
j = abs(region - upper_bound)+1+1*(strict[[1]] == 1 & stat >= lower_bound)

barplot(y, ylim = c(0, ymax), col=c(rep("blue",i),rep("white",j)), axes = FALSE)
barplot(y, ylim = c(0, ymax), xlab = "Values", ylab = "Probability", names.arg = x, main=graphmain, col=c(rep("orange",i),rep("white",j)), density=c(rep(3,i),rep(0,j)), add = TRUE)

prob = dist$probability(stat,params)
subheader = paste("P( ",dist$variable," \u2264 ",stat, ") = ", signif(prob, digits=3))
prob = dist$probability(stat-1*(strict[[1]] == 1),params)
ineqsym = if(strict[[1]]==0){" \u2264 "}else{" < "}
subheader = paste("P( ",dist$variable, ineqsym ,stat, ") = ", signif(prob, digits=3))
}
else if(section == "bounded"){
if(length(stat)!= 2) stop("Incorrect Number of Stat Parameters Supplied for Bounded Condition")
disupper = upper = stat[[2]]; dislower = lower = stat[[1]]
print(upper);print(lower);

#Map the bounds
if(upper > upper_bound){
upper = upper_bound
if(lower > upper_bound) lower = upper_bound
}
else if(lower < lower_bound){
if(lower < lower_bound){
lower = lower_bound
if(upper < lower_bound) upper = lower_bound
}


#Calculate necessary adjustments.
#Figure out whether the stat is outside of the graph display
bounds_adjust = -1*((dislower < lower_bound & disupper < lower_bound) || (disupper > upper_bound & dislower > upper_bound))
lower_adjust = 1*(lower_bound == lower & upper_bound != upper); upper_adjust = 1*(upper_bound == upper & lower_bound != lower)
#Adjust if inequalities are strict, while checking stat with graph display.
strict_adjust = -1*(strict[[1]]==1 & bounds_adjust != -1 & lower_adjust != 1) - 1*(strict[[2]]==1 & bounds_adjust != -1 & upper_adjust != 1)

#Build the grid.
i = length(seq(lower_bound,lower,by=1)) - 1
j = length(seq(lower,upper,by=1)) - 1*(dislower < lower_bound & disupper < lower_bound || disupper > upper_bound & dislower > upper_bound)
k = length(seq(upper,upper_bound,by=1)) - 1
i = abs(lower-lower_bound) + 1*(strict[[1]]==1 & dislower >= lower_bound)
j = abs(upper-lower) + 1 + strict_adjust + bounds_adjust
k = abs(upper_bound-upper) + 1*(strict[[2]]==1 & disupper <= upper_bound)

#plot the distribution
barplot(y, ylim = c(0, ymax), col=c(rep("white",i),rep("blue",j),rep("white",k)), axes = FALSE)
barplot(y, ylim = c(0, ymax), xlab = "Values", ylab = "Probability", names.arg = x, main=graphmain, col=c(rep("white",i),rep("orange",j), rep("white",k)), density=c(rep(0,i),rep(3,j),rep(0,k)), add = TRUE)

#Generate subtitle
prob = dist$probability(disupper,params) - dist$probability(dislower-1,params)
subheader = paste("P(",dislower," \u2264 ",dist$variable," \u2264 ",disupper,") =", signif(prob, digits=3))
prob = dist$probability(disupper-1*(strict[[2]]==1),params) - dist$probability(dislower-1*(strict[[1]]==0),params)
if(prob < 0) {prob = 0}
low_ineq = if(strict[[1]]==0){" \u2264 "}else{" < "}
upper_ineq = if(strict[[2]]==0){" \u2264 "}else{" < "}
subheader = paste("P(",dislower,low_ineq,dist$variable,upper_ineq,disupper,") =", signif(prob, digits=3))
}
else if(section == "upper"){
span = upper_bound-lower_bound+1
if(stat <= upper_bound & stat > lower_bound) region = length(seq(stat[[1]],upper_bound,1))
else if(stat <= lower_bound) region = span
if(stat <= upper_bound & stat >= lower_bound) region = upper_bound-stat[[1]]+1
else if(stat < lower_bound) region = span
else region = 0
i = abs(region - (upper_bound-lower_bound+1)) #region-span of graph
j = region

i = abs(region - (upper_bound-lower_bound+1))+1*(strict[[1]]==1 & stat >= lower_bound) #region-span of graph
j = region-1*(strict[[1]]==1 & stat <= upper_bound & stat >= lower_bound)

barplot(y, ylim = c(0, ymax), col=c(rep("white",i),rep("blue",j)), axes = FALSE)
barplot(y, ylim = c(0, ymax), xlab = "Values", ylab = "Probability", names.arg = x, main=graphmain, col=c(rep("white",i),rep("orange",j)), density=c(rep(0,i),rep(3,j)), add = TRUE)
prob = 1-dist$probability(stat-1,params)
subheader = paste("P( ",dist$variable," \u2265 ", stat, " ) =", signif(prob, digits=3))
}
else{
stop("Section not specified. Please choose either lower, bounded, or upper.")
prob = 1-dist$probability(stat-1*(strict[[1]] == 0),params)
ineqsym = if(strict[[1]]==0){" \u2265 "}else{" > "}
subheader = paste("P( ",dist$variable, ineqsym, stat, " ) =", signif(prob, digits=3))
}
else{ stop("Section not specified. Please choose either lower, bounded, or upper.") }
mtext(subheader,3)
title(sub = paste("\u03BC = ", signif(mean, digits=3),", \u03C3\u00B2 = ",signif(var, digits=3)))
}
4 changes: 2 additions & 2 deletions R/visualize.geom.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
visualize.geom <-
function(stat = 1, prob = .3, section = "lower") {
visualize.it('geom', stat = stat, list(prob = prob), section = section)
function(stat = 1, prob = .3, section = "lower", strict = FALSE) {
visualize.it('geom', stat = stat, list(prob = prob), section = section, strict=strict)
}
4 changes: 2 additions & 2 deletions R/visualize.hyper.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
visualize.hyper <-
function(stat = 1, m = 5, n = 4, k = 2, section = "lower") {
visualize.it('hyper', stat = stat, list(m = m, n = n, k = k), section = section)
function(stat = 1, m = 5, n = 4, k = 2, section = "lower", strict = FALSE) {
visualize.it('hyper', stat = stat, list(m = m, n = n, k = k), section = section, strict=strict)
}
27 changes: 22 additions & 5 deletions R/visualize.it.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
visualize.it <-
function(dist='norm', stat = c(0,1), params = list(mu = 0, sd = 1), section = "lower") {
function(dist='norm', stat = c(0,1), params = list(mu = 0, sd = 1), section = "lower", strict = c(0,1)) {
dist = visualize.distributions[[casefold(dist)]]
if(is.null(dist)) stop("Distribution not found.")
if(length(params) != dist$params) stop("Invalid amount of parameters provided.")
if(length(stat)>1 & section != "bounded"){ section = "bounded"; cat("Supplied stat length > 1, reverting to bounded.")}
else if(length(stat)<2 & section == "bounded"){ section = "lower"; cat("Supplied stat length < 2, reverting to lower.")}
if(dist$type == "continuous") visualize.continuous(dist, stat, params, section)
else visualize.discrete(dist, stat, params, section)

if(length(stat)>1 & section != "bounded"){
section = "bounded"; cat("Supplied stat length > 1, reverting to bounded.")
}
else if(length(stat)<2 & section == "bounded"){
section = "lower"; cat("Supplied stat length < 2, reverting to lower.")
}

#distribution specific graphing call.
if(dist$type == "continuous"){ visualize.continuous(dist, stat, params, section)}
else{

#Ensures array is inbounds according to conditions
inequality = if(strict[[1]] == 0) {"equal to"} else {"strict"}
if(length(strict)<2 & section == "bounded"){
strict = c(strict[[1]],strict[[1]])
cat(paste("Supplied strict length < 2, setting inequalities to ", inequality, " inequality."))
}

visualize.discrete(dist, stat, params, section, strict)
}
}
4 changes: 2 additions & 2 deletions R/visualize.nbinom.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
visualize.nbinom <-
function(stat = 1, size = 6, prob = .5, section = "lower") {
visualize.it('nbinom', stat = stat, list(size = size, prob = prob), section = section)
function(stat = 1, size = 6, prob = .5, section = "lower", strict = FALSE) {
visualize.it('nbinom', stat = stat, list(size = size, prob = prob), section = section, strict=strict)
}
4 changes: 2 additions & 2 deletions R/visualize.pois.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
visualize.pois <-
function(stat = 1, lambda = 3.5, section = "lower") {
visualize.it('pois', stat = stat, list(lambda = lambda), section = section)
function(stat = 1, lambda = 3.5, section = "lower", strict = FALSE) {
visualize.it('pois', stat = stat, list(lambda = lambda), section = section, strict=strict)
}
23 changes: 15 additions & 8 deletions man/visualize-package.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
\alias{visualize}
\docType{package}
\title{
Graphs the pdf or pmf and highlights what area or probability is present in defined locations.
Graph Probability Distributions with User Supplied Parameters and Stats.
}
\description{
Graphs the pdf or pmf and highlights what area or probability is present in defined locations. Supports lower, bounded, and upper calculations. Also provided on the graph is the mean and variance of the distribution.
Graphs the pdf or pmf and highlights what area or probability is present in user defined locations. Visualize is able to provide lower tail, bounded, and upper tail calculations. Supports strict and equal to inequalities. Also provided on the graph is the mean and variance of the distribution.
}
\details{
\tabular{ll}{
Package: \tab visualize\cr
Type: \tab Package\cr
Version: \tab 2.0\cr
Date: \tab 2012-12-30\cr
Version: \tab 3.0\cr
Date: \tab 2013-02-15\cr
License: \tab MIT License\cr
}
}
Expand All @@ -24,13 +24,20 @@ Maintainer: James Balamuta <james.balamuta@gmail.com>
}
\note{
Update Notes: \cr
v2.1 \cr
v3.0 - "Seeing is Strict" \cr
* New feature: Evaluate probabilities with strict inequalities! \cr
* Rewrote sections to increase efficiency and reliability. \cr

v2.2 - Hotfix - "Seeing is Correct" \cr
* Corrects rendering issue with supplied stat not meeting certain criteria. \cr

v2.1 - Hotfix - "Seeing is Off" \cr
* Corrects minor graphing labels. \cr
* Revised error handling.\cr

v2.0 - "Seeing is Believing" \cr
* New Continuous Distributions: Cauchy, F, Log Normal, Logistic, T, and Wilcox. \cr
* New Bar graph rendering for discrete probability distributions. \cr
* New feature: Bar graph rendering for discrete probability distributions. \cr
* Rewritten Distribution Wrapper\cr

v1.0 - "Seeing is Magic" \cr
Expand All @@ -44,8 +51,8 @@ v1.0 - "Seeing is Magic" \cr
##visualize.it acts as the general wrapper.
##For guided application of visualize, see the visualize.distr_name list.
#Binomial distribution evaluated at lower tail.
visualize.it('binom', stat = 2, params = list(n=4,p=.5), section ="lower")
visualize.binom(stat = 2, size = 4, prob =.5, section ="lower")
visualize.it(dist = 'binom', stat = 2, params = list(size = 4,prob = .5), section ="lower", strict = TRUE)
visualize.binom(stat = 2, size = 4, prob =.5, section ="lower", strict = TRUE)

#Set to shade inbetween a bounded region.
visualize.it(dist = 'norm', stat = c(-1,1), list(mu=0,sd=1), section="bounded")
Expand Down
17 changes: 10 additions & 7 deletions man/visualize.binom.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Visualize Binomial Distribution
Generates a plot of the Binomial distribution with user specified parameters.
}
\usage{
visualize.binom(stat = 1, size = 3, prob = 0.5, section = "lower")
visualize.binom(stat = 1, size = 3, prob = 0.5, section = "lower", strict = FALSE)
}

\arguments{
Expand All @@ -22,6 +22,9 @@ visualize.binom(stat = 1, size = 3, prob = 0.5, section = "lower")
}
\item{section}{
Select how you want the statistic(s) evaluated via \code{section=} either \code{"lower"},\code{"bounded"}, or \code{"upper"}.
}
\item{strict}{
Determines whether the probability will be generated as a strict (<, >) or equal to (<=, >=) inequality. \code{strict=} requires either values = 0 or =FALSE for equal to OR values =1 or =TRUE for strict. For bounded condition use: \code{strict=c(0,1)} or \code{strict=c(FALSE,TRUE)}.
}
}
\details{
Expand All @@ -36,13 +39,13 @@ James Balamuta
\code{\link{visualize.it}} , \code{\link{dbinom}}.
}
\examples{
#Evaluates lower tail.
visualize.binom(stat = 1, size = 3, prob = 0.5, section = "lower")
#Evaluates lower tail with equal to inequality.
visualize.binom(stat = 1, size = 3, prob = 0.5, section = "lower", strict = FALSE)

#Evaluates bounded region.
visualize.binom(stat = c(1,2), size = 5, prob = 0.35, section = "bounded")
#Evaluates bounded region with lower bound equal to and upper bound strict inequality.
visualize.binom(stat = c(1,2), size = 5, prob = 0.35, section = "bounded", strict = c(0,1))

#Evaluates upper tail.
visualize.binom(stat = 1, size = 3, prob = 0.5, section = "upper")
#Evaluates upper tail with strict inequality.
visualize.binom(stat = 1, size = 3, prob = 0.5, section = "upper", strict = TRUE)
}
\keyword{ visualize }
7 changes: 5 additions & 2 deletions man/visualize.discrete.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Graphing function for Discrete Distributions.
Handles how discrete distributions are graphed. Users should not use this function. Instead, users should use \code{link{visualize.it}}.
}
\usage{
visualize.discrete(dist, stat = c(0, 1), params, section = "lower")
visualize.discrete(dist, stat = c(0, 1), params, section = "lower", strict)
}
%- maybe also 'usage' for other objects documented here.
\arguments{
Expand All @@ -22,6 +22,9 @@ A list that must contain the necessary parameters for each distribution. For exa
}
\item{section}{
Select how you want the statistic(s) evaluated via \code{section=} either \code{"lower"},\code{"bounded"}, or \code{"upper"}.
}
\item{strict}{
Determines whether the probability will be generated as a strict (<, >) or equal to (<=, >=) inequality. \code{strict=} requires either values = 0 or =FALSE for equal to OR values =1 or =TRUE for strict. For bounded condition use: \code{strict=c(0,1)} or \code{strict=c(FALSE,TRUE)}.
}
}
\author{
Expand All @@ -33,7 +36,7 @@ James Balamuta
}
\examples{
#Function does not have dist look up, must go through visualize.it
visualize.it(dist='geom', stat = c(2,4), params = list(prob = .75), section = "bounded")
visualize.it(dist='geom', stat = c(2,4), params = list(prob = .75), section = "bounded", strict = c(0,1))
}
% Add one or more standard keywords, see file 'KEYWORDS' in the
% R documentation directory.
Expand Down
33 changes: 17 additions & 16 deletions man/visualize.distributions.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,31 @@ All of visualize's supported distributions with their density, probability, and

}

The distributions currently listed are:
The distributions currently available to use are:
\tabular{llll}{
Distribution \tab r Name \tab Distribution \tab r Name \cr
Beta \tab beta \tab Lognormal \tab lnorm\cr
Beta \tab beta \tab Lognormal* \tab lnorm\cr
Binomial \tab binom \tab Negative Binomial \tab nbinom\cr
Cauchy \tab cauchy \tab Normal \tab norm\cr
Cauchy* \tab cauchy \tab Normal \tab norm\cr
Chisquare \tab chisq \tab Poisson \tab pois\cr
Exponential \tab exp \tab Student t \tab t\cr
F \tab f \tab Uniform \tab unif\cr
Gamma \tab gamma \tab Tukey \tab tukey\cr
Geometric \tab geom \tab Weibull \tab weib\cr
Hypergeometric \tab hyper \tab Wilcoxon \tab wilcox\cr
Logistic \tab logis\tab \tab \cr
Exponential \tab exp \tab Student t* \tab t\cr
F* \tab f \tab Uniform \tab unif\cr
Gamma \tab gamma \tab Geometric \tab geom \cr
Hypergeometric \tab hyper \tab Wilcoxon* \tab wilcox\cr
Logistic* \tab logis\tab \tab \cr
}
* denotes the distribution was added in v2.0.
}
\author{
James Balamuta
}
\examples{
visualize.distributions <- list(
visualize.distributions = list(
'beta' = list(
type = "continuous",
name = "Beta Distribution",
variable = "b",
varsymbols = c("\u03B1","\u03B2"),
params = 2,
init = function(params, ...) {
shape1 = params[[1]]; shape2 = params[[2]]
Expand All @@ -75,17 +76,17 @@ visualize.distributions <- list(
var = (shape1 * shape2)/((shape1 + shape2 + 1)*(shape1 + shape2)^2)
c(mean, var)
},
density = function(x,params, noncent = 0, lowtail = TRUE, problog = FALSE, ...){
density = function(x,params, ncp = 0, lower.tail = TRUE, log = FALSE, ...){
if(params[[1]] <= 0 || params[[2]] <= 0) stop("Error: Need alpha, beta > 0")
dbeta(x,params[[1]], params[[2]], ncp = noncent, log = problog)
dbeta(x,params[[1]], params[[2]], ncp = ncp, log = log)
},
probability = function(x,params, noncent = 0, lowtail = TRUE, problog = FALSE, ...){
probability = function(q,params, ncp = 0, lower.tail = TRUE, log.p = FALSE, ...){
if(params[[1]] <= 0 || params[[2]] <= 0) stop("Error: Need alpha, beta > 0")
pbeta(x,params[[1]], params[[2]], ncp = noncent, lower.tail = lowtail, log.p = problog)
pbeta(q,params[[1]], params[[2]], ncp = ncp, lower.tail = lower.tail, log.p = log.p)
},
quantile = function(x,params, noncent = 0, lowtail = TRUE, problog = FALSE, ...){
quantile = function(p,params, ncp = 0, lower.tail = TRUE, log.p = FALSE, ...){
if(params[[1]] <= 0 || params[[2]] <= 0) stop("Error: Need alpha, beta > 0")
qbeta(x,params[[1]], params[[2]], ncp = noncent, lower.tail = lowtail, log.p = problog)
qbeta(p,params[[1]], params[[2]], ncp = ncp, lower.tail = lower.tail, log.p = log.p)
}
)
)
Expand Down
Loading

0 comments on commit d7f656d

Please sign in to comment.