给出一个由两类组成的数据集,这些数据集是从多元正态分布生成的,如何在R中绘制贝叶斯决策边界。注-我不想分类或应用任何分类技术。由于我已经生成了数据,因此我知道观察值属于哪个类。问题在于在R中绘制决策边界。
gen_sample_class <- function(n, vec_mu){
ret = matrix(rep(0, 2*n), ncol = 2)
for(i in 1:n){
s = c(0, 0)
for(k in 1:10){
s = s + mvrnorm(1, mu = vec_mu[k,], Sigma = v_sigma[(k*2-1):(k*2),])
}
ret[i,] = s
}
return(ret)
}
d_mix <- function (x, vec_mu){
ret = 0
for(k in 1:10){
ret = ret + dmvnorm(x, mean = vec_mu[k,], sigma = v_sigma[(k*2-1):(k*2),])
}
return(log(ret))
}
set.seed(1)
gen_random_sigma <- function (x){
sigma = matrix(rep(0,4), ncol = 2)
sigma[1,1] = runif(1, min = 0, max = 1)
sigma[2,2] = runif(1, min = 0, max = 1)
return(sigma)
}
v_sigma = matrix(sapply(1:10, gen_random_sigma), ncol = 2, byrow = T)
v_mean_0 = matrix(runif(20, min = -0.5, max = 0), ncol = 2)
v_mean_1 = matrix(runif(20, min = 0.1, max = 0.5), ncol = 2)
n0 = 50
n1 = 50
y0 = rep(0, n0)
X0 = gen_sample_class(n0, v_mean_0)
y1 = rep(1, n1)
X1 = gen_sample_class(n1, v_mean_1)
y = c(y0, y1)
X = rbind(X0, X1)
v_mean_0 = matrix(runif(20, min = -0.3, max = 0), ncol = 2)
v_mean_1 = matrix(runif(20, min = 1, max = 1.5), ncol = 2)
n0 = 50
n1 = 50
y0 = rep(0, n0)
X0 = gen_sample_class(n0, v_mean_0)
y1 = rep(1, n1)
X1 = gen_sample_class(n1, v_mean_1)
y = c(y0, y1)
X = rbind(X0, X1)
col_y = y
col_y[col_y==0] = "red"
col_y[col_y==1] = "blue"
plot(X[,1], X[,2], col = col_y, xlab = "X1", ylab = "x2")