简体   繁体   English

高斯分布的判别函数

[英]Discriminant function for Gaussian distribution

Hello community i have the following error:你好社区我有以下错误:

Error in X - Q$Mu : argument non numérique pour un opérateur binaire
Called from: transpose(X - Q$Mu)

Here is my code(r language), i hope it will be helpful i want to simulate a dataset and applying the gaussian distribution , but it gives me the error above这是我的代码(r语言),我希望它会有所帮助我想模拟一个数据集并应用高斯分布,但它给了我上面的错误

library(MixSim)
library(rlist)
Q <- MixSim(MaxOmega = 0.0, K = 2, p = 2)
A <- simdataset(n = 500, Pi = Q$Pi, Mu = Q$Mu, S = Q$S)
A
vk=data.frame(A$id)
v=count(vk,1)
v ########### count the elements of eah class (this exemple has 5 classes)
v[1,2]###### elements for class 1
v[2,2]######## elements for class 2

g <- function(X,sigma,i) {
  return((1/2)*transpose(X-Q$Mu)*solve(Q$S[,,i])*(X-Q$Mu))-(1/2)*ln(sigma)+ln(v[i,2]/500)
}

g(A,sigma,1)
sigma=Q$Mu
mahalanobis(A,  sigma, Q$S)
Q$Mu
ln(sigma)
Q$Mu

I want it to calculate this function Discriminant Gaussian distribution我想让它计算这个函数判别高斯分布

P is the probability , Q$Mu is the mean Q$S[,,i] is the covariance matrix for class i P 是概率,Q$Mu 是均值 Q$S[,,i] 是第 i 类的协方差矩阵

The error is caused by XQ$Mu part of your function.该错误是由函数的XQ$Mu部分引起的。 As you call it as g(A,sigma,1) , it is essentially AQ$Mu .正如你所说的g(A,sigma,1) ,它本质上是AQ$Mu A is a list and Q$Mu is a matrix - you can't subtract a matrix from a list. A是一个列表, Q$Mu是一个矩阵 - 你不能从列表中减去一个矩阵。
You probably meant the matrix part of X , ie X$X .您可能指的是X的矩阵部分,即X$X But even then, Q$mu is a matrix with incompatible dimensions - you can't subtract it.但即便如此, Q$mu还是一个维度不兼容的矩阵——你不能减去它。
Additionally, your return() statement seems to have a typo - there's additional part after the closing parenthesis, it won't be returned.此外,您的return()语句似乎有一个错字 - 在右括号之后还有额外的部分,它不会被返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM