How to debug my code in my matrix calculation?

x1=c(131,125,131,119,136,138,139,125,131,134,129,134,126,132,141,131,135,132,139,132,126,135,134,128,130,138,128,127,131,124,124,133,138,148,126,135,132,133,131,133,133,131,131,138,130,131,138,123,130,134,137,126,135,129,134,131,132,130,135,130,137,129,132,130,134,140,138,136,136,126,137,137,136,137,129,135,129,134,138,136,132,133,138,130,136,134,136,133,138,138)
x2=c(138,131,132,132,143,137,130,136,134,134,138,121,129,136,140,134,137,133,136,131,133,135,124,134,130,135,132,129,136,138,138,134,134,129,124,136,145,130,134,125,136,139,136,134,136,128,129,131,129,130,136,131,136,126,139,134,130,132,132,128,141,133,138,134,134,133,138,145,131,136,129,139,126,133,142,138,135,125,134,135,130,131,137,127,133,123,137,131,133,133)
 x3=c(89,92,99,96,100,89,108,93,102,99,95,95,109,100,100,97,103,93,96,101,102,103,93,103,104,100,93,106,114,101,101,97,98,104,95,98,100,102,96,94,103,98,99,98,104,98,107,101,105,93,106,100,97,91,101,90,104,93,98,101,96,93,87,106,96,98,95,99,92,95,100,97,101,90,104,102,92,90,96,94,91,100,94,99,91,95,101,96,100,91)
x4=c(49,48,50,44,54,56,48,48,51,51,50,53,51,50,51,54,50,53,50,49,51,47,53,50,49,55,53,48,54,46,48,48,45,51,45,52,54,48,50,46,53,51,56,49,53,45,53,51,47,54,49,48,52,50,49,53,50,52,54,51,52,47,48,50,45,50,47,55,46,56,53,50,50,49,47,55,50,60,51,53,52,50,51,45,49,52,54,49,55,46)
data <- data.frame(x1,x2,x3,x4)
q523 <- data[1:30,]

My first question is if I run mean command (I want to get the mean vector of each column), I cannot get the desired outcome.

> mean(q523)
[1] NA
Warning message:
In mean.default(q523) : argument is not numeric or logical: returning NA

So I turn to the silly method to get mean vector.

x <- matrix(c(mean(q523$x1), mean(q523$x2), mean(q523$x3), mean(q523$x4)), ncol=1)
S = cov(q523)
(q523[1,] - x) %*% solve(S)
Error in (q523[1, ] - x) %*% solve(S) : 
  requires numeric/complex matrix/vector arguments

Oops, another error.

Hi, the simplest solution is to use lapply() to have it iterate through each column: lapply(q523, mean), and if you want to get a vector of only the results, you can use as.numeric() to wrap that first command: as.numeric(lapply(q523, mean))

Here is a full example:

x1=c(131,125,131,119,136,138,139,125,131,134,129,134,126,132,141,131,135,132,139,132,126,135,134,128,130,138,128,127,131,124,124,133,138,148,126,135,132,133,131,133,133,131,131,138,130,131,138,123,130,134,137,126,135,129,134,131,132,130,135,130,137,129,132,130,134,140,138,136,136,126,137,137,136,137,129,135,129,134,138,136,132,133,138,130,136,134,136,133,138,138)
x2=c(138,131,132,132,143,137,130,136,134,134,138,121,129,136,140,134,137,133,136,131,133,135,124,134,130,135,132,129,136,138,138,134,134,129,124,136,145,130,134,125,136,139,136,134,136,128,129,131,129,130,136,131,136,126,139,134,130,132,132,128,141,133,138,134,134,133,138,145,131,136,129,139,126,133,142,138,135,125,134,135,130,131,137,127,133,123,137,131,133,133)
x3=c(89,92,99,96,100,89,108,93,102,99,95,95,109,100,100,97,103,93,96,101,102,103,93,103,104,100,93,106,114,101,101,97,98,104,95,98,100,102,96,94,103,98,99,98,104,98,107,101,105,93,106,100,97,91,101,90,104,93,98,101,96,93,87,106,96,98,95,99,92,95,100,97,101,90,104,102,92,90,96,94,91,100,94,99,91,95,101,96,100,91)
x4=c(49,48,50,44,54,56,48,48,51,51,50,53,51,50,51,54,50,53,50,49,51,47,53,50,49,55,53,48,54,46,48,48,45,51,45,52,54,48,50,46,53,51,56,49,53,45,53,51,47,54,49,48,52,50,49,53,50,52,54,51,52,47,48,50,45,50,47,55,46,56,53,50,50,49,47,55,50,60,51,53,52,50,51,45,49,52,54,49,55,46)
data <- data.frame(x1,x2,x3,x4)
q523 <- data[1:30,]

