Skip to content

Distance matrix based on correlation coefficient

2 messages · Dragos Zaharescu, Gavin Simpson

#
On Wed, 2010-07-21 at 05:38 -0700, Dragos Zaharescu wrote:
It is important to read the help pages carefully! I've never used fso()
nor had it installed on my home PC but it took just 30 seconds to see
what the problem was by reading the help for fso()
....
Arguments:

 formula: a formula in the form of ~x+y+z (no LHS)

     dis: a dist object such as that returned by ?dist?, ?dsvdis?, or
          ?vegdist?

    data: a data frame that holds variables listed in the formula
....

You are supplying a /matrix/ as argument 'dis' and this is *not* what is
returned by any of the functions listed in the argument summary for
'dis'. I showed you this with the last line of my initial response:

dis <- as.dist(sqrt(2-2 * cor(MET, method="spearman"))

## or, if you want 1 - dis, and reusing your code, do

dis <- as.dist(1 - sim)

will do what you want (the former following Jari's canonical
transformation of correlation to distance).

See further below...
<snip />
Notice that this is a /matrix/. fso wants a 'dist' object. It could be a
bit clearer in the error message. Perhaps email Dave Roberts and suggest
a rewording for the error?
Hmm, you should rarely ever need to attach - I'm guessing but the above
warnings probably arise from multiple copies of CC on your search path
due to repeated attach()ing? This can be a recipe for disaster...
... and totally not needed. In the formula method this is what argument
'data' is for.

z <- fso(~ SpringFreezLevel + PrecipitDays + SnowDays, data = CC,
         dis = dis, permute = 1000)

will fit the model without the attach()ing mess. Learning how to use R's
formula interface is a key skill to get right early on.

Also, when not in formulas, consider looking at function with() so that
instead of 

attach(CC); mean(SnowDays)

or 

mean(CC$SnowDays)

you can do

with(CC, mean(SnowDays))

The latter is very explicit about what you want it to do and you don't
need those horrible (IMHO) $ operators.
HTH

G