[R] Calculating Kendall's tau

William Dunlap wdunlap at tibco.com
Thu Apr 2 17:21:02 CEST 2015


> MannKendalltau<- numeric(nc) simply makes MannKendalltau a single
> integer equal to nc; that doesn't look sensible when the next thing you
> do is treat MannKendalltau as a vector.

No, numeric(nc) makes a "numeric" (double precision) vector of length nc
filled with zeros.

Perhaps you were thinking of as.numeric(nc), which makes a numeric vector
of length one containing the value nc.


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Thu, Apr 2, 2015 at 7:06 AM, S Ellison <S.Ellison at lgcgroup.com> wrote:

> > I am analyzing trend  using Mann-kendall  test for 31 independent
> sample, each
> > sample  have 34 years dataset.  I supposed to find Kendall “tau” for each
> > sample. The data is arranged in column wise (I attached  the data).To
> find
> > Kendall tau, I wrote R script as:
> > ...
> > Anyone can tell me how can I get orderly displayed  “tau” value?
>
> Usually, in R, a hypothesis test returns an object, and you can extract an
> individual element of that object.
>
> MannKendall seems to be no exception. Looking at the help page, a
> MannKendall test returns...
> " A list with class Kendall.
> tau     Kendall’s tau statistic
> sl      two-sided p-value
> S       Kendall Score
> D       denominator, tau=S/D
> varS    variance of S"
>
> To get just tau, say something like
> MannKendalltau[i]<-MannKendall(y[,i])$tau
>
> But your code is a bit of a mess....
> MannKendalltau<- numeric(nc) simply makes MannKendalltau a single integer
> equal to nc; that doesn't look sensible when the next thing you do is treat
> MannKendalltau as a vector. R's been kind to you and extended
> MannKendalltau when you tried to add things to later, non-existent,
> elements, but it clearly wasn't the right thing to do. Look up ?numeric,
> and then look up ?vector for next time you want to set up an empty vector.
>
> Second, since MannKendall(y[,i]) ) returns a list object of class Kendall,
> MannKendalltau[i]<-MannKendall(y[,i]) assigns a whole  object containing 5
> values to each new element of your MannKendalltau. So your result is a list
> of lists.
>
> Finally, you don’t need a loop at all. On a data frame, sapply would work
> nicely, so (although I've not tested it) something like
>
> sapply(desta[,2:nc], 2, function(x) ManKendall(x)$tau)
>
> ought to do the whole thing in one shot and package it nicely into a named
> vector while it's about it.
>
> S Ellison
>
>
> *******************************************************************
> This email and any attachments are confidential. Any u...{{dropped:17}}



More information about the R-help mailing list