set.seed(2020)
n=200 # length of the sample
nv<-3 # number of explanatory variables
p<-nv+1 # nv + intercept
m<-4 # dimension of Y
q0=p*m
X<-array(0,c(q0,m,n))
for(i in 1:n) {
aux=rep(1,p)
aux[2:p]<-rMN(1,mu=rnorm(nv),Sigma=diag(nv)) ##simulating covariates
mi=matrix(0,q0,m)
for (j in 1:m) mi[((j-1)*p+1):(j*p),j]=aux
X[,,i]<-mi
} ##X is the simulated regressor matrix
betas<-matrix(rnorm(q0),ncol=1) ##True betas
Sigmas <- clusterGeneration::genPositiveDefMat(m,rangeVar=c(1,3),
lambdaLow=1, ratioLambda=3)$Sigma ##True Sigma
y=matrix(0,n,m)
for(i in 1:n) {
mui<-t(X[,,i])%*%betas
y[i,]<-rMN(n=1,c(mui),Sigmas) ## simulating the response vector
}
fit.MN=estimate.MN(y,X) #fit the MN model
mahal.MN=distMahal(fit.MN) #compute the Mahalanobis distances for MN model
plot(mahal.MN) #plot the Mahalanobis distances for MN model
mahal.MN$Mahal #presents the Malahanobis distances
Run the code above in your browser using DataLab