Title: | A Package for Learning Statistics |
---|---|
Description: | Some simple objects and functions to do statistics using linear models and a Bayesian framework. |
Authors: | Petter Mostad <[email protected]> |
Maintainer: | Petter Mostad <[email protected]> |
License: | GPL-2 |
Version: | 1.9 |
Built: | 2024-11-13 02:57:13 UTC |
Source: | https://github.com/cran/lestat |
The package contains a number of simple functions which can be combined to implement simple Bayesian computations.
Package: | lestat |
Type: | Package |
Version: | 1.6 |
Date: | 2011-12-15 |
License: | GPL-2 |
LazyLoad: | yes |
With this package, functions can be used to create objects representing probability distributions of many different types. These distributions can then be transformed and combined in different ways, representing statistical modelling. The result is an object-oriented way to do Bayesian computation with R.
Petter Mostad <[email protected]>
Please see more information at www.math.chalmers.se/~mostad/
prior <- normalexpgamma() #Generate a two-parameter flat prior full <- linearpredict(prior, rep(1, 7)) #Extend with normal distribution data <- runif(7) #Generate data posterior <- conditional(full, 1:7, data) #Condition on parameters credibilityinterval(marginal(posterior, 1)) #Investigate posterior
prior <- normalexpgamma() #Generate a two-parameter flat prior full <- linearpredict(prior, rep(1, 7)) #Extend with normal distribution data <- runif(7) #Generate data posterior <- conditional(full, 1:7, data) #Condition on parameters credibilityinterval(marginal(posterior, 1)) #Investigate posterior
Given data and a matrix describing a design for a linear model, the function creates an ANOVA table, using sums of squares based on a subdivision of the columns of the design matrix given as the third argument for the function.
anovatable(data, design, subdivisions = c(1, dim(design)[2] - 1))
anovatable(data, design, subdivisions = c(1, dim(design)[2] - 1))
data |
A vector with data values |
design |
A matrix with the same number of rows as there are data values. The matrix represents the design matrix for the linear model the ANOVA table is based on. |
subdivisions |
A vector of integers summing to the number of columns in the design matrix. The number of rows of the ANOVA table will be equal to the length of this vector. |
An ANOVA table.
Petter Mostad <[email protected]>
data1 <- simulate(normal(2.7, log(0.7)), 3) data2 <- simulate(normal(4.0, log(0.7)), 5) data3 <- simulate(normal(3.2, log(0.7)), 3) data4 <- simulate(normal(4.1, log(0.7)), 4) anovatable(c(data1, data2, data3, data4), designManyGroups(c(3,5,3,4)))
data1 <- simulate(normal(2.7, log(0.7)), 3) data2 <- simulate(normal(4.0, log(0.7)), 5) data3 <- simulate(normal(3.2, log(0.7)), 3) data4 <- simulate(normal(4.1, log(0.7)), 4) anovatable(c(data1, data2, data3, data4), designManyGroups(c(3,5,3,4)))
Create an object representing a Beta-Binomial distribution. This can be used for a Binomial distribution where there is uncertainty about the probability of success, and this uncertainty is represented by a Beta distribution.
betabinomial(n, alpha, beta)
betabinomial(n, alpha, beta)
n |
the number of trials in the binomial distribution (a positive integer). |
alpha |
the alpha parameter of the underlying Beta distribution. |
beta |
the beta parameter of the underlying Beta distribution. |
An object of class "betabinomial" and class "probabilitydistribution".
Petter Mostad <[email protected]>
betadistribution
,
binomialdistribution
,
binomialbeta
dist <- betabinomial(10, 5.5, 3.3) cdf(dist, 3)
dist <- betabinomial(10, 5.5, 3.3) cdf(dist, 3)
Create an object representing a Beta distribution.
betadistribution(alpha, beta)
betadistribution(alpha, beta)
alpha |
The alpha parameter. |
beta |
The beta parameter. |
A Beta probability distribution.
Petter Mostad <[email protected]>
dist <- betadistribution(4, 6) plot(dist)
dist <- betadistribution(4, 6) plot(dist)
Create an object representing a bivariate distribution, where the first variable is marginally Beta distributed, and the second variable is binomially distributed with probability given by the first variable.
binomialbeta(n, alpha, beta)
binomialbeta(n, alpha, beta)
n |
the number of trials in the binomial distribution (a positive integer). |
alpha |
the alpha parameter of the Binomial distribution. |
beta |
the beta parameter of the Binomial distribution. |
An object of class "binomialbeta" and class "probabilitydistribution".
Petter Mostad <[email protected]>
betadistribution
,
binomialdistribution
,
betabinomial
dist <- binomialbeta(10, 5.5, 12.3) plot(dist)
dist <- binomialbeta(10, 5.5, 12.3) plot(dist)
Create an object representing a Binomial distribution
binomialdistribution(ntrials, probability)
binomialdistribution(ntrials, probability)
ntrials |
the number of trials in the binomial distribution (a positive integer). |
probability |
the probability for success in each trial (a number between 0 and 1). |
An object of class "binomialdistribution" and class "probabilitydistribution".
Petter Mostad <[email protected]>
dist <- binomialdistribution(10, 0.4) cdf(dist, 3)
dist <- binomialdistribution(10, 0.4) cdf(dist, 3)
Compute the value of the cumulative distribution function for univariate distributions.
cdf(object, val)
cdf(object, val)
object |
A univariate probability distribution. |
val |
The probability less than or equal to |
The probability that a variable with distribution object
is
less than or equal to val
.
Petter Mostad <[email protected]>
cdf(normal(3, 2), 1)
cdf(normal(3, 2), 1)
The command can be used to generate a new distribution from an old, which is given as the first argument. The new distribution has the old as the marginal for the first variable. The conditional distribution for the second variable is specified with the remaining arguments.
compose(object, type, ...)
compose(object, type, ...)
object |
A probability distribution |
type |
A text string specifying the type of the conditional distribution given the old distribution. |
... |
Additional arguments specifying the conditional distribution. |
Depends on the input; may be a multivariate discrete distribution, or a Binomialbeta distribution.
Petter Mostad <[email protected]>
joint <- compose(uniformdistribution(), "binomialdistribution", 5) joint2 <- compose(discretedistribution(1:6), "discretedistribution", 1:6, matrix(c(1:36), 6, 6))
joint <- compose(uniformdistribution(), "binomialdistribution", 5) joint2 <- compose(discretedistribution(1:6), "discretedistribution", 1:6, matrix(c(1:36), 6, 6))
Given a multivariate distribution, the conditional distribution is computed when the variables with the given indices are set to the given values.
conditional(object, v, val)
conditional(object, v, val)
object |
A multivariate probability distribution. |
v |
A vector of the indices of the variables whose values should be fixed. |
val |
A vector, of the same length as |
An object representing the conditional probability distribution.
Petter Mostad <[email protected]>
prior <- normalexpgamma() #Generate a two-parameter flat prior full <- linearpredict(prior, rep(1, 7)) #Normal extension data <- simulate(uniformdistribution(), 7) #Generate data posterior <- conditional(full, 1:7, data) #Condition on parameters credibilityinterval(marginal(posterior, 1)) #Investigate posterior
prior <- normalexpgamma() #Generate a two-parameter flat prior full <- linearpredict(prior, rep(1, 7)) #Normal extension data <- simulate(uniformdistribution(), 7) #Generate data posterior <- conditional(full, 1:7, data) #Condition on parameters credibilityinterval(marginal(posterior, 1)) #Investigate posterior
For some distributions, like the multivariate Normal-ExpGamma and the multivariate Normal-Gamma, a new distribution is constructed from a linear combination of all but the last variables, and the last variable.
contrast(object, v)
contrast(object, v)
object |
A multivariate Normal-ExpGamma distribution or multivariate Normal-Gamma distribution. |
v |
A vector specifying the linear combination. |
A Normal-ExpGamma distribution or a Normal-Gamma distribution, depending on the input.
Petter Mostad <[email protected]>
data1 <- simulate(normal(13, log(0.4)), 3) data2 <- simulate(normal(14, log(0.4)), 5) data3 <- simulate(normal(12, log(0.4)), 6) dist <- linearmodel(c(data1, data2, data3), designManyGroups(c(3,5,6))) diff <- contrast(dist, c(0, 1, -1)) credibilityinterval(marginal(diff, 1))
data1 <- simulate(normal(13, log(0.4)), 3) data2 <- simulate(normal(14, log(0.4)), 5) data3 <- simulate(normal(12, log(0.4)), 6) dist <- linearmodel(c(data1, data2, data3), designManyGroups(c(3,5,6))) diff <- contrast(dist, c(0, 1, -1)) credibilityinterval(marginal(diff, 1))
Given a univariate continuous distribution, a credibility interval is computed. Note that the interval is constructed so that there is an equal probability to be above or below the interval.
credibilityinterval(object, prob = 0.95)
credibilityinterval(object, prob = 0.95)
object |
A univariate continuous distribution. |
prob |
The probability inside the credibility interval. |
A vector of length two, specifying the interval.
Petter Mostad <[email protected]>
credibilityinterval(normal())
credibilityinterval(normal())
The function creates a design matrix suitable for analyzing results from
an experiment where a set of factors are analysed in a balanced design:
The argument factors
lists the number of levels of each factor, and
each possible combination of levels of factors is tried out a number of times
given by replications
.
designBalanced(factors, replications = 1, interactions = FALSE)
designBalanced(factors, replications = 1, interactions = FALSE)
factors |
A vector of integers, listing the number of levels of each of the factors. |
replications |
An integer: The number of times each combination of factor levels is tried out. |
interactions |
If true, the design matrix will include columns for all possible interactions of the factors. |
A matrix where the number of rows equals the product of factors
and replications
.
The matrix will have only 0's and 1's as values.
Petter Mostad <[email protected]>
designFactorial
, designOneGroup
, designTwoGroups
, designManyGroups
designBalanced(c(3, 3), 2)
designBalanced(c(3, 3), 2)
The function creates a design matrix suitable for analyzing results from a factorial experiment where all factors have two levels.
designFactorial(nfactors, replications = 1, interactions = FALSE)
designFactorial(nfactors, replications = 1, interactions = FALSE)
nfactors |
The number of two-level factors in the experiment. |
replications |
The number of replications at each combination of factor levels. |
interactions |
If TRUE, columns will be included representing the interactions between all the factors. |
A matrix where the number of rows is , where
is the number of factors and
is the number of replications. The entries are -1's and 1's.
Petter Mostad <[email protected]>
designBalanced
, designOneGroup
, designTwoGroups
, designManyGroups
designFactorial(3,2)
designFactorial(3,2)
A design matrix is created, to be used for the analysis of data assumed to come from several normal distributions.
designManyGroups(v)
designManyGroups(v)
v |
A vector of integers, indicating how many observations there are in each group. |
A matrix consisting of 0's and 1's. The number of columns is equal to the
length of v
. The number of rows is equal to the sum of teh values of
v
.
Petter Mostad <[email protected]>
designOneGroup
, designTwoGroups
, designBalanced
, designFactorial
data1 <- simulate(normal(3.3, log(2)), 9) data2 <- simulate(normal(4.5, log(2)), 8) data3 <- simulate(normal(2.9, log(2)), 7) design <- designManyGroups(c(9,8,7)) posterior <- linearmodel(c(data1, data2, data3), design) plot(posterior)
data1 <- simulate(normal(3.3, log(2)), 9) data2 <- simulate(normal(4.5, log(2)), 8) data3 <- simulate(normal(2.9, log(2)), 7) design <- designManyGroups(c(9,8,7)) posterior <- linearmodel(c(data1, data2, data3), design) plot(posterior)
A design matrix is created, to be used for the analysis of data assumed to come from one normal distribution.
designOneGroup(n)
designOneGroup(n)
n |
The number of data values. |
A matrix consisting only of 1's, with one column and with the number of rows
given by n
.
Petter Mostad <[email protected]>
designTwoGroups
, designManyGroups
, designBalanced
, designFactorial
data <- simulate(normal(4, log(1.3)), 9) design <- designOneGroup(9) posterior <- linearmodel(data, design) credibilityinterval(marginal(posterior, 1))
data <- simulate(normal(4, log(1.3)), 9) design <- designOneGroup(9) posterior <- linearmodel(data, design) credibilityinterval(marginal(posterior, 1))
A design matrix is created, to be used for the analysis of data assumed to come from two normal distributions.
designTwoGroups(n, m)
designTwoGroups(n, m)
n |
The number of data values in the first group. |
m |
The number of data values in the second group. |
A matrix consisting of 1's and 0's, with two columns, and with the number
of rows given by .
Petter Mostad <[email protected]>
designOneGroup
, designManyGroups
, designBalanced
, designFactorial
data1 <- simulate(normal(3, log(2)), 7) data2 <- simulate(normal(5, log(2)), 9) design <- designTwoGroups(7,9) posterior <- linearmodel(c(data1, data2), design) credibilityinterval(marginal(posterior, 1))
data1 <- simulate(normal(3, log(2)), 7) data2 <- simulate(normal(5, log(2)), 9) design <- designTwoGroups(7,9) posterior <- linearmodel(c(data1, data2), design) credibilityinterval(marginal(posterior, 1))
Given two univariate distributions, an attempt is made to create the (approximate) difference between these.
difference(object1, object2)
difference(object1, object2)
object1 |
A univariate normal or tdistribution. |
object2 |
A univariate normal or tdistribution. |
A univariate normal or tdistribution, as appropriate.
Petter Mostad <[email protected]>
data1 <- simulate(normal(8, log(1.5)), 6) posterior1 <- marginal(linearmodel(data1, designOneGroup(6)), 1) data2 <- simulate(normal(10, log(2.8)), 7) posterior2 <- marginal(linearmodel(data2, designOneGroup(7)), 1) posterior <- difference(posterior1, posterior2) credibilityinterval(posterior)
data1 <- simulate(normal(8, log(1.5)), 6) posterior1 <- marginal(linearmodel(data1, designOneGroup(6)), 1) data2 <- simulate(normal(10, log(2.8)), 7) posterior2 <- marginal(linearmodel(data2, designOneGroup(7)), 1) posterior <- difference(posterior1, posterior2) credibilityinterval(posterior)
An object representing a discrete distribution is created, based on explicitly given possible values and probabilities for these.
discretedistribution(vals, probs = rep(1, length(vals)))
discretedistribution(vals, probs = rep(1, length(vals)))
vals |
A vector listing the possible values of the discrete distribution. |
probs |
If given, |
A discrete probability distribution.
Petter Mostad <[email protected]>
dist <- discretedistribution(1:10) expectation(dist) variance(dist)
dist <- discretedistribution(1:10) expectation(dist) variance(dist)
Compute the expectation of a probability distribution.
expectation(object)
expectation(object)
object |
A probability distribution. |
The expectation of the probability distribution.
Petter Mostad <[email protected]>
expectation(normal(3, log(2))) expectation(binomialdistribution(7, 0.3))
expectation(normal(3, log(2))) expectation(binomialdistribution(7, 0.3))
Create an ExpGamma Distribution: If a variable has a Gamma distribution with parameters alpha and beta, then its logarithm has an ExpGamma distribution with parameters alpha, beta, and gamma = 1.
expgamma(alpha = 1, beta = 1, gamma = -2)
expgamma(alpha = 1, beta = 1, gamma = -2)
alpha |
The "shape" parameter of the corresponding Gamma distribution. |
beta |
The "rate" parameter of the corresponding Gamma distribution. |
gamma |
The scale parameter for the logarithmhic scale. By default, |
The ExpGamma has probability density function
An ExpGamma distribution.
Petter Mostad <[email protected]>
dist <- expgamma(4, 6) plot(dist)
dist <- expgamma(4, 6) plot(dist)
Create a univariate F distribution.
fdistribution(df1 = 1, df2 = 1)
fdistribution(df1 = 1, df2 = 1)
df1 |
The first degree of freedom: Should be a positive number. |
df2 |
The second degree of freedom: Should be a positive number. |
An F distribution.
Petter Mostad <[email protected]>
dist <- fdistribution(10, 3) cdf(dist, 4) expectation(dist)
dist <- fdistribution(10, 3) cdf(dist, 4) expectation(dist)
Given a vector of data values and a design matrix, the fitted values for a linear model is computed.
fittedvalues(data, design)
fittedvalues(data, design)
data |
A data vector. |
design |
A design matrix. The number of rows must be equal to the length of the data vector. |
The fitted values represent the expected values all but the last variables in the posterior for the linear model.
A vector of values of length equal to the number of columns in the design matrix.
Petter Mostad <[email protected]>
linearmodel
, leastsquares
,
linearpredict
xdata <- simulate(uniformdistribution(), 14) ydata <- xdata + 4 + simulate(normal(), 14)*0.1 plot(xdata,ydata) design <- cbind(1, xdata) lines(xdata, fittedvalues(ydata, design))
xdata <- simulate(uniformdistribution(), 14) ydata <- xdata + 4 + simulate(normal(), 14)*0.1 plot(xdata,ydata) design <- cbind(1, xdata) lines(xdata, fittedvalues(ydata, design))
Create a Gamma distribution.
gammadistribution(alpha = 1, beta = 1)
gammadistribution(alpha = 1, beta = 1)
alpha |
The first parameter of the Gamma distribution: The "shape" parameter. |
beta |
The second parameter of the Gamma distribution: The "rate" parameter. |
The density of the distribution is proportional to
A Gamma probability distribution.
Petter Mostad <[email protected]>
dist <- gammadistribution(4, 2) plot(dist)
dist <- gammadistribution(4, 2) plot(dist)
Compute the inverse of the cumulative distribution function for a univariate probability distribution.
invcdf(object, val)
invcdf(object, val)
object |
A univariate probability distribution. |
val |
A value between 0 and 1. |
A value such that the probability that
is given by
val
.
Petter Mostad <[email protected]>
invcdf(normal(), 0.975) invcdf(binomialdistribution(10, 0.4), 0.5)
invcdf(normal(), 0.975) invcdf(binomialdistribution(10, 0.4), 0.5)
Given a vector of data and a design matrix, the least squares estimates for a linear model is computed.
leastsquares(data, design)
leastsquares(data, design)
data |
A data vector. |
design |
A design matrix. The number of rows must be equal to the length of the data vector. |
The fitted values represent the expected values all but the last variables in the posterior for the linear model.
A vector of values of length equal to the number of columns in the design matrix.
Petter Mostad <[email protected]>
linearmodel
, fittedvalues
,
linearpredict
xdata <- simulate(uniformdistribution(), 14) ydata <- xdata + 4 + simulate(normal(), 14)*0.1 plot(xdata,ydata) design <- cbind(1, xdata) leastsquares(ydata, design)
xdata <- simulate(uniformdistribution(), 14) ydata <- xdata + 4 + simulate(normal(), 14)*0.1 plot(xdata,ydata) design <- cbind(1, xdata) leastsquares(ydata, design)
Given a vector of data and a design matrix, describing how these data are thought to relate to some predictors in a linear model, the posterior for the parameters of this linear model is found, using a flat prior.
linearmodel(data, design)
linearmodel(data, design)
data |
A vector with data values. |
design |
A design matrix. The number of rows must be equal to the length of the data vector. The number of columns corresponds to the number of explanatory variables. |
If is the i'th data value and
is the
j'th unknown parameter, and if
is the value in the i'th row
and j'th column of the design matrix, then one assumes that
is normally distributed with exptectation
and logged standard deviation . The computed probability
distribution is then the posterior for the joint distribution of
.
If is the number of columns in the design matrix and if
,
then the output is a multivariate Normal-ExpGamma distribution representing
the posterior for the corresponding
values and the logged scale
parameter in the linear model. If
, the output is a Normal-ExpGamma
distribution representing the posterior.
Petter Mostad <[email protected]>
fittedvalues
, leastsquares
,
linearpredict
data1 <- simulate(normal(3.3, log(2)), 9) data2 <- simulate(normal(4.5, log(2)), 8) data3 <- simulate(normal(2.9, log(2)), 7) design <- designManyGroups(c(9,8,7)) posterior <- linearmodel(c(data1, data2, data3), design) plot(posterior)
data1 <- simulate(normal(3.3, log(2)), 9) data2 <- simulate(normal(4.5, log(2)), 8) data3 <- simulate(normal(2.9, log(2)), 7) design <- designManyGroups(c(9,8,7)) posterior <- linearmodel(c(data1, data2, data3), design) plot(posterior)
Extends the given probability distribution with new variables which are (multivariate) normally distributed with parameters based on the values of the given probability distribution and values given to the function.
linearpredict(object, ...)
linearpredict(object, ...)
object |
The probability distribution to be extended. Currently, it should be either a (multivariate) normal distribution, or a (multivariate) normal distribution extended with an extra parameter with either a Gamma or an ExpGamma distribution. |
... |
A second optional argument may be given, which should then be a matrix with the same number of columns as there are normally distributed variables in the input object. If the matrix has only one row or column, it may be given as a vector. The default is a matrix with one row, consisting of only 1's. A third optional argument may also be given, which is then the precision matrix
of the new normally distributed variables. By default, this matrix is the identity.
If the input object contains a Gamma-distributed variable, its value is multiplied
with the precision matrix. If the input object contains an ExpGamma-distributed
variable |
The input is either a (multivariate) variable with a normal distribution,
or a joint distribution consisting of a Gamma- or ExpGamma-distributed
variable
, and conditionally on this a (multivariate) normally
distributed
. The output is a joint distribution for
or
, where the marginal distribution for
or
is
unchanged, while the conditional distribution for
given
or
is (multivariate) normal. The expectation and precision for
this conditional distribution is
and
, respectively.
Here,
is the expectation of
, while
is the
optional second argument. The matrix
is the optional third argument,
while
is either equal to
, when
has a Gamma distribution,
or equal to
, when
has an ExpGamma distribution.
A multivariate normal, multivariate Normal-Gamma, or multivariate Normal-ExpGamma distribution, depending on the input.
Petter Mostad <[email protected]>
prior <- normalgamma() full <- linearpredict(prior, rep(1, 7)) data <- simulate(normal(), 7) posterior <- conditional(full, 1:7, data) plot(posterior)
prior <- normalgamma() full <- linearpredict(prior, rep(1, 7)) data <- simulate(normal(), 7) posterior <- conditional(full, 1:7, data) plot(posterior)
Given a multivariate distribution, one of its marginal distributions is computed.
marginal(object, v)
marginal(object, v)
object |
The multivariate probability distribution whose marginal should be computed. |
v |
A vector of indices, indicating which parts of the |
The index or indices of the parameter(s) whose marginal distribution
is computed is given in v
.
A probability distribution.
Petter Mostad <[email protected]>
data <- simulate(normal(3, log(3)), 11) posterior <- linearmodel(data, designOneGroup(11)) credibilityinterval(marginal(posterior, 1)) credibilityinterval(marginal(posterior, 2))
data <- simulate(normal(3, log(3)), 11) posterior <- linearmodel(data, designOneGroup(11)) credibilityinterval(marginal(posterior, 1)) credibilityinterval(marginal(posterior, 2))
An object representing a multivariate discrete distribution is created, based on explicitly given possible values and probabilities for these.
mdiscretedistribution(probs, nms=NULL)
mdiscretedistribution(probs, nms=NULL)
probs |
This must be a matrix, or more generally an array with the same number of dimensions as the desired variable. The values in the matrix must be non-negative and represent the probabilities of the variable. |
nms |
If given, |
A multivariate discrete probability distribution.
Petter Mostad <[email protected]>
dist <- mdiscretedistribution(array(1:24, c(2,3,4))) expectation(dist) variance(dist)
dist <- mdiscretedistribution(array(1:24, c(2,3,4))) expectation(dist) variance(dist)
Creates an object representing a multivariate normal distribution.
mnormal(expectation = c(0,0), P = diag(length(expectation)))
mnormal(expectation = c(0,0), P = diag(length(expectation)))
expectation |
A vector of length at least 2 specifying the expectation of the distribution. By default, the vector (0,0). |
P |
A matrix of size |
If is the expectation vector
and
is the precision matrix, then the probability density function
is proportional to
A multivariate normal probability distribution.
Petter Mostad <[email protected]>
plot(mnormal()) plot(mnormal(c(1,2,3))) plot(mnormal(c(1,2), matrix(c(1, 0.5, 0.5, 1), 2, 2)))
plot(mnormal()) plot(mnormal(c(1,2,3))) plot(mnormal(c(1,2), matrix(c(1, 0.5, 0.5, 1), 2, 2)))
Creates an object representing a multivariate Normal-ExpGamma distribution.
If has a multivariate Normal-ExpGamma
distribution, then the marginal distribution of
is an ExpGamma distribution, and the conditional
distribution of
given
is multivariate normal.
mnormalexpgamma(mu=c(0,0), P, alpha, beta)
mnormalexpgamma(mu=c(0,0), P, alpha, beta)
mu |
The |
P |
The |
alpha |
The |
beta |
The |
If has a multivariate
Normal-ExpGamma distribution with parameters
,
,
, and
, then the marginal distribution of
has an ExpGamma
distribution with parameters
,
, and -2, and conditionally on
,
has a multivariate
normal distribution with expectation
and precision matrix
. The probability density is proportional to
where is the dimension.
A multivariate Normal-ExpGamma probability distribution.
Petter Mostad <[email protected]>
gamma
,normal
,expgamma
,
normalgamma
,normalexpgamma
mnormal
,mnormalgamma
plot(mnormalexpgamma(alpha=3, beta=3))
plot(mnormalexpgamma(alpha=3, beta=3))
Creates an object representing a multivariate Normal-Gamma distribution.
If has a multivariate Normal-Gamma
distribution, then the marginal distribution of
is an Gamma distribution, and the conditional
distribution of
given
is multivariate normal.
mnormalgamma(mu=c(0,0), P, alpha, beta)
mnormalgamma(mu=c(0,0), P, alpha, beta)
mu |
The |
P |
The |
alpha |
The |
beta |
The |
If has a multivariate Normal-Gamma distribution with parameters
,
,
, and
, then the marginal distribution of
has a Gamma
distribution with parameters
,
, and conditionally on
,
has a multivariate normal distribution with expectation
and
precision matrix
. The probability density is proportional to
where is the dimension.
A multivariate Normal-Gamma probability distribution.
Petter Mostad <[email protected]>
gamma
,normal
,expgamma
,
normalgamma
, normalexpgamma
,
mnormal
,mnormalexpgamma
plot(mnormalgamma(alpha=3, beta=3))
plot(mnormalgamma(alpha=3, beta=3))
Creates an object representing a multivariate non-centered t-distribution.
mtdistribution(expectation = c(0,0), degreesoffreedom = 10000, P = diag(length(expectation)))
mtdistribution(expectation = c(0,0), degreesoffreedom = 10000, P = diag(length(expectation)))
expectation |
A vector of length at least 2 specifying the expectation of the distribution. By default, the vector (0,0). |
degreesoffreedom |
The degrees of freedom parameter. |
P |
A matrix of size |
If is the expectation,
the degrees of freedom,
is the last parameter, and
the dimension,
then the probability density function is proportional to
A multivariate t-distribution.
Petter Mostad <[email protected]>
plot(mtdistribution()) plot(mtdistribution(c(1,2,3), 3)) plot(mtdistribution(c(1,2), 3, matrix(c(1, 0.5, 0.5, 1), 2, 2)))
plot(mtdistribution()) plot(mtdistribution(c(1,2,3), 3)) plot(mtdistribution(c(1,2), 3, matrix(c(1, 0.5, 0.5, 1), 2, 2)))
An object representing a multivariate univariate muniform distribution is created.
muniformdistribution(startvec, stopvec)
muniformdistribution(startvec, stopvec)
startvec |
A vector with the lower bounds for the distribution. |
stopvec |
A vector with the upper bounds for the distribution. |
A multivariate uniform probability distribution.
Petter Mostad <[email protected]>
dist <- muniformdistribution(rep(0, 5), rep(1, 5)) expectation(dist) variance(dist)
dist <- muniformdistribution(rep(0, 5), rep(1, 5)) expectation(dist) variance(dist)
Create an object representing a univariate normal distribution.
normal(expectation = 0, lambda, P = 1)
normal(expectation = 0, lambda, P = 1)
expectation |
The expectation of the distribution. |
lambda |
THE NATURAL LOGARITHM OF THE STANDARD DEVIATION OF THE DISTRIBUTION. Thus, if the desired standard deviation is |
P |
If given, this argument specifies the precision of the distribution, i.e., the inverse of the variance. |
A univariate normal probability distribution.
Petter Mostad <[email protected]>
dist <- normal(3, log(0.7)) variance(dist) dist <- normal(5, log(0.49)/2) variance(dist) dist <- normal(7, P = 2) variance(dist)
dist <- normal(3, log(0.7)) variance(dist) dist <- normal(5, log(0.49)/2) variance(dist) dist <- normal(7, P = 2) variance(dist)
Creates an object representing a Normal-ExpGamma distribution. If has a Normal-ExpGamma
distribution, then the marginal distribution of
is an ExpGamma distribution, and the conditional
distribution of
given
is normal.
normalexpgamma(mu, kappa, alpha, beta)
normalexpgamma(mu, kappa, alpha, beta)
mu |
The |
kappa |
The |
alpha |
The |
beta |
The |
If has a Normal-ExpGamma distribution with parameters
,
,
, and
, then the marginal distribution of
has an ExpGamma
distribution with parameters
,
, and -2, and conditionally on
,
has a normal distribution with expectation
and logged standard deviation
. The probability density is proportional to
A Normal-ExpGamma probability distribution.
Petter Mostad <[email protected]>
gamma
, normal
, expgamma
, normalgamma
,
mnormal
, mnormalgamma
, mnormalexpgamma
plot(normalexpgamma(3,4,5,6))
plot(normalexpgamma(3,4,5,6))
Creates an object representing a Normal-Gamma distribution. If has a Normal-Gamma
distribution, then the marginal distribution of
is a Gamma distribution, and the conditional
distribution of
given
is normal.
normalgamma(mu, kappa, alpha, beta)
normalgamma(mu, kappa, alpha, beta)
mu |
The |
kappa |
The |
alpha |
The |
beta |
The |
If has a Normal-Gamma distribution with parameters
,
,
, and
, then the marginal distribution of
has a Gamma
distribution with parameters
and
, and conditionally on
,
has a normal distribution with expectation
and logged standard deviation
. The probability density is proportional to
A Normal-Gamma probability distribution.
Petter Mostad <[email protected]>
gamma
, normal
, expgamma
, normalexpgamma
,
mnormal
, mnormalgamma
, mnormalexpgamma
plot(normalgamma(3,4,5,6))
plot(normalgamma(3,4,5,6))
The p-value of a distribution is here interpreted as the probability outside the smallest credibility interval or region containing a point; if no point is explicitly given, it is assumed to be zero, or the origin.
p.value(object, point)
p.value(object, point)
object |
The probability distribution for which the p-value should be computed. |
point |
The point which should be included in the credibility interval or region. |
The probability outside the smallest credibility interval or region
containing the point
.
Petter Mostad <[email protected]>
data <- simulate(normal(3, log(2)), 10) posterior <- linearmodel(data, designOneGroup(10)) p.value(marginal(posterior, 1))
data <- simulate(normal(3, log(2)), 10) posterior <- linearmodel(data, designOneGroup(10)) p.value(marginal(posterior, 1))
A plot is constructed convering the central part of a probability distribution. The purpose is simply to illustrate the properties of the distribution.
## S3 method for class 'normal' plot(x, ...) ## S3 method for class 'binomialdistribution' plot(x, ...)
## S3 method for class 'normal' plot(x, ...) ## S3 method for class 'binomialdistribution' plot(x, ...)
x |
A probability distribution. |
... |
Other arguments (not currently in use). |
For univariate discrete distributions, a plot is generated showing with a histogram the probabilities of each of the possible values of the distribution. For univariate continuous distributions, a plot is made of roughly the central 99 of the distribution. For multivariate distributions, a combined plot is made, where one can find the marginal distributions along the diagonal, and contour plots for bivariate marginal distributions off the diagonal.
Petter Mostad <[email protected]>
plot(normal()) plot(mnormal(c(3,4,5), diag(3))) plot(poissondistribution(3))
plot(normal()) plot(mnormal(c(3,4,5), diag(3))) plot(poissondistribution(3))
Create an object representing a Poisson distribution.
poissondistribution(rate)
poissondistribution(rate)
rate |
The rate parameter of the distribution. |
An object representing a Poisson distribution.
Petter Mostad <[email protected]>
dist <- poissondistribution(4) cdf(dist, 3)
dist <- poissondistribution(4) cdf(dist, 3)
Given a vector of data, this function computes the bivariate posteror for the expectation parameter and the logged scale parameter of a normal distribution, assuming that the data represents independent observations from the normal distribution. One assumes a flat prior.
posteriornormal1(data)
posteriornormal1(data)
data |
A vector with data values. |
An object representing a bivariate Normal-ExpGamma distribution.
Petter Mostad <[email protected]>
data <- simulate(normal(3.3, log(2)), 9) posteriornormal1(data) linearmodel(data, designOneGroup(length(data))) #Gives same result
data <- simulate(normal(3.3, log(2)), 9) posteriornormal1(data) linearmodel(data, designOneGroup(length(data))) #Gives same result
Given a vectors data1 and data2 of data, this function assumes data1 is a sample from one normal distribution while data2 is a sample from another, while both distributions are assumed to have the same logged scale. The bivariate posterior for the difference between the expectations of the two distributions and the common logged scale of the distributions is computed, assuming a flat prior.
posteriornormal2(data1, data2)
posteriornormal2(data1, data2)
data1 |
A vector with data values. Assumed to be a sample from the first normal distribution. |
data2 |
Another vector with data values. Assumed to be a sample from the second normal distribution. |
An object representing a Normal-ExpGamma distribution.
Petter Mostad <[email protected]>
data1 <- simulate(normal(3.3, log(2)), 9) data2 <- simulate(normal(5.7, log(2)), 4) posteriornormal2(data1, data2) marginal(linearmodel(c(data1, data2), designTwoGroups(length(data1), length(data2))), 2:3) #Gives same result
data1 <- simulate(normal(3.3, log(2)), 9) data2 <- simulate(normal(5.7, log(2)), 4) posteriornormal2(data1, data2) marginal(linearmodel(c(data1, data2), designTwoGroups(length(data1), length(data2))), 2:3) #Gives same result
Compute the precision (i.e., the inverse of the variance) of a probability distribution.
precision(object)
precision(object)
object |
A probability distribution. |
The precision of the probability distribution: Either a number or a matrix.
Petter Mostad <[email protected]>
precision(normal(3, log(0.7))) precision(binomialdistribution(7, 0.4))
precision(normal(3, log(0.7))) precision(binomialdistribution(7, 0.4))
When a probability distribution is printed, its main features are listed.
## S3 method for class 'normal' print(x, ...)
## S3 method for class 'normal' print(x, ...)
x |
The object to be printed. |
... |
Other possible arguments (not currently implemented). |
Readable text describing the object.
Petter Mostad <[email protected]>
print(normal())
print(normal())
Given a possible value for a probability distribution, the probability at that value is computed.
probability(object, val)
probability(object, val)
object |
A discrete probability distribution. |
val |
The value at which the probability should be computed. |
The probability at val
.
Petter Mostad <[email protected]>
probability(poissondistribution(3), 1) probability(binomialdistribution(10, 0.24), 2)
probability(poissondistribution(3), 1) probability(binomialdistribution(10, 0.24), 2)
Computes the probability density at a value for a continuous distribution.
probabilitydensity(object, val, log = FALSE, normalize = TRUE)
probabilitydensity(object, val, log = FALSE, normalize = TRUE)
object |
A continuous probability distribution. |
val |
The point at which the probability density should be computed. |
log |
If TRUE, the logarithm of the probability density is returned. |
normalize |
If FALSE, unnormalized densities are returned. |
The probability density at val
.
Petter Mostad <[email protected]>
probabilitydensity(normal(), 1) probabilitydensity(mnormal(c(0,0), diag(2)), c(1,1))
probabilitydensity(normal(), 1) probabilitydensity(mnormal(c(0,0), diag(2)), c(1,1))
Simulate independent values from a given probability distribution.
## S3 method for class 'normal' simulate(object, nsim = 1, ...) ## S3 method for class 'binomialdistribution' simulate(object, nsim = 1, ...) ## S3 method for class 'discretedistribution' simulate(object, nsim = 1, ...) ## S3 method for class 'expgamma' simulate(object, nsim = 1, ...) ## S3 method for class 'fdistribution' simulate(object, nsim = 1, ...) ## S3 method for class 'gammadistribution' simulate(object, nsim = 1, ...) ## S3 method for class 'mnormalexpgamma' simulate(object, nsim = 1, ...) ## S3 method for class 'mnormalgamma' simulate(object, nsim = 1, ...) ## S3 method for class 'mnormal' simulate(object, nsim = 1, ...) ## S3 method for class 'mtdistribution' simulate(object, nsim = 1, ...) ## S3 method for class 'normalexpgamma' simulate(object, nsim = 1, ...) ## S3 method for class 'normalgamma' simulate(object, nsim = 1, ...) ## S3 method for class 'poissondistribution' simulate(object, nsim = 1, ...) ## S3 method for class 'tdistribution' simulate(object, nsim = 1, ...) ## S3 method for class 'uniformdistribution' simulate(object, nsim = 1, ...)
## S3 method for class 'normal' simulate(object, nsim = 1, ...) ## S3 method for class 'binomialdistribution' simulate(object, nsim = 1, ...) ## S3 method for class 'discretedistribution' simulate(object, nsim = 1, ...) ## S3 method for class 'expgamma' simulate(object, nsim = 1, ...) ## S3 method for class 'fdistribution' simulate(object, nsim = 1, ...) ## S3 method for class 'gammadistribution' simulate(object, nsim = 1, ...) ## S3 method for class 'mnormalexpgamma' simulate(object, nsim = 1, ...) ## S3 method for class 'mnormalgamma' simulate(object, nsim = 1, ...) ## S3 method for class 'mnormal' simulate(object, nsim = 1, ...) ## S3 method for class 'mtdistribution' simulate(object, nsim = 1, ...) ## S3 method for class 'normalexpgamma' simulate(object, nsim = 1, ...) ## S3 method for class 'normalgamma' simulate(object, nsim = 1, ...) ## S3 method for class 'poissondistribution' simulate(object, nsim = 1, ...) ## S3 method for class 'tdistribution' simulate(object, nsim = 1, ...) ## S3 method for class 'uniformdistribution' simulate(object, nsim = 1, ...)
object |
The probability distribution to be simulated from. |
nsim |
The number of simulated values. Default is 1. |
... |
Additional parameters. Currently not in use. |
For univariate distributions, a vector of length nsim
is produced.
For multivariate distributions, a matrix with nsim
rows is
produced.
Petter Mostad <[email protected]>
simulate(normal()) simulate(normal(), 10) simulate(mnormal(), 10)
simulate(normal()) simulate(normal(), 10) simulate(mnormal(), 10)
Lists the main features of a probability distribution object.
## S3 method for class 'normal' summary(object, ...)
## S3 method for class 'normal' summary(object, ...)
object |
The object to be summarized. |
... |
Other possible arguments (not currently implemented). |
Readable text describing the object.
Petter Mostad <[email protected]>
summary(normal())
summary(normal())
Create an object representing a univariate non-centered t-distribution.
tdistribution(expectation = 0, degreesoffreedom = 1e+20, lambda, P = 1)
tdistribution(expectation = 0, degreesoffreedom = 1e+20, lambda, P = 1)
expectation |
The expectation of the distribution. |
degreesoffreedom |
The degrees of freedom parameter. |
lambda |
The natural logarithm of the scale |
P |
An alternative to specifying the logged scale |
The probability density of a t-distribution with expectation ,
degrees of freedom
, and logged scale
is proportional to
A t-distribution.
Petter Mostad <[email protected]>
dist <- tdistribution(3) plot(dist)
dist <- tdistribution(3) plot(dist)
An object representing a univariate uniform distribution is created.
uniformdistribution(a = 0, b = 1)
uniformdistribution(a = 0, b = 1)
a |
The lower bound for the distribution. The default is 0. |
b |
The upper bound for the distribution. The default is 1. |
A uniform probability distribution.
Petter Mostad <[email protected]>
dist <- uniformdistribution() expectation(dist) variance(dist)
dist <- uniformdistribution() expectation(dist) variance(dist)
Compute the variance of a probability distribution.
variance(object)
variance(object)
object |
A probability distribution. |
The variance of the probability distribution.
Petter Mostad <[email protected]>
variance(normal(3, log(0.7))) variance(binomialdistribution(7, 0.4))
variance(normal(3, log(0.7))) variance(binomialdistribution(7, 0.4))