[R] function gdist, dist and vegdist in mvpart

Gavin Simpson gavin.simpson at ucl.ac.uk
Wed Mar 8 19:40:46 CET 2006

On Wed, 2006-03-08 at 19:12 +0100, Sina Muster wrote:
> Dear R community,
> What I am missing? Where is the problem?

That dist returns an object of class "dist", even if you use upper =
TRUE. ?dist says:

   upper: logical value indicating whether the upper triangle of the
          distance matrix should be printed by 'print.dist'.

Beware confusing the printed representation of the object with what the
object actually looks like.

The reason gdist works, is that is does not do what its help page says:


     Should be interchangeable with 'dist' and returns a distance
     object of the same type.

Which the following sequence illustrates does not happen (using the
spider dataset from mvpart):

> spider.dist <- gdist(spider[1:12,])
> class(spider.dist)
[1] "dist"
> spider.dist <- gdist(spider[1:12,], full = TRUE)
> class(spider.dist)
[1] "matrix"

You might consider contacting the maintainer and submit a bug report to
him about this...

So even though you thought the two (dist and gdist) were the same, they
are not.

To solve this problem then, a simple modification to your mvpart call is
required, as shown below:

> spider.dist2 <- dist(spider[, 1:12], method="minkowski", p=1, diag=T,
> class(spider.dist2)
[1] "dist"
> mvpart(spider.dist2~herbs+reft+moss+sand+twigs+water,spider)
Error in model.frame(formula, rownames, variables, varnames, extras,
extranames,  :
        variable lengths differ
> mvpart(as.matrix(spider.dist2)~herbs+reft+moss+sand+twigs

The key is the as.matrix() wrapped around the lhs of the formula.

It would have been useful if you'd included an reproducible example
using a built in data set - like I do above. Readers of the list wont
have your "ba12", so extra work is required to investigate the problem.

> Thanks for helping.
> Sina

You're welcome, hope this helps.


Gavin Simpson                     [T] +44 (0)20 7679 5522
ENSIS Research Fellow             [F] +44 (0)20 7679 7565
ENSIS Ltd. & ECRC                 [E] gavin.simpsonATNOSPAMucl.ac.uk
UCL Department of Geography       [W] http://www.ucl.ac.uk/~ucfagls/cv/
26 Bedford Way                    [W] http://www.ucl.ac.uk/~ucfagls/
London.  WC1H 0AP.

More information about the R-help mailing list