Package Prob': R Topics Documented
Package Prob': R Topics Documented
Package Prob': R Topics Documented
R topics documented:
prob-package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
addrv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
cards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
CharFunc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
countrep . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
empirical . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
euchredeck . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
gen2wayTable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
genIndepTable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1
2 prob-package
genLogRegData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
genXdata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
iidspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
intersect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
is.probspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
isin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
isrep . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
marginal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
noorder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
nsamp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
permsn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
prob . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
probspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
rolldie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
roulette . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
setdiff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
sim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
subset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
tosscoin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
union . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
urnsamples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Index 36
Description
A framework for performing elementary probability calculations on finite sample spaces. It is built
around the concept of a probability space, which is an object of outcomes and an object probs of
probabilities associated with the outcomes.
There are two ways to represent a probability space in the prob package. The first is with a data
frame that has a probs column. Entries of probs should be nonnegative and sum to one. The
second way is with a list having two components: outcomes and probs. The component outcomes
is a list containing elements of the most arbitrary sort; they can be data frames, vectors, more lists,
whatever. The probs component is a vector (of the same length as outcomes), which associates to
each element of outcomes a nonnegative number. As before, the only additional requirement is that
the sum of probs be one.
There are functions in the prob package to address many topics in a standard course in elementary
probability. In particular, there are methods for setting up sample spaces, counting tools, defin-
ing probability spaces, performing set algebra, calculating probability and conditional probability,
tools for simulation and checking the law of large numbers, adding random variables, and finding
marginal distributions. See vignette("prob") for details.
There are some functions included to set up some of the standard sample spaces usually encountered
in an elementary probability course. Examples include tossing a coin, rolling a die, drawing from a
52 card deck, etc. If you know of topics that would be of general interest and could be incorporated
addrv 3
in the prob package framework, I would be happy to hear about them. Comments and suggestions
are always welcomed.
The prob package is a first step toward addressing probability in R, and has been written in the spirit
of simplicity. The procedures work best to solve problems that are manageable in scope. Users that
wish to investigate especially large or intricate problems are encouraged to modify and streamline
the code to suit their individual needs.
Characteristic functions for the base probability distributions have been included. For details, type
vignette("charfunc") at the command prompt.
Details
Package: prob
Version: 1.0-0
Date: 2017-02-15
Depends: combinat, fAsianOptions
Suggests: VGAM, hypergeo
LazyLoad: no
License: GPL version 3 or newer
URL: http://prob.r-forge.r-project.org, http://gkerns.people.ysu.edu/
Author(s)
Description
Adds a column to a data frame probability space containing the values of a random variable com-
puted from the existing columns of the space.
Usage
Arguments
space a data frame with a probs column.
FUN a function to be applied to each row of outcomes in space
invars a character vector indicating input columns of space
name an (optional) name to give the defined random variable.
... an expression defining a random variable.
Details
There are two ways to add a random variable to a data frame probability space; see the examples.
The argument FUN has precedence and will be used if specified. If name is not specified, then the new
random variable will be called X. Note that this function only works for data frames, as a method
for objects of class ps has not been implemented.
Value
The input data frame with an additional column called name.
Author(s)
G. Jay Kerns <[email protected]>.
See Also
See transform to add a column to a data frame of outcomes (not yet a probability space).
Examples
S <-rolldie(3, makespace = TRUE)
addrv(S, sum, name = "Y")
addrv(S, Z = X3 - X2 )
Description
The title says it all.
Usage
cards(jokers = FALSE, makespace = FALSE)
CharFunc 5
Arguments
jokers logical.
makespace logical.
Details
This generates a data frame sample space of a standard deck of 52 playing cards. Optionally, the
user can specify that Jokers be included, which have a rank but with suit a missing value.
Value
A data frame with columns rank and suit, and optionally a column of equally likely probs.
See Also
rolldie, tosscoin, and roulette
Examples
cards()
cards(makespace = TRUE)
Description
The characteristic functions for selected probability distributions supported by R. All base distri-
butions are included, with the exception of wilcox and signedrank. For more resources please
see the References, and for complete details and formulas see the charfunc vignette, which can
be accessed by vignette("charfunc") at the command prompt. Only the simplest formulas are
listed below.
Usage
cfbeta(t, shape1, shape2, ncp = 0)
cfbinom(t, size, prob)
cfcauchy(t, location = 0, scale = 1)
cfchisq(t, df, ncp = 0)
cfexp(t, rate = 1)
cff(t, df1, df2, ncp, kmax = 10)
cfgamma(t, shape, rate = 1, scale = 1/rate)
cfgeom(t, prob)
cfhyper(t, m, n, k)
cflnorm(t, meanlog = 0, sdlog = 1)
cflogis(t, location = 0, scale = 1)
cfnbinom(t, size, prob, mu)
6 CharFunc
cfnorm(t, mean = 0, sd = 1)
cfpois(t, lambda)
cfsignrank(t, n)
cft(t, df, ncp)
cfunif(t, min=0, max=1)
cfweibull(t, shape, scale = 1)
cfwilcox(t, m, n)
Arguments
t numeric value. Some of the above are vectorized functions.
df degrees of freedom (> 0, maybe non-integer)
df1, df2 numerator and denominator degrees of freedom, must be positive
k the number of balls drawn from the urn.
kmax the number of terms in the series.
lambda vector of (positive) means.
location, scale
location and scale parameters; scale must be positive.
m the number of white balls in the urn.
meanlog, sdlog mean and standard deviation of the distribution on the log scale with default
values of 0 and 1 respectively.
mean vector of means.
min, max (unif) lower and upper limits of the distribution. Must be finite and in the correct
order.
mu (nbinom) alternative parametrization via mean
n the number of black balls in the urn.
ncp non-centrality parameter
prob probability of success in each trial.
rate an alternative way to specify the scale; must be positive.
sd vector of standard deviations.
size number of trials (binom) or target for number of successful trials (nbinom).
shape shape parameter, must be positive (gamma, weibull)
shape1, shape2 positive parameters of the Beta distribution.
Details
The characteristic function of a random variable X is defined by
(t) = EeitX
Value
a complex number in rectangular (cartesian) coordinates.
Beta distribution
For the probability density function, see dbeta.
The characteristic function for central Beta is given by
(t) =1 F1 (; + , it)
where F is the confluent hypergeometric function calculated with kummerM in the fAsianOptions
package.
As of the time of this writing, we must calculate the characteristic function of the noncentral Beta
with numerical integration according to the definition.
Binomial distribution
For the probability mass function, see dbinom.
The characteristic function is given by
(t) = [peit + (1 p)]n
Cauchy Distribution
For the probability density function, see dcauchy.
The characteristic function is given by
(t) = e( it |t|)
Chi-square Distribution
For the probability density function, see dchisq.
The characteristic function is given by
it
exp( 12it )
(t) =
(1 2it)df /2
Exponential Distribution
For the probability density function, see dexp.
This is the special case of gamma when = 1.
F Distribution
For the probability density function, see df.
For the central F we use confluent hypergeometric function of the second kind, also known as
kummerU, from the fAsianOptions package.
For noncentral F we use confluent hypergeometric function of the first kind. See the vignette for
details.
8 CharFunc
Gamma Distribution
For the probability density function, see dgamma.
The characteristic function is given by
(t) = (1 it)( )
Geometric Distribution
For the probability mass function, see dgeom.
This is the special case of negative binomial when r = 1.
Hypergeometric Distribution
For the probability mass function, see dhyper.
The formula for the characteristic function is based on the Gaussian hypergeometric series, calcu-
lated with hypergeo in package hypergeo. It is too complicated to be included here; please see the
vignette.
Logistic Distribution
For the probability density function, see dlogis.
The characteristic function is given by
(t) = t/ sinh(t)
Lognormal Distribution
For the probability density function, see dlnorm.
This characteristic function is uniquely complicated and delicate, but there is a recent numerical
algorithm for computation due to Beaulieu (2008). See the vignette and the References.
Normal Distribution
For the probability density function, see dnorm.
The characteristic function is 2
2 /2
(t) = eit+t
Poisson Distribution
For the probability mass function, see dpois.
The characteristic function is it
1)
(t) = e(e
CharFunc 9
Students t Distribution
For the probability density function, see dt.
See the vignette for a formula for the characteristic function for central t.
As of the time of this writing, we must calculate the characteristic function of the noncentral t with
numerical integration according to the definition.
Weibull Distribution
For the probability density function, see dweibull.
We must at the time of this writing calculate the characteristic function with numerical integration
according to the definition.
Author(s)
G. Jay Kerns <[email protected]>.
Source
For clnorm a fast numerical algorithm is used that originated with and was published and commu-
nicated to me by N. C. Beaulieu: see
References
Abramowitz, M. and Stegun, I. A. (1972) Handbook of Mathematical Functions. New York: Dover.
Beaulieu, N.C. (2008) Fast convenient numerical computation of lognormal characteristic functions,
IEEE Transactions on Communications, Volume 56, Issue 3, 331333.
Hurst, S. (1995) The Characteristic Function of the Student-t Distribution, Financial Mathematics
Research Report No. FMRR006-95, Statistics Research Report No. SRR044-95.
Johnson, N. L., Kotz, S., and Kemp, A. W. (1992) Univariate Discrete Distributions, Second Edi-
tion. New York: Wiley.
10 countrep
Johnson, N. L., Kotz, S. and Balakrishnan, N. (1995) Continuous Univariate Distributions, volume
1. New York: Wiley.
Johnson, N. L., Kotz, S. and Balakrishnan, N. (1995) Continuous Univariate Distributions, volume
2. New York: Wiley.
Lukacs, E. (1970) Characteristic Functions, Second Edition. London: Griffin.
See Also
besselK kummerM kummerU hypergeo
Description
Counts the number of repetitions of vals in a given vector x.
Usage
countrep(x, ...)
## Default S3 method:
countrep(x, vals = unique(x), nrep = 2, ...)
Arguments
x an object in which repeats should be counted.
vals values that may be repeated.
nrep exact number of repeats desired, defaults to pairs.
... further arguments to be passed to or from other methods.
Details
This is a generic function, with methods supplied for data frames and vectors. The default behavior
counts the number of pairs of elements of x. One can find the number of triples, etc., by changing
the nrep argument. If there are specific values for which one is looking for repeats, these can be
specified with the vals argument. Note that the function only checks for exactly nrep instances, so
two pairs of a specific element would be counted as 0 pairs and 1 quadruple. See the examples.
The data frame method uses apply to apply countrep.default to each row of the data frame.
Value
If x is a vector, then the value is an integer. If x is a data frame then the value is a vector, with entries
the corresponding value for the respective rows of x
empirical 11
Author(s)
G. Jay Kerns <[email protected]>.
See Also
isrep
Examples
x <- c(3,3,2,2,3,3,4,4)
countrep(x) # one pair each of 2s and 4s
countrep(x, nrep = 4)
countrep(x, vals = 4) # one pair of 4s
Description
Calculates relative frequencies of the rows of a data frame.
Usage
empirical(x)
Arguments
x a data frame.
Details
The function works by adding a probs column to x with equally likely entries of 1/n, where n is the
number of rows. Then it aggregates the duplicated rows of x while accumulating the probabilities
associated with each.
Value
A data frame formed by aggregating the rows of x. A probs column is added giving the relative
frequencies of each of the rows.
Author(s)
G. Jay Kerns <[email protected]>.
See Also
sim
12 euchredeck
Examples
S <- tosscoin(2, makespace = TRUE)
sims <- sim(S, ntrials = 50000)
empirical(sims)
Description
Usage
Arguments
benny logical.
makespace logical.
Details
This is a conventional Euchre deck which uses a deck of 24 standard playing cards consisting of
Ace, King, Queen, Jack, 10, and 9 of each of the four suits. If benny = TRUE then a Joker is added
to the deck.
Value
A data frame with columns value and suit, and optionally a column of equally likely probs.
See Also
Examples
euchredeck()
euchredeck(benny = TRUE, makespace = TRUE)
gen2wayTable 13
Description
A function to randomly generate arbitrary two-way tables
Usage
gen2wayTable(n = sample(100:500, size = 1), pmatrix = matrix(1:12, nrow = 3),
dmnames = list(X = paste("x", 1:nrow(pmatrix), sep = ""),
Y = paste("y", 1:ncol(pmatrix), sep = "")),
addmargins = TRUE, as.df = FALSE, untable = TRUE)
Arguments
n sum total observations
pmatrix matrix of nonnegative weights for the probability distribution
dmnames names of the table dimensions
addmargins should margins be added?
as.df table will be returned as a data frame
untable should counts be untabled to single observation per row
Value
An object of class table containing the generated values.
Author(s)
G. Jay Kerns
Description
A function to generate a two-way table with independent margins
Usage
genIndepTable(n = sample(100:500, size = 1), prow = 1:3, pcol = 1:4,
dmnames = list(X = paste("x", 1:length(prow), sep = ""),
Y = paste("y", 1:length(pcol), sep = "")), addmargins = TRUE,
as.df = FALSE, untable = TRUE)
14 genLogRegData
Arguments
n sum total of observations generated
prow nonnegative weights for the row marginal distribution
pcol nonnegative weights for the col marginal distribution
dmnames names for the table dimensions
addmargins should margins be added to the table
as.df should the result be returned as a data frame
untable if true then data frame will be expanded to one observation per row
Details
This function will generate a two-way table with independent marginal distributions.
Value
Either an object of class table or a data frame.
Author(s)
G. Jay Kerns
Description
This function generates data ready for a logistic regression model
Usage
genLogRegData(xdata, beta = rep(1, ncol(xdata)), yname = "y")
Arguments
xdata the model matrix
beta vector of parameters to multiply the model matrix
yname the name for the generated y values
Details
This function generates data ready for a logistic regression model
Value
A data frame with the model matrix and the generated y values added
genXdata 15
Author(s)
G. Jay Kerns
Description
This function generates correlated normal data to serve as a model matrix in a regression model.
Usage
Arguments
Details
This function generates correlated normal data to serve as a model matrix in a regression model.
Value
Author(s)
G. Jay Kerns
16 iidspace
Description
Sets up a probability space corresponding to independent, identical experiments.
Usage
iidspace(x, ntrials, probs = NULL)
Arguments
x a vector of outcomes.
ntrials number of times to perform the experiment.
probs vector of non-negative weights corresponding to x.
Details
The elementary experiment to be repeated consists of drawing an element of x according to the
probabilities contained in probs. The entries of probs need not sum to one, but they will be
normalized before any computations. If probs is not specified, the equally likely model will be
assumed.
Value
A data frame, with a probs column, where probs is calculated to be the probability of observing
the outcome in its row under the assumption of independence and identical distribution of draws
from x.
Author(s)
G. Jay Kerns <[email protected]>.
See Also
probspace
Examples
iidspace( 1:6, ntrials = 3) # same as rolldie(3)
iidspace( 1:6, ntrials = 3, probs = 3:8 ) # unbalanced die
intersect 17
Description
Calculates the intersection of subsets of a probability space. Comparisons are made row-wise, so
that in the data frame case, intersect(A,B) is a data frame with those rows that are both in A and
in B.
Usage
intersect(x, ...)
## Default S3 method:
intersect(x, y, ...)
Arguments
x, y vectors, data frames, or ps objects containing a sequence of elements (concep-
tually).
... further arguments to be passed to or from other methods.
Details
This is a generic function, extended from the intersect function in the base package. The ele-
ments of intersect(x,y) are those elements in x and in y. The original definition is preserved in
the case that x and y are vectors of the same mode.
Value
A vector, data frame, or subset of a probability space of the same type as its arguments.
Author(s)
G. Jay Kerns <[email protected]>, based on a suggestion made by Brian Ripley in R-devel, 12/11/07.
See Also
union, setdiff
18 is.probspace
Examples
S <- cards()
A <- subset(S, suit == "Heart")
B <- subset(S, rank == "A" )
intersect(A, B)
Description
Decides whether a given object is a probability space.
Usage
is.probspace(x)
Arguments
x an object for which probability space status should be checked.
Details
It first checks if the class of the object includes ps, and if so TRUE is returned. If not, then it checks
that the object is a data frame and contains a probs column. Lastly, it checks whether all entries of
probs are nonnegative. Note that it does not check whether the sum of probs is one, to allow for
the possibility that the input object is a proper subset of a probability space.
Value
Logical.
Author(s)
G. Jay Kerns <[email protected]>.
See Also
probspace
Examples
S <- rolldie(3, makespace = TRUE)
is.probspace(S)
isin 19
Description
Usage
isin(x, ...)
## Default S3 method:
isin(x, y, ordered = FALSE, ...)
Arguments
x, y vectors.
ordered logical.
... further arguments to be passed to or from other methods.
Details
The function will only return TRUE if every element of y is present in the vector x, counting multi-
plicity. See the examples below. Of ordered = TRUE, then elements must be in the vector x in the
order specified in y. Compare this to the behavior of the %in% function in the base package.
This is a generic function with a moethod for data frames, which applies isin() to each row of the
data frame, with a vector as a result.
Value
Author(s)
See Also
isrep
20 isrep
Examples
x <- 1:10
y <- 3:7
z <- c(3,3,7)
isin(x,y)
isin(x,z)
isin(x, c(3,4,5), ordered = TRUE)
isin(x, c(3,5,4), ordered = TRUE)
S <- rolldie(4)
subset(S, isin(S, c(2,2,6), ordered = TRUE))
Description
Tests for a certain number of repetitions of vals in a given vector x.
Usage
isrep(x, ...)
## Default S3 method:
isrep(x, vals = unique(x), nrep = 2, ...)
Arguments
x an object with potential repeated values.
vals values that may be repeated.
nrep exact number of repeats desired, defaults to pairs.
... further arguments to be passed to or from other methods.
Details
This is a generic function, with methods supplied for data frames and vectors. The default behavior
tests for existence of pairs of elements of x. One can test existence of triples, etc., by changing
the nrep argument. If there are specific values for which one is looking for repeats, these can be
specified with the vals argument. Note that the function only checks for exactly nrep instances, so
two pairs of a specific element would be counted as 0 pairs and 1 quadruple. See the examples.
The data frame method uses apply to apply isrep.default to each row of the data frame.
marginal 21
Value
Logical.
Author(s)
G. Jay Kerns <[email protected]>.
See Also
countrep
Examples
x <- c(3,3,2,2,3,3,4,4)
isrep(x) # one pair each of 2s and 4s
isrep(x, nrep = 4)
isrep(x, vals = 4) # one pair of 4s
Description
Computes the marginal distribution of a set of variables.
Usage
marginal(space, vars = NULL)
Arguments
space a data frame probability space or a subset of one.
vars an optional character vector of variable names in space.
Details
If vars is not specified, then marginal() will set vars to be all non-probs columns, which can be
useful in the case that it is desired to aggregate duplicated rows.
Value
A data frame with a probs column.
Author(s)
G. Jay Kerns <[email protected]>.
22 noorder
See Also
See addrv for adding random variables to a data frame probability space.
Examples
S <- rolldie(3, makespace = TRUE)
marginal(S, vars = c("X1", "X2"))
Description
This function sorts the rows (outcomes) of a data frame probability space, effectively removing
the original order present) and aggregates the sorted rows into a new probability data frame with
unique, sorted outcomes.
Usage
noorder( space )
Arguments
space a data frame probability space or a subset of one.
Details
The data frame space must have at least two non-probs columns or an error will result.
Value
A data frame with a probs column and sorted, unique rows.
Author(s)
G. Jay Kerns <[email protected]>.
See Also
addrv, marginal
Examples
S <- tosscoin(3, makespace = TRUE)
noorder(S)
nsamp 23
Description
Calculates the number of samples from an urn under different sampling scenarios.
Usage
nsamp(n, k, replace = FALSE, ordered = FALSE)
Arguments
n an integer or integer vector.
k an integer or integer vector.
replace logical indicating whether sampling should be done with replacement.
ordered logical indicating whether order among samples is important.
Details
The nsamp() function will calculate the number of samples from an urn under assorted assumptions
on the sampling procedure. The arguments are: n, the number of (distinguishable) objects in the
urn, k, the sample size, and replace, ordered as documented in urnsamples.
nsamp() is vectorized, so that entering vectors instead of numbers for n, k, replace, and ordered
results in a vector of corresponding answers.
The formulas used in the four possible combinations of replace and ordered are as follows.
Value
A number.
Author(s)
G. Jay Kerns <[email protected]>.
See Also
urnsamples
24 permsn
Examples
nsamp(n=3, k=2, replace = TRUE, ordered = TRUE)
nsamp(n=3, k=2, replace = TRUE, ordered = FALSE)
nsamp(n=3, k=2, replace = FALSE, ordered = FALSE)
nsamp(n=3, k=2, replace = FALSE, ordered = TRUE)
Description
Generate all permutations of the elements of x taken m at a time. If x is a positive integer, returns
all permutations of the elements of seq(x) taken m at a time.
Usage
permsn(x, m)
Arguments
Value
Author(s)
G. Jay Kerns <[email protected]>, modified from the combn function in the package utils.
See Also
combn
Examples
permsn(3,2)
prob 25
Description
Calculates probability and conditional probability of events.
Usage
Prob(x, ...)
## Default S3 method:
Prob(x, event = NULL, given = NULL, ...)
Arguments
x a probability space or a subset of one.
event logical expression indicating elements or rows of space to keep: missing values
are taken as false.
given either a subset of a probability space or a logical expression indicating elements
or rows of space to keep: missing values are taken as false.
... further arguments to be passed to or from other methods.
Details
This function calculates the probability of events or subsets of a given sample space. Conditional
probability is also implemented. In essence, the Prob() function operates by summing the probs
column of its argument. It will find subsets on the fly if desired.
The event argument is used to define a subset of x, that is, the only outcomes used in the probability
calculation will be those that are elements of x and satisfy event simultaneously. In other words,
Prob(x,event) calculates Prob(intersect(x, subset(x, event))). Consequently, x should
be the entire probability space in the case that event is non-null.
There is some flexibility in the given argument in that it can be either a data frame or it can be a
logical expression that defines the subset. However, that flexibility is limited. In particular, if given
is a logical expression, then event must also be specified (also a logical expression). And in this
case, the argument x should be the entire sample space, not a subset thereof.
Value
A number in the interval [0,1].
Author(s)
G. Jay Kerns <[email protected]>.
26 probspace
See Also
probspace, iidspace
Examples
S <- rolldie(times = 3, makespace = TRUE )
Prob(S, X1+X2 > 9 )
Prob(S, X1+X2 > 9, given = X1+X2+X3 > 7 )
Description
Forms a probability space from a set of outcomes and (optional) vector of probabilities.
Usage
probspace(x, ...)
## Default S3 method:
probspace(x, probs, ...)
Arguments
x a vector, data frame, or list of outcomes.
probs a vector of non-negative weights of the same length as outcomes
... further arguments to be passed to or from other methods.
Details
The elements of probs will be normalized to ensure that their sum is one. If probs is not specified,
then the equally likely model is assumed in which every outcome has the same probability.
Value
If outcomes is a vector or data frame, then the value is a data frame with an added probs column.
If outcomes is a list, then the value is a list with components outcomes (the supplied list) and a
probs component.
Author(s)
G. Jay Kerns <[email protected]>.
rolldie 27
See Also
iidspace
Examples
R <- rolldie(3)
probspace(R)
Description
Sets up a sample space for the experiment of rolling a die repeatedly.
Usage
rolldie(times, nsides = 6, makespace = FALSE)
Arguments
times number of rolls.
nsides number of sides on the die.
makespace logical.
Details
The function uses expand.grid() to generate all possible rolls resulting from the experiment of
rolling a die. Sides on the die are 1:nsides. Columns of the data frame are called X1, X2, up to
Xtimes
Value
A data frame, with an equally likely probs column if makespace is TRUE.
Author(s)
G. Jay Kerns <[email protected]>.
See Also
tosscoin
Examples
rolldie(2)
rolldie(3, nsides = 4)
rolldie(3, nsides = 4, makespace = TRUE)
28 roulette
roulette Roulette
Description
Sets up a sample space for the experiment of spinning a roulette wheel once.
Usage
Arguments
european logical.
makespace logical.
Details
If european is TRUE, then a traditional EU roulette wheel with 37 pockets is used, otherwise, a
standard US roulette wheel with 38 pockets is used. Entries in the data frame are ordered in the
customary way to facilitate the calculation probabilities regarding called bets.
Value
A data frame, with columns num and color, and an equally likely probs column if makespace is
TRUE.
Author(s)
See Also
cards
Examples
roulette()
roulette(european = TRUE, makespace = TRUE)
setdiff 29
Description
Usage
setdiff(x, ...)
## Default S3 method:
setdiff(x, y, ...)
Arguments
Details
This function operates row-wise on dataframes, and element-wise among the outcomes of ps ob-
jects. The elements of setdiff(x,y) are those elements in x but not in y. The definition is taken
to match the version in the base package.
Value
A data frame or subset of a probability space of the same type as its arguments.
Author(s)
G. Jay Kerns <[email protected]>, essentially verbatim from a suggestion made by Brian Ripley on
R-devel, 12/11/07.
See Also
intersect, union
30 sim
Examples
S <- cards()
A <- subset(S, suit == "Heart")
B <- subset(S, rank == "A" )
setdiff(B, A)
Description
Simulates the experiment of drawing from a sample space.
Usage
sim(x, ...)
## Default S3 method:
sim(x, ntrials, ...)
Arguments
x a probability space or a subset of one.
ntrials number of times to repeat the experiment.
... further arguments to be passed to or from other methods.
Details
The sim() function is a wrapper for sample(), except that it strips the probs component from
the result and (if x is a data frame) renames the rownames of the data frame consecutively from
1:ntrials.
Value
A data frame if space is a data frame, or a list if space is of class ps.
Author(s)
G. Jay Kerns <[email protected]>.
See Also
empirical
subset 31
Examples
S <- cards(makespace = TRUE)
sim(S, ntrials = 5)
T <- urnsamples(S, 2)
U <- probspace(T)
sim(U, ntrials = 4)
Description
This is a method for subset() for the case when the input object is a probability space of class ps.
Usage
subset(x, ...)
Arguments
x a probability space.
subset logical expression indicating elements or rows of space to keep: missing values
are taken as false.
... further arguments to be passed to or from other methods.
Details
This function simply extends the existing subset() function to ps objects.
Value
A ps object, a subset of a probability space.
Author(s)
G. Jay Kerns <[email protected]>.
See Also
intersect, setdiff, union, isin
32 tosscoin
Examples
L <- tosscoin(2)
M <- urnsamples(L, 3)
N <- probspace(M)
subset(N, all(toss1=="H"))
subset(N, any(toss2=="T"))
Description
Sets up a sample space for the experiment of tossing a coin repeatedly with the outcomes "H" or
"T".
Usage
tosscoin( times, makespace = FALSE )
Arguments
times number of tosses.
makespace logical.
Details
The function uses expand.grid() to generate all possible sequences of flips resulting from the ex-
periment of tossing a coin. Columns of the dataframe are denoted toss1, toss2, up to tosstimes,
Value
A data frame, with an equally likely probs column if makespace is TRUE.
Author(s)
G. Jay Kerns <[email protected]>.
See Also
rolldie
Examples
tosscoin(2)
tosscoin(3, makespace = TRUE)
union 33
Description
Usage
union(x, ...)
## Default S3 method:
union(x, y, ...)
Arguments
Details
This function operates row-wise on dataframes, and element-wise among the outcomes of ps ob-
jects. The elements of union(x,y) are those elements in x or y, or both. The definition is taken to
match the version in the base package.
Value
A data frame or subset of a probability space of the same type as its arguments.
Author(s)
G. Jay Kerns <[email protected]>, based on a suggestion made by Brian Ripley in R-devel, 12/11/07.
See Also
intersect, setdiff
34 urnsamples
Examples
S <- cards()
A <- subset(S, suit == "Heart")
B <- subset(S, rank == "A" )
union(A, B)
Description
This function creates a sample space associated with the experiment of sampling distinguishable
objects from an urn.
Usage
urnsamples(x, ...)
## Default S3 method:
urnsamples(x, size, replace = FALSE, ordered = FALSE, ...)
Arguments
x a vector or data frame from which sampling should take place.
size number indicating the sample size.
replace logical indicating whether sampling should be done with replacement.
ordered logical indicating whether order among samples is important.
... further arguments to be passed to or from other methods.
Details
The function operates on the indices of the urn (or rows, in the case urn is a data frame). It then
takes those samples and substitutes back into urn to generate the entries of the data frame (or list,
respectively). In the case that urn has repeated values, the result will be repeated values in the
output.
Note that urnsamples strips x of any existing probs column before sampling.
Value
A data frame if urn is a vector, and a list if urn is a data frame.
urnsamples 35
Author(s)
G. Jay Kerns <[email protected]>.
See Also
iidspace, probspace nsamp
Examples
urnsamples(1:10, size = 5)
S <- cards()
urnsamples(S, size = 2)
Index
36
INDEX 37
dsignrank, 9
dt, 9
dunif, 9
dweibull, 9
dwilcox, 9
empirical, 11, 30
euchredeck, 12
gen2wayTable, 13
genIndepTable, 13
genLogRegData, 14
genXdata, 15
hypergeo, 8, 10
kummerM, 7, 10
kummerU, 7, 10
marginal, 21, 22
noorder, 22
nsamp, 23, 35
permsn, 24
Prob (prob), 25
prob, 25
prob-package, 2
Prob.default (prob), 25
Prob.ps (prob), 25
probspace, 16, 18, 26, 26, 35
rolldie, 5, 27, 32
roulette, 5, 12, 28