Home > Mobile >  Joint probability in R
Joint probability in R

Time:11-29

I have a finite sequence of discrete values X = {x1, x2, x3...xn} and I want to compute the joint probability of each pair of elements in the sequence. i.e, P(x1, x2), P(x2, x3) etc. How should I implement this in R? I don't have much experience doing such analysis in R. Are there any built in functions to do the same?

CodePudding user response:

You need to use

den2d <- kde2d(x,y)
image(den2d)
persp(den2d,phi=30,theta=20)

This is described in the MASS book Section 5.6 Density Estimation, subsection Two-dimensional data. There are examples and further references. I cannot post their examples here due to copyright.

CodePudding user response:

If you assume independence then the joint probability is trivial trivial you have $P(a,b)=P(a)P(b)$. Otherwise you need to estimate the probabilities of each pair individually.

To do so, you can get a list of pairs of the same form using mapply, then you can estimate the probabilities using the relative abundance of each pair. Here I'm just illustrating a point, I haven't run this code.

L <- length(X)
X.pairs <- mapply(
    function(x1, x2) paste(x1, x2, sep=','),
    X[1:(L-1)],
    X[2:L]
)
table(X.pairs)

Edit: this is for discrete values.

  • Related