Classification of functional data. A mathematical optimization approach.
Abstract
Functional data appear more and more in modern societies, in which many processes are monitored in real time. Classification (both unsupervised and supervised) problems with such data are a challenging problem. In this work we describe some techniques for classification of functional data using Mathematical Optimization.
Full text
University of Seville Faculty of Mathematics Final Degree Project Mathematics Degree Classification of functional data. A mathematical optimization approach. Bel´en del Roc´ıo Garc´ıa Camino Supervised by Emilio Carrizosa Priego Statistics and Operational Research Department
Abstract Functional data appear more and more in modern societies, in which many processes are monitored in real time. Classification (both unsupervised and supervised) problems with such data are a challenging problem. In this work we describe some techniques for classification of functional data using Mathematical Optimization.
Resumen Cada vez es m´as frecuente el uso de datos funcionales en las sociedades modernas, en las que muchos de los procesos son monitorizados en tiempo real. Abordar el problema de clasificaci´on (tanto no supervisada como supervisada) con este tipo de datos es un desaf´ıo. En este trabajo se describen algunas t´ecnicas de clasificaci´on de datos funcionales mediante optimizaci´on matem´atica.
Contents 1 Introduction 5 2 Functional Data Introduction 6 2.1 Where do the data live ......................... 7 2.2 Processing the data: basis representation and smoothing ....... 7 2.3 Cubic Spline Interpolation ........................ 8 2.4 Sample mean and variance ........................ 9 2.5 Euclidean Distance and Dynamic Time Warping (DTW) Distance . . 10 3 Introduction to the Functional Data Classification 17 3.1 Supervised and unsupervised functional classification ......... 17 3.2 Unsupervised Classification ....................... 18 3.2.1 K-means Clustering ........................ 18 3.2.2 Gaussian Mixtures ........................ 18 3.3 Supervised classification ......................... 19 3.3.1 k-Nearest Neighbors Method ................... 19 3.3.2 Prototype Methods ........................ 19 3.4 K-fold cross-validation .......................... 20 4 Automatic procedures by R 21 4.1 Introduction to splines2 ........................ 21 4.2 Conversion of raw data or other functional data classes into fdata class 21 4.3 Central and dispersion measures for functional data .......... 22 4.4 Derivatives of functional data ...................... 24 4.5 Euclidean distance matrix computation ................. 25 4.6 Dynamic time warping distance matrix ................. 26 4.7 Functional Classification using K-fold cross-validation ......... 27 4.8 k-Nearest Neighbors classifier from Functional Data .......... 27 4.9 Predicts from a fitted classif object ................... 28 4.10 K-means clustering for functional data ................. 29 5 Experiments 31 5.1 growth dataset .............................. 31 1
5.1.1 Data description ......................... 31 5.1.2 Descriptive statistics ....................... 34 5.1.3 Unsupervised classification .................... 45 5.1.4 Supervised classification ..................... 47 5.2 CanadianWeather dataset ........................ 49 5.2.1 Data description ......................... 49 5.2.2 Descriptive statistics ....................... 54 5.2.3 Unsupervised classification .................... 60 5.2.4 Supervised classification ..................... 61 References 64 2
List of Figures 5.1 Measures of central tendency by groups ................. 35 5.2 Mean, trimmed mean and median using FM criteria, mode criteria and RP criteria. .............................. 36 5.3 Measures of dispersion: variance, trimmed variance using FM criteria, FM criteria with trimmed parameter trim = 0.1, mode criteria and RP criteria. ................................ 37 5.4 Comparing “boys” and “girls” curves with their mean curves. . . . . 38 5.5 First and second order derivatives for “boys” functional data ..... 40 5.6 First and second order derivatives for “girls” functional data ..... 41 5.7 Euclidean distance between ”boys” and ”girls” ............. 42 5.8 DTW distances between ”boys” and ”girls ............... 43 5.9 Figure 5.8 expanded in [0,20]×[0,1000]. ................. 44 5.10 Warping path between the first boy in fdata.m and the first girl in fdata.f................................... 45 5.11 Red lines correspond to those curves classified as “boy” and green lines to “girls”. .............................. 46 5.12 Average daily temperature for each day of the year at 35 different weather stations in Canada (left plot) and average daily rainfall for each day of the year rounded to 0.1 mm at 35 different weather stations in Canada. .............................. 54 5.13 Coordinates of the 35 different weather stations in Canada. In red those that belong to ”Atlantic” climate zone, green corresponds to ”Continental” climate zone, black is for ”Arctic” region and blue, those that belong to ”Pacific” zone (left plot). At right the point shapes vary depending on the province: the places belong to ”Quebec” are represented with △, + correspond with those that are in ”British Colombia” province, ×match with ”Northwest Territories”. The rest of provinces are assigned with #................ 55 3
5.14 Average temperature in degrees Celsius for each month of the year (left plot) and average daily precipitation in millimeters for each month of the year (right plot). ...................... 55 5.15 Average monthly temperature by region (left plot) and average monthly precipitation in millimeters by region (right plot). ........... 57 5.16 Monthly temperatures first derivatives (left plot) and monthly precipitations first derivatives. ........................ 58 5.17 Euclidean distances between 35 places in Canada according to temperature (left plot) and their precipitations (right plot). ........ 60 5.18 DTW distances between 35 places in Canada according to temperature (left plot) and their precipitations (right plot). .......... 61 5.19 K-means. Average monthly temperature curves colored depending on the group in which they are classified. ................ 62 5.20 K-means. Average monthly precipitation curves colored depending on the group in which they are classified. ................ 62 4
Chapter 1 Introduction This work analyses classification procedures for functional data. To do that, we first review (Chapter 2) some basic ideas of Functional Data, then we address in Chapter 3 some basic classification procedures for such data, and then we describe in Chapter 4 the Rroutines enabling us to perform classification. The work ends with Chapter 5, in which we illustrate the approach with two real datasets growth and CanadianWeather (fda package). 5
Chapter 2. Functional Data Introduction 12 Example 2.8 Let pan (4,3)-warping path p= (p1, p2, p3, p4) with pℓ= (nℓ, mℓ) for ℓ∈ {1,2,3,4}and p1= (1,1), p2= (2,2), p3= (3,3), p4= (4,3). Then n1= 1, m1= 1 n2= 2, m2= 2 n3= 3, m3= 3 n4= 4, m4= 3 (2.13) The warping path pdefines an alignment between two sequences X= (x1, x2, x3, x4) and Y= (y1, y2) by assigning xnℓof Xto the element ymℓfor ℓ∈ {1,2,3,4}. The assignment is xn1=x1to ym1=y1 xn2=x2to ym2=y2 xn3=x3to ym3=y3 xn4=x4to ym4=y3 (2.14) The warping path above is shows in the next figure dev.new() plot(NA, xlim=c(0, 4), ylim=c(0, 3), xlab=’’, ylab=’’,xaxt="na",yaxt="na") rect(0, 0, 1, 1, col=’red’) rect(1, 1, 2, 2, col="red") rect(2, 2, 3, 3, col="red") rect(3, 2, 4, 3, col="red") # Vertical grid axis(1, at = c(0:4), tck = 1, lty = 2, col = "gray") # Horizontal grid axis(2, at = c(0:3), tck = 1, lty = 2, col = "gray", las=1) Definition 2.9 The total cost cp(X, Y )of a warping path between Xand Ywith respect to the local cost measure cis defined as cp(X, Y ) := L X ℓ=1 c(xnℓ, ymℓ).(2.15) FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 2. Functional Data Introduction 13 01234 0 1 2 3 ■ There are exponentially many warping paths that satisfy the above conditions. However, we are only interested in the path that minimizes the total cost. Definition 2.10 An optimal warping path between Xand Yis a warping path p∗having a minimal total cost among all possible warping paths. ■ Definition 2.11 The DTW distance DTW(X, Y )between Xand Yis then defined as the total cost of p∗: DTW(X, Y ) := cp∗(X, Y ) = min{cp(X, Y )|pis an (N, M)-warping path}(2.16) ■ As a result, time series with similar patterns occurring in different time periods are considered similar. To determine an optimal path p∗, one could test every possible warping path between Xand Y. The complexity of this procedure is exponential in the lengths Nand M. We will now introduce an O(NM) algorithm that is based on dynamic programming. To this end, we define the sequences X(1 : n) := (x1, x2, . . . , xn) for n∈ {1, . . . , N}and Y(1 : m) := (y1, y2, . . . , ym) for m∈ {1, . . . , M}and set D(n, m) = DTW(X(1 : n), Y (1 : m)).(2.17) The values D(n, m) define an N×Mmatrix D, which is also referred to as the accumulated cost matrix. One has X(1 : N) is equal to Xand Y(1 : M) is equal FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 2. Functional Data Introduction 14 to Y, so D(N, M) = DTW(X(1 : N), Y (1 : M)) = DTW(X, Y ). The next theorem shows how Dcan be computed efficiently. Theorem 2.12 The accumulated cost matrix Dsatisfies the following identities: D(n, 1) = n X k=1 c(xk, y1)forn∈ {1, . . . , N}, D(1, m) = m X k=1 c(x1, yk)form∈ {1, . . . , M}, and (2.18) D(n, m) = min{D(n−1, m −1) + D(n−1, m) + D(n, m −1)}+c(xn, ym) (2.19) for 1< n ≤Nand 1< m ≤M. In particular DTW(X, Y ) = D(N, M)can be computed with O(NM)operations. Proof. Let m= 1 and n∈ {1, . . . , N}. Then D(n, 1) = DTW(X(1 : n), Y (1 : 1)) = DTW(X(1 : n), y1) =cp∗(X(1 : n), y1) = n X k=1 c(xk, y1) (2.20) The first equality is given by the definition of an accumulated cost matrix element 2.19. The second is by the definition of sequence Y(1 : m) with m= 1 2.5. Then we use the definition of the DTW distance 2.11. There is only one possible warping path between X(1 : n) and Y(1 : 1) = y1given by p∗= ((1,1),(2,1),...,(n, 1)) that satisfies the three conditions of warping path definition 2.6, so it is the minimum of all the possible warping paths. Finally, we obtain the total cost of this warping path 2.9 as c(x1, y1) + c(x2, y1) + · · · +c(xn, y1) = n X k=1 c(xk, y1). Similarly, one obtains the formula for D(1, m) = D(1, m) = m X k=1 c(x1, yk) for m∈ {1, . . . , M}. Now, let n > 1 and m > 1 and let q= (q1, . . . , qL−1, qL) be an optimal path for X(1 : n) and Y(1 : m). Then the boundary condition implies qL= (n, m). Setting (a, b) := qL−1, the step size condition implies (a, b)∈ {(n−1, m −1),(n− 1, m),(n, m −1)}. Furthermore, it follows that (q1, . . . , qL−1) must be an optimal warping path for X(1 : a) and Y(1 : b) (otherwise, qwould not be optimal for FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 2. Functional Data Introduction 15 X(1 : n) and Y(1 : m)). Since D(n, m) = DTW(X(1 : n), Y (1 : m)) =cq(X(1 : n), Y (1 : m)) =c(q1,...,qL−1)(X(1 : a), Y (1 : b)) + c(xn, ym) =min{D(n, m −1), D(n−1, m), D(n−1, m −1)}+c(xn, ym) (2.21) The DTW distance between the sequences X(1 : n) and Y(1 : m) is the total cost of the optimal warping path q. This cost can be broken down into two addends: the total cost of the optimal warping path for X(1 : a) and Y(1 : b), where a=nor a=n−1 and b=mor b=m−1 attend the step size condition, and the local cost, for example, the Euclidean distance, between xnand ym. The election of aand b shall determine which of the next set {D(n, m −1), D(n−1, m), D(n−1, m −1)} corresponds whit the DTW distance, the one who gives the minimum.■ Theorem 2.12 facilitates a recursive computation of the matrix D. The initialization can be simplified by extending the matrix D with an additional row and column and formally setting D(n, 0) := ∞for n∈ {1, . . . , N},D(0, m) := ∞ for m∈ {1, . . . , M}and D(0,0) = 0. Then the recursion of (2.19) holds for n∈ {1, . . . , N}and m∈ {1, . . . , M}. The time and space complexity of this method is O(NM). The following algorithm compute an optimal warping path p∗. Algorithm: Optimal Warping Path Input: Accumulated cost matrix D. Output: Optimal warping path p∗. Procedure: The optimal path p∗= (p1, . . . , pL) is computed in reverse order of the indices starting with pL= (N, M). Suppose pℓ= (n, m) has been computed. In case (n,m)=(1,1), we are finished. Otherwise, pℓ−1:= (1, m −1) ifn= 1 (n−1,1) ifm= 1 arg min{D(n, m −1), D(n−1, m), D(n−1, m −1)}otherwise (2.22) where we take the lexicographically smallest pair in case arg min{D(n, m−1), D(n− 1, m), D(n−1, m −1)}is not unique. Observation 2.13 The Euclidean distance between two sequences can be seen as a special case of the DTW distance, where each element of the optimal warping path FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 2. Functional Data Introduction 16 is constrained to pℓ= (nℓ, mℓ) with nℓ=mℓ=ℓ. Note that it is only defined in the special case where the two sequences have the same length N=M. [5] FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 3 Introduction to the Functional Data Classification Our aim here is to summarize the main ideas and techniques used so far in the classification of functional data. 3.1 Supervised and unsupervised functional classification In statistics, the word classification also has the same usual double meaning as in the ordinary language, where this term stands for both “to assign (an element) to a particular class or category” and for “arrange (a group of elements) in classes according to shared characteristics”. The first meaning corresponds to the statistical methodology called supervised classification or discriminant analysis. The second one would fit better with clustering methodology, which roughly corresponds to the unsupervised classification theory. Here, the term “supervised” in the first problem refers to the fact that there is a “training”(sample) dataset of elements which are assumed to be well-classified. Then the problem is to classify the new incoming elements. In the unsupervised class, no such help is available, the problem is just to group the data into “clusters” of mutually alike elements. The reader is referred to [2] for more details 17
Chapter 3. Introduction to the Functional Data Classification 18 3.2 Unsupervised Classification The purpose of unsupervised classification techniques is to partition a (usually large) data sample {X1, . . . , XN}into a number Kof clusters or groups. The members of the same cluster are similar to each other according to certain characteristics. Some well-known algorithms for grouping a given dataset are based on the mutual distances between the data. So, they can be adapted to the case of functional data, provided that a suitable distance is defined. The number of groups Kmust be given in advance, but most grouping procedures include some guidelines for the choice of K. Unsupervised classification is typically used when a large amount of data is available and some internal structure of data groups is suspected. 3.2.1 K-means Clustering K-means clustering is a method for finding clusters and cluster centers in a set of unlabeled data. Once the desired number of cluster centers was elected, say K, and the K-means procedure iteratively moves the centers to minimize the total within variance [13]. Given an initial set of Kcenters, the K-means algorithm alternates the two steps: •for each center Xkfor k∈ {1, . . . , K}we identify the subset of training functional data (its cluster) that is closer to it than any other center. •the mean 2.4 for the functional data in each cluster are computed, and this mean becomes the new center for that cluster. These two steps are iterated until convergence. Typically, the initial centers are K randomly chosen observations from the training data. 3.2.2 Gaussian Mixtures Each cluster is described in terms of a Gaussian density, which has a centroid and a covariance matrix. The two steps of the alternating EM algorithm are very similar to the two steps in K-means: •In the E-step, each observation is assigned a weight for each cluster, based on the likelihood of each of the corresponding Gaussian’s. Observations close to FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 3. Introduction to the Functional Data Classification 19 the center of a cluster will most likely get weight 1 for that cluster and weight 0 for every other cluster. Observations half-way between two clusters divide their weights accordingly. •In the M-step, each observation contributes to the weighted means (and covariance) for every cluster. 3.3 Supervised classification This methodology applies when Kpopulations are given in advance. The available data consist of a “training sample” (Xn, Yn) where Xnfor i∈ {1, . . . , N}are functional data and Yn=kif the n-th individual belongs to the population Pk. The final aim is to classify a newly appearing observation Xinto one of the populations Pk, i.e., we want to predict the corresponding value Yusing the information provided by the training sample. 3.3.1 k-Nearest Neighbors Method Assume that Xis a metric space. To classify a data X, look at the ktraining data closest to X(in the metric of X) and assign Xto Pjwhen the majority of these kdata belong to Pj, in the case of our training sample consist of Kclusters P1, . . . , PK. For the latter, an appropriate functional distance must be chosen. This is equivalent to specifying the feature metric space Xwhere the functional data are supposed to take values. See Section 2.1. 3.3.2 Prototype Methods Throughout this section, our training data consists of the Npairs (X1, Y1). . . , (XN, YN) where Ynfor n∈ {1, . . . , N}is a class label taking values in 1,2, . . . , K. Prototype methods represent the training data by a set of functional data in the feature space X. These prototypes are typically not examples from the dataset. Each prototype has an associated class label, and the classification of a query point Xis made into the class of the closest prototype. Closeness is usually defined by the Euclidean distance in the feature space. The Euclidean distance usually requires two time series to have the same length. Otherwise, the DTW distance is used, as we discuss in Section 2.5. The main challenge is to figure out how many prototypes to use and where to put them. FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 3. Introduction to the Functional Data Classification 20 3.4 K-fold cross-validation Ideally, if we had enough data, we would set aside a validation set and use it to assess the performance of our prediction model. Since data are often scarce, this is not usually possible. To avoid the problem, K-fold cross-validation uses part of the available data to fit the model, and a different part to test it. We split the data into Kroughly equal-sized parts. For the k-th part k∈ {1, . . . , K}, we fit the model to the other K−1 parts of the data and calculate the prediction error of the fitted model when predicting the k-th part of the data. We do this for k= 1,2, ..., K and combine the Kestimates of the prediction error. The way we combine the Kestimates is doing the mean. Typical choices for Kare 5 or 10. If we have a dataset {X1, . . . , XN}, the case K=Nis known as leave-one-out cross-validation: for each functional data Xn, we fit the model with the other N−1 time series, and calculate the prediction error of the fitted model when predicting Xn. We repite this for each function in our dataset and estimate the prediction error as the mean of the Nestimators. Observation 3.1 It is important to randomly select the sample because it is possible that the records follow a pattern linked with their position in the dataset. For example, growth dataset of fda.usc package [6] consists of heights measured in boys and girls at 31 discrete time points between the ages of 1 and 18. Then, we could choose “boys” curves to test the model and “girls” ones to fit it. In this case, the error will be considerably greater. In the folds of different models, there is a common part; this is the reason that the Kestimators of the error will not be independent. However, the so-obtained estimator will be more precise in the sense of variance. FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 4 Automatic procedures by R 4.1 Introduction to splines2 The Rpackage splines2 provides functions to construct basis matrices of natural cubic splines, along with their integrals and derivatives of a given order. The function naturalSpline() returns nonnegative basis functions (within the boundary) for natural cubic splines. When integral = TRUE,naturalSpline() returns the integral of each natural spline basis. To obtain the derivatives of the spline basis functions, we may specify the argument derivs = k,kbeing the order of the derivative, or the deriv() method. 4.2 Conversion of raw data or other functional data classes into fdata class The function fdata() of the fda.usc package [6] creates a functional data object of class fdata from matrix,data.frame,numeric,integer,fd,fds,fts or sfts class data. fdata(mdata, argvals = NULL, rangeval = NULL, names = NULL, fdata2d = FALSE) 21
Chapter 4. Automatic procedures by R 28 classif.knn( group, fdataobj, knn = NULL, metric, ... ) Arguments •group is a factor vector of length Nof the corresponding group for each functional data in fdataobj. Following the notation in Section 3.3.1,group = (Y1,...,YN). •fdataobj is an object of fdata class. •knn is a vector of number of nearest neighbors considered. •metric metric function, “metric.lp”, “metric.dist” or “metric.DTW” are available. 4.9 Predicts from a fitted classif object The function predict of the package fda.usc [6] classifies a functional data by kernel method using functional data object of class classif. It returns the predicted classes using a previously trained model. predict(object, new.fdataobj = NULL, type = "class", ...) Arguments •object Object estimated by: k-nearest neighbors method classif.knn, kernel method classif.kernel. •new.fdataobj New functional explanatory data of fdata class. FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 4. Automatic procedures by R 29 •type Type of prediction (class or probability of each group membership). If type=“class”, produces a vector of predictions. If type=“probs”, a list with the following components is returned: •group.pred the vector of predictions. •prob.group the matrix of predicted probability by factor level. 4.10 K-means clustering for functional data The function kmeans.fd of fda.usc package [6] performs K-means clustering for functional data. Returns a vector of the indexes of groups assigned clusters and afdata object of curves centers. kmeans.fd( fdataobj, ncl = 2, metric = metric.lp, max.iter = 100, method = "sample", cluster.size = 5, draw = TRUE, ... ) Arguments •fdataobj fdata class object. •if ncl is an integer, indicating the number of groups to classify, ncl initial centers are selected using kmeans.center.ini function of the fda.usc package [6]. If ncl is a vector of integers, indicating the position of the initial centers with length(ncl) equal to number of groups. If ncl isafdata class objecct, ncl are the initial centers curves with nrow(ncl) number of groups. •metric Metric function, by default metric.lp. FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 4. Automatic procedures by R 30 •max.iter. Maximum number of iterations for the detection of centers. •method for selectiong initial centers. If method=“sample” takes a random selection by the ncl centers. The ncl curves with greater distance are the initial centers. If method=“exact”, all combinations of ncl centers are calculated (if ¡ 1e+6) of ncl centers. The ncl curves with greater distance are the initial centers (this method may be too slow). •cluster.size Minimum cluster size (by default is 5). If a cluster has fewer curves, it is eliminated and the process is continued with a less cluster. •draw=TRUE, draw the curves in the color of the centers. FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 5 Experiments 5.1 growth dataset 5.1.1 Data description Berkeley (California) Growth Study data, downloaded from the package fda [12] is a list containing the heights of 39 boys and 54 girls between the ages of 1 to 18, and a vector with such ages. This list contains the following components: •growth$hgtm Is a 31 by 39 numeric matrix giving the heights in centimeters of 39 boys at 31 ages between 1 and 18 years. •growth$hgtf Is a 31 by 54 numeric matrix giving the heights in centimeters of 54 girls at 31 ages between 1 and 18 years. •growth$age a numeric vector of length 31 giving the ages at which the heights were measured. Ages are not equally spaced. First, we upload the fda [12] library to use the dataset growth and visualize the elements of the list. library(fda) data(growth) 31
Chapter 5. Experiments 32 growth$hgtm[,1:4] growth$hgtf[,1:4] growth$age > library(fda) > data(growth) > growth$hgtm[,1:4] boy01 boy02 boy03 boy04 1 81.3 76.2 76.8 74.1 1.25 84.2 80.4 79.8 78.4 1.5 86.4 83.2 82.6 82.6 1.75 88.9 85.4 84.7 85.4 2 91.4 87.6 86.7 88.1 3 101.1 97.0 94.2 98.6 4 109.5 104.6 100.4 104.4 5 115.8 112.3 107.1 111.0 6 121.9 118.9 112.3 116.3 7 130.0 125.0 118.6 123.2 8 138.2 130.1 124.0 129.9 8.5 141.1 133.0 126.5 133.0 9 144.3 135.4 128.9 136.0 9.5 147.5 137.5 131.2 138.7 10 150.5 139.7 133.4 141.4 10.5 153.4 142.2 135.8 144.0 11 156.2 144.2 138.4 146.4 11.5 159.7 146.2 141.0 148.8 12 163.8 148.1 143.6 151.4 12.5 168.8 149.8 146.8 154.5 13 174.9 151.6 150.8 157.7 13.5 181.2 153.8 155.1 162.2 14 186.3 156.3 159.5 167.4 14.5 189.6 159.2 163.3 172.5 15 191.3 163.3 166.8 176.3 15.5 192.1 167.7 167.8 178.5 16 192.8 171.5 168.8 179.8 16.5 193.2 174.3 169.8 180.7 FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 5. Experiments 33 17 193.8 176.1 170.9 181.4 17.5 194.3 177.4 171.2 181.6 18 195.1 178.7 171.5 181.8 > growth$hgtf[,1:4] girl01 girl02 girl03 girl04 1 76.2 74.6 78.2 77.7 1.25 80.4 78.0 81.8 80.5 1.5 83.3 82.0 85.4 83.3 1.75 85.7 86.9 87.9 87.0 2 87.7 90.0 89.6 90.3 3 96.0 94.9 97.1 98.6 4 103.8 102.1 109.2 106.3 5 110.7 109.2 116.2 113.9 6 116.8 115.6 122.9 120.7 7 122.2 122.7 128.0 125.7 8 127.4 128.2 134.2 131.0 8.5 130.6 131.1 138.0 134.1 9 133.4 134.8 141.5 137.1 9.5 135.9 138.2 145.2 140.2 10 138.6 140.9 148.8 143.0 10.5 142.4 143.4 152.3 145.7 11 146.8 146.1 155.6 148.5 11.5 150.3 149.4 158.2 151.5 12 153.1 152.9 159.6 154.8 12.5 155.0 156.4 160.1 158.4 13 156.2 159.5 160.3 161.2 13.5 157.1 161.6 160.8 163.5 14 157.7 162.6 161.6 165.2 14.5 158.0 163.6 161.8 166.0 15 158.2 165.0 161.7 166.5 15.5 158.4 165.3 161.8 166.9 16 158.6 165.6 161.9 167.2 16.5 158.7 165.9 161.9 167.4 17 158.7 166.1 161.7 167.4 17.5 158.8 166.0 161.9 167.6 18 158.9 166.0 162.2 167.8 > growth$age [1] 1.00 1.25 1.50 1.75 2.00 3.00 4.00 5.00 6.00 [10] 7.00 8.00 8.50 9.00 9.50 10.00 10.50 11.00 11.50 [19] 12.00 12.50 13.00 13.50 14.00 14.50 15.00 15.50 16.00 [28] 16.50 17.00 17.50 18.00 FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 5. Experiments 34 5.1.2 Descriptive statistics Then we convert functional data into fdata class with fdata function. The upload of fda.usc package is necessary. We create three fdata objects, corresponding to “boys” functional data fdata.m, “girls” functional data fdata.f and both together fdata.growth, where argvals is the vector that collects the ages at which heights were measured. Following the indications in Section 4.2, we transpose the numeric data matrix: the curves must be by rows and the observation time points in columns. library(fda.usc) argvals<-growth$age fdata.m=fdata(t(growth$hgtm), argvals = argvals, names=list(main="Boys",xlab="ages",ylab="heigth")) fdata.f=fdata(t(growth$hgtf), argvals = argvals, names=list(main="Girls",xlab="ages",ylab="heigth")) fdata.growth<-fdata(t(cbind(growth$hgtm,growth$hgtf)), argvals = argvals) Now, the objective is to summarize numerically and graphically the created fdata object. We computed central and dispersion measures for functional data as we could see in Section 4.3. The measures of the central tendency by groups are shown in Figure 5.1. fac<-factor(rep(c("boy","girl"),c(39,54))) ldata<-list("df" = data.frame(fac), "fdataobj" = fdataobj) a<-func.mean.formula(fdataobj~fac, data = ldata) dev.new() FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 5. Experiments 35 plot(a) legend(2,160,c("boys mean","girls mean"),fill=c(1,2),border=0, bty="n") 5 10 15 80 100 120 140 160 180 mean t X(t) boys mean girls mean Figure 5.1: Measures of central tendency by groups Other measures of central tendency, as we can see in Figure 5.2, are calculated for the whole dataset. a1<-func.mean(fdataobj) a2<-func.trim.FM(fdataobj) a3<-func.trim.mode(fdataobj) a4<-func.trim.RP(fdataobj) a5<-func.med.FM(fdataobj) a6<-func.med.mode(fdataobj) FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 5. Experiments 36 a7<-func.med.RP(fdataobj) dev.new() par(mfrow=c(1,2)) plot(c(a1,a2,a3,a4),ylim=c(70,170),col=c(2,3,4,5),main="Central tendency: trimmed mean") legend(2,160,c("mean","trimmed mean FM criteria","trimmed mean mode criteria", "trimmed mean RP criteria"),fill=c(2,3,4,5),border=0, bty="n") plot(c(a1,a5,a6,a7),ylim=c(70,170),main="Central tendency: median", col=c(2,3,4,5)) legend(2,160,c("mean","median FM criteria","median mode criteria", "median RP criteria"),fill=c(2,3,4,5),border=0, bty="n") 5 10 15 80 100 120 140 160 Central tendency: trimmed mean t X(t) mean trimmed mean FM criteria trimmed mean mode criteria trimmed mean RP criteria 5 10 15 80 100 120 140 160 Central tendency: median t X(t) mean median FM criteria median mode criteria median RP criteria Figure 5.2: Mean, trimmed mean and median using FM criteria, mode criteria and RP criteria. Measures of dispersion such as variance and trimmed variance using FM criteria, FM criteria with trimmed parameter trim = 0.1, mode criteria and RP criteria are being calculated, among others. A graphical representation of these dispersion measures is shown in Figure 5.3. FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 5. Experiments 37 b1<-func.var(fdataobj) b2<-func.trimvar.FM(fdataobj) b3<-func.trimvar.FM(fdataobj,trim=0.1) b4<-func.trimvar.mode(fdataobj) b5<-func.trimvar.RP(fdataobj) dev.new() par(mfrow=c(1,2)) plot(c(b1,b2,b3),col=c(2,3,4),ylim=c(0,90),main="Measures of dispersion I") legend(2,80,fill=c(2,3,4),border=0, bty="n",c("variance","trimmed var FM criteria", "trimmed var FM criteria 10%")) plot(c(b1,b4,b5),col=c(2,3,4),ylim=c(0,90),main="Measures of dispersion II") legend(2,80,fill=c(2,3,4),border=0, bty="n",c("variance", "trimmed var mode criteria", "trimmed var RP criteria")) 5 10 15 0 20 40 60 80 Measures of dispersion I t X(t) variance trimmed var FM criteria trimmed var FM criteria 10% 5 10 15 0 20 40 60 80 Measures of dispersion II t X(t) variance trimmed var mode criteria trimmed var RP criteria Figure 5.3: Measures of dispersion: variance, trimmed variance using FM criteria, FM criteria with trimmed parameter trim = 0.1, mode criteria and RP criteria. Now, we compare the mean curve of each group, “boys” and “girls” with the rest of “boys” and “girls” curves, respectively. See Figure 5.4. FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 5. Experiments 44 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 5 10 15 20 0 200 400 600 800 1000 dtw 2 2 2 2 222 2 2 2 2 2 2 2 2 2 2 2 2 2222 2 2 22 2 2 22 2 2 2 2 2 2 22 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 22 2 2 2 2 2 2 2 3 3 3 3 333 3 3 3 3 3 3 3 3 3 3 3 3 3 33 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 33 3 3 3 3 3 3 3 3 33 3 3 3 3 33 3 3 3 3 3 33 3 3 3 3 3 3 3 3 3 3 33 3 3 33 3 33 3 3 33 3 33 4 4 4 4 4 4 4 4 4 4 4 4 4 4444 4 4 44 4 4 4 4 44 4 4 44 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 44 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 55 5 5 5 5 5 5 5 5 5 5 5 5555 5 5 5 5 5 5 5 5 5 5 55 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 55 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 55 5 5 55 55 5 6 66 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6666 6 6 6 6 6 6 6 6 6 6 66 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 66 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 666 6 6 6 6 6 66 6 666 6 66 7 7 7 7 77 7 7 7 7 7 7 7 7 7 7 7 7 7 7 77 7 7 7 7 7 7 7 7 7 7 77 7 7 7 7 7 7 7 7 7 7 7 7 7 7 77 7 7 77 7 7 7 7 77 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 77 7 7 77 77 7 8 8 8 8 8 8 8 8 8 88 8 88 8 88 8 8 88 8 8 8 8 88 8 8 8 8 8 8 8 8 888 8 8 8 8 8 8 8 8 8 8 8 8 8 8 88 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 9 9 9 9 999 9 9 9 9 9 9 9 9 9 9 9 99 99 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 99 9 9 9 9 9 9 9 9 9 9 9 9 9 9 99 9 9 9 9 9 99 9 9 9 9 9 9 9 9 9 9 99 9 9 9 9 9 99 9 9 99 9 9 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 00 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 a a a a a a a a aa a a a aaaaa a aa a a a aaa a a aa a a aa a aa a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a aa a a a a a a a b b b b b b b b b b b b b b b b b b b bb b b b b bb b b bb b b b b b b b b b b b b b b b b b b b b b bb b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b c c c c c c c c c c c c c ccc c c c cc c c c c cc c c cc c c c cc cc c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c cc c c c c c c c d d d d d d d d d dddd d d d d d d dd d d d d dd d d dd d d d d d dd d d d d d d d d d d d d d d dd d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d e e e e e e eee e e e e e e ee e e ee e e e ee e e e e e e e e e e e ee e e e e e e e e e e e e e ee e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e f f f f f f f f f f f f ff f f f f f ff f f f f f f f f ff f f f fff f f f f f f f f f f f f f f f ff f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f g g g g g g g g g g g g g ggg g g g gg g g g g gg g g gg g g g g g gg g g g g g g g g g g g g g g gg g g g g g g g g g g g g g g g g g g g g g g g g g g g g g g gg g g g g g g g h h h h h h h h h h h h h h h hh h h hh hh h h h h h h hh h h h h hh hh h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h hh h h h h h h h i ii i iii i i i i i i i i i i i i ii iiii i i i i i i i ii i i i i i i i i i i i iii i i i i i i i i i i i i i i i i i i i i i i i i i i i i ii i i i i i i i i i i ii ii i j j j jj jj j j j j j j j j j j j j jj jj j j j j j j j j j jj j j j j j j j j j j j j j j j j j j jj j j j j j j j j j j j j j j j j j j j j j j j j j j j j j j j j j j j j j j j k kk k kkk k k k k k k k k k k kkkkkkk k k kk k k k k kk k k k k k k k k k k k k k k k k k k kk k k k k k k k k k k k k k k k k k k k k k k kk k k k k k k kk k k k k k kk l ll l lll l l l l l l l l l l lllll l l l l l l l l l l ll l l l l l l l l l l l l l l l l l l ll l l l l l l l l l l l l l l l l l l l l l l lll l l l l l ll l ll l l ll m m m m m m m m m m m m m m mm m m m mm m m m m mm m m mm m mm m m m mm m m m m m m m m m m m m m mm m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m n n n n nnn n n n n n n n n n n n n n nn n n n n n n n n n n nn n n n n n n n n nn n n n n nn n n nn n n n n nn n n n n n n n n n n n n n n n n n nn n n nn n nn n n nn n nn o oo o oo o o o o o o o o o o o oo ooo oo o o o o o o o o oo o o o o o o o o o o o o oo o o o o o o o o o o o o o o o o o o o o o o o o o o o o ooo o o o o o oo o o o o o oo p p p p p p p p p pp p pp p pp pp pp p p p p p p p p pp pp p pp p p p p p p p p p p p p p p p p pp p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p q q q q q q q q q q q q qqqq qq q qq q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q r r r r rrr r r r r r r r r r r r r r rr r r r r r r r r r r r r r r r r r r r r rr r r r r r r r r r r r r rr rr r r r r r rr r rr r rr rr r r rr r r r r r rr r r r rr rr s s s s s s s s s s s s s s s ss s s s s s s s s s s s s ss s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s t t t t t t t t t t t t t t ttt tt tt t t t t tt t t tt t t tt tttt t t t t t t t t t t t t t tt t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t tt t t t t t t t u u u u u u u u u u u u u uuuu u u uu u u u u uu u u uu u u u u u u u u u u u u u u u u u u u u u uu u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u v v v v v v v v v v v v v v v vv v v vv v v v v v v v v vv v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v w w ww ww w w w w w w w w w ww w wwww ww w w w w w ww w w w w w w w w w w w w w w w w w w w w w w w w w w w ww w w w w w w w w w w w w w w w w w w w w w w w w ww w w w w w ww x xx x xx x x x x x x x x x x x xxxxx x x x x x x x x x x x x x x x x x x x x x x x x xx x x x x x x x x x x x x x x x x x x x x x x x x x x x x xx x x x x x x xx x x x x x xx y y y y y y y y y y y y y y y yy y y y y y y y y y y y yyy y y y y y y y y y y y y y y y y y y y y y yy y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y z z z z z zz z z z z z z z z z z z z zz z z z z z z z z zz z z zz z z zz z z z z z z z z z z z z z z z z z z z z z z z z z z z z z z z z z z z z z z z z z z z z z z z z z z z z z z A A A A A A A A A A A A A AAAA A A AA A A A A A A A A AA A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A AA A A A A A A A B B B B B B B B BB B B B B BB BB B BB B B B B B B B B BB B B BB B B B B B B B B B B B B B B B B B BB B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B C C CC C C C C C C C C C C C C C CCCC CC C C C C C C C C C CC C C C C C C C C C C C C C C C C C C CC C C C C C C C C C C C C C C C C C C C C C C C C C C C C C C CC C C C C C C C D D D D DD D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D DD D D D D D D D D D D D D D DD D D D D D D D D D D D D DD D D D D D D D D E E E E EE E E E E E E E E E E E E E E EE E E E E E E E E E E EE E E E E E E E E EEE E E E E EE E E E E E E E E E E E E E E EE E EE E E E E E E E E E E E E EE EE E E E E EEE F F F F FF F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F FF F F F F F F F F F F F F F F F F F F F FF F F F F F F F F F F F F FF F F F F F F F F FF F F F F F F F G G G G GGG G G G G G G G G G G G G G GG G G G G G G G G G G GG G G G G G G G G G GG G G G G G G G G G G G GG G GG G G G G GG G GG G GG GG G G G G G G G G G GG G G G GG GG H H H H HHH H H H H H H H H H H H H H HH H HH H H H H H H H HH H H H H H H H H H H H H H H HH H H H H H H H H HH H H H H H HH H H H H H H H H HH HH H H HH H HH H H H H H HH I I I I II I I I I I I I I I I I I I I II I I I I I I I I I I II I I I I I I I II I I I I I I II I I I I I I I I I II I I I II I III I I I II I I I I I I I III II I I I II J J J J J J J J J J J J J J J J J J J J J J J J J J J J J J J J JJ J J J J J J J JJ J J J J J J JJ J J J J J J J J J JJ J J J JJ J JJ J J J J J J J J J J J J J J JJ J J J J J J J K K K K K K K K K K K K KK K KK K K KK K K K K K K K K KK K K K K K KK K K K K K K K K K K K K K K KK K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K K L L L L LL L L L L L L L L L L L L L L LL L L L L L L L L L L LL L L L L L L LLL L L L L L L LL L L L L L L L L L L L L L L LL L L L L L L L LL L L L L L L LL LL LL L L L LL M MM M MMM M M M M M M M M M M M MMMM M M M M M M M M M M MM M M M M M M M M M M M M M M M M M M MM M M M M MM M M M M M M M M M M M M M M M M MM M M M M M M MM M M M M M M M N N N N NN N N N N N N N N N N N N N N NN N N N N N N N N N N N N N N N N N N N N NNNN N N N N N N NN N N N NN N NN N N N NN N N NN N NNN N N N N N N N N N NN N N N N N NN O O O O OO O O O O O O O O O O O O O O O O O O O O O O O O O O OO O O O O O O O O O O OO O O O O O O O O O O O O O O OO O O O OO O OO O O O O OO O O O O O O O OOO O O O O O OO P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P Q Q Q Q QQQ Q Q Q Q Q Q Q Q Q Q Q Q Q QQ Q QQ Q Q Q Q Q Q Q QQ Q Q Q Q Q Q Q Q QQ Q Q Q Q QQ Q Q Q Q Q Q Q Q QQ Q Q Q Q Q QQ Q Q Q Q Q Q Q Q QQ QQ Q Q QQ Q QQ Q Q QQ Q QQ R R R R RRR R R R R R R R R R R R R R RR R R R R R R R R R R R R R R R R R R R R R R R R R R RR R R R R R R R R RR R R R R R R R R R R R R R R R R R RR R R R R R RR R R RRR RR S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S SS S S S S S S S S S S S S S S S S S S S S S S T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T TT T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T TT T T T T T T T U U U U UUU U U U U U U U U U U U U U UU U UU U U U U U U U U U U U U U U U U U U U U U U U U U U U UU U U U U UU U U U U U U U U U U U U U U U U U UU U U U U U UU U U UU U UU V V V V VV V V V V V V V V V V V V V V VV V V V V V V V V V V VV V V V V V V V VVV V V V V V VV V V V V V V V V V VV V V V VV V VVV V V V V V V V V V V V V V VV V V V V V VV W W W W WWW W W W W W W W W W W W W W WW W W W W W W W W W W WW W W W W W W W W WW W W W W W W W W W W W W W W W W W W W W W WW W W W W W W W W W W WW W W W W W WW W W W WWW W X X X X XXX X X X X X X X X X X X X X XX X XX X X X X X X X X X X X X X X X X X X X X X X X X X X X XX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X XX X X XX X X X Y Y Y Y YY Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y YY Y Y Y Y Y Y YY Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y YY Y Y Y Y Y Y Y YY Y Y Y Y Y Y Y Y Y Y Y Y Y YY Y Y Y Y Y YY Z Z Z Z ZZ Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z ZZ Z Z Z Z Z ZZZ Z Z ZZ Z Z Z Z Z Z Z Z Z Z Z Z Z Z ZZ Z Z ZZ Z Z Z Z Z Z Z Z ZZ Z Z Z Z Z Z Z ZZZ Z Z Z Z Z ZZ 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 222 2 2 22 2 2 2 2 22 2 2 22 2 2 2 2 222 2 2 2 2 2 2 2 2 2 2 2 2 2 2 22 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 22 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 33 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 333 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 44 4 4 4 4 4 4 4 4 4 4 44 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 44 4 4 4 4 44 4 4 4 4 4 4 4 4 4 4 4 44 4 4 4444 4 4 44 4 4 4444 4 4 4 4 4 44 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 55 5 5 5 5 5 5 5 5 5 5 55 5 5 5 5 55 5 5 55 55 5 5 5 5 5 5 5 5 5 5 5 55 5 5 5 5 5 5 5 5 5 555 5 5555 5 555 5 5 55 55 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 66 7 7 7 7 777 7 7 7 7 7 7 7 7 7 7 7 7 7 77 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 77 7 7 7 7 7 7 7 7 77 7 7 777 77 7 7 7 7 77 7 7 7 77 7 77 7 7 77 7 7 7 77 77 7 7 7 7 777 8 8 8 8 88 8 8 8 8 8 8 8 8 8 8 8 8 8 8 88 8 8 8 8 8 8 8 8 8 8 88 8 8 8 8 8 8 888 8 8 8 8 8 8 88 8 8 8 8 88 8 8 8 8 8 8 8 8 88 8 8 8 8 8 888 8 8 8 88 8 8 88 88 8 8 8 8 888 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 99 9 99 9 9 9 9 9 9 9 9 9 9 9 9 9 9 99 9 9 9 99 9 99 9 9 9 9 99 9 99 9 9 9 9 999 9 9 9 9 9 9 9 0 00 0 000 0 0 0 0 0 0 0 0 0 0 0 0000 0 00 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 00 0 00 0 0 0 0 a a a a aa a a a a a a a a a a a a a a a a a a a a a a a a a a aa a a a a a a aa a a aa a a a aa a a a a a a a a a aa a a a aa a aaa a a a aa a a a a a a a a aa a a a a a aa b b b b bbb b b b b b b b b b b b b b bb b b b b b b b b b b b b b b b b b b b b bb b b b bb b b b bb b b b b bb b b b bb bb b b b b b b b b b b bb b b b b b bb b b b bbbb c c c c cc c c c c c c c c c c c c c c cc c c c c c c c c c c c c c c c c c c c c c c c c c c c cc c cc c cc c c c cc c c c cc c c cc c c c c c c c c c c c c c cc c c c c ccc d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d dd d d d d d d d dd d dd d d d dd d d dd d d d d d dd d d d dd d dd d d d d d d d dd d d d d d dd d d d d d d d e e eeee e e e e e e e e e e e eeeeee e e e e e e e e e ee e e e ee e e e e e e e e e e e e e ee e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e f f f f fff f f f f f f f f f f f f f ff f ff f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f ff f f f f f f f f f f f f f f f f f f f f f ff f ff f f ff ff f g g g g ggg g g g g g g g g g g g g g g g g g g g g g g g g g gg g g g g g g g g gg g g g ggg g g g g g g g g gg g g g g g gg g gg g g g g g gg g g g g g g g gg g g g g g gg h h h h hh h h h h h h h h h h h h h h h h h h h h h h h h h h hh h h h h h hhh h h hh h h h h h h h h h h h h h h hh h h h h h h h h h h h h hh h h h h h h h hhh h h h h h hh i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i i j j j j jjj j j j j j j j j j j j jj jj j jj j j j j j j j jj j j j j j j j j j j j j j j j j j j jj j j j j jj j j j j j j j j j j j j j j j j jjj j j j j j j j j j j j j j j k k k k kkk k k k k k k k k k k k k k kk k k k k k k k k k k kk k k k k k k k k kk k k k k k k k k k k k k kk k kk k k k k kk k kk kk k k k k k k k k k k k k kk k k k k k kk l l l l ll l l l l l l l l l l l l l l l l l l l l l l l l l l ll l l l l l l ll l l l l l l l l l l l l l l l l l l ll l l l ll l lll l l l l l l l l l l l l l ll l l l l l l l m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m m mm m m m m m mmm m m mm m m m m m m m m m m m m m m mm m m mm m mm m m m m m m m m m m m m m m m mm m m m m m mm n n n n n n n n n n n n n n n n n n n n n n n n n n n n n n n n n n n n n n n n n n n n nn n n n n n nn n n n n n n n nn n n nn nn n n n n n n n n n n n n n n n nnn n n n n n nn o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o oo o o o o o o o o o o o o o o o o o o o o o o p p p ppp p p p p p p p p p p p pp pp pp p p pp p p pp p pp p p p p p p p p p p p p p p p p p p pp p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p pp p p p p p p p q q q q qqq q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q qq q q q qq q q q q q q q qq qq q q q q q qq q q q q q q q q q q qq q q q q q qq q q q qqqq r r r r rrr r r r r r r r r r r rrr rr r rr r r r r r r r rr r r r r r r r r r r r r r r r r r r r r r r r r rr r r r r r r r r r r r r r r r r rr r r r r r r rr r rr r r rr s s s s s s s s s s s s s s s s s s s s ss s s s s s s s s s s s s s s s s s s s s ss ss s s s s s s ss s ss ss s ss s s s ss s s s s s ssss s s s s s s ssss s s s s s s s t t t t t tt t t t t t t t t t t t t t tt t t t t t t t t t t tt t t t t t t t t tt t t t t t t t t tt t t t tt tt t t tt tt t tt t tt t t t t t t t t t t t tt t t t t t t t u u u u uuu u u u u u u u u u u u u u uu u u u u u u u u u u uu u u u u u u u u uu u u u uuu u u uu u u u u u u u u u u u uu u u u u u u u u u u uu u u u u u uu u u u uu u u Figure 5.9: Figure 5.8 expanded in [0,20]×[0,1000]. time series using the Algorithm 2.5. We can see in Figure 5.10 how this warping between these two functional data has been executed. D1=fda.usc:::DTW(fdata.m$data[1,],fdata.f$data[1,],p=2) aa1=fda.usc:::findPath(D1$D) dev.new() plot(c(fdata.m[1,],fdata.f[1,]),col=c("blue","red")) segments(fdata.m$argvals[aa1[,1]], fdata.m[1]$data[aa1[,1]],fdata.f$argvals[aa1[,2]], fdata.f[1]$data[aa1[,2]],lwd = 0.5,col=8) In general, the results obtained about central and dispersion measures suggest that boys in Berkeley (California) are taller than girls. In addition, while examining the smoothness of the derivative curves, boys grow more sharply than girls, where the stages of the most rapid growth are childhood and adolescence (over 13 years). In Figure 5.7 and clearer in Figure 5.8, we can see that the most similar curves to the boys curves are themselves, and the same for girls functional data. More FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 5. Experiments 45 5 10 15 80 100 120 140 160 180 Boys ages heigth Figure 5.10: Warping path between the first boy in fdata.m and the first girl in fdata.f. extreme values are observed in Figure 5.8 than in Figure 5.7, i.e., two time series are very similar or very different for the DTW distance, while for the Euclidean distance the distances are more homogeneous. This phenomenon can be explained by Figure 5.10, where the ages at which boy01 grows more (13 years of age or older) are warped to the same point in the curve corresponding to girl01, which is experiencing stagnation of growth about 12 years old. 5.1.3 Unsupervised classification We can consider the unsupervised classification problem. For that, K-means clustering method is introduced. Set ncl = 2 the number of clusters and method="exact" since the sample size is not too large. Figure 5.11 shows the output of the evaluated function kmeans.fd. All functional data are represented and colored in red or green depending on the group in which they have been classified (red for “boys” and green for “girls”). In the right plot, we can see the center curves in the last step of the iterative K-means process. Furthermore, the confusion matrix is provided. Then we calculate the estimate of the total probability of correct classification. FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 5. Experiments 46 5 10 15 80 100 120 140 160 180 200 Assigning groups t X(t) 5 10 15 80 100 120 140 160 180 200 Update centers t X(t) Figure 5.11: Red lines correspond to those curves classified as “boy” and green lines to “girls”. dev.new() unsu.classif=kmeans.fd(fdata.growth,ncl=2,draw=TRUE,method="exact") table(unsu.classif$cluster,groups) sum(diag(table(unsu.classif$cluster,groups)))/nrow.fdata(fdata.growth) > table(unsu.classif$cluster,groups) groups boy girl 1 27 27 2 12 27 > sum(diag(table(unsu.classif$cluster,groups)))/nrow.fdata(fdata.growth) [1] 0.5806452 FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 5. Experiments 47 5.1.4 Supervised classification Every functional data in fdata.growth belongs to one of the two groups: ”boys” or ”girls” (2 populations). Then, we should consider the supervised classification problem with growth dataset. The final aim is to classify a new time series Xinto one of these populations, using the information provided by the training sample. We will analyze the previous survey by the k-Nearest neighbors method, first by hand calculating in Rand then using automatic procedures already implemented. We choose an arbitrary functional data in fdata.growth, for example the first observation, boy01. We shall forget this functional data belongs to “boys” group for classify it using the information of the rest of the dataset (the training sample). Implementation of k-Nearest neighbors method needs a chosen distance, for example the Euclidean distance. Now, we compute the ktraining data closest to boy01 and assign it to Pj(“boys” or “girls”) when the majority of these kdata belong to Pj. See Section 2.1. Let k= 5, Figure 5.7 shows that the closest training data to boy01 are those labeled as “v”, “s”, “0”, “y”, “A”, which corresponds to the functional training data boy32, boy29, boy10, boy35 and boy37. All of them have been classified as “boys”, so we predict that boy01 is a “boy” for k= 5. The distances among them are calculated bellow. > metric.dist(t(growth$hgtm[,1]), t(growth$hgtm[,c(32,29,10,35,37)])) boy32 boy29 boy10 boy35 boy37 [1,] 10.13262 13.10458 18.38614 28.09609 33.81272 Also, the function predict could classify the functional data boy01 by k-Nearest neighbors method as follows. train.sample<-fdata.growth[c(2:93),] groups<-data[32,] train.groups<-groups[c(2:93)] FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 5. Experiments 48 out<-classif.knn(train.groups,train.sample,knn=5) summary(out) test.sample<-fdata.growth[1,] pred<-predict(out,test.sample) pred Our training data train.sample consists of fdata.growth but excluding the first functional data boy01 since it is the time series of which we want to know the group. To construct the classif object using the k-Nearest neighbors method, a training sample, their corresponding groups and the neighbors number knn are needed. This is the reason why the character vector groups is created. The summary of the classif object and the resulting prediction follows. > summary(out) - SUMMARY - -Probability of correct classification by group (prob.classification): y boy girl 1.0000000 0.9444444 -Confusion matrix between the theoretical groups (by rows) and estimated groups (by column) boy girl boy 38 0 girl 3 51 -Vector of probability of correct classification by number of neighbors (knn): 5 0.9674 FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 5. Experiments 49 -Optimal number of neighbors: knn.opt= 5 with highest probability of correct classification max.prob= 0.9673913 -Probability of correct classification: 0.9674 > pred [1] boy Levels: boy girl These results are in line with those obtained by hand calculations. Furthermore, summary of classif object, give us information about how good is the classification model with the training sample providing the confusion matrix. Moreover, an estimation of the probability of correct classification is displayed (properly classified number divided by sample size). 5.2 CanadianWeather dataset 5.2.1 Data description CanadianWeather collects information about Canadian average annual weather cycle. This dataset included in fda package [12], contains the daily temperature and precipitation at 35 different locations in Canada averaged over 1960 to 1994. CanadianWeather is a list with the following components: •dailyAv a three dimensional array c(365, 35, 3) summarizing data collected at 35 different weather stations in Canada on the following: –dailyAv[,,1]=[,, ’Temperature.C’]: average daily temperature for each day of the year. –dailyAv[,,2]=[,, ’Precipitation.mm’]: average daily rainfall for each day of the year rounded to 0.1 mm. –dailyAv[,,3]=[,, ’log10precip’]: base 10 logarithm of dailyAv[,,2] after first replacing 27 zeros by 0.05 mm [11]. FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 5. Experiments 50 •place Names of the 35 different weather stations in Canada whose data are summarized in dailyAv. These names vary between 6 and 11 characters in length. •province names of the Canadian province that contains each place. •coordinates a numeric matrix giving N.latitude and W.longitude for each place. •region Which of the 4 climate zones contain each place: Atlantic, Pacific, Continental and Arctic. •monthlyTemp A matrix of dimensions (12×35) giving the average temperature in degrees Celsius for each month of the year. •monthlyPrecip A matrix of dimensions (12 ×35) giving the average daily precipitation in millimeters for each month of the year. •geogindex Order the weather stations from East to West to North. First, we load the CanadianWeather data. Also, the fda [12] library must be uploaded if we had not done it before in Section 5.1. Let us then the different components of the list, displaying it in the first four places. Also, Figure 5.12, Figure 5.13 and Figure 5.14 show dataset graphical descriptions. Figure 5.12 shows that temperatures and precipitations waver much since there are many discrete time points of observation. A map of Canada is sensed in Figure 5.13. Places names are colored according to the climate zone (Atlantic, Pacific, Continental and Arctic) to which they belong (left plot) and the point shape changes depending on the province (right plot). Monthly temperature and precipitation curves are represented in Figure 5.14. library(fda) data("CanadianWeather") x<-CanadianWeather$dailyAv x[100:103,1:4,1] x[100:103,1:4,2] FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 5. Experiments 51 x[100:103,1:4,3] dev.new() par(mfrow=c(1,2)) matplot(x[,,1], type="l") matplot(x[,,2], type="l") place<-CanadianWeather$place place[1:4] province<-CanadianWeather$province province[1:4] coordinates<-CanadianWeather$coordinates coordinates[1:4,] region<-CanadianWeather$region region[1:4] dev.new() par(mfrow=c(1,2)) plot(-coordinates[,2],coordinates[,1],type="n") text(-coordinates[,2],coordinates[,1],place,cex=0.7, col=1+(region=="Atlantic")+2*(region=="Continental")+ 3*(region=="Artic")+4*(region=="Pacific")) plot(-coordinates[,2],coordinates[,1], col=1+(region=="Atlantic")+2*(region=="Continental")+ 3*(region=="Artic")+4*(region=="Pacific"), pch=1+(province=="Quebec")+2*(province=="British Colombia")+3*(province=="Northwest Territories")) y<-CanadianWeather$monthlyTemp y[,1:4] z<-CanadianWeather$monthlyPrecip z[,1:4] dev.new() par(mfrow=c(1,2)) matplot(y,type="l") matplot(z,type="l") FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 5. Experiments 52 geo<-CanadianWeather$geogindex > x[100:103,1:4,1] St. Johns Halifax Sydney Yarmouth apr10 1.0 2.4 1.6 3.7 apr11 1.1 2.8 1.4 3.8 apr12 0.7 2.0 0.6 3.3 apr13 0.2 2.2 0.5 3.5 > x[100:103,1:4,2] St. Johns Halifax Sydney Yarmouth apr10 3.3 6.1 6.8 4.8 apr11 6.6 5.0 6.0 3.1 apr12 5.3 2.2 3.8 1.4 apr13 2.8 2.5 1.3 3.0 > x[100:103,1:4,3] St. Johns Halifax Sydney Yarmouth apr10 0.5185139 0.7853298 0.8325089 0.6812412 apr11 0.8195439 0.6989700 0.7781513 0.4913617 apr12 0.7242759 0.3424227 0.5797836 0.1461280 apr13 0.4471580 0.3979400 0.1139434 0.4771213 > place[1:4] [1] "St. Johns" "Halifax" "Sydney" "Yarmouth" > province[1:4] St. Johns Halifax Sydney Yarmouth "Newfoundland" "Nova Scotia" "Nova Scotia" "Nova Scotia" > coordinates[1:4,] N.latitude W.longitude St. Johns 47.34 52.43 Halifax 44.39 63.36 Sydney 46.09 60.11 Yarmouth 43.50 66.07 > region[1:4] St. Johns Halifax Sydney Yarmouth FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 5. Experiments 53 "Atlantic" "Atlantic" "Atlantic" "Atlantic" > y[,1:4] St. Johns Halifax Sydney Yarmouth Jan -4.654839 -6.158065 -5.722581 -3.2161290 Feb -5.325000 -6.182143 -6.796429 -3.4892857 Mar -2.532258 -1.738710 -2.935484 0.1516129 Apr 1.256667 3.623333 1.850000 4.6866667 May 5.793548 9.441935 7.496774 9.3419355 Jun 10.786667 14.776667 13.140000 13.4033333 Jul 15.206452 18.380645 17.487097 16.2935484 Aug 15.280645 18.203226 17.635484 16.5967742 Sep 11.623333 13.866667 13.306667 13.5933333 Oct 7.019355 8.490323 8.274194 9.2451613 Nov 2.953333 3.240000 3.533333 4.9000000 Dec -1.845161 -2.987097 -2.029032 -0.4645161 > z[,1:4] St. Johns Halifax Sydney Yarmouth Jan 4.651613 4.632258 4.764516 4.022581 Feb 4.735714 4.146429 4.446429 3.639286 Mar 4.235484 4.135484 4.422581 3.309677 Apr 3.616667 4.010000 4.076667 3.373333 May 3.251613 3.529032 3.180645 3.093548 Jun 3.270000 3.126667 2.976667 3.046667 Jul 2.651613 3.022581 2.754839 2.638710 Aug 3.822581 3.403226 3.116129 2.780645 Sep 4.126667 3.203333 3.506667 2.953333 Oct 4.909677 4.122581 4.551613 3.509677 Nov 4.783333 5.140000 5.193333 4.516667 Dec 4.680645 5.364516 5.509677 4.641935 Using the function summary, we can see the highest temperature or millimeters of precipitation achieved at each location during the year, as well as the minimum or the mean. A temperature summary of the first four location appearing in dataset follows. > summary(x[,,1])[,1:4] St. Johns Halifax Sydney Yarmouth FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 5. Experiments 60 [2,] 1.215699 0.0000000 0.6262885 1.727500 [3,] 1.838427 0.6262885 0.0000000 2.246582 [4,] 1.494167 1.7275001 2.2465817 0.000000 1 11111 1 111111111 1 1 1 1 1 1 1 1 111 11 1 11 11 1 0 5 10 15 20 25 30 35 0 20 40 60 80 100 dy 2 22222 2 22 222222 2 2 2 2 2 2 2 2 22 22 22 2 22 22 2 333 333 3 33 333333 3 3 3 3 3 3 3 3 3 333 33 3 33 33 3 444 4 44 4 44 4444 44 4 4 4 4 4 4 4 4 444444 4 44 44 4 555 5 5 5 5 55 555555 5 5 5 5 5 5 5 5 5 5 55 5 5 5 55 55 5 6 666 6 6 6 66 66666666 6 6 6 6 6 66 6 66 6 66 66 66 6 777 7 77 7 77 77 77 77 77 7 7 7 7 7 7 7 7 77 7 7 777 77 7 888 8 88 8 8888 88 88 88 8 8 8 8 8 88 8 88 8 8 8 88 88 8 999 9 99 9 99 99 99 99 99 9 9 99 9 9 9 9 99 9 9 9 99 99 9 0000 00 0 00 0000 00 00 0 0 0 0 0 00 0 00 0 0 0 00 00 0 aaaa aa a aa aa aa aa a a a a a a a aa a aa a a a aa aa a b bbb bb b bb bb bb bb bb b b b b b bbb bb b bb bb bb b c ccc cc c cc cc cc cccc c c c c c ccc cc c cc cc cc c d ddddd d dd dd dd dd dd d d d d d d dd dd d d d dd dd d e eeeee e ee ee ee ee ee e e e e e e ee ee ee e ee ee e fff f ff f ffff ff ff f f f f ff f f f f ff f f f ff ff f gggg gg g gggggg gg g g g g gg g g g g gg g g g gg gg g hhh h hhh hh hh hh hh hh h h h h hh h h hh h h h hh hh h iii i ii i ii ii ii ii ii i i i i i i i i ii i i i i iii i jjjj jj j jjjjjj jj jj j j j j j j j j jj j j j jj jj j kkk k kkk kk kk kk kk kkk k k k k k k k kk k k k kk kk k lll l ll l ll ll ll ll ll ll l l l l l l ll l l l ll ll l mmmm mm m mmmm mm mm mmm m mm m m m m mm m m m mm mm m nnnn nn n nnnnnnnnn n n n n n n n n n nn n nn nn nn n o ooooo o oo oo oo oo oo o o o o o o o o oooo o oo oo o ppp p pp p pp pppp pp p p p p p p p p p p pp p p p pp pp p qqq q qq q qq qqqq qq q q q q q q q q q q qq q q q qq qq q rrrrrr r rrrrrrrrr r r r r r r r r r rr r rr rr rr r sss s ss s ss ssss ss s s s s s s s s ss ss s s s ss ss s tttt ttttt tt tt tt ttt t tt t t t t tt t t t tt tt t uuu u uu u uu uu uu uu uu uu u u u u u u uu u u u u u u u u vvv v vv v vv vv vv vv vv v v v v v v v v vv v v v v v v v v www w ww w ww ww ww ww ww w w w w w w w w ww w w w w w w w w xxx x xx x xx xx xx xx xx x x x x x x x x xx x x x xxx x x yyy y yy y yy yy yy yy yy y y y y y y y y yy y y y yy yy y Arctic Atlantic Continental Pacific 1 11111 111 11111 1 1 111111111 1 1 1 11111 11 0 5 10 15 20 25 30 35 0 5 10 15 20 25 dz 2 22222 222 22222 2 2 222222222 2 2 2 2 222222 333 333 333 33333 3 3 333333333 3 3 3 3333333 444 444 444 44444 4 4 444444444 4 4 4 4 444444 555 555 555 55555 5 5 555555555 555 5 5555 55 666 666 66666666 6 6 666666666 666 6 6666 66 777 777 7 77 7777777 777777777 77 7 7 777 7 77 888 888 8 88 8888888 88888888888 8 8 888 8 88 999 999 999 9999999 99999999999 9 9 999 9 99 000 000 000 0 0 000 0 0 000000000 00 0 0 000 0 00 aaa aaaaaaa a aaaa a aaaaaaaaa aa a a aaa a aa bbb bbbbbbbb bbbb b bbbbbbbbbbb b b bbb b bb ccc cccccc cc cc cc c ccccccccccc c c ccc c cc ddd ddd ddd dd dd d dd ddddddddd dd d d ddd d dd eee eeeeeeee eee e e eeeeeeeeeee e e eee e ee fff fff fff fffff f f ffffffff f ff f f fff f ff ggg ggg ggg ggggg g g g ggggg gg g g g g g gggggg hhh hhh hhh hh hhh h h h hhhhhhhh h h h h hhhh hh iii iii iii ii iii i iiii iii iii i i i i iiiiii jjj jjj jjj jj jjj j j jjj jjjjj j j j j j jjjjjj kkk kkk kkk kk kkk k k kkkkk kkk k k k k k kkkkkk lll lll lll ll lll l llllll l lll l l l l llllll mmm mmm mmm mmmmm m m mmmmm m m m m m m m m mmmmmm nnn nnn nnn nn nnn n n nnn nn nn n n n n n n nnnnnn ooo ooo ooo oo ooo o o oooooo oo o o o o o oooooo pppppp pppppppp p p ppppppppp p p p p pppppp qqq qqq qqqqqqqqq qqqqqqqqqq q q q q qqqqqq rrr rrr rrr rr rrr r rrrrrrrrrr r r r r rrrrrr sss sss sss sssss s s sssssssss s s s s ssssss ttt ttt ttt tt ttt t t tttttt tt t t t t t tttttt uuu uuu uuu uu uuu u u uuuuuu uuu u u u u uuuuuu vvv vvv vvv vv vvv v v vvvvv v vv v v v v v vvv v vv www www www ww ww w w wwww www www w w w w www w ww xxx xxx xxx xx xxx x x xxxxxx xx x x x x x xxx x xx yyy yyy yyy yy yy y y y yyyyyy yy y y y y y yyy y yy Arctic Atlantic Continental Pacific Figure 5.17: Euclidean distances between 35 places in Canada according to temperature (left plot) and their precipitations (right plot). In Figure 5.17, we can see that with the Euclidean distance and considering the temperature, there is clearly a region which differs from the other, the Arctic region (the coldest region). The most distinctive one in terms of the precipitations are the Pacific, the most rainy region (as we also saw in Figure 5.15). We see in Figure 5.18 that the results are equivalent. 5.2.3 Unsupervised classification The unsupervised classification problem is considered, which we will resolve using automatic procedures implemented in R. For that, kmeans.fd function is introduced in Section 4.10. Set ncl = 4, the number of clusters and method="exact" since the sample size is not too large. We choose the numbers of groups equals to 4 for same the idea above in 5.2.2. FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 5. Experiments 61 111111 1 11111111111 1 11 1 1111111 1 111 1 1 0 5 10 15 20 25 30 35 0 2000 4000 6000 8000 dtwy 222222 2 22222222222 2 22 2 22222222 22 22 2 333333 3 33333333333 3 33 3 33333333 33 33 3 444444 4 44444444444 4 44 4 4444444 4 44 4 4 4 555555 5 55555555555 5 55 5 55555555 55 55 5 666666 6 66666666666 6 66 6 666 66 666 66 66 6 7777 77 7777777 77 777777777 7 77 7 7 77777 7 8888888888888888888 888888 88 8 8 8 8888 8 9999999999999999999 999999 99 9 9 9 9999 9 000000 0 00000000000 0 000 000 00 0 0 0 00 00 0 aaaaaa a aaaaaaaaaaa a aa a aaa aa a aa aaaa a bbbbbb b bbbbbbbbbbb b bb b bbb bb bbb bb bb b cccccc c ccccccccccc c cc c ccc cc c cc cc cc c dddddd d ddddddddddd d dd d dddddddd dd dd d eeeeee e eeeeeeeeeee e ee e eeeeeeee ee ee e fffffffffffffffffff ffffff ff f f f ffff f ggggggggggggggggggg gggggg gg g g ggggg g hhhh hhhhhhhhhhh hhhhhhhhhh hh h h hhhhh h iii i ii i iiiiii i i iiii iii i i i ii i i ii i iii jjjjjjjjjjjjjjjjjjj jjjjjj jj j j jjjjj j kkkk kkkkkkkkkkk kkkkkkkkkk kk k k kkkkk k lll l ll lllllll ll lllllll ll l ll l l lllll l mmmmmmmmmmmmmmmmmm m mmm mmm mm m m m mmmm m nnnnnn n nnnnnnnnnnn n nn n nnnnn nnn nnnn n oooooo o ooooooooooo o oo o oooooooo oo oo o pppppp p pppppppp pp p p p p p p pppp p p p pp p p p qqqqqq q qqqqqqqq q q q q q q q q qqqq q q q qq q q q rrrrrr r rrrrrrrrrrr r rr r rrrrr rrr rrrr r ssssss s ssssssss ss s s ss s sssssss s ss s s s ttttttttttttttt ttttttttt t tt t t ttttt t uuu u uu u uuuuuu uu uuuuuuu u u u uu u u uuuuuu vvv v vv vvvvvvv vv vvvvvvv v v v vv v v vvvvvv www w ww w ww ww ww ww wwww www w w w ww w w wwwwww xxx x xx x xx xxxx xx xxxx xxx x x x xx x x x xxxxx yyy y y y y yy yy yy yy yy y y y y y y y y yy y y y yyyyy Arctic Atlantic Continental Pacific 111111 111111111 1 111111111 11 1 1111111 0 5 10 15 20 25 30 35 0 100 200 300 400 500 600 dtwz 222222 222222222 2 222222222 22 2 2222222 333333 333 333333 3 333333333 33 3 3 333333 444444 444444444 4444444444 444 4 444444 5555555555555555555555555 555 5 555555 6666666666666666666666666 666 6 666666 7777777777777777777777777777 7 777777 8888888888888888888888888888 8 888888 9999999999999999999999999999 9 999999 0000000000000000000000000 000 0 000000 aaaaaaaaaaaaaaaaaaaaaaaaa aaa a aaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbb b bbbbbb cccccccccccccccccccccccccccc c cccccc dddddddddddddddddddddddddddd d dddddd eeeeeeeeeeeeeeeeeeeeeeeeeeee e eeeeee fff ffffffffffffffffffffff ff f f ffffff ggg ggg ggggggggggggggggggg g g g g gggggg hhh hhh hhh hhhhhhhhhhhhhhhh h h h h hhhhhh iii iii iii iiiiiiiiiiiiiiii i i i i iiiiii jjj jjj jjj jjjjjjjjjjjjjjjj j j j j jjjjjj kkk kkk kkk kkkkkkkkkkkkkkkk k k k k kkkkkk lll lll lll llllllllllllllll l l l l llllll mmm mmm mmmmmmmmmmmmmmmmmmm m m m m mmmmmm nnn nnn nnn nnnnnnnnnnnnnnnn n n n n nnnnnn ooo ooo ooo oooooooooooooooo o o o o oooooo ppppppppppppppp pppppppppp pp p ppppppp qqqqqqqqqqqqqqqqqqqqqqqqq qqq q qqqqqq rrr rrrrrrrrrrrrrrrrrrrrrr rrr r rrrrrr sss s s s sss s sss s s s sss ssssss s s s s sss s s s ttt ttt ttt tttttttttttttttt t t t t tttttt uuu uuu uuu uuuuuuuuuuuuuuuu u u u u uuuuuu vvv vvv vvv vvvvvv vvvvvvvvvv v v v v vvvvvv www www www wwwwwwwwwwwwwwww w w w w wwwwww xxx xxx xxx xxxxxx xxxxxxxxxx x x x x xxxxxx yyy yyy yyy yyyyyy yyyyyyyyyy y y y y yyyyyy Arctic Atlantic Continental Pacific Figure 5.18: DTW distances between 35 places in Canada according to temperature (left plot) and their precipitations (right plot). dev.new() temp.cluster=kmeans.fd(fdata.t,ncl=4,draw=TRUE,method="sample",cluster.size=1) dev.new() prec.cluster=kmeans.fd(fdata.p,ncl=4,draw=TRUE,method="sample",cluster.size=1) In both, Figure 5.19 and Figure 5.20 all functional data are represented and colored depending on the group in which they have been classified. On the right, we can see the center curves in the last step of the iterative K-means process. 5.2.4 Supervised classification We could consider the supervised classification problem with monthlyPrecip dataset since every functional data in fdata.p belongs to a region (Arctic, Atlantic, Continental, Pacific). The final aim is to classify the first curve in fdata.p by taking the rest of dataset as a training sample. For this, we are going to create a classification model using k-Nearest neighbors method implemented by classif.knn function. To construct the classif object using the k-Nearest neighbors method, a training sample, their corresponding groups and the neighbors number knn are needed. FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 5. Experiments 62 2 4 6 8 10 12 −30 −20 −10 0 10 20 Assigning groups t X(t) 2 4 6 8 10 12 −30 −20 −10 0 10 20 Update centers t X(t) Figure 5.19: K-means. Average monthly temperature curves colored depending on the group in which they are classified. 2 4 6 8 10 12 0 2 4 6 8 10 12 Assigning groups t X(t) 2 4 6 8 10 12 0 2 4 6 8 10 12 Update centers t X(t) Figure 5.20: K-means. Average monthly precipitation curves colored depending on the group in which they are classified. FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Chapter 5. Experiments 63 Training data train.sample consists of fdata.p but excluding “St. Johns” row, since it is the time series of which we want to know the group. The associated vector of the groups train.groups is the character vector region, but converted from “character” to “factor”. The summary of the classif object and the resulting prediction follows. train.sample<-fdata.p[2:35,] train.groups<-fac[2:35] out<-classif.knn(train.groups,train.sample,knn=3) summary(out) test.sample<-fdata.p[1,] pred<-predict(out,test.sample) pred >summary(out) - SUMMARY - -Probability of correct classification by group (prob.classification): y Arctic Atlantic Continental Pacific 0.0000000 1.0000000 0.9166667 0.0000000 -Confusion matrix between the theoretical groups (by rows) and estimated groups (by column) Arctic Atlantic Continental Pacific Arctic 0 0 3 0 Atlantic 0 14 0 0 Continental 0 1 11 0 Pacific 0 3 2 0 -Vector of probability of correct classification FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
References 64 by number of neighbors (knn): 3 0.7353 -Optimal number of neighbors: knn.opt= 3 with highest probability of correct classification max.prob= 0.7352941 -Probability of correct classification: 0.7353 > pred [1] Atlantic Levels: Arctic Atlantic Continental Pacific The prediction is correct since “St. Johns” belongs to the Atlantic region. But looking at the confusion matrix, we realize that this model is not very reliable. In the dataset, three of the curves within the Arctic region, and none of them are classified as such. The same happens for the five functional data that belong to “Pacific”, three of them are classified as “Atlantic” and the other two as “Continental”. This problem may arise from a lack of functional data, in general, or from a lack of data within the Arctic and Pacific regions compared to the other two regions (“Atlantic” and “Continental” data are three times more than “Arctic” and “Pacific” data). FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree
Bibliography [1] Pablo Le´on Alcaide. Ag-costane: Algoritmos gen´eticos para el c´alculo del centroide de un grupo de series temporales mediante una distancia no eucl´ıdea. 2017. [2] Amparo Ba´ıllo, Antonio Cuevas, and Ricardo Fraiman. Classification methods for functional data. The Oxford handbook of functional data analysis, 2011. [3] Michael R. Berthold and Frank H¨oppner. On clustering time series using euclidean distance and pearson correlation. CoRR, abs/1601.02213, 2016. [4] Antonio Cuevas. A partial overview of the theory of statistics with functional data. Journal of Statistical Planning and Inference, 147:1–23, 2014. [5] Chotirat Ann Ratanamahatana Eamonn Keogh. Exact indexing of dynamic time warping. Knowledge and Information Systems, 7:358–386, 2005. [6] Manuel Febrero-Bande and Manuel Oviedo de la Fuente. Statistical computing in functional data analysis: The r package fda.usc. Journal of Statistical Software, 51(4):1–28, 2012. [7] Hesam Izakian, Witold Pedrycz, and Iqbal Jamal. Fuzzy clustering of time series data using dynamic time warping distance. Engineering Applications of Artificial Intelligence, 39:235–244, 2015. [8] Piotr Kokoszka and Matthew Reimherr. Introduction to functional data analysis. Chapman and Hall/CRC, 2017. [9] Carlos Arturo Lesmes. Como hacer un reporte estad´ıstico con sweave. r y latex via sweave. Comunicaciones en Estad´ıstica, 6(1):9–19, 2013. [10] Meinard M¨uller. Dynamic Time Warping, pages 69–84. Springer Berlin Heidelberg, Berlin, Heidelberg, 2007. 65
References 66 [11] James O Ramsay and Bernard W Silverman. Fitting differential equations to functional data: Principal differential analysis. Springer, 2005. [12] JO Ramsay, Hadley Wickham, Maintainer JO Ramsay, and Suggests deSolve. Package ‘fda’, 2021. [13] Jerome Friedman Trevor Hastie, Robert Tibshirani. The Elements of Statistical Learning. Springer, 2001. [14] Jane-Ling Wang, Jeng-Min Chiou, and Hans-Georg M¨uller. Functional data analysis. Annual Review of Statistics and Its Application, 3(1):257–295, 2016. [15] Wenjie Wang and Jun Yan. Shape-restricted regression splines with R package splines2. Journal of Data Science, 19(3):498–517, 2021. [16] Wenjie Wang and Jun Yan. splines2: Regression Spline Functions and Classes, 2021. R package version 0.4.5. FDP Bel´en del Roc´ıo Garc´ıa Camino. Mathematics Degree