R, Statistics, Data Science.
sum((root.means[i,] - total.means[i]) * (root.means[j,] - total.means[j])) H[j,i] <- n * sum((root.means[j,] - total.means[j]) * (root.means[i,] - total.means[i])) } } H # Output: ## [,1] [,2] [,3] [,4] ## [1,] 0.07356042 0.5373852 0.3322646 0.208470 ## [2,] 0.53738521 4.1996619 2.3553885 1.637108