[R] < 0 x 0 matrix >

(Ted Harding) Ted.Harding at manchester.ac.uk
Sat Sep 5 22:26:36 CEST 2009


On 05-Sep-09 10:00:26, Markku Karhunen wrote:
>> On 04-Sep-09 10:45:27, Markku Karhunen wrote:
>>> True. Should have read ?diag.
>>>
>>> However, this provokes a more general question: Is there some way I
>>> can declare some scalar and _all its functions_ as matrices?
>>>
>>> For instance, I would like to
>>>
>>> A = as.matrix(0.98)
>>> B = function(A)
>>> C = diag(sqrt(B))
>>>
>>> so that all scalars are explicitly [1,1] matrices.
>>> BR, Markku
>>
>> Hmmm, it might be a good idea to explain why you want to do this.
>> For instance:
>>
>>   M <- matrix(c(1,2,3,4),nrow=2)
>>   c <- matrix(2,nrow=1)
>>   c%*%M
>>   # Error in c %*% M : non-conformable arguments
>>   c*M
>>   # Error in c * M : non-conformable arrays
>>   c+M
>>   # Error in c + M : non-conformable arrays
>>
>> So what would you want to use the [1,1]-matrix scalars for, that
>> cannot be done just using them as numbers?
>>
>> Ted.
> 
> Broadly speaking, I would like to use the same code for multivariate  
> and univariate cases. For instance, I use the inverse Wishart  
> densities of MCMCpack. If I take diwish(x) of a scalar x, the  
> programme crashes, because diwish() by default checks  
> ncol(x)==nrow(x). However, I would like to have an inverse gamma  
> density.
> 
> Best,
> Markku

I see. In such a case, it might be worth wrapping diwish() inside
a function of your own, which tests for 'x' being a scalar and,
if it is, converting it to a 1x1 matrix within the function.
For example:

  diWish <- function(x){
    if( all.equal(dim(x),c(1,1)) ) {X <- x} else
    if( (is.vector(x))&(length(x)==1) ) X <- as.matrix(x)
    diwish(X)
  }

(This may not be optimal, but it gives the idea).
Hoping this helps,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 05-Sep-09                                       Time: 21:26:29
------------------------------ XFMail ------------------------------




More information about the R-help mailing list