Elisabetta Petracci
2008-Jan-23 12:18 UTC
[R] from a normal bivariate distribution to the marginal one
Hello, I'm quite new with R and so I would like to know if there is a command to calculate an integral. In particular I simulated a bivariate normal distribution using these simple lines: rbivnorm <- function(n, # sample size mux, # expected value of x muy, # expected value of Y sigmax, # standard deviation of X sigmay, # standard deviation of Y rho){ # correlation coefficient x <- rnorm(n,mux,sigmax) y <- rnorm(n,muy+rho*sigmay*(x-mux)/sigmax, sigmay*sqrt(1-rho2)) cbind(x,y) } In this way I've sampled from a normal bivariate distribution and I've obtained two vectors of values:x and y.. Now, I would like to find the marginal distribution of y from the bivariate distribution. Is there a command to do this? Thank you, Elisabetta
David Winsemius
2008-Jan-23 15:02 UTC
[R] from a normal bivariate distribution to the marginal one
Elisabetta Petracci <elisabetta.petracci at unimi.it> wrote in news:479730AA.3040505 at unimi.it:> Hello, > > I'm quite new with R and so I would like to know if there is a > command to calculate an integral. > In particular I simulated a bivariate normal distribution using > these simple lines: > > rbivnorm <- function(n, # sample size > > mux, # expected value of x > > muy, # expected value of Y > > sigmax, # standard deviation of X > > sigmay, # standard deviation of Y > > rho){ # correlation coefficient > > > x <- rnorm(n,mux,sigmax) > y <- rnorm(n,muy+rho*sigmay*(x-mux)/sigmax, > sigmay*sqrt(1-rho2)) > > cbind(x,y) > } > > In this way I've sampled from a normal bivariate distribution and > I've obtained two vectors of values:x and y.. > > Now, I would like to find the marginal distribution of y from the > bivariate distribution. > Is there a command to do this?It appears you left out a "^" between rho and 2. There is an integrate() function but it requires a function (not a finite set of points). I think that what you may want is density(). Try: mv.x.y<-rbivnorm(200,1,4,0.3,0.4,0.3) plot(mv.x.y) y.dens<-density(mv.x.y[,2]) plot(y.dens) If you want the cdf rather than an estimate of the pdf, then look at ecdf(). -- David Winsemius
Giovanni Petris
2008-Jan-23 19:08 UTC
[R] from a normal bivariate distribution to the marginal one
Somebody has already replied in probably more constructive terms. However, I would like to add a comment.> Now, I would like to find the marginal distribution of y from the > bivariate distribution.The distribution of y is a theoretical property that cannot be _found_ from a data set. You can use a set of data to _estimate_ a distribution (this is what density() does), but if you want to _find_ the distribution of y then you have to take paper and pencil and work it out. R can do it for you numerically (using integrate()) but, again, in this case you don't need any data. Best, Giovanni -- Giovanni Petris <GPetris at uark.edu> Department of Mathematical Sciences University of Arkansas - Fayetteville, AR 72701 Ph: (479) 575-6324, 575-8630 (fax) http://definetti.uark.edu/~gpetris/