\name{WeatherRegime}
\alias{WeatherRegime}
\title{
Function for Calculating the Cluster analysis
}
\description{
This function computes the weather regimes from a cluster analysis. It can be applied over the dataset with dimensions 
c(year/month, month/day, lon, lat), or by using PCs obtained from the application of the EOFs analysis to filter the dataset.  
The cluster analysis can be performed with the traditional k-means or those methods included in the hclust (stats package). }
\usage{
WeatherRegime(data, EOFS = TRUE,  neofs=30, threshold = 90, lon = NULL, lat = NULL, ncenters = NULL, method = "kmeans",nstart = 30, listindex = NULL)
}
\arguments{
  \item{data}{
Array of data with dimensions c(year/month, month/day, lon, lat).
  }
  \item{EOFs}{
Whether to compute the EOFs (default = 'TRUE') or not (FALSE) over data.
  }
  \item{threshold}{
Value with the percentage of variance to be explained by the PCs. Only sufficient PCs to explain this much variance will be used in the clustering.
  }
  \item{lon}{
Vector of longitudes.
  }
  \item{lat}{
Vector of latitudes.
  }
  \item{ncenters}{
Number of clusters to be calculated with the clustering function.
  }
  \item{method}{
Different options to estimate the clusters. The most traditional approach is the k-means analysis (default=’kmeans’) 
but the function also support the different methods included in the hclust . These methods are:  
"ward.D", "ward.D2", "single", "complete", "average" (= UPGMA), "mcquitty" (= WPGMA), "median" (= WPGMC) or "centroid" (= UPGMC). 
For more details about these methods see the hclust function documentation included in the stats package.
  }
  \item{nstarts}{
Parameter for the cluster analysis determining how many random sets to choose (Only if method='kmeans' is selected).
  }
  \item{iter.max}
Parameter to select the maximum number of iterations allowed (Only if method='kmeans' is selected).
  }

  \item{ncores}{
The number of multicore threads to use for parallel computation}
  }
\value{
List with elements :\cr
\item{composite}{
 3-d array (x, y, k) containing the composites k=1,..,K for case (*1)
    or only k=1 for any specific cluster, i.e., case (*2).
}
\item{pvalue}{
 3-d array (x, y, k) containing the pvalue of the
    composites obtained through a t-test that accounts for the serial
    dependence of the data with the same structure as Composite.
}

\item{cluster}{
A time series of integers (from 1:k) indicating the cluster to which each point is allocated.
}

\item{center}{
A vector of integers (from 1:ncenters) indicating the cluster to which each point is allocated (only if method=’kmeans’ has been selected).
}

\item{cluster_lengths}{
Length of repeated values in the cluster time series (only if method=’kmeans’ has been selected).
}

\item{cluster_values}{
The value of the regime whose length is given in cluster_lengths (only if method=’kmeans’ has been selected)
}
\item{persistence}{ 
Percentage of days in a month/season before a cluster is replaced for a new one (only if method=’kmeans’ has been selected)
}
\item{frequency}{
Percentage of days in a month/season belonging to each cluster (only if method=’kmeans’ has been selected).
}
}
\examples{
See Weather Regime vignetes: https://earth.bsc.es/gitlab/es/s2dverification/blob/develop-Regimes/vignettes/WeatherRegimesAnalysis.md
}
\author{
History:\cr
1.0  -  2018-05  (A. Hunter, \email{alasdair.hunter at bsc.es} and V.Torralba, \email{veronica.torralba at bsc.es} ) -  Original code
}

\keyword{datagen}

