Hotelling T2的乘法矩阵

时间:2018-11-10 17:14:58

标签: r statistics linear-algebra

所以,我有以下值

 u <- a 10x1 matrix
 Y <- a 10x250 matrix
 Ybar <- the column means of Y, a 10x1matrix
 n <- the length of Ybar
 S <- variance of y

以下是我的代码:

 u <- cbind(1,2,3,4,5,6,7,8,9,10)
 Y <- stuff.matrix
 Ybar <- colMeans(stuff.matrix)
 n <- length(Ybar)
 S <- var(Y)
 T2 <- n*t(Ybar-u)%*%solve(S)%*%(Ybar-u)

我正尝试将矩阵相乘,如您在T2中看到的那样,但我一直遇到错误

  

不一致的参数

2 个答案:

答案 0 :(得分:1)

由于矩阵/向量的维数,它们不能在某些步骤上相乘。为此,我假设Y的尺寸实际上是250x10,否则您将得到与所描述的错误完全不同的错误(即,您将无法计算Ybar-u

查看T2计算的明细:

n*t(Ybar-u)为您提供了10x1的矢量

但是solve(S)为您提供了10x10的矩阵

因此您必须在计算乘积之前转置向量:

t(n*t(Ybar-u)) %*% solve(S)

然后得出1x10

但是最后一部分(Ybar-u)也是1x10,因此您需要先对它进行转置,然后再乘以前一位

那么,以下内容应该为您提供T2值:

t(n*t(Ybar-u)) %*% solve(S) %*% t(Ybar-u)

答案 1 :(得分:1)

记住公式

enter image description here

使用

enter image description here

enter image description here

enter image description here

样本均值向量和空均值向量应为列向量,而不是行向量。在您的代码中,我认为是u <- c(),而不是cbind()cbind()构成行向量。有关详细信息,

mu <- 1:10 # just in vector form, not cbind
n <- 250

library(foreach)
set.seed(100)
stuff_matrix <-
  foreach(mean = mu, .combine = cbind) %do% {
    rnorm(n, mean = mean) # 10x250 matrix, j-th column population mean = j
  }

head(stuff_matrix)
#>       result.1 result.2 result.3 result.4 result.5 result.6 result.7
#> [1,] 0.4978076 1.228592 1.553713 3.944082 6.097650 4.516851 5.847401
#> [2,] 1.1315312 1.492658 3.315856 3.627955 6.181037 7.471557 7.464161
#> [3,] 0.9210829 1.729977 2.657252 4.474300 5.587511 6.156381 7.728932
#> [4,] 1.8867848 2.748117 1.068647 3.938295 6.076173 6.115914 7.410617
#> [5,] 1.1169713 2.668594 3.242821 1.872311 6.136653 6.478609 5.538853
#> [6,] 1.3186301 1.544701 2.637232 2.997763 5.760293 7.493758 6.981092
#>      result.8  result.9 result.10
#> [1,] 8.376403 10.348863 11.785226
#> [2,] 6.621152  8.770257  9.481647
#> [3,] 7.539515  7.267515 10.253921
#> [4,] 8.423786  8.372691  8.902402
#> [5,] 7.994807  8.759492 10.128915
#> [6,] 7.690636  7.662423 12.078510

然后是样本均值向量:

(Ybar <- colMeans(stuff_matrix))
#>  result.1  result.2  result.3  result.4  result.5  result.6  result.7 
#>  0.988688  1.936097  3.055288  4.087147  4.990908  5.986308  6.990446 
#>  result.8  result.9 result.10 
#>  8.048926  8.976961  9.995163

和协方差矩阵:

(S <- var(stuff_matrix))
#>              result.1     result.2     result.3    result.4     result.5
#> result.1   0.92858045 -0.023293593 -0.024779065 -0.08134832  0.016999154
#> result.2  -0.02329359  1.093821723 -0.029355823  0.00390076  0.019511651
#> result.3  -0.02477907 -0.029355823  1.065980338 -0.04811192  0.008041174
#> result.4  -0.08134832  0.003900760 -0.048111921  1.15907162 -0.116626977
#> result.5   0.01699915  0.019511651  0.008041174 -0.11662698  0.925901216
#> result.6   0.00890253  0.022505728  0.050931240  0.04569653  0.058137013
#> result.7   0.12743198 -0.049968418 -0.005315344 -0.05691760 -0.012378235
#> result.8  -0.05746120  0.081199620 -0.065209860  0.01852119 -0.096882114
#> result.9  -0.02708189 -0.006763137 -0.086584652  0.03334430 -0.004256071
#> result.10  0.02440036 -0.035886159 -0.052768514  0.04605898  0.026418037
#>               result.6     result.7     result.8     result.9   result.10
#> result.1   0.008902530  0.127431979 -0.057461196 -0.027081892  0.02440036
#> result.2   0.022505728 -0.049968418  0.081199620 -0.006763137 -0.03588616
#> result.3   0.050931240 -0.005315344 -0.065209860 -0.086584652 -0.05276851
#> result.4   0.045696527 -0.056917597  0.018521188  0.033344299  0.04605898
#> result.5   0.058137013 -0.012378235 -0.096882114 -0.004256071  0.02641804
#> result.6   1.053106430  0.022910130  0.003415378 -0.036031419 -0.14352911
#> result.7   0.022910130  1.033750387  0.044022251 -0.045046275  0.13470016
#> result.8   0.003415378  0.044022251  0.845688387  0.053563920 -0.07633182
#> result.9  -0.036031419 -0.045046275  0.053563920  1.058638250  0.01074519
#> result.10 -0.143529114  0.134700158 -0.076331823  0.010745193  1.14241143

计算Hotelling的T平方

n * t(Ybar - mu) %*% solve(S) %*% (Ybar - mu)
#>          [,1]
#> [1,] 4.567061

由于mu是矢量,因此已经计算出T2