R Hotelling'的乘法矩阵;T2
因此,我有以下值R Hotelling'的乘法矩阵;T2,r,statistics,linear-algebra,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由于矩阵/向量的维数,它们在某些步骤中不能相乘。为此,我假设Y的尺寸实际上是250x10,否则您将得到与您描述的完全不同的错误(即,您将无法计算Ybar-u) 参见T2计算的细分: n
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由于矩阵/向量的维数,它们在某些步骤中不能相乘。为此,我假设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)
记住公式
与
,
,及
样本平均向量和空平均向量应为列向量,而不是行向量。在你的代码中,我想u什么是“stuff.matrix”?你能做一个可复制的例子(可能用随机数填充一些矩阵),这样我们可以剪切、粘贴和复制你的错误吗?提示:尝试T2计算的每一位,以缩小问题所在。
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)
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
n * t(Ybar - mu) %*% solve(S) %*% (Ybar - mu)
#> [,1]
#> [1,] 4.567061