

   cor {base}                                   R Documentation

   CCoorrrreellaattiioonn aanndd CCoovvaarriiaannccee MMaattrriicceess

   DDeessccrriippttiioonn::

        Compute the correlation or covariance matrix of the
        columns of `x' and the columns of `y'.

   UUssaaggee::

        cor(x, y=x, use="all.obs")
        cov(x, y=x, use="all.obs")

   AArrgguummeennttss::

          x: a matrix or data frame.

          y: a matrix or data frame.

        use: a character string giving the method for handling
             missing observations. This must be one of the
             stringss `"all.obs"', `"complete.obs"' or `"pair-
             wise.complete.obs"' (abbreviations are accept-
             able).

   DDeettaaiillss::

        If `use' is `"all.obs"', then the presence of missing
        observations will cause the computation to fail.  If
        `use' has the value `"complete.obs"' then missing val-
        ues are handled by casewise deletion.  Finally, if
        `use' has the value `"pairwise.complete.obs"' then the
        correlation between each pair of variables is computed
        using all complete pairs of observations on those vari-
        ables.  This can result in covariance or correlation
        matrices which are not positive semidefinite.

   SSeeee AAllssoo::

        `cov.wt' for weighted covariance computation.

   EExxaammpplleess::

        ## Two simple vectors
        cor(1:10,2:11)# == 1

        ## Correlation Matrix of Multivariate sample:
        data(longley)
        (Cl <- cor(longley))
        ## Graphical Correlation Matrix:
        symnum(Cl) # highly correlated

        ##--- Missing value treatment:
        data(swiss)
        C1 <- cov(swiss)
        range(eigen(C1, only=T)$val) # 6.19  1921
        swiss[1,2] <- swiss[7,3] <- swiss[25,5] <- NA # create 3 "missing"

         C2 <- cov(swiss) # Error: missing obs...

        C2 <- cov(swiss, use = "complete")
        range(eigen(C2, only=T)$val) # 6.46  1930
        C3 <- cov(swiss, use = "pairwise")
        range(eigen(C3, only=T)$val) # 6.19  1938

