title: "Getting Started with binomial"
author: "Ranqi Zhong"
date: "r Sys.Date()
"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Introduction to binomial}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
knitr::opts_chunk$set(collapse = T, comment = "#>") library(binomial)
1.1) Private Checker Functions Function check_prob() A private auxiliary function check_prob() to test if an input prob is a valid probability check_prob() takes one input: prob. If prob is valid, then check_prob() should return TRUE. If prob is invalid, then check_prob() should stop() execution with an error¡ªe.g. something like 'invalid prob value', or 'p has to be a number betwen 0 and 1'.
check_prob(0.5)
Function check_trials() A private auxiliary function check_trials() to test if an input trials is a valid value for number of trials (i.e. n is a non-negative integer). check_trials() takes one input: trials. If trials is valid, then check_trials() should return TRUE. If trials is invalid, then check_trials() should stop() execution with an error.
check_trials(20)
Function check_success() Write a private auxiliary function check_success() to test if an input success is a valid value for number of successes check_success() takes two inputs: success and trials. success should be a vector of non-negative integer(s) less than or equal to trials. Notice that success can be of length greater than 1 (i.e. multiple successes). If success is valid, then check_success() should return TRUE. If success is invalid, then check_success() should stop() execution with an error¡ª
check_success(3,4)
1.2) Private Auxiliary Functions The following private auxiliary functions: aux_mean() aux_variance() aux_mode() aux_skewness() aux_kurtosis() All these functions take two arguments: trials and prob. And return the corresponding value from the computed summary measure (formulas provided in the introduction of this 5 document). Each of these functions will be invoked by their corresponding main functions e.g. bin_mean(), bin_variance(), etc.
aux_mean(10, 0.3) aux_variance(10, 0.3) aux_mode(10, 0.3) aux_skewness(10, 0.3) aux_kurtosis(10, 0.3)
1.3) Function bin_choose() Use factorial() to write a main function bin_choose() that calculates the number of combinations in which k successes can occur in n trials.
bin_choose(n = 5, k = 2) bin_choose(5, 0) bin_choose(5, 1:3)
1.4) Function bin_probability() Use bin_choose() to create a main function bin_probability(). bin_probability() should take three arguments: success, trials, and prob. Use check_trials() to check that trials is valid Use check_prob() to check that prob is valid Use check_success() to check that success is valid
bin_probability(success = 2, trials = 5, prob = 0.5) bin_probability(success = 0:2, trials = 5, prob = 0.5) bin_probability(success = 55, trials = 100, prob = 0.45)
1.5) Function bin_distribution() Use bin_probability() to create a main function bin_distribution(). There are two arguments trials, and prob. The returned output should be a data.frame with two classes: c("bindis", "data.frame")
bin_distribution(trials = 5, prob = 0.5)
Function plot.bindis() There is a plotting method (i.e. a function) plot.bindis() that graphs a barplot to display the probability histogram of a binomial distribution object "bindis".
plot.bindis(trials = 5, prob = 0.5)
1.6) Function bin_cumulative() Use bin_cumulative() to create a main function bin_cumulative(). This function should have two arguments trials, and prob. The returned output should be a data.frame with two classes: c("bincum", "data.frame") This function should return a data frame with both the probability distribution and the cumulative probabilities: sucesses in the first column, probability in the second column, and cumulative in the third column.
bin_cumulative(trials = 5, prob = 0.5)
Function plot.bincum() This is a plotting method (i.e. a function) plot.bincum() that graphs the cumulative distribution in ab object "bincum".
plot.bincum(trials = 5, prob = 0.5)
1.7) Function bin_variable() Another function to include in your "binomial" package is bin_variable(). This is a main function that takes two arguments: trials and prob This function should return an object of class "binvar", that is, a binomial random variable object. This function should invoke check_trials() and check_prob() The returned object should be a list with named elements: trials: number of trials prob: probability of success
bin_variable(trials = 10, p = 0.3)
Methods summary.binvar() and print.summary.binvar() To get a full summary description of an object "binvar",it is needed to create a function summary.binvar(). This function takes an object of class "binvar" The returned output is a list of class "summary.binvar" containing named elements: trials: number of trials prob: probability of success mean: mean or expected value variance: variance mode: mode skewness: skewness kurtosis: kurtosis
summary.binvar(trials = 10, p = 0.3)
print.summary.binvar(trials = 10, p = 0.3)
1.8) Functions of measures Finally, "binomial" package should also contain main functions for each of the summary measures: e.g. bin_mean(), bin_variance(), etc. These are main functions that take two arguments: trials and prob Use check_trials() to check that trials is valid Use check_prob() to check that prob is valid Invoke your auxiliary functions to do the corresponding calculation. For instance: aux_mean() gets called by bin_mean().
bin_mean(10, 0.3) bin_variance(10, 0.3) bin_mode(10, 0.3) bin_skewness(10, 0.3) bin_kurtosis(10, 0.3)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.