Aim

We need to measure the overall similarity between two vectors. This is the overall similarity between two groups of numbers. Additionally, we use a set of weights to indicate the importance of the elements in the two vectors. In the following example, each element has different importance. Similarity results, are sensitive to weights.

Download the goal library

install.packages("http://www.dimitrisk.gr/Data/goal_0.1.tar.gz", repos=NULL, type="source")

Load library and create two vectors and the weights which reflect the importance of each element in vectors.

library(goal)

weights=c(0.5, 0.25, 0.25)
final=c(42,5,36)
initial=c(40,5,36)

Now plot the two vectors.

plot(final, initial, cex=weights*2, col="red",
  xlim=range(initial, final), ylim=range(initial, final))
abline(0,1)

Calculate the weighted similarity

per = stats.SimilarityOfVectors(weights, final, initial)
print(per)
## [1] 0.9756098

The two vectors have 97.6 weighted similarity.