Title: | Elementary Probability on Finite Sample Spaces |
---|---|
Description: | Performs elementary probability calculations on finite sample spaces, which may be represented by data frames or lists. This package is meant to rescue some widely used functions from the archived 'prob' package (see <https://cran.r-project.org/src/contrib/Archive/prob/>). Functionality includes setting up sample spaces, counting tools, defining 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. Characteristic functions for all base R distributions are included. |
Authors: | G. Jay Kerns [aut, cph], Joe gr. Schlarmann [cre] |
Maintainer: | Joe gr. Schlarmann <[email protected]> |
License: | GPL (>= 3) |
Version: | 0.9.9 |
Built: | 2024-12-06 06:48:40 UTC |
Source: | CRAN |
Adds a column to a data frame probability space containing the values of a random variable computed from the existing columns of the space.
addrv(space, FUN = NULL, invars = NULL, name = NULL, ...)
addrv(space, FUN = NULL, invars = NULL, name = NULL, ...)
space |
a data frame with a |
FUN |
a function to be applied to each row of outcomes in |
invars |
a character vector indicating input columns of |
name |
an (optional) name to give the defined random variable. |
... |
an expression defining a random variable. |
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.
The input data frame with an additional column called name
.
G. Jay Kerns [email protected].
See transform
to add a column to a data frame of outcomes (not yet a probability space).
S <- rolldie(3, makespace = TRUE) addrv(S, sum, name = "Y") addrv(S, Z = X3 - X2)
S <- rolldie(3, makespace = TRUE) addrv(S, sum, name = "Y") addrv(S, Z = X3 - X2)
The title says it all.
cards(jokers = FALSE, makespace = FALSE)
cards(jokers = FALSE, makespace = FALSE)
jokers |
logical. Include jokers in the deck. |
makespace |
logical. Include a column of equally likely probabilities. |
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.
A data frame with columns rank
and suit
, and optionally a column of equally likely probs
.
G. Jay Kerns [email protected].
rolldie
, tosscoin
, and roulette
cards() cards(makespace = TRUE)
cards() cards(makespace = TRUE)
vals
in a given vector x
.Count Repetitions
Counts the number of repetitions of vals
in a given vector x
.
countrep(x, ...)
countrep(x, ...)
x |
an object in which repeats should be counted. |
... |
further arguments to be passed to or from other methods. |
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.
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
G. Jay Kerns [email protected].
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
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
vals
in a given vector x
.Count Repetitions
Counts the number of repetitions of vals
in a given vector x
.
## S3 method for class 'data.frame' countrep(x, ...)
## S3 method for class 'data.frame' countrep(x, ...)
x |
an object in which repeats should be counted. |
... |
further arguments to be passed to or from other methods. |
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.
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
G. Jay Kerns [email protected].
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
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
vals
in a given vector x
.Count Repetitions
Counts the number of repetitions of vals
in a given vector x
.
## Default S3 method: countrep(x, vals = unique(x), nrep = 2, ...)
## Default S3 method: countrep(x, vals = unique(x), nrep = 2, ...)
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. |
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.
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
G. Jay Kerns [email protected].
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
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
Calculates relative frequencies of the rows of a data frame.
empirical(x)
empirical(x)
x |
a data frame. |
The function works by adding a probs
column to x
with equally likely entries of , where
is the number of rows. Then it aggregates the duplicated rows of
x
while accumulating the probabilities associated with each.
A data frame formed by aggregating the rows of x
. A probs
column is added giving the relative frequencies of each of the rows.
G. Jay Kerns [email protected].
S <- tosscoin(2, makespace = TRUE) sims <- sim(S, ntrials = 50000) empirical(sims)
S <- tosscoin(2, makespace = TRUE) sims <- sim(S, ntrials = 50000) empirical(sims)
The title says it all.
euchredeck(benny = FALSE, makespace = FALSE)
euchredeck(benny = FALSE, makespace = FALSE)
benny |
logical. Include a Joker if |
makespace |
logical. Include a column of equally likely probabilities if |
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.
A data frame with columns value
and suit
, and optionally a column of equally likely probs
.
euchredeck() euchredeck(benny = TRUE, makespace = TRUE)
euchredeck() euchredeck(benny = TRUE, makespace = TRUE)
A function to randomly generate arbitrary two-way tables.
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 )
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 )
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 |
An object of class table containing the generated values.
G. Jay Kerns
A function to generate a two-way table with independent margins.
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 )
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 )
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 |
This function will generate a two-way table with independent marginal distributions.
Either an object of class table or a data frame.
G. Jay Kerns
This function generates data ready for a logistic regression model.
genLogRegData(xdata, beta = rep(1, ncol(xdata)), yname = "y")
genLogRegData(xdata, beta = rep(1, ncol(xdata)), yname = "y")
xdata |
the model matrix |
beta |
vector of parameters to multiply the model matrix |
yname |
the name for the generated y values |
This function generates data ready for a logistic regression model.
A data frame with the model matrix and the generated y values added.
G. Jay Kerns
This function generates correlated normal data to serve as a model matrix in a regression model.
genXdata( n, nvar = 1, mu = rep(0, nvar), Sigma = diag(length(mu)), varnames = paste("x", 1:length(mu), sep = ""), roundto = NULL )
genXdata( n, nvar = 1, mu = rep(0, nvar), Sigma = diag(length(mu)), varnames = paste("x", 1:length(mu), sep = ""), roundto = NULL )
n |
how many rows |
nvar |
how many columns |
mu |
the mean of the multivariate normal distribution |
Sigma |
the variance-covariance matrix of the normal distribution |
varnames |
how you would like the variables to be named in the result |
roundto |
number of places to round the generated values |
This function generates correlated normal data to serve as a model matrix in a regression model.
A data frame of generated data
G. Jay Kerns
Independent Identical Experiments Sets up a probability space corresponding to independent, identical experiments.
iidspace(x, ntrials, probs = NULL)
iidspace(x, ntrials, probs = NULL)
x |
a vector of outcomes |
ntrials |
number of times to perform the experiment. |
probs |
vector of non-negative weights corresponding to |
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.
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
.
G. Jay Kerns [email protected].
iidspace( 1:6, ntrials = 3) # same as rolldie(3) iidspace( 1:6, ntrials = 3, probs = 3:8 ) # unbalanced die
iidspace( 1:6, ntrials = 3) # same as rolldie(3) iidspace( 1:6, ntrials = 3, probs = 3:8 ) # unbalanced die
intersect(A,B)
is a data frame with those rows that are both in A
and in B
.Intersection of Subsets
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
.
intersect(x, ...)
intersect(x, ...)
x |
vectors, data frames, or |
... |
further arguments to be passed to or from other methods. |
This is a generic function, extended from the intersect
function in the base
package. The elements 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.
A vector, data frame, or subset of a probability space of the same type as its arguments.
G. Jay Kerns [email protected], based on a suggestion made by Brian Ripley in R
-devel, 12/11/07.
S <- cards() A <- subset(S, suit == "Heart") B <- subset(S, rank == "A" ) intersect(A, B)
S <- cards() A <- subset(S, suit == "Heart") B <- subset(S, rank == "A" ) intersect(A, B)
intersect(A,B)
is a data frame with those rows that are both in A
and in B
.Intersection of Subsets
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
.
## S3 method for class 'data.frame' intersect(x, y, ...)
## S3 method for class 'data.frame' intersect(x, y, ...)
x |
vectors, data frames, or |
y |
vectors, data frames, or |
... |
further arguments to be passed to or from other methods. |
This is a generic function, extended from the intersect
function in the base
package. The elements 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.
A vector, data frame, or subset of a probability space of the same type as its arguments.
G. Jay Kerns [email protected], based on a suggestion made by Brian Ripley in R
-devel, 12/11/07.
S <- cards() A <- subset(S, suit == "Heart") B <- subset(S, rank == "A" ) intersect(A, B)
S <- cards() A <- subset(S, suit == "Heart") B <- subset(S, rank == "A" ) intersect(A, B)
intersect(A,B)
is a data frame with those rows that are both in A
and in B
.Intersection of Subsets
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
.
## Default S3 method: intersect(x, y, ...)
## Default S3 method: intersect(x, y, ...)
x |
vectors, data frames, or |
y |
vectors, data frames, or |
... |
further arguments to be passed to or from other methods. |
This is a generic function, extended from the intersect
function in the base
package. The elements 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.
A vector, data frame, or subset of a probability space of the same type as its arguments.
G. Jay Kerns [email protected], based on a suggestion made by Brian Ripley in R
-devel, 12/11/07.
S <- cards() A <- subset(S, suit == "Heart") B <- subset(S, rank == "A" ) intersect(A, B)
S <- cards() A <- subset(S, suit == "Heart") B <- subset(S, rank == "A" ) intersect(A, B)
intersect(A,B)
is a data frame with those rows that are both in A
and in B
.Intersection of Subsets
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
.
## S3 method for class 'ps' intersect(x, y, ...)
## S3 method for class 'ps' intersect(x, y, ...)
x |
vectors, data frames, or |
y |
vectors, data frames, or |
... |
further arguments to be passed to or from other methods. |
This is a generic function, extended from the intersect
function in the base
package. The elements 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.
A vector, data frame, or subset of a probability space of the same type as its arguments.
G. Jay Kerns [email protected], based on a suggestion made by Brian Ripley in R
-devel, 12/11/07.
S <- cards() A <- subset(S, suit == "Heart") B <- subset(S, rank == "A" ) intersect(A, B)
S <- cards() A <- subset(S, suit == "Heart") B <- subset(S, rank == "A" ) intersect(A, B)
Testing for a Probability Space Decides whether a given object is a probability space.
is.probspace(x)
is.probspace(x)
x |
an object for which probability space status should be checked. |
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.
Logical.
G. Jay Kerns [email protected].
S <- rolldie(3, makespace = TRUE) is.probspace(S)
S <- rolldie(3, makespace = TRUE) is.probspace(S)
Test Whether One Vector Is In Another Vector
isin(x, ...)
isin(x, ...)
x |
vectors |
... |
further arguments to be passed to or from other methods. |
The function will only return TRUE
if every element of y
is present in the vector x
, counting multiplicity. 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 method for data frames, which applies isin()
to each row of the data frame, with a vector as a result.
Logical, or a vector of logicals.
G. Jay Kerns [email protected].
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))
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))
Test Whether One Vector Is In Another Vector
## S3 method for class 'data.frame' isin(x, ...)
## S3 method for class 'data.frame' isin(x, ...)
x |
vectors |
... |
further arguments to be passed to or from other methods. |
The function will only return TRUE
if every element of y
is present in the vector x
, counting multiplicity. 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 method for data frames, which applies isin()
to each row of the data frame, with a vector as a result.
Logical, or a vector of logicals.
G. Jay Kerns [email protected].
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))
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))
Test Whether One Vector Is In Another Vector
## Default S3 method: isin(x, y, ordered = FALSE, ...)
## Default S3 method: isin(x, y, ordered = FALSE, ...)
x |
vectors |
y |
vectors |
ordered |
logical |
... |
further arguments to be passed to or from other methods. |
The function will only return TRUE
if every element of y
is present in the vector x
, counting multiplicity. 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 method for data frames, which applies isin()
to each row of the data frame, with a vector as a result.
Logical, or a vector of logicals.
G. Jay Kerns [email protected].
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))
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))
vals
in a given vector x
.Is Repeated in a Vector
Tests for a certain number of repetitions of vals
in a given vector x
.
isrep(x, ...)
isrep(x, ...)
x |
an object with potential repeated values. |
... |
further arguments to be passed to or from other methods. |
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.
Logical.
G. Jay Kerns [email protected].
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
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
vals
in a given vector x
.Is Repeated in a Vector
Tests for a certain number of repetitions of vals
in a given vector x
.
## S3 method for class 'data.frame' isrep(x, ...)
## S3 method for class 'data.frame' isrep(x, ...)
x |
an object with potential repeated values. |
... |
further arguments to be passed to or from other methods. |
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.
Logical.
G. Jay Kerns [email protected].
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
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
vals
in a given vector x
.Is Repeated in a Vector
Tests for a certain number of repetitions of vals
in a given vector x
.
## Default S3 method: isrep(x, vals = unique(x), nrep = 2, ...)
## Default S3 method: isrep(x, vals = unique(x), nrep = 2, ...)
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. |
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.
Logical.
G. Jay Kerns [email protected].
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
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
Computes the marginal distribution of a set of variables.
marginal(space, vars = NULL)
marginal(space, vars = NULL)
space |
a data frame probability space or a subset of one. |
vars |
an optional character vector of variable names in |
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.
A data frame with a probs
column.
G. Jay Kerns [email protected].
See addrv
for adding random variables to a data frame probability space.
S <- rolldie(3, makespace = TRUE) marginal(S, vars = c("X1", "X2"))
S <- rolldie(3, makespace = TRUE) marginal(S, vars = c("X1", "X2"))
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.
noorder(space)
noorder(space)
space |
a data frame probability space or a subset of one. |
The data frame space
must have at least two non-probs
columns or an error will result.
A data frame with a probs
column and sorted, unique rows.
G. Jay Kerns [email protected].
S <- tosscoin(3, makespace = TRUE) noorder(S)
S <- tosscoin(3, makespace = TRUE) noorder(S)
Calculates the number of samples from an urn under different sampling scenarios.
nsamp(n, k, replace = FALSE, ordered = FALSE)
nsamp(n, k, replace = FALSE, ordered = FALSE)
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. |
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:
When replace = TRUE
and ordered = TRUE
, the value is .
When replace = FALSE
and ordered = TRUE
, the value is .
When replace = FALSE
and ordered = FALSE
, the value is .
When replace = TRUE
and ordered = FALSE
, the value is .
A number.
G. Jay Kerns [email protected].
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)
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)
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.
permsn(x, m)
permsn(x, m)
x |
vector source for permutations, or integer n for x <- seq(n). |
m |
number of elements to permute. |
a list or array (in nondegenerate cases).
G. Jay Kerns [email protected], modified from the combn
function in the package utils
.
permsn(3, 2)
permsn(3, 2)
Probability and Conditional Probability Calculates probability and conditional probability of events.
Prob(x, ...)
Prob(x, ...)
x |
a probability space or a subset of one. |
... |
further arguments to be passed to or from other methods. |
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.
A number in the interval [0,1]
.
G. Jay Kerns [email protected].
S <- rolldie(times = 3, makespace = TRUE ) Prob(S, X1+X2 > 9 ) Prob(S, X1+X2 > 9, given = X1+X2+X3 > 7 )
S <- rolldie(times = 3, makespace = TRUE ) Prob(S, X1+X2 > 9 ) Prob(S, X1+X2 > 9, given = X1+X2+X3 > 7 )
Probability and Conditional Probability Calculates probability and conditional probability of events.
## Default S3 method: Prob(x, event = NULL, given = NULL, ...)
## Default S3 method: Prob(x, event = NULL, given = NULL, ...)
x |
a probability space or a subset of one. |
event |
logical expression indicating elements or rows of |
given |
either a subset of a probability space or a logical expression indicating elements or rows of |
... |
further arguments to be passed to or from other methods. |
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.
A number in the interval [0,1]
.
G. Jay Kerns [email protected].
S <- rolldie(times = 3, makespace = TRUE ) Prob(S, X1+X2 > 9 ) Prob(S, X1+X2 > 9, given = X1+X2+X3 > 7 )
S <- rolldie(times = 3, makespace = TRUE ) Prob(S, X1+X2 > 9 ) Prob(S, X1+X2 > 9, given = X1+X2+X3 > 7 )
Probability and Conditional Probability Calculates probability and conditional probability of events.
## S3 method for class 'ps' Prob(x, event = NULL, given = NULL, ...)
## S3 method for class 'ps' Prob(x, event = NULL, given = NULL, ...)
x |
a probability space or a subset of one. |
event |
logical expression indicating elements or rows of |
given |
either a subset of a probability space or a logical expression indicating elements or rows of |
... |
further arguments to be passed to or from other methods. |
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.
A number in the interval [0,1]
.
G. Jay Kerns [email protected].
S <- rolldie(times = 3, makespace = TRUE ) Prob(S, X1+X2 > 9 ) Prob(S, X1+X2 > 9, given = X1+X2+X3 > 7 )
S <- rolldie(times = 3, makespace = TRUE ) Prob(S, X1+X2 > 9 ) Prob(S, X1+X2 > 9, given = X1+X2+X3 > 7 )
Probability Spaces Forms a probability space from a set of outcomes and (optional) vector of probabilities.
probspace(x, ...)
probspace(x, ...)
x |
a vector, data frame, or list of outcomes. |
... |
further arguments to be passed to or from other methods. |
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.
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.
G. Jay Kerns [email protected].
R <- rolldie(3) probspace(R)
R <- rolldie(3) probspace(R)
Probability Spaces Forms a probability space from a set of outcomes and (optional) vector of probabilities.
## Default S3 method: probspace(x, probs, ...)
## Default S3 method: probspace(x, probs, ...)
x |
a vector, data frame, or list of outcomes. |
probs |
a vector of non-negative weights of the same length as |
... |
further arguments to be passed to or from other methods. |
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.
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.
G. Jay Kerns [email protected].
R <- rolldie(3) probspace(R)
R <- rolldie(3) probspace(R)
Probability Spaces Forms a probability space from a set of outcomes and (optional) vector of probabilities.
## S3 method for class 'list' probspace(x, probs, ...)
## S3 method for class 'list' probspace(x, probs, ...)
x |
a vector, data frame, or list of outcomes. |
probs |
a vector of non-negative weights of the same length as |
... |
further arguments to be passed to or from other methods. |
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.
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.
G. Jay Kerns [email protected].
R <- rolldie(3) probspace(R)
R <- rolldie(3) probspace(R)
Sets up a sample space for the experiment of rolling a die repeatedly.
rolldie(times, nsides = 6, makespace = FALSE)
rolldie(times, nsides = 6, makespace = FALSE)
times |
number of rolls. |
nsides |
number of sides on the die. |
makespace |
logical. Include a column of equally likely probabilities if |
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
.
A data frame, with an equally likely probs
column if makespace
is TRUE
.
G. Jay Kerns [email protected].
rolldie(2) rolldie(3, nsides = 4) rolldie(3, nsides = 4, makespace = TRUE)
rolldie(2) rolldie(3, nsides = 4) rolldie(3, nsides = 4, makespace = TRUE)
Sets up a sample space for the experiment of spinning a roulette wheel once.
roulette(european = FALSE, makespace = FALSE)
roulette(european = FALSE, makespace = FALSE)
european |
logical. Use a European roulette wheel with 37 pockets if |
makespace |
logical. Include a column of equally likely probabilities if |
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.
A data frame, with columns num
and color
, and an equally likely probs
column if makespace
is TRUE
.
G. Jay Kerns [email protected].
roulette() roulette(european = TRUE, makespace = TRUE)
roulette() roulette(european = TRUE, makespace = TRUE)
Set Difference of Subsets Calculates the (nonsymmetric) set difference of subsets of a probability space.
setdiff(x, ...)
setdiff(x, ...)
x |
vectors, data frames, or |
... |
further arguments to be passed to or from other methods. |
This function operates row-wise on dataframes, and element-wise among the outcomes of ps
objects. 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.
A data frame or subset of a probability space of the same type as its arguments.
G. Jay Kerns [email protected], essentially verbatim from a suggestion made by Brian Ripley on R
-devel, 12/11/07
S <- cards() A <- subset(S, suit == "Heart") B <- subset(S, rank == "A" ) setdiff(B, A)
S <- cards() A <- subset(S, suit == "Heart") B <- subset(S, rank == "A" ) setdiff(B, A)
Set Difference of Subsets Calculates the (nonsymmetric) set difference of subsets of a probability space.
## S3 method for class 'data.frame' setdiff(x, y, ...)
## S3 method for class 'data.frame' setdiff(x, y, ...)
x |
vectors, data frames, or |
y |
vectors, data frames, or |
... |
further arguments to be passed to or from other methods. |
This function operates row-wise on dataframes, and element-wise among the outcomes of ps
objects. 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.
A data frame or subset of a probability space of the same type as its arguments.
G. Jay Kerns [email protected], essentially verbatim from a suggestion made by Brian Ripley on R
-devel, 12/11/07
S <- cards() A <- subset(S, suit == "Heart") B <- subset(S, rank == "A" ) setdiff(B, A)
S <- cards() A <- subset(S, suit == "Heart") B <- subset(S, rank == "A" ) setdiff(B, A)
Set Difference of Subsets Calculates the (nonsymmetric) set difference of subsets of a probability space.
## Default S3 method: setdiff(x, y, ...)
## Default S3 method: setdiff(x, y, ...)
x |
vectors, data frames, or |
y |
vectors, data frames, or |
... |
further arguments to be passed to or from other methods. |
This function operates row-wise on dataframes, and element-wise among the outcomes of ps
objects. 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.
A data frame or subset of a probability space of the same type as its arguments.
G. Jay Kerns [email protected], essentially verbatim from a suggestion made by Brian Ripley on R
-devel, 12/11/07
S <- cards() A <- subset(S, suit == "Heart") B <- subset(S, rank == "A" ) setdiff(B, A)
S <- cards() A <- subset(S, suit == "Heart") B <- subset(S, rank == "A" ) setdiff(B, A)
Set Difference of Subsets Calculates the (nonsymmetric) set difference of subsets of a probability space.
## S3 method for class 'ps' setdiff(x, y, ...)
## S3 method for class 'ps' setdiff(x, y, ...)
x |
vectors, data frames, or |
y |
vectors, data frames, or |
... |
further arguments to be passed to or from other methods. |
This function operates row-wise on dataframes, and element-wise among the outcomes of ps
objects. 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.
A data frame or subset of a probability space of the same type as its arguments.
G. Jay Kerns [email protected], essentially verbatim from a suggestion made by Brian Ripley on R
-devel, 12/11/07
S <- cards() A <- subset(S, suit == "Heart") B <- subset(S, rank == "A" ) setdiff(B, A)
S <- cards() A <- subset(S, suit == "Heart") B <- subset(S, rank == "A" ) setdiff(B, A)
Simulates the experiment of drawing from a sample space.
Simulates the experiment of drawing from a sample space.
Simulates the experiment of drawing from a sample space.
sim(x, ...) ## Default S3 method: sim(x, ntrials, ...) ## S3 method for class 'ps' sim(x, ntrials, ...)
sim(x, ...) ## Default S3 method: sim(x, ntrials, ...) ## S3 method for class 'ps' sim(x, ntrials, ...)
x |
a probability space or a subset of one. |
... |
further arguments to be passed to or from other methods. |
ntrials |
number of times to repeat the experiment. |
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
.
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
.
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
.
A data frame if space
is a data frame, or a list if space
is of class ps
.
A data frame if space
is a data frame, or a list if space
is of class ps
.
A data frame if space
is a data frame, or a list if space
is of class ps
.
G. Jay Kerns [email protected].
S <- cards(makespace = TRUE) sim(S, ntrials = 5) T <- urnsamples(S, 2) U <- probspace(T) sim(U, ntrials = 4) S <- cards(makespace = TRUE) sim(S, ntrials = 5) T <- urnsamples(S, 2) U <- probspace(T) sim(U, ntrials = 4) S <- cards(makespace = TRUE) sim(S, ntrials = 5) T <- urnsamples(S, 2) U <- probspace(T) sim(U, ntrials = 4)
S <- cards(makespace = TRUE) sim(S, ntrials = 5) T <- urnsamples(S, 2) U <- probspace(T) sim(U, ntrials = 4) S <- cards(makespace = TRUE) sim(S, ntrials = 5) T <- urnsamples(S, 2) U <- probspace(T) sim(U, ntrials = 4) S <- cards(makespace = TRUE) sim(S, ntrials = 5) T <- urnsamples(S, 2) U <- probspace(T) sim(U, ntrials = 4)
subset()
for the case when the input object is a probability space of class ps
.Subsets of Probability Spaces
This is a method for subset()
for the case when the input object is a probability space of class ps
.
## S3 method for class 'ps' subset(x, subset, ...)
## S3 method for class 'ps' subset(x, subset, ...)
x |
a probability space. |
subset |
logical expression indicating elements or rows of |
... |
further arguments to be passed to or from other methods. |
This function simply extends the existing subset()
function to ps
objects.
A ps
object, a subset of a probability space.
G. Jay Kerns [email protected].
L <- tosscoin(2) M <- urnsamples(L, 3) N <- probspace(M) subset(N, all(toss1=="H")) subset(N, any(toss2=="T"))
L <- tosscoin(2) M <- urnsamples(L, 3) N <- probspace(M) subset(N, all(toss1=="H")) subset(N, any(toss2=="T"))
Sets up a sample space for the experiment of tossing a coin repeatedly with the outcomes "H" or "T".
tosscoin(times, makespace = FALSE)
tosscoin(times, makespace = FALSE)
times |
number of tosses. |
makespace |
logical. Include a column of equally likely probabilities if |
The function uses expand.grid()
to generate all possible sequences of flips resulting from the experiment of tossing a coin. Columns of the dataframe are denoted toss1
, toss2
, up to tosstimes
.
A data frame, with an equally likely probs
column if makespace
is TRUE
.
G. Jay Kerns [email protected].
tosscoin(2) tosscoin(3, makespace = TRUE)
tosscoin(2) tosscoin(3, makespace = TRUE)
Union of Subsets Calculates the union of subsets of a probability space.
union(x, ...)
union(x, ...)
x |
vectors, data frames, or |
... |
further arguments to be passed to or from other methods. |
This function operates row-wise on dataframes, and element-wise among the outcomes of ps
objects. 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.
A data frame or subset of a probability space of the same type as its arguments.
G. Jay Kerns [email protected], based on a suggestion made by Brian Ripley in R
-devel, 12/11/07.
S <- cards() A <- subset(S, suit == "Heart") B <- subset(S, rank == "A" ) union(A, B)
S <- cards() A <- subset(S, suit == "Heart") B <- subset(S, rank == "A" ) union(A, B)
Union of Subsets Calculates the union of subsets of a probability space.
## S3 method for class 'data.frame' union(x, y, ...)
## S3 method for class 'data.frame' union(x, y, ...)
x |
vectors, data frames, or |
y |
vectors, data frames, or |
... |
further arguments to be passed to or from other methods. |
This function operates row-wise on dataframes, and element-wise among the outcomes of ps
objects. 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.
A data frame or subset of a probability space of the same type as its arguments.
G. Jay Kerns [email protected], based on a suggestion made by Brian Ripley in R
-devel, 12/11/07.
S <- cards() A <- subset(S, suit == "Heart") B <- subset(S, rank == "A" ) union(A, B)
S <- cards() A <- subset(S, suit == "Heart") B <- subset(S, rank == "A" ) union(A, B)
Union of Subsets Calculates the union of subsets of a probability space.
## Default S3 method: union(x, y, ...)
## Default S3 method: union(x, y, ...)
x |
vectors, data frames, or |
y |
vectors, data frames, or |
... |
further arguments to be passed to or from other methods. |
This function operates row-wise on dataframes, and element-wise among the outcomes of ps
objects. 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.
A data frame or subset of a probability space of the same type as its arguments.
G. Jay Kerns [email protected], based on a suggestion made by Brian Ripley in R
-devel, 12/11/07.
S <- cards() A <- subset(S, suit == "Heart") B <- subset(S, rank == "A" ) union(A, B)
S <- cards() A <- subset(S, suit == "Heart") B <- subset(S, rank == "A" ) union(A, B)
Union of Subsets Calculates the union of subsets of a probability space.
## S3 method for class 'ps' union(x, y, ...)
## S3 method for class 'ps' union(x, y, ...)
x |
vectors, data frames, or |
y |
vectors, data frames, or |
... |
further arguments to be passed to or from other methods. |
This function operates row-wise on dataframes, and element-wise among the outcomes of ps
objects. 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.
A data frame or subset of a probability space of the same type as its arguments.
G. Jay Kerns [email protected], based on a suggestion made by Brian Ripley in R
-devel, 12/11/07.
S <- cards() A <- subset(S, suit == "Heart") B <- subset(S, rank == "A" ) union(A, B)
S <- cards() A <- subset(S, suit == "Heart") B <- subset(S, rank == "A" ) union(A, B)
Sampling from Urns This function creates a sample space associated with the experiment of sampling distinguishable objects from an urn.
urnsamples(x, ...)
urnsamples(x, ...)
x |
a vector or data frame from which sampling should take place. |
... |
further arguments to be passed to or from other methods. |
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.
A data frame if urn
is a vector, and a list if urn
is a data frame.
G. Jay Kerns [email protected].
urnsamples(1:10, size = 5) S <- cards() urnsamples(S, size = 2)
urnsamples(1:10, size = 5) S <- cards() urnsamples(S, size = 2)
Sampling from Urns This function creates a sample space associated with the experiment of sampling distinguishable objects from an urn.
## S3 method for class 'data.frame' urnsamples(x, size, replace = FALSE, ordered = FALSE, ...)
## S3 method for class 'data.frame' urnsamples(x, size, replace = FALSE, ordered = FALSE, ...)
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. |
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.
A data frame if urn
is a vector, and a list if urn
is a data frame.
G. Jay Kerns [email protected].
urnsamples(1:10, size = 5) S <- cards() urnsamples(S, size = 2)
urnsamples(1:10, size = 5) S <- cards() urnsamples(S, size = 2)
Sampling from Urns This function creates a sample space associated with the experiment of sampling distinguishable objects from an urn.
## Default S3 method: urnsamples(x, size, replace = FALSE, ordered = FALSE, ...)
## Default S3 method: urnsamples(x, size, replace = FALSE, ordered = FALSE, ...)
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. |
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.
A data frame if urn
is a vector, and a list if urn
is a data frame.
G. Jay Kerns [email protected].
urnsamples(1:10, size = 5) S <- cards() urnsamples(S, size = 2)
urnsamples(1:10, size = 5) S <- cards() urnsamples(S, size = 2)