Monday, April 4, 2011

Bug fix in phyl.resid()

I just updated a couple of minor issues with phyl.resid().

First, I realized that with a very minor change to the code I could allow the function to accept multiple variables for x. This would be useful for multivariable regression and size correction. Basically, in the method="lambda" portion of the code, I simply changed:

beta<-matrix(NA,2,ncol(Y),dimnames=list(colnames(X),colnames(Y)))

to:

beta<-matrix(NA,ncol(X),ncol(Y),dimnames=list(colnames(X),colnames(Y)))

(method="BM", the default, already worked for multiple x or size variables without modification).

Second, in making this change I realized that there was a small error when the number of y variables was only one. Again, this affected method="lambda". It turns out that although for matrix Y, when:

> Y
        [,1]       [,2]
5  2.5386828 -0.3796148
3  0.8730943 -1.7258868
4  0.6358844  0.4417738
1 -0.9690405 -0.6481676
2 -0.4468455 -0.8031442
> Y<-Y[as.character(1:5),]
> Y
        [,1]       [,2]
1 -0.9690405 -0.6481676
2 -0.4468455 -0.8031442
3  0.8730943 -1.7258868
4  0.6358844  0.4417738
5  2.5386828 -0.3796148


(i.e., in reordering a matrix stays as a matrix). However, for:

> Y
        [,1]
5  2.5386828
3  0.8730943
4  0.6358844
1 -0.9690405
2 -0.4468455
> Y<-Y[as.character(1:5),]
> Y
         1          2          3          4          5 
-0.9690405 -0.4468455  0.8730943  0.6358844  2.5386828


Thus, for the same operation, R effectively changes a matrix into a vector. This makes the expression ncol(Y)=NULL, which is obviously no good.

To fix this I just added a call to as.matrix(...) around my reordering of Y, i.e.:

Y<-as.matrix(Y[tree$tip.label,])

The updated function (v0.2) is on my R-phylogenetics page. Direct link to code here.

1 comment:

  1. Note that we never fit a model with multiple Y variables; rather the number of columns of Y determines the number of separate models that we fit. This just allows us to, say, correct for size using phylogenetic regression for a set of different morphological traits simultaneously.

    ReplyDelete

Note: due to the very large amount of spam, all comments are now automatically submitted for moderation.