Multiplying Matrices for Hotelling's T2 [closed]
up vote
-1
down vote
favorite
So, I have the following values
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
The following is my code:
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)
I'm trying to multiply the matrices as you see in T2
but I just keep getting an error
non-conformable arguments
r statistics linear-algebra
closed as off-topic by G. Grothendieck, Spacedman, phiver, jogo, Rui Barradas Nov 11 at 19:57
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – G. Grothendieck, phiver, jogo, Rui Barradas
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-1
down vote
favorite
So, I have the following values
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
The following is my code:
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)
I'm trying to multiply the matrices as you see in T2
but I just keep getting an error
non-conformable arguments
r statistics linear-algebra
closed as off-topic by G. Grothendieck, Spacedman, phiver, jogo, Rui Barradas Nov 11 at 19:57
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – G. Grothendieck, phiver, jogo, Rui Barradas
If this question can be reworded to fit the rules in the help center, please edit the question.
what's "stuff.matrix"? Can you make a reproducible example (maybe fill some matrices with random numbers) so we can cut and paste and duplicate your error?
– Spacedman
Nov 10 at 18:24
hint: try each bit of the T2 calculation to narrow down where the problem is.
– Spacedman
Nov 10 at 18:30
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
So, I have the following values
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
The following is my code:
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)
I'm trying to multiply the matrices as you see in T2
but I just keep getting an error
non-conformable arguments
r statistics linear-algebra
So, I have the following values
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
The following is my code:
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)
I'm trying to multiply the matrices as you see in T2
but I just keep getting an error
non-conformable arguments
r statistics linear-algebra
r statistics linear-algebra
edited Nov 11 at 5:17
Al Fahad
739621
739621
asked Nov 10 at 17:14
haramassive
456
456
closed as off-topic by G. Grothendieck, Spacedman, phiver, jogo, Rui Barradas Nov 11 at 19:57
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – G. Grothendieck, phiver, jogo, Rui Barradas
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by G. Grothendieck, Spacedman, phiver, jogo, Rui Barradas Nov 11 at 19:57
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – G. Grothendieck, phiver, jogo, Rui Barradas
If this question can be reworded to fit the rules in the help center, please edit the question.
what's "stuff.matrix"? Can you make a reproducible example (maybe fill some matrices with random numbers) so we can cut and paste and duplicate your error?
– Spacedman
Nov 10 at 18:24
hint: try each bit of the T2 calculation to narrow down where the problem is.
– Spacedman
Nov 10 at 18:30
add a comment |
what's "stuff.matrix"? Can you make a reproducible example (maybe fill some matrices with random numbers) so we can cut and paste and duplicate your error?
– Spacedman
Nov 10 at 18:24
hint: try each bit of the T2 calculation to narrow down where the problem is.
– Spacedman
Nov 10 at 18:30
what's "stuff.matrix"? Can you make a reproducible example (maybe fill some matrices with random numbers) so we can cut and paste and duplicate your error?
– Spacedman
Nov 10 at 18:24
what's "stuff.matrix"? Can you make a reproducible example (maybe fill some matrices with random numbers) so we can cut and paste and duplicate your error?
– Spacedman
Nov 10 at 18:24
hint: try each bit of the T2 calculation to narrow down where the problem is.
– Spacedman
Nov 10 at 18:30
hint: try each bit of the T2 calculation to narrow down where the problem is.
– Spacedman
Nov 10 at 18:30
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
Your matrices/vectors cant be multiplied at certain steps because of their dimensions. For this I'm assuming the dimensions of Y
is actually 250x10, otherwise you'll get a completely different error than the one you're describing (i.e. you won't be able to calculate Ybar-u
)
See the breakdown of the T2 calculation:
n*t(Ybar-u)
gives you a 10x1 vector
but solve(S)
gives you a 10x10 matrix
so you have to transpose the vector before you calculate the product:
t(n*t(Ybar-u)) %*% solve(S)
That then results in a 1x10
But the last part, (Ybar-u)
is also a 1x10, so you need to transpose that before you multiply by the previous bit
The following, then, should give you your T2 value:
t(n*t(Ybar-u)) %*% solve(S) %*% t(Ybar-u)
add a comment |
up vote
1
down vote
Remember the formula
with
,
, and
sample mean vector and null mean vector should be column vector, not row vector. In your code, I think u <- c()
, rather than cbind()
. cbind()
makes row vector. For detail,
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
Then the sample mean vector:
(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
and covariance matrix:
(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
Calculating Hotelling's T-square
n * t(Ybar - mu) %*% solve(S) %*% (Ybar - mu)
#> [,1]
#> [1,] 4.567061
Since mu
is vector, the T2
has been calculated.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Your matrices/vectors cant be multiplied at certain steps because of their dimensions. For this I'm assuming the dimensions of Y
is actually 250x10, otherwise you'll get a completely different error than the one you're describing (i.e. you won't be able to calculate Ybar-u
)
See the breakdown of the T2 calculation:
n*t(Ybar-u)
gives you a 10x1 vector
but solve(S)
gives you a 10x10 matrix
so you have to transpose the vector before you calculate the product:
t(n*t(Ybar-u)) %*% solve(S)
That then results in a 1x10
But the last part, (Ybar-u)
is also a 1x10, so you need to transpose that before you multiply by the previous bit
The following, then, should give you your T2 value:
t(n*t(Ybar-u)) %*% solve(S) %*% t(Ybar-u)
add a comment |
up vote
1
down vote
Your matrices/vectors cant be multiplied at certain steps because of their dimensions. For this I'm assuming the dimensions of Y
is actually 250x10, otherwise you'll get a completely different error than the one you're describing (i.e. you won't be able to calculate Ybar-u
)
See the breakdown of the T2 calculation:
n*t(Ybar-u)
gives you a 10x1 vector
but solve(S)
gives you a 10x10 matrix
so you have to transpose the vector before you calculate the product:
t(n*t(Ybar-u)) %*% solve(S)
That then results in a 1x10
But the last part, (Ybar-u)
is also a 1x10, so you need to transpose that before you multiply by the previous bit
The following, then, should give you your T2 value:
t(n*t(Ybar-u)) %*% solve(S) %*% t(Ybar-u)
add a comment |
up vote
1
down vote
up vote
1
down vote
Your matrices/vectors cant be multiplied at certain steps because of their dimensions. For this I'm assuming the dimensions of Y
is actually 250x10, otherwise you'll get a completely different error than the one you're describing (i.e. you won't be able to calculate Ybar-u
)
See the breakdown of the T2 calculation:
n*t(Ybar-u)
gives you a 10x1 vector
but solve(S)
gives you a 10x10 matrix
so you have to transpose the vector before you calculate the product:
t(n*t(Ybar-u)) %*% solve(S)
That then results in a 1x10
But the last part, (Ybar-u)
is also a 1x10, so you need to transpose that before you multiply by the previous bit
The following, then, should give you your T2 value:
t(n*t(Ybar-u)) %*% solve(S) %*% t(Ybar-u)
Your matrices/vectors cant be multiplied at certain steps because of their dimensions. For this I'm assuming the dimensions of Y
is actually 250x10, otherwise you'll get a completely different error than the one you're describing (i.e. you won't be able to calculate Ybar-u
)
See the breakdown of the T2 calculation:
n*t(Ybar-u)
gives you a 10x1 vector
but solve(S)
gives you a 10x10 matrix
so you have to transpose the vector before you calculate the product:
t(n*t(Ybar-u)) %*% solve(S)
That then results in a 1x10
But the last part, (Ybar-u)
is also a 1x10, so you need to transpose that before you multiply by the previous bit
The following, then, should give you your T2 value:
t(n*t(Ybar-u)) %*% solve(S) %*% t(Ybar-u)
edited Nov 11 at 1:42
answered Nov 10 at 23:04
Simon
4,11863070
4,11863070
add a comment |
add a comment |
up vote
1
down vote
Remember the formula
with
,
, and
sample mean vector and null mean vector should be column vector, not row vector. In your code, I think u <- c()
, rather than cbind()
. cbind()
makes row vector. For detail,
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
Then the sample mean vector:
(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
and covariance matrix:
(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
Calculating Hotelling's T-square
n * t(Ybar - mu) %*% solve(S) %*% (Ybar - mu)
#> [,1]
#> [1,] 4.567061
Since mu
is vector, the T2
has been calculated.
add a comment |
up vote
1
down vote
Remember the formula
with
,
, and
sample mean vector and null mean vector should be column vector, not row vector. In your code, I think u <- c()
, rather than cbind()
. cbind()
makes row vector. For detail,
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
Then the sample mean vector:
(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
and covariance matrix:
(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
Calculating Hotelling's T-square
n * t(Ybar - mu) %*% solve(S) %*% (Ybar - mu)
#> [,1]
#> [1,] 4.567061
Since mu
is vector, the T2
has been calculated.
add a comment |
up vote
1
down vote
up vote
1
down vote
Remember the formula
with
,
, and
sample mean vector and null mean vector should be column vector, not row vector. In your code, I think u <- c()
, rather than cbind()
. cbind()
makes row vector. For detail,
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
Then the sample mean vector:
(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
and covariance matrix:
(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
Calculating Hotelling's T-square
n * t(Ybar - mu) %*% solve(S) %*% (Ybar - mu)
#> [,1]
#> [1,] 4.567061
Since mu
is vector, the T2
has been calculated.
Remember the formula
with
,
, and
sample mean vector and null mean vector should be column vector, not row vector. In your code, I think u <- c()
, rather than cbind()
. cbind()
makes row vector. For detail,
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
Then the sample mean vector:
(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
and covariance matrix:
(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
Calculating Hotelling's T-square
n * t(Ybar - mu) %*% solve(S) %*% (Ybar - mu)
#> [,1]
#> [1,] 4.567061
Since mu
is vector, the T2
has been calculated.
answered Nov 11 at 10:43
Blended
38117
38117
add a comment |
add a comment |
what's "stuff.matrix"? Can you make a reproducible example (maybe fill some matrices with random numbers) so we can cut and paste and duplicate your error?
– Spacedman
Nov 10 at 18:24
hint: try each bit of the T2 calculation to narrow down where the problem is.
– Spacedman
Nov 10 at 18:30