pprobability: pprobability

View source: R/pprobability.R

pprobabilityR Documentation

pprobability

Description

Creates for each value of a discrete random variable a polynomial and estimates the least squares and the maximum likelihood solution.

  • If sample is not given then the sample contains each x value once.

  • If sample is an integer then it is interpreted as the sample size and a sample is generated by rmultinom(1, sample, ddiscrete(runif(length(x)))).

  • If sample is a vector then it is interpreted such that the according x[i] value is i times in the sample. Thus sum(sample) is the sample size.

  • If coeff is a polylist of length(x) then these polynomials are taken.

  • If coeff is a matrix with length(x) columns and power+1 rows then the columns are interpreted as the coefficients of a polynomial.

  • Otherwise coeff is interpreted as a vector from which the coefficient are sampled. The intercepts are sampled via ddiscrete(runif(length(x)), zero=zero). If coeff is not given then it is ensured that the least squares and the maximum likelihood solution exists and the estimated proabilities are between zero and one. Otherwise is the results may contain NA or the estimated proabilities are outside the interval [0;1].

Usage

pprobability(
  x,
  power = 1,
  zero = FALSE,
  coef = round(seq(-1, 1, by = 0.1), 1),
  sample = rep(1, length(x)),
  pl = NULL,
  tol = 1e-09
)

Arguments

x

numeric: values of a discrete random variable

power

integer: degree for poylnomials (default: 1), must be larger 0

zero

logical: are zero coefficients and zero sample allowed (default: FALSE)

coef

matrix: for each degree coefficients to sample from (default: seq(-1, 1, by=0.1))

sample

integer: number of x values in the sample or sample size (default: rep(1, length(x)))

pl

polylist: a list of polynomial which describe the probability for x (default: NULL)

tol

numeric: tolerance to detect zero values (default: 1e-9)

Value

a list the components:

  • p: the polynomials for the probabilities

  • ep: the expected value as polynomial

  • x: the values for the discrete random variable, the same as the input x

  • sample: the sample given or generated

  • LS$pi: the summands for the least squares problem

  • LS$pl: the summands for the least squares problen in LaTeX

  • LS$pf: the sum of LS$pi

  • LS$df: the derivative of LS$pf

  • LS$pest: the estimated parameter, minimum of LS$pf

  • LS$p: the estimated probabilities

  • ML$pi: the factors for the maximum likelihood problem

  • ML$pl: the summands for the maximum likelihood problem in LaTeX

  • ML$pf: the product of ML$pi

  • ML$df: the derivative of ML$pf

  • ML$pest: the estimated parameter, maximum of ML$pf

  • ML$p: the estimated probabilities

Examples

# linear polynomials
pprobability(0:2)
pprobability(0:2, power=1)
# constant polynomials, some NAs are generated
pprobability(0:3, power=0)
# polynomials generated from a different set
pprobability(0:2, coef=seq(-2, 2, by=0.1))
pprobability(0:2, 0, coef=seq(-2, 2, by=0.1))
# polynomials (x, x, 1-2*x) are used
pprobability(0:2, 0, coef=matrix(c(0.4, 0.4, 0.3), ncol=3))
pprobability(0:2, 1, coef=polylist(c(0,1), c(0,1), c(1, -2)))

sigbertklinke/exams2moodle documentation built on July 6, 2023, 3:26 p.m.