lapply(q523, mean)
#> $x1
#> [1] 131.3667
#> 
#> $x2
#> [1] 133.6
#> 
#> $x3
#> [1] 99.16667
#> 
#> $x4
#> [1] 50.53333

as.numeric(lapply(q523, mean))
#> [1] 131.36667 133.60000  99.16667  50.53333

Created on 2021-11-03 by the reprex package (v2.0.1)

I think you want your data to be a matrix and not a data.frame.

x1=c(131,125,131,119,136,138,139,125,131,134,129,134,126,132,141,131,135,132,139,132,126,135,134,128,130,138,128,127,131,124,124,133,138,148,126,135,132,133,131,133,133,131,131,138,130,131,138,123,130,134,137,126,135,129,134,131,132,130,135,130,137,129,132,130,134,140,138,136,136,126,137,137,136,137,129,135,129,134,138,136,132,133,138,130,136,134,136,133,138,138)
x2=c(138,131,132,132,143,137,130,136,134,134,138,121,129,136,140,134,137,133,136,131,133,135,124,134,130,135,132,129,136,138,138,134,134,129,124,136,145,130,134,125,136,139,136,134,136,128,129,131,129,130,136,131,136,126,139,134,130,132,132,128,141,133,138,134,134,133,138,145,131,136,129,139,126,133,142,138,135,125,134,135,130,131,137,127,133,123,137,131,133,133)
x3=c(89,92,99,96,100,89,108,93,102,99,95,95,109,100,100,97,103,93,96,101,102,103,93,103,104,100,93,106,114,101,101,97,98,104,95,98,100,102,96,94,103,98,99,98,104,98,107,101,105,93,106,100,97,91,101,90,104,93,98,101,96,93,87,106,96,98,95,99,92,95,100,97,101,90,104,102,92,90,96,94,91,100,94,99,91,95,101,96,100,91)
x4=c(49,48,50,44,54,56,48,48,51,51,50,53,51,50,51,54,50,53,50,49,51,47,53,50,49,55,53,48,54,46,48,48,45,51,45,52,54,48,50,46,53,51,56,49,53,45,53,51,47,54,49,48,52,50,49,53,50,52,54,51,52,47,48,50,45,50,47,55,46,56,53,50,50,49,47,55,50,60,51,53,52,50,51,45,49,52,54,49,55,46)
data <- as.matrix(data.frame(x1,x2,x3,x4))
q523 <- data[1:30,]
apply(q523, 2, mean)
#>        x1        x2        x3        x4 
#> 131.36667 133.60000  99.16667  50.53333
S = cov(q523)
S
#>           x1         x2         x3         x4
#> x1 26.309195  4.1517241  0.4540230  7.2459770
#> x2  4.151724 19.9724138 -0.7931034  0.3931034
#> x3  0.454023 -0.7931034 34.6264368 -1.9195402
#> x4  7.245977  0.3931034 -1.9195402  7.6367816

Created on 2021-11-03 by the reprex package (v2.0.1)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.