Lamothy
Lamothy

Reputation: 347

Strange behavior of dmvnorm

It appears dnorm(x) and dmvnorm(x) (from mvtnorm package v1.0.5) generates the same result when x is of length 1. However, when working with integration function, the result for dmvnorm(x) seems to be quite off. Here is the example, I defined two integrand functions:

integrand1 <- function(x) 
{ pnorm((-2-sqrt(0.2)*x)/sqrt(0.8))*dmvnorm(x)} 

integrand2 <- function(x) 
{ pnorm((-2-sqrt(0.2)*x)/sqrt(0.8))*dnorm(x)} 

When evaluating at individual values, integrand1(x) = integrand2(x).

However, integration along these two functions generates totally different results:

integrate(integrand1,lower = -10,upper = 10)$value
[1] 7.567463e-236

integrate(integrand2,lower = -10,upper = 10)$value
[1] 0.02275013

Similar phenomena is also observed for curve function. Just wonder if it is a bug or I use it incorrectly.

Upvotes: 2

Views: 1154

Answers (1)

nicola
nicola

Reputation: 24490

It's not a bug. The dmvnorm function expects to evaluate the density distribution function of a multinormal, while dnorm handle single variate distributions. Let's see an example:

dnorm(1:5)
#[1] 2.419707e-01 5.399097e-02 4.431848e-03 1.338302e-04 1.486720e-06    
dmvnorm(1:5)
#[1] 1.151999e-14

First difference: dnorm returns a vector of length 5, while dmvnorm a single value. We note also that:

prod(dnorm(1:5))
#[1] 1.151999e-14

basically the same value returned by dmvnorm. In short, these are the difference:

  • dnorm(1:5) evaluates the density of a normal distribution at 1, then 2, 3, 4 and 5 with mean 0 and standard deviation 1. So we have 5 values.
  • dmvnorm is made to treat multivariate distributions. We now ask which are the density of a 5-variate multinormal (since we provided 1:5, i.e. a vector of length 5) where the first variable is at 1, the second at 2 and so on. The means are all zeroes and the covariance matrix is diag(5), i.e. a 5 dimensional identity matrix. Both are the default values, since we didn't specify them. See ?dmvnorm.

In the latter case, the five variable are independent and so the density is the product of 5 independent (single-variate) normal distribution at 1, 2, 3, 4 and 5.

Use dnorm when you are dealing with single-variate distributions and dmvnorm otherwise.

Upvotes: 4

Related Questions