fork download
  1. set.seed(123)
  2. dt <-
  3. data.frame(
  4. x1 = (1:10)+rnorm(10),
  5. x2 = (11:20)+rnorm(10),
  6. x3 = (10:1)+rnorm(10),
  7. x1r = (10:1)+rnorm(10),
  8. x2r = (20:11)+rnorm(10),
  9. x3r = (1:10)+rnorm(10),
  10. y1 = (1:10)+rnorm(10),
  11. y2 = (1:10)+rnorm(10),
  12. y3 = (1:10)+rnorm(10)
  13. )
  14. pairs(dt)
  15. cat("\n\n=== Correlation coefficients ===\n")
  16. cor(dt)
  17.  
  18. dt.x <- dt[, 1:3]
  19. dt.xr <- dt[, 4:6]
  20. dt.y <- dt[, 7:9]
  21. # remind that dim(dt.x) == dim(dt.xr)
  22.  
  23. betas <- matrix(NA_real_, 3, 3, dimnames = list(paste0("x", 1:ncol(dt.x)), paste0("y", 1:ncol(dt.y))))
  24. is.reverse <- matrix(NA, 3, 3, dimnames = list(paste0("x", 1:ncol(dt.x)), paste0("y", 1:ncol(dt.y))))
  25. for(i in 1:ncol(dt.y)) {
  26. for(j in 1:ncol(dt.x)) {
  27. beta <- coef(lm(dt.y[, i] ~ dt.x[, j]))[2]
  28. if(beta < 0) {
  29. betas[i, j] <- coef(lm(dt.y[, i] ~ dt.xr[, j]))[2]
  30. is.reverse[i, j] <- T
  31. } else {
  32. betas[i, j] <- beta
  33. is.reverse[i, j] <- F
  34. }
  35. }
  36. }
  37.  
  38.  
  39. cat("\n\n=== 'Adjusted' coefficients ===\n")
  40. betas
  41. cat("\n\n=== Sum of 'adjusted' coefficients by y ===\n")
  42. colSums(betas)
  43. cat("\n\n=== Is 'adjusted'? ===\n")
  44. is.reverse
  45.  
Success #stdin #stdout 0.27s 185344KB
stdin
Standard input is empty
stdout

=== Correlation coefficients ===
            x1         x2         x3        x1r        x2r        x3r
x1   1.0000000  0.9495822 -0.9364842 -0.9109121 -0.9267716  0.9188387
x2   0.9495822  1.0000000 -0.9449853 -0.9241597 -0.9084418  0.9113991
x3  -0.9364842 -0.9449853  1.0000000  0.9316247  0.8907153 -0.9630257
x1r -0.9109121 -0.9241597  0.9316247  1.0000000  0.9353999 -0.9482217
x2r -0.9267716 -0.9084418  0.8907153  0.9353999  1.0000000 -0.8853208
x3r  0.9188387  0.9113991 -0.9630257 -0.9482217 -0.8853208  1.0000000
y1   0.9263851  0.9517014 -0.9240711 -0.9842837 -0.9574304  0.9365267
y2   0.9782547  0.9600370 -0.9623442 -0.9243094 -0.9348988  0.9346636
y3   0.9355959  0.9253659 -0.9078969 -0.9794401 -0.9270736  0.9452135
            y1         y2         y3
x1   0.9263851  0.9782547  0.9355959
x2   0.9517014  0.9600370  0.9253659
x3  -0.9240711 -0.9623442 -0.9078969
x1r -0.9842837 -0.9243094 -0.9794401
x2r -0.9574304 -0.9348988 -0.9270736
x3r  0.9365267  0.9346636  0.9452135
y1   1.0000000  0.9517949  0.9753518
y2   0.9517949  1.0000000  0.9311038
y3   0.9753518  0.9311038  1.0000000


=== 'Adjusted' coefficients ===
         y1       y2       y3
x1 1.170109 1.253301 1.120753
x2 1.135033 1.161353 1.027465
x3 1.045991 1.078631 1.001208


=== Sum of 'adjusted' coefficients by y ===
      y1       y2       y3 
3.351133 3.493285 3.149426 


=== Is 'adjusted'? ===
      y1    y2   y3
x1 FALSE FALSE TRUE
x2 FALSE FALSE TRUE
x3 FALSE FALSE TRUE