Advertisement
kukis03

Multivariante - Holding

Dec 8th, 2023
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. x1=matrix(c(2,12,4,10,3,8),byrow=T,nrow=3)
  2. x2=matrix(c(5,7,3,9,4,5),byrow=T,nrow=3)
  3. barx1=as.matrix(colMeans(x1))
  4. barx2=as.matrix(colMeans(x2))
  5. s1=2*cov(x1)
  6. s2=2*cov(x2)
  7. s.pooled=1/4 * (s1+s2)
  8. hat.a=barx1-barx2
  9.  
  10. hat.y=t(hat.a)%*%solve(s.pooled)
  11. hat.y
  12. bar.y1=t(hat.a)%*%barx1
  13. bar.y2=t(hat.a)%*%barx2
  14.  
  15. m=1/2*(bar.y1+bar.y2)
  16.  
  17. p=c(0,0,0)
  18.  
  19. clasifica=function(m){
  20. for (i in 1:dim(m)[1]){
  21. if(t(hat.a)%*%m[i,]>=22){
  22. p[i]=1
  23. } else{
  24. p[i]=2
  25. }
  26. }
  27. print(p)
  28. }
  29.  
  30. clasifica(x1)
  31. clasifica(x2)
  32.  
  33. #Primera observación de x1
  34.  
  35. obs1x1=as.matrix(x1[1,])
  36. x1h1=x1[2:3,]
  37. barx1h1=as.matrix(colMeans(x1h1))
  38. s1h1=cov(x1h1)
  39.  
  40. s.hpooled1h1=1/3 * (s1h1+s2)
  41. solve(s.hpooled1h1)
  42.  
  43. t(obs1x1-barx1h1)%*%solve(s.hpooled1h1)%*%(obs1x1-barx1h1)
  44. t(obs1x1-barx2)%*%solve(s.hpooled1h1)%*%(obs1x1-barx2)
  45.  
  46. #Segunda observación de x1
  47.  
  48. obs1x2=as.matrix(x1[2,])
  49. x1h2=x1[c(1,3),]
  50. barx1h2=as.matrix(colMeans(x1h2))
  51. s1h2=cov(x1h2)
  52.  
  53. s.hpooled1h2 = 1/3 * (s1h2+s2)
  54. solve(s.hpooled1h2)
  55.  
  56. t(obs1x2-barx1h2)%*%solve(s.hpooled1h2)%*%(obs1x2-barx1h2)
  57. t(obs1x2-barx2)%*%solve(s.hpooled1h2)%*%(obs1x2-barx2)
  58.  
  59. #Tercera observación de x1
  60.  
  61. obs1x3=as.matrix(x1[3,])
  62. x1h3=x1[1:2,]
  63. barx1h3=as.matrix(colMeans(x1h3))
  64. s1h3=cov(x1h3)
  65.  
  66. s.hpooled1h3 = 1/3 * (s1h3+s2)
  67. solve(s.hpooled1h3)
  68.  
  69. t(obs1x3-barx1h3)%*%solve(s.hpooled1h3)%*%(obs1x3-barx1h3)
  70. t(obs1x3-barx2)%*%solve(s.hpooled1h3)%*%(obs1x3-barx2)
  71.  
  72. #Primera observación de x2
  73.  
  74. obs2x1=as.matrix(x2[1,])
  75. x2h1=x2[2:3,]
  76. barx2h1=as.matrix(colMeans(x2h1))
  77. s2h1=cov(x2h1)
  78.  
  79. s.hpooled2h1 = 1/3 * (s1+s2h1)
  80. solve(s.hpooled2h1)
  81.  
  82. t(obs2x1-barx1)%*%solve(s.hpooled2h1)%*%(obs2x1-barx1)
  83. t(obs2x1-barx2h1)%*%solve(s.hpooled2h1)%*%(obs2x1-barx2h1)
  84.  
  85. #Segunda observación de x2
  86.  
  87. obs2x2=as.matrix(x2[2,])
  88. x2h2=x2[c(1,3),]
  89. barx2h2=as.matrix(colMeans(x2h2))
  90. s2h2=cov(x2h2)
  91.  
  92. s.hpooled2h2 = 1/3 * (s1+s2h2)
  93. solve(s.hpooled2h2)
  94.  
  95. t(obs2x2-barx1)%*%solve(s.hpooled2h2)%*%(obs2x2-barx1)
  96. t(obs2x2-barx2h2)%*%solve(s.hpooled2h2)%*%(obs2x2-barx2h2)
  97.  
  98. #Tercera observación de x3
  99.  
  100. obs2x3=as.matrix(x2[3,])
  101. x2h3=x2[1:2,]
  102. barx2h3=as.matrix(colMeans(x2h3))
  103. s2h3=cov(x2h3)
  104.  
  105. s.hpooled2h3 = 1/3 * (s1+s2h3)
  106. solve(s.hpooled2h3)
  107.  
  108. t(obs2x3-barx1)%*%solve(s.hpooled2h3)%*%(obs2x3-barx1)
  109. t(obs2x3-barx2h3)%*%solve(s.hpooled2h3)%*%(obs2x3-barx2h3)
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement