apply statement applied to a 2x3 matrix gives an error when used with a function

OUTPUT:
tableStats (class=list)

[[1]]
[,1] [,2] [,3]
[1,] 170 30 0.1500000
[2,] 59 86 0.4068966

[[2]]
[,1] [,2] [,3]
[1,] 0.000000 0.000000 0.00000000
[2,] 2.828427 2.828427 0.01950639

[[3]]
[,1] [,2] [,3]
[1,] 0 0 0.0000000
[2,] 2 2 0.0137931

OUTPUT:
mean.mat (class=matrix or array)
[,1] [,2] [,3]
[1,] 170 30 0.1500000
[2,] 59 86 0.4068966

dim(mean.mat)  =  2 3
Error2.f <- function(mat)
1 - sum(diag(mat[,1:2]))/sum(mat[,1:2])
apply(mean.mat,1:2,Error2.f)

Error in mat[, 1:2] : incorrect number of dimensions
Why is this not operating on first two columns of mean.mat?

Thanks. M

your apply says to take each cell in turn MARGINS=c(1,2) and apply a function that expects a matrix input ; but its not a matrix input, its a scalar value, the value of a cell of your matrix input

I am not sure how to fix this. rewrite error function or refer to matrix differently.

We would have to know what your intentions are in order to best advise you

matrix .mat is a 2x3 matix
I want to work on only first 2 columns ; let's call it matrix2
Then I want to compute error rate using function seen in the code.

Doing it this way allows for computing the error rate for a matrix of three variables (matrix3).
matrix3 <- {a b c, d e f,g h i}.
I have not worked with such a problem but it seems reasonable that off diagonal elements are components of misclassification.

Latest try:

mean.mat

output:
[,1] [,2] [,3]
[1,] 151.94878 28.05122 0.1559543
[2,] 55.23124 75.26876 0.4234487

error.rate.f <- function(mat){
1- sum(diag(mat))/sum(mat)}
apply(mean.mat, 1:2, error.rate.f)
        [,1]        [,2] [,3]

[1,] 0.006244078 0.001825946 1
[2,] 0.004186761 0.003570671 1

Desired output: error rate = (28.05122+55.23124)/345. = .24140

I need to understand what is going on here. 1:2 seems to be a problem in the apply statement but I don't know what to change it to?

M

Is it your intention to use the error function once, or multiple times ? If once you dont need to use apply at all.

I will want to use error function multiple times.

With respect to the single 2x3 matrix ? Then, if
If so, which subsets would you wish to apply it to ?

I would like to apply it to the 4 cells with indices 11,12,21,22 leaving column with indices 13,23 unmodified or removed

(m1 <- matrix(c(151.94878, 28.05122 ,0.1559543,
55.23124, 75.26876 ,0.4234487), ncol = 3,byrow = TRUE))

(subset_of_interest <- m1[1:2,1:2])

error.rate.f <- function(mat){
  1- sum(diag(mat))/sum(mat)}

error.rate.f(subset_of_interest)

Subsetting is exactly what I wanted to do. I was getting confused with the 1:2 referring to rows and columns of the subsetted matrix and the 1:2 in the apply statement.

I'm finding the apply statement to be confusing but I'm continuing to work at its interpretation.

Thank you very much.
M

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.