0
votes

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

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

1
double-check the brackets in your function: it seems there's a closing parenthesis placed too early in the return() statement - Vasily A

1 Answers

1
votes

The error is caused by X-Q$Mu part of your function. As you call it as g(A,sigma,1), it is essentially A-Q$Mu. A is a list and Q$Mu is a matrix - you can't subtract a matrix from a list.
You probably meant the matrix part of X, i.e. X$X. But even then, Q$mu is a matrix with incompatible dimensions - you can't subtract it.
Additionally, your return() statement seems to have a typo - there's additional part after the closing parenthesis, it won't be returned.