R-  3.1.1
About: R (also known as "GNU S") is a system for statistical computation and graphics.
  Fossies Dox: R-3.1.1.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cov.c
Go to the documentation of this file.
1 /*
2  * R : A Computer Language for Statistical Data Analysis
3  * Copyright (C) 1995-2013 The R Core Team
4  * Copyright (C) 2003 The R Foundation
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, a copy is available at
18  * http://www.r-project.org/Licenses/
19  */
20 
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24 
25 #ifdef HAVE_LONG_DOUBLE
26 # define SQRTL sqrtl
27 #else
28 # define SQRTL sqrt
29 #endif
30 
31 #include <Defn.h>
32 #include <Rmath.h>
33 
34 #include "statsR.h"
35 #undef _
36 #ifdef ENABLE_NLS
37 #include <libintl.h>
38 #define _(String) dgettext ("stats", String)
39 #else
40 #define _(String) (String)
41 #endif
42 
43 static SEXP corcov(SEXP x, SEXP y, SEXP na_method, SEXP kendall, Rboolean cor);
44 
45 
46 SEXP cor(SEXP x, SEXP y, SEXP na_method, SEXP kendall)
47 {
48  return corcov(x, y, na_method, kendall, TRUE);
49 }
50 SEXP cov(SEXP x, SEXP y, SEXP na_method, SEXP kendall)
51 {
52  return corcov(x, y, na_method, kendall, FALSE);
53 }
54 
55 
56 
57 #define COV_SUM_UPDATE \
58  sum += xm * ym; \
59  if(cor) { \
60  xsd += xm * xm; \
61  ysd += ym * ym; \
62  }
63 
64 #define ANS(I,J) ans[I + J * ncx]
65 
66 /* Note that "if (kendall)" and "if (cor)" are used inside a double for() loop;
67  which makes the code better readable -- and is hopefully dealt with
68  by a smartly optimizing compiler
69 */
70 
73 #define COV_PAIRWISE_BODY \
74  LDOUBLE sum, xmean = 0., ymean = 0., xsd, ysd, xm, ym; \
75  int k, nobs, n1 = -1; /* -Wall initializing */ \
76  \
77  nobs = 0; \
78  if(!kendall) { \
79  xmean = ymean = 0.; \
80  for (k = 0 ; k < n ; k++) { \
81  if(!(ISNAN(xx[k]) || ISNAN(yy[k]))) { \
82  nobs ++; \
83  xmean += xx[k]; \
84  ymean += yy[k]; \
85  } \
86  } \
87  } else /*kendall*/ \
88  for (k = 0 ; k < n ; k++) \
89  if(!(ISNAN(xx[k]) || ISNAN(yy[k]))) \
90  nobs ++; \
91  \
92  if (nobs >= 2) { \
93  xsd = ysd = sum = 0.; \
94  if(!kendall) { \
95  xmean /= nobs; \
96  ymean /= nobs; \
97  n1 = nobs-1; \
98  } \
99  for(k=0; k < n; k++) { \
100  if(!(ISNAN(xx[k]) || ISNAN(yy[k]))) { \
101  if(!kendall) { \
102  xm = xx[k] - xmean; \
103  ym = yy[k] - ymean; \
104  \
105  COV_SUM_UPDATE \
106  } \
107  else { /* Kendall's tau */ \
108  for(n1=0 ; n1 < k ; n1++) \
109  if(!(ISNAN(xx[n1]) || ISNAN(yy[n1]))) { \
110  xm = sign(xx[k] - xx[n1]); \
111  ym = sign(yy[k] - yy[n1]); \
112  \
113  COV_SUM_UPDATE \
114  } \
115  } \
116  } \
117  } \
118  if (cor) { \
119  if(xsd == 0. || ysd == 0.) { \
120  *sd_0 = TRUE; \
121  sum = NA_REAL; \
122  } \
123  else { \
124  if(!kendall) { \
125  xsd /= n1; \
126  ysd /= n1; \
127  sum /= n1; \
128  } \
129  sum /= (SQRTL(xsd) * SQRTL(ysd)); \
130  if(sum > 1.) sum = 1.; \
131  } \
132  } \
133  else if(!kendall) \
134  sum /= n1; \
135  \
136  ANS(i,j) = (double) sum; \
137  } \
138  else \
139  ANS(i,j) = NA_REAL
140 
141 
142 static void cov_pairwise1(int n, int ncx, double *x,
143  double *ans, Rboolean *sd_0, Rboolean cor,
144  Rboolean kendall)
145 {
146  for (int i = 0 ; i < ncx ; i++) {
147  double *xx = &x[i * n];
148  for (int j = 0 ; j <= i ; j++) {
149  double *yy = &x[j * n];
150 
152 
153  ANS(j,i) = ANS(i,j);
154  }
155  }
156 }
157 
158 static void cov_pairwise2(int n, int ncx, int ncy, double *x, double *y,
159  double *ans, Rboolean *sd_0, Rboolean cor,
160  Rboolean kendall)
161 {
162  for (int i = 0 ; i < ncx ; i++) {
163  double *xx = &x[i * n];
164  for (int j = 0 ; j < ncy ; j++) {
165  double *yy = &y[j * n];
166 
168  }
169  }
170 }
171 #undef COV_PAIRWISE_BODY
172 
173 
174 /* method = "complete" or "all.obs" (only difference: na_fail):
175  * -------- -------
176 */
177 #define COV_ini_0 \
178  LDOUBLE sum, tmp, xxm, yym; \
179  double *xx, *yy; \
180  int i, j, k, n1=-1/* -Wall */
181 
182 #define COV_n_le_1(_n_,_k_) \
183  if (_n_ <= 1) {/* too many missing */ \
184  for (i = 0 ; i < ncx ; i++) \
185  for (j = 0 ; j < _k_ ; j++) \
186  ANS(i,j) = NA_REAL; \
187  return; \
188  }
189 
190 #define COV_init(_ny_) \
191  COV_ini_0; int nobs; \
192  \
193  /* total number of complete observations */ \
194  nobs = 0; \
195  for(k = 0 ; k < n ; k++) { \
196  if (ind[k] != 0) nobs++; \
197  } \
198  COV_n_le_1(nobs, _ny_)
199 
200 #define COV_ini_na(_ny_) \
201  COV_ini_0; \
202  COV_n_le_1(n, _ny_)
203 
204 
205 /* This uses two passes for better accuracy */
206 #define MEAN(_X_) \
207  /* variable means */ \
208  for (i = 0 ; i < nc##_X_ ; i++) { \
209  xx = &_X_[i * n]; \
210  sum = 0.; \
211  for (k = 0 ; k < n ; k++) \
212  if(ind[k] != 0) \
213  sum += xx[k]; \
214  tmp = sum / nobs; \
215  if(R_FINITE((double)tmp)) { \
216  sum = 0.; \
217  for (k = 0 ; k < n ; k++) \
218  if(ind[k] != 0) \
219  sum += (xx[k] - tmp); \
220  tmp = tmp + sum / nobs; \
221  } \
222  _X_##m [i] = (double)tmp; \
223  }
224 
225 /* This uses two passes for better accuracy */
226 #define MEAN_(_X_,_HAS_NA_) \
227  /* variable means (has_na) */ \
228  for (i = 0 ; i < nc##_X_ ; i++) { \
229  if(_HAS_NA_[i]) \
230  tmp = NA_REAL; \
231  else { \
232  xx = &_X_[i * n]; \
233  sum = 0.; \
234  for (k = 0 ; k < n ; k++) \
235  sum += xx[k]; \
236  tmp = sum / n; \
237  if(R_FINITE((double)tmp)) { \
238  sum = 0.; \
239  for (k = 0 ; k < n ; k++) \
240  sum += (xx[k] - tmp); \
241  tmp = tmp + sum / n; \
242  } \
243  } \
244  _X_##m [i] = (double)tmp; \
245  }
246 
247 
248 static void
249 cov_complete1(int n, int ncx, double *x, double *xm,
250  int *ind, double *ans, Rboolean *sd_0, Rboolean cor,
251  Rboolean kendall)
252 {
253  COV_init(ncx);
254 
255  if(!kendall) {
256  MEAN(x);/* -> xm[] */
257  n1 = nobs - 1;
258  }
259  for (i = 0 ; i < ncx ; i++) {
260  xx = &x[i * n];
261 
262  if(!kendall) {
263  xxm = xm[i];
264  for (j = 0 ; j <= i ; j++) {
265  yy = &x[j * n];
266  yym = xm[j];
267  sum = 0.;
268  for (k = 0 ; k < n ; k++)
269  if (ind[k] != 0)
270  sum += (xx[k] - xxm) * (yy[k] - yym);
271  ANS(j,i) = ANS(i,j) = (double)(sum / n1);
272  }
273  }
274  else { /* Kendall's tau */
275  for (j = 0 ; j <= i ; j++) {
276  yy = &x[j * n];
277  sum = 0.;
278  for (k = 0 ; k < n ; k++)
279  if (ind[k] != 0)
280  for (n1 = 0 ; n1 < n ; n1++)
281  if (ind[n1] != 0)
282  sum += sign(xx[k] - xx[n1])
283  * sign(yy[k] - yy[n1]);
284  ANS(j,i) = ANS(i,j) = (double)sum;
285  }
286  }
287  }
288 
289  if (cor) {
290  for (i = 0 ; i < ncx ; i++)
291  xm[i] = sqrt(ANS(i,i));
292  for (i = 0 ; i < ncx ; i++) {
293  for (j = 0 ; j < i ; j++) {
294  if (xm[i] == 0 || xm[j] == 0) {
295  *sd_0 = TRUE;
296  ANS(j,i) = ANS(i,j) = NA_REAL;
297  }
298  else {
299  sum = ANS(i,j) / (xm[i] * xm[j]);
300  if(sum > 1.) sum = 1.;
301  ANS(j,i) = ANS(i,j) = (double)sum;
302  }
303  }
304  ANS(i,i) = 1.0;
305  }
306  }
307 } /* cov_complete1 */
308 
309 static void
310 cov_na_1(int n, int ncx, double *x, double *xm,
311  int *has_na, double *ans, Rboolean *sd_0, Rboolean cor,
312  Rboolean kendall)
313 {
314 
315  COV_ini_na(ncx);
316 
317  if(!kendall) {
318  MEAN_(x, has_na);/* -> xm[] */
319  n1 = n - 1;
320  }
321  for (i = 0 ; i < ncx ; i++) {
322  if(has_na[i]) {
323  for (j = 0 ; j <= i ; j++)
324  ANS(j,i) = ANS(i,j) = NA_REAL;
325  }
326  else {
327  xx = &x[i * n];
328 
329  if(!kendall) {
330  xxm = xm[i];
331  for (j = 0 ; j <= i ; j++)
332  if(has_na[j]) {
333  ANS(j,i) = ANS(i,j) = NA_REAL;
334  } else {
335  yy = &x[j * n];
336  yym = xm[j];
337  sum = 0.;
338  for (k = 0 ; k < n ; k++)
339  sum += (xx[k] - xxm) * (yy[k] - yym);
340  ANS(j,i) = ANS(i,j) = (double)(sum / n1);
341  }
342  }
343  else { /* Kendall's tau */
344  for (j = 0 ; j <= i ; j++)
345  if(has_na[j]) {
346  ANS(j,i) = ANS(i,j) = NA_REAL;
347  } else {
348  yy = &x[j * n];
349  sum = 0.;
350  for (k = 0 ; k < n ; k++)
351  for (n1 = 0 ; n1 < n ; n1++)
352  sum += sign(xx[k] - xx[n1]) * sign(yy[k] - yy[n1]);
353  ANS(j,i) = ANS(i,j) = (double)sum;
354  }
355  }
356  }
357  }
358 
359  if (cor) {
360  for (i = 0 ; i < ncx ; i++)
361  if(!has_na[i]) xm[i] = sqrt(ANS(i,i));
362  for (i = 0 ; i < ncx ; i++) {
363  if(!has_na[i]) for (j = 0 ; j < i ; j++) {
364  if (xm[i] == 0 || xm[j] == 0) {
365  *sd_0 = TRUE;
366  ANS(j,i) = ANS(i,j) = NA_REAL;
367  }
368  else {
369  sum = ANS(i,j) / (xm[i] * xm[j]);
370  if(sum > 1.) sum = 1.;
371  ANS(j,i) = ANS(i,j) = (double)sum;
372  }
373  }
374  ANS(i,i) = 1.0;
375  }
376  }
377 } /* cov_na_1() */
378 
379 static void
380 cov_complete2(int n, int ncx, int ncy, double *x, double *y,
381  double *xm, double *ym, int *ind,
382  double *ans, Rboolean *sd_0, Rboolean cor, Rboolean kendall)
383 {
384  COV_init(ncy);
385 
386  if(!kendall) {
387  MEAN(x);/* -> xm[] */
388  MEAN(y);/* -> ym[] */
389  n1 = nobs - 1;
390  }
391  for (i = 0 ; i < ncx ; i++) {
392  xx = &x[i * n];
393  if(!kendall) {
394  xxm = xm[i];
395  for (j = 0 ; j < ncy ; j++) {
396  yy = &y[j * n];
397  yym = ym[j];
398  sum = 0.;
399  for (k = 0 ; k < n ; k++)
400  if (ind[k] != 0)
401  sum += (xx[k] - xxm) * (yy[k] - yym);
402  ANS(i,j) = (double)(sum / n1);
403  }
404  }
405  else { /* Kendall's tau */
406  for (j = 0 ; j < ncy ; j++) {
407  yy = &y[j * n];
408  sum = 0.;
409  for (k = 0 ; k < n ; k++)
410  if (ind[k] != 0)
411  for (n1 = 0 ; n1 < n ; n1++)
412  if (ind[n1] != 0)
413  sum += sign(xx[k] - xx[n1])
414  * sign(yy[k] - yy[n1]);
415  ANS(i,j) = (double)sum;
416  }
417  }
418  }
419 
420  if (cor) {
421 
422 #define COV_SDEV(_X_) \
423  for (i = 0 ; i < nc##_X_ ; i++) { /* Var(X[i]) */ \
424  xx = &_X_[i * n]; \
425  sum = 0.; \
426  if(!kendall) { \
427  xxm = _X_##m [i]; \
428  for (k = 0 ; k < n ; k++) \
429  if (ind[k] != 0) \
430  sum += (xx[k] - xxm) * (xx[k] - xxm); \
431  sum /= n1; \
432  } \
433  else { /* Kendall's tau */ \
434  for (k = 0 ; k < n ; k++) \
435  if (ind[k] != 0) \
436  for (n1 = 0 ; n1 < n ; n1++) \
437  if (ind[n1] != 0 && xx[k] != xx[n1]) \
438  sum ++; /* = sign(. - .)^2 */ \
439  } \
440  _X_##m [i] = (double)SQRTL(sum); \
441  }
442 
443  COV_SDEV(x); /* -> xm[.] */
444  COV_SDEV(y); /* -> ym[.] */
445 
446  for (i = 0 ; i < ncx ; i++)
447  for (j = 0 ; j < ncy ; j++)
448  if (xm[i] == 0. || ym[j] == 0.) {
449  *sd_0 = TRUE;
450  ANS(i,j) = NA_REAL;
451  }
452  else {
453  ANS(i,j) /= (xm[i] * ym[j]);
454  if(ANS(i,j) > 1.) ANS(i,j) = 1.;
455  }
456  }/* cor */
457 
458 }/* cov_complete2 */
459 #undef COV_SDEV
460 
461 static void
462 cov_na_2(int n, int ncx, int ncy, double *x, double *y,
463  double *xm, double *ym, int *has_na_x, int *has_na_y,
464  double *ans, Rboolean *sd_0, Rboolean cor, Rboolean kendall)
465 {
466  COV_ini_na(ncy);
467 
468  if(!kendall) {
469  MEAN_(x, has_na_x);/* -> xm[] */
470  MEAN_(y, has_na_y);/* -> ym[] */
471  n1 = n - 1;
472  }
473  for (i = 0 ; i < ncx ; i++) {
474  if(has_na_x[i]) {
475  for (j = 0 ; j < ncy; j++)
476  ANS(i,j) = NA_REAL;
477  }
478  else {
479  xx = &x[i * n];
480  if(!kendall) {
481  xxm = xm[i];
482  for (j = 0 ; j < ncy ; j++)
483  if(has_na_y[j]) {
484  ANS(i,j) = NA_REAL;
485  } else {
486  yy = &y[j * n];
487  yym = ym[j];
488  sum = 0.;
489  for (k = 0 ; k < n ; k++)
490  sum += (xx[k] - xxm) * (yy[k] - yym);
491  ANS(i,j) = (double)(sum / n1);
492  }
493  }
494  else { /* Kendall's tau */
495  for (j = 0 ; j < ncy ; j++)
496  if(has_na_y[j]) {
497  ANS(i,j) = NA_REAL;
498  } else {
499  yy = &y[j * n];
500  sum = 0.;
501  for (k = 0 ; k < n ; k++)
502  for (n1 = 0 ; n1 < n ; n1++)
503  sum += sign(xx[k] - xx[n1]) * sign(yy[k] - yy[n1]);
504  ANS(i,j) = (double)sum;
505  }
506  }
507  }
508  }
509 
510  if (cor) {
511 
512 #define COV_SDEV(_X_) \
513  for (i = 0 ; i < nc##_X_ ; i++) \
514  if(!has_na_##_X_[i]) { /* Var(X[j]) */ \
515  xx = &_X_[i * n]; \
516  sum = 0.; \
517  if(!kendall) { \
518  xxm = _X_##m [i]; \
519  for (k = 0 ; k < n ; k++) \
520  sum += (xx[k] - xxm) * (xx[k] - xxm); \
521  sum /= n1; \
522  } \
523  else { /* Kendall's tau */ \
524  for (k = 0 ; k < n ; k++) \
525  for (n1 = 0 ; n1 < n ; n1++) \
526  if (xx[k] != xx[n1]) \
527  sum ++; /* = sign(. - .)^2 */ \
528  } \
529  _X_##m [i] = (double) SQRTL(sum); \
530  }
531 
532  COV_SDEV(x); /* -> xm[.] */
533  COV_SDEV(y); /* -> ym[.] */
534 
535  for (i = 0 ; i < ncx ; i++)
536  if(!has_na_x[i]) {
537  for (j = 0 ; j < ncy ; j++)
538  if(!has_na_y[j]) {
539  if (xm[i] == 0. || ym[j] == 0.) {
540  *sd_0 = TRUE;
541  ANS(i,j) = NA_REAL;
542  }
543  else {
544  ANS(i,j) /= (xm[i] * ym[j]);
545  if(ANS(i,j) > 1.) ANS(i,j) = 1.;
546  }
547  }
548  }
549  }/* cor */
550 
551 }/* cov_na_2 */
552 
553 #undef ANS
554 #undef COV_init
555 #undef MEAN
556 #undef MEAN_
557 #undef COV_SDEV
558 
559 /* This might look slightly inefficient, but it is designed to
560  * optimise paging in virtual memory systems ...
561  * (or at least that's my story, and I'm sticking to it.)
562 */
563 #define NA_LOOP \
564  for (i = 0 ; i < n ; i++) \
565  if (ISNAN(z[i])) { \
566  if (na_fail) error(_("missing observations in cov/cor"));\
567  else ind[i] = 0; \
568  }
569 
570 #define COMPLETE_1 \
571  double *z; \
572  int i, j; \
573  for (i = 0 ; i < n ; i++) \
574  ind[i] = 1; \
575  for (j = 0 ; j < ncx ; j++) { \
576  z = &x[j * n]; \
577  NA_LOOP \
578  }
579 
580 static void complete1(int n, int ncx, double *x, int *ind, Rboolean na_fail)
581 {
582  COMPLETE_1
583 }
584 
585 static void
586 complete2(int n, int ncx, int ncy, double *x, double *y, int *ind, Rboolean na_fail)
587 {
588  COMPLETE_1
589 
590  for(j = 0 ; j < ncy ; j++) {
591  z = &y[j * n];
592  NA_LOOP
593  }
594 }
595 
596 #define NA_CHECK(_HAS_NA_) \
597  for (i = 0 ; i < n ; i++) \
598  if (ISNAN(z[i])) { \
599  _HAS_NA_[j] = 1; break; \
600  }
601 
602 #define HAS_NA_1(_X_,_HAS_NA_) \
603  for (j = 0 ; j < nc##_X_ ; j++) { \
604  z = &_X_[j * n]; \
605  _HAS_NA_[j] = 0; \
606  NA_CHECK(_HAS_NA_) \
607  }
608 
609 
610 static void find_na_1(int n, int ncx, double *x, int *has_na)
611 {
612  double *z;
613  int i, j;
614  HAS_NA_1(x, has_na)
615 }
616 
617 static void
618 find_na_2(int n, int ncx, int ncy, double *x, double *y, int *has_na_x, int *has_na_y)
619 {
620  double *z;
621  int i, j;
622  HAS_NA_1(x, has_na_x)
623  HAS_NA_1(y, has_na_y)
624 }
625 
626 #undef NA_LOOP
627 #undef COMPLETE_1
628 #undef NA_CHECK
629 #undef HAS_NA_1
630 
631 /* co[vr](x, y, use =
632  { 1, 2, 3, 4, 5 }
633  "all.obs", "complete.obs", "pairwise.complete", "everything", "na.or.complete"
634  kendall = TRUE/FALSE)
635 */
636 static SEXP corcov(SEXP x, SEXP y, SEXP na_method, SEXP skendall, Rboolean cor)
637 {
638  SEXP ans, xm, ym, ind;
639  Rboolean ansmat, kendall, pair, na_fail, everything, sd_0, empty_err;
640  int i, method, n, ncx, ncy, nprotect = 2;
641 
642  /* Arg.1: x */
643  if(isNull(x)) /* never allowed */
644  error(_("'x' is NULL"));
645  /* length check of x -- only if(empty_err) --> below */
646  x = PROTECT(coerceVector(x, REALSXP));
647  if ((ansmat = isMatrix(x))) {
648  n = nrows(x);
649  ncx = ncols(x);
650  }
651  else {
652  n = length(x);
653  ncx = 1;
654  }
655  /* Arg.2: y */
656  if (isNull(y)) {/* y = x : var() */
657  ncy = ncx;
658  } else {
659  y = PROTECT(coerceVector(y, REALSXP));
660  nprotect++;
661  if (isMatrix(y)) {
662  if (nrows(y) != n)
663  error(_("incompatible dimensions"));
664  ncy = ncols(y);
665  ansmat = TRUE;
666  }
667  else {
668  if (length(y) != n)
669  error(_("incompatible dimensions"));
670  ncy = 1;
671  }
672  }
673  /* Arg.3: method */
674  method = asInteger(na_method);
675 
676  /* Arg.4: kendall */
677  kendall = asLogical(skendall);
678 
679  /* "default: complete" (easier for -Wall) */
680  na_fail = FALSE; everything = FALSE; empty_err = TRUE;
681  pair = FALSE;
682  switch(method) {
683  case 1: /* use all : no NAs */
684  na_fail = TRUE;
685  break;
686  case 2: /* complete */
687  /* did na.omit in R */
688  if (!LENGTH(x)) error(_("no complete element pairs"));
689  break;
690  case 3: /* pairwise.complete */
691  pair = TRUE;
692  break;
693  case 4: /* "everything": NAs are propagated */
694  everything = TRUE;
695  empty_err = FALSE;
696  break;
697  case 5: /* "na.or.complete": NAs are propagated */
698  empty_err = FALSE;
699  break;
700  default:
701  error(_("invalid 'use' (computational method)"));
702  }
703  if (empty_err && !LENGTH(x))
704  error(_("'x' is empty"));
705 
706  if (ansmat) PROTECT(ans = allocMatrix(REALSXP, ncx, ncy));
707  else PROTECT(ans = allocVector(REALSXP, ncx * ncy));
708  sd_0 = FALSE;
709  if (isNull(y)) {
710  if (everything) { /* NA's are propagated */
711  PROTECT(xm = allocVector(REALSXP, ncx));
712  PROTECT(ind = allocVector(LGLSXP, ncx));
713  find_na_1(n, ncx, REAL(x), /* --> has_na[] = */ LOGICAL(ind));
714  cov_na_1 (n, ncx, REAL(x), REAL(xm), LOGICAL(ind), REAL(ans), &sd_0, cor, kendall);
715 
716  UNPROTECT(2);
717  }
718  else if (!pair) { /* all | complete "var" */
719  PROTECT(xm = allocVector(REALSXP, ncx));
720  PROTECT(ind = allocVector(INTSXP, n));
721  complete1(n, ncx, REAL(x), INTEGER(ind), na_fail);
722  cov_complete1(n, ncx, REAL(x), REAL(xm),
723  INTEGER(ind), REAL(ans), &sd_0, cor, kendall);
724  if(empty_err) {
725  Rboolean indany = FALSE;
726  for(i = 0; i < n; i++) {
727  if(INTEGER(ind)[i] == 1) { indany = TRUE; break; }
728  }
729  if(!indany) error(_("no complete element pairs"));
730  }
731  UNPROTECT(2);
732  }
733  else { /* pairwise "var" */
734  cov_pairwise1(n, ncx, REAL(x), REAL(ans), &sd_0, cor, kendall);
735  }
736  }
737  else { /* Co[vr] (x, y) */
738  if (everything) {
739  SEXP has_na_y;
740  PROTECT(xm = allocVector(REALSXP, ncx));
741  PROTECT(ym = allocVector(REALSXP, ncy));
742  PROTECT(ind = allocVector(LGLSXP, ncx));
743  PROTECT(has_na_y = allocVector(LGLSXP, ncy));
744 
745  find_na_2(n, ncx, ncy, REAL(x), REAL(y), INTEGER(ind), INTEGER(has_na_y));
746  cov_na_2 (n, ncx, ncy, REAL(x), REAL(y), REAL(xm), REAL(ym),
747  INTEGER(ind), INTEGER(has_na_y), REAL(ans), &sd_0, cor, kendall);
748  UNPROTECT(4);
749  }
750  else if (!pair) { /* all | complete */
751  PROTECT(xm = allocVector(REALSXP, ncx));
752  PROTECT(ym = allocVector(REALSXP, ncy));
753  PROTECT(ind = allocVector(INTSXP, n));
754  complete2(n, ncx, ncy, REAL(x), REAL(y), INTEGER(ind), na_fail);
755  cov_complete2(n, ncx, ncy, REAL(x), REAL(y), REAL(xm), REAL(ym),
756  INTEGER(ind), REAL(ans), &sd_0, cor, kendall);
757  if(empty_err) {
758  Rboolean indany = FALSE;
759  for(i = 0; i < n; i++) {
760  if(INTEGER(ind)[i] == 1) { indany = TRUE; break; }
761  }
762  if(!indany) error(_("no complete element pairs"));
763  }
764  UNPROTECT(3);
765  }
766  else { /* pairwise */
767  cov_pairwise2(n, ncx, ncy, REAL(x), REAL(y), REAL(ans),
768  &sd_0, cor, kendall);
769  }
770  }
771  if (ansmat) { /* set dimnames() when applicable */
772  if (isNull(y)) {
773  x = getAttrib(x, R_DimNamesSymbol);
774  if (!isNull(x) && !isNull(VECTOR_ELT(x, 1))) {
775  PROTECT(ind = allocVector(VECSXP, 2));
776  SET_VECTOR_ELT(ind, 0, duplicate(VECTOR_ELT(x, 1)));
777  SET_VECTOR_ELT(ind, 1, duplicate(VECTOR_ELT(x, 1)));
778  setAttrib(ans, R_DimNamesSymbol, ind);
779  UNPROTECT(1);
780  }
781  }
782  else {
783  x = getAttrib(x, R_DimNamesSymbol);
784  y = getAttrib(y, R_DimNamesSymbol);
785  if ((length(x) >= 2 && !isNull(VECTOR_ELT(x, 1))) ||
786  (length(y) >= 2 && !isNull(VECTOR_ELT(y, 1)))) {
787  PROTECT(ind = allocVector(VECSXP, 2));
788  if (length(x) >= 2 && !isNull(VECTOR_ELT(x, 1)))
789  SET_VECTOR_ELT(ind, 0, duplicate(VECTOR_ELT(x, 1)));
790  if (length(y) >= 2 && !isNull(VECTOR_ELT(y, 1)))
791  SET_VECTOR_ELT(ind, 1, duplicate(VECTOR_ELT(y, 1)));
792  setAttrib(ans, R_DimNamesSymbol, ind);
793  UNPROTECT(1);
794  }
795  }
796  }
797  if(sd_0)/* only in cor() */
798  warning(_("the standard deviation is zero"));
799  UNPROTECT(nprotect);
800  return ans;
801 }
#define VECSXP
Definition: Rinternals.h:119
#define NA_REAL
Definition: Arith.h:54
#define UNPROTECT(n)
Definition: Rinternals.h:633
LibExtern SEXP R_DimNamesSymbol
Definition: Rinternals.h:672
double *() REAL(SEXP x)
Definition: memory.c:3377
Rboolean
Definition: Boolean.h:31
#define REALSXP
Definition: Rinternals.h:112
SEXP SET_VECTOR_ELT(SEXP x, R_xlen_t i, SEXP v)
Definition: memory.c:3414
#define MEAN_(_X_, _HAS_NA_)
Definition: cov.c:226
SEXP coerceVector(SEXP v, SEXPTYPE type)
Definition: coerce.c:1164
#define HAS_NA_1(_X_, _HAS_NA_)
Definition: cov.c:602
void warning(const char *format,...)
Definition: errors.c:261
SEXP() VECTOR_ELT(SEXP x, R_xlen_t i)
Definition: memory.c:3345
j
Definition: qsort-body.c:133
lzma_index ** i
Definition: index.h:625
#define COV_init(_ny_)
Definition: cov.c:190
#define y(k)
#define _(String)
Definition: cov.c:40
INLINE_FUN R_len_t length(SEXP s)
Definition: Rinlinedfuns.h:122
INLINE_FUN Rboolean isMatrix(SEXP s)
Definition: Rinlinedfuns.h:494
static void n
Definition: printvector.c:263
void error(const char *format,...)
Definition: errors.c:744
int *() LOGICAL(SEXP x)
Definition: memory.c:3355
SEXP setAttrib(SEXP vec, SEXP name, SEXP val)
Definition: attrib.c:220
Definition: inflate.h:48
SEXP cor(SEXP x, SEXP y, SEXP na_method, SEXP kendall)
Definition: cov.c:46
int asLogical(SEXP x)
Definition: coerce.c:1617
int nrows(SEXP s)
Definition: util.c:77
int ncols(SEXP s)
Definition: util.c:93
#define COMPLETE_1
Definition: cov.c:570
#define MEAN(_X_)
Definition: cov.c:206
#define COV_SDEV(_X_)
#define isNull
Definition: Rinternals.h:1109
#define ANS(I, J)
Definition: cov.c:64
int asInteger(SEXP x)
Definition: coerce.c:1646
#define COV_ini_na(_ny_)
Definition: cov.c:200
INTt k
Definition: qsort-body.c:48
Definition: Boolean.h:31
#define NA_LOOP
Definition: cov.c:563
#define COV_PAIRWISE_BODY
Definition: cov.c:73
Definition: Boolean.h:31
struct SEXPREC * SEXP
Definition: Rinternals.h:424
#define PROTECT(s)
Definition: Rinternals.h:632
#define LGLSXP
Definition: Rinternals.h:109
SEXP getAttrib(SEXP vec, SEXP name)
Definition: attrib.c:157
double sign(double x)
Definition: sign.c:35
SEXP cov(SEXP x, SEXP y, SEXP na_method, SEXP kendall)
Definition: cov.c:50
SEXP duplicate(SEXP s)
Definition: duplicate.c:126
INLINE_FUN SEXP allocVector(SEXPTYPE type, R_xlen_t length)
Definition: Rinlinedfuns.h:187
#define INTSXP
Definition: Rinternals.h:111
SEXP allocMatrix(SEXPTYPE mode, int nrow, int ncol)
Definition: array.c:199
int *() INTEGER(SEXP x)
Definition: memory.c:3363