How to do it...

In this recipe, we will generate random vectors according to a bivariate Gaussian distribution. We will contaminate it and see how both the standard and the robust methods work.

  1. First, we will generate bivariate Gaussian numbers and then estimate the correlation matrix. Since we don't have any outliers, both the robust and non-robust methods should be roughly similar:
library(MASS)library(robust)Sigma <- matrix(c(2,1,1,2),2,2)d <- mvrnorm(n = 1000, mu=c(5,5), Sigma)covClassic(d,cor = TRUE)

The following screenshot shows the correlation matrix estimated via classic methods

  1. The robust correlation matrix looks perfect as well. ...

Get R Statistics Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.