0
votes

Error in diag(nrow(V) * tausq, nrow = ncol(V), ncol = ncol(V)) : 'nrow' or 'ncol' cannot be specified when 'x' is a matrix

This is the error I get when I try to run

D <- diag(nrow(V)*tausq, nrow=ncol(V), ncol=ncol(V))

which is part of a function I wrote.

It's the first line of the function and V is a matrix which is part of the argument.

What does this error mean?

1
Welcome to stack overflow, and thanks for sharing the code that gave you the error. Could you please update your question to also include some sample data? Since we don't have V or tausq, we can't run the code in your question and get the same output. Posting something like the output of dput(V) and dput(tausq) would be ideal.josliber

1 Answers

1
votes

From ?diag, you can read that the function does one of two things -- it either extracts the main diagonal of a passed matrix or it constructs a new diagonal matrix. If you are trying to extract the main diagonal of a matrix (by passing a matrix as the first argument to diag), then you can't pass the nrow and ncol arguments to the diag function.

The error is telling you that apparently nrow(V)*tausq is a matrix in your code -- since nrow(V) is a constant, we conclude that tausq must be a matrix. As a result, it's giving you an error due to setting nrow and ncol.

Long story short -- you seem to be assuming nrow(V)*tausq is a constant, but in fact it's a matrix. You need to convert tausq to a constant to proceed as you want to.