-
Notifications
You must be signed in to change notification settings - Fork 0
/
HW1P4.R
52 lines (40 loc) · 1.36 KB
/
HW1P4.R
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
# MA 554 Applied Multivariate Analysis Fall 2019
# Author: Yuchen Dong
# Homework 1 Problem 4
library(MASS)
library(mvtnorm)
library(graphics)
set.seed(100) # Fix a random seed
mu = c(3,5)
var1 = 10
var2 = 4
corr12 = 0.9
Sigma = matrix(c(var1, sqrt(var1)*corr12*sqrt(var2),sqrt(var1)*corr12*sqrt(var2), var2), nrow=2 ,ncol=2)
# Generate the contour plot
D = det(Sigma)
x = seq(-3,9,length.out=100) # mu+/-3*sigma
y = seq(-4,14,length.out=100)
grids <- expand.grid(x,y)
f = dmvnorm(grids, mu, Sigma, log=FALSE)
z = matrix(f,100,100)
contour(x,y,z, main ='Contour plot',xlab='X_1', ylab='X_2')
# Generate 100 numbers of multivariate normal distribution
num = mvrnorm(n = 100, mu, Sigma)
plot(num)
# Sample mean
xbar = colMeans(num)
xbar
# Sample covariance matrix
S =var(num)
S
# Sketch the equidistance set given by the sample Mahalanobis distance
c = c(1,2,3,4,5);
level = exp(-c^2/2)/(2*pi*sqrt(det(S)));
x = seq(-3,9,length.out=100) # mu+/-3*sigma
y = seq(-4,14,length.out=100)
grids <- expand.grid(x,y)
f_new = dmvnorm(grids, xbar, S, log=FALSE)
z_new = matrix(f_new,100,100)
contour(x,y,z_new,xlim=c(-3,9), ylim=c(-4,14), main ='E_c for c=1',xlab='X_1', ylab='X_2', levels= level)
# Method 2: Use mahalanobis function to compute the Mahalanobis distance
# z = matrix(mahalanobis(grids, xbar, S, inverted = FALSE),100,100)