'How can I fix the non-conformable arguments error in my R code?
x_bar = numeric(ncol(radio))
for(i in 1:ncol(radio)){
x_bar[i] = mean(radio[,i])
}
covmat = function(x){
n = ncol(x)
A = matrix(dat = numeric(n^2), ncol = n, nrow = n)
for(i in 1:n){
for(j in 1:n){
A[i,j] = cov(x[,i],x[,j])
}
}
return(A)
}
S = covmat(radio);invS = solve(S)
m = nrow(radio)
di = numeric(m)
for(i in 1:m){
di[i] = t(radio[i,]-x_bar)%*%invS%*%(radio[i,]-x_bar)
}
The code above is for calculating the 'mahalanobis distance' of the data 'radio' which is 98x6 dimensions.
whenever i run for(i in 1:m){di[i] = t(radio[i,]-x_bar)%*%invS%*%(radio[i,]-x_bar)}
the program send me a error which says the vector calculation code is not available because of the vector dimension problem.
However, When I check the matrices(S, invS) and vectorsradio[1,]-x_bar
, the matrices have rank 6 and dimension of the vectors are all 6.
Please help me. thank you
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|