[R] Error in mvpart example

Peter Ehlers
Sat, 27 Feb 2010 14:28:19 -0000

On 2010-02-26 6:55, Wearn, Oliver wrote:
> Dear all,
>
> I'm getting an error in one of the stock examples in the
'mvpart' package. I tried:
>
> require(mvpart)
> data(spider)
> fit3<-
rpart(gdist(spider[,1:12],meth="bray",full=TRUE,sq=TRUE)~water+twigs+reft+herbs+moss+sand,spider,method="dist")
#directly from ?rpart
> summary(fit3)
>
> ...which returned the following:
>
> Error in apply(formatg(yval, digits - 3), 1, paste, collapse =
",", sep = "") :
>    dim(X) must have a positive length
>
> This seems to be a problem with the cross-validation, since the
"xerror" and "xstd" columns are missing from the summary
table as well.
>
> Using the mpart() wrapper results in the same error:
>
>
fit4<-mvpart(gdist(spider[,1:12],meth="bray",full=TRUE,sq=TRUE)~water+twigs+reft+herbs+moss+sand,spider,method="dist")
> summary(fit4)
>
> Note, changing the 'method' argument to ="mrt" seems,
superficially, to solve the problem. However, when the dependent variable is a
dissimilarity matrix, shouldn't method="dist" be used (as per the
examples)?
>
> Thanks, in advance, for any help on this error.
>
> Oliver

The cross-validation idea is a red herring; the documentation clearly
states:
   Weights and cross-validation are currently not implemented for
   method="dist".

The error message provides a clue: apply() is not happy with what it's
being fed. Since it mentions "dim", we can guess that the problem is
with the X in apply(X, .....).
This in turn suggests that formatg() may not be returning an array
and indeed in your example it returns a vector. I don't know what will
be broken if the last line in formatg() is changed to force the
returned value to be a matrix, but this will work for your example:


formatg <-
function(x, digits= unlist(options('digits')),
                  format= paste("%.", digits, "g",
sep='')) {
     if (!is.numeric(x)) stop("x must be a numeric vector")

     n <- length(x)
     #
     # the resultant strings could be up to 8 characters longer,
     #   assume that digits =4,  -0.dddde+104 is a worst case, where
     #   dddd are the 4 significant digits.
     dummy  <- paste(rep(" ", digits+8), collapse='')
     temp <- .C("formatg", as.integer(n),
                       as.double(x),
                           rep(format,n),
                           out= rep(dummy, n), NAOK=TRUE,
                                    PACKAGE="mvpart")$out
     if (is.matrix(x)) matrix(temp, nrow=nrow(x))
#    else temp
     else matrix(temp, nrow=1)
     }

Source this and

   summary(fit3)

seem to return reasonable values.

   -Peter Ehlers