-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRobustCovariance.Rmd
88 lines (71 loc) · 1.76 KB
/
RobustCovariance.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
---
title: "R: Robust Covariance"
author: "Max Chen 2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Library
Load required packages
```{r lib}
# install.packages("mvtnorm")
# install.packages("robust")
# install.packages("car")
# install.packages("pracma")
# install.packages("ggplot2")
suppressMessages(library(mvtnorm))
suppressMessages(library(robust))
suppressMessages(library(car))
suppressMessages(library(pracma))
suppressMessages(library(ggplot2))
```
## Data
Generate sample data
```{r dat}
rndsample = rmvnorm(100,mean=c(3,5),sigma=matrix(c(1,0.5,0.5,2),2,2))
colnames(rndsample) = c("X1","X2")
df = data.frame(rndsample)
par(pty="s")
plot(df,asp=1,xlim=c(-2,8),ylim=c(0,10))
```
## Calculation
Calculate robust covariance estimator
```{r cal}
robfit = covRob(df)
print(robfit$center)
print(robfit$cov)
```
Transform back to unit distribution
```{r unit}
decov = svd(robfit$cov)
matS = diag(sqrt(decov$d))
matR = decov$u
matC = robfit$center
df_unit = as.matrix(sweep(df,2,matC,"-")) %*% solve(matR) %*% solve(matS)
par(pty="s")
plot(df_unit,asp=1)
```
## Visualize
Plot confidence ellipse
```{r plt}
nDim = 2
cumP = 0.95
radiProb = sqrt(qchisq(cumP,nDim))
dist = sqrt(rowSums(df_unit^2))
radiEvlp = max(dist)
par(pty="s")
plot(df,asp=1,xlim=c(-2,8),ylim=c(0,10))
ellipse(robfit$center,robfit$cov,radiProb)
ellipse(robfit$center,robfit$cov,radiEvlp,lty="dashed")
```
Plot density function
```{r pdf}
XY = meshgrid(seq(-2,8,length=100),seq(0,10,length=100))
XY = data.frame(as.vector(XY$X),as.vector(XY$Y))
colnames(XY) = c("X1","X2")
XY$pdf = dmvnorm(XY,mean=robfit$center,sigma=robfit$cov)
ggplot(XY,aes(X1,X2)) +
geom_raster(aes(fill=pdf),interpolate=TRUE) +
scale_fill_gradientn(colours=c("white","blue"))
```