Advertisement
yusufbrima

correlation matrix

Dec 4th, 2020
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. corr_arr <- function(a,b){
  2.  
  3. corr = 0
  4. a_bar <- sum(a)/length(a)
  5.  
  6. b_bar <- sum(b)/length(b)
  7.  
  8. var_a = sum((a - a_bar)^2)/(length(a) - 1)
  9.  
  10. var_b = sum((b - b_bar)^2)/(length(b) - 1)
  11.  
  12. covar_a_b = sum((a - a_bar) * (b - b_bar))/ (length(b) - 1)
  13.  
  14. corr = covar_a_b/sqrt(var_a * var_b)
  15. return(corr)
  16. }
  17. data <- data_loader("death_rates.csv",",")
  18.  
  19. female_data = data[data$Sex == "F",]
  20. male_data = data[data$Sex == "M",]
  21.  
  22. display_correlation <- function(female_data){
  23. #we are encoding the sex feature (F,M) -> (1,2)
  24. data$Sex = as.numeric(factor(data$Sex,levels = c("F","M"),labels = c(1, 2)))
  25. result <- matrix(, nrow = ncol(data), ncol =ncol(data))
  26. for (i in 1:ncol(data)){
  27. for (j in 1:ncol(data)){
  28. result[i,j] <- corr_arr(data[,i],data[,j])
  29. }
  30. }
  31. colnames(result) <- colnames(data)
  32. rownames(result) <- colnames(data)
  33. return(result)
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement