[R] how to estimate conditional density

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Wed Nov 17 10:37:20 CET 2004


On 17-Nov-04 Yulei He wrote:
> Hi, there.
> 
> Suppose I have a bivariate data set y1 and y2. Can anybody
> tell me how to estimate the conditional density of f(y1|y2)
> and vice versa? Thanks.
> 
> Yulei

In the absence of a parametric model for the distribution,
a simple-minded approach could be the following:

1. Use 'f<-kde2d(...)' from the MASS library to generate a
   kernel density estimate of the bivariate distribution,
   ensuring that your (y1,y2) grid includes the value of y2
   at which you want to get f(y1|y2). Suppose that different
   values of y2 correspond to different rows of the matrix
   f$z in the returned result (see "?kde2d").

2. For the row [i] corresponding to the conditioning value
   of y2, normalise the values so that sum(f$z[i,]*dy1)=1,
   where dy1 is the step between different values of y1 in
   the grid used in (1).

The resulting normalised row of values is then an estimate
of f(y1|y2), for each such value of y2.

Similarly, applying (2) to the columns of f$z, you can get
an estimate of f(y2|y1).

[Note: for each single value of y2, you don't need to estimate
 the density of y2, i.e. for this purpose you can forget about
 the definition f(y1,y2)/f(y2) of f(y1|y2).]

Hoping this helps,
Ted.


--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861  [NB: New number!]
Date: 17-Nov-04                                       Time: 09:37:20
------------------------------ XFMail ------------------------------




More information about the R-help mailing list