cor                   package:base                   R Documentation

_C_o_r_r_e_l_a_t_i_o_n, _V_a_r_i_a_n_c_e _a_n_d _C_o_v_a_r_i_a_n_c_e (_M_a_t_r_i_c_e_s)

_D_e_s_c_r_i_p_t_i_o_n:

     `var', `cov' and `cor' compute the variance of `x' and the
     covariance or correlation of `x' and `y' if these are vectors.  If
     `x' and `y' are matrices then the covariance (correlation) between
     the columns of `x' and the columns of `y' are computed.

_U_s_a_g_e:

     var(x, y = NULL, na.rm = FALSE, use)
     cor(x, y = NULL, use = "all.obs")
     cov(x, y = NULL, use = "all.obs")

_A_r_g_u_m_e_n_t_s:

       x: a numeric vector, matrix or data frame.

       y: `NULL' (default) or a vector, matrix or data frame with
          compatible dimensions to `x'.  The default is equivalent to
          `y = x' (but more efficient).

     use: an optional character string giving a method for computing
          covariances in the presence of missing values.  This must be
          (an abbreviation of) one of the strings `"all.obs"',
          `"complete.obs"' or `"pairwise.complete.obs"'.

_D_e_t_a_i_l_s:

     `var' is just another interface to `cov', where `na.rm' is used to
     determine the default for `use' when that is unspecified.  If
     `na.rm' is `TRUE' then the complete observations (rows) are used
     (`use = "complete"') to compute the variance.  Otherwise (`use =
     "all"'), `var' will give an error if there are missing values.

     If `use' is `"all.obs"', then the presence of missing observations
     will produce an error. If `use' is `"complete.obs"' then missing
     values 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 variables. This can result in covariance or
     correlation matrices which are not positive semidefinite.

     The denominator n - 1 is used which gives an unbiased estimator of
     the (co)variance for i.i.d. observations. These functions return
     `NA' when there is only one observation.

_S_e_e _A_l_s_o:

     `cov.wt' for weighted covariance computation.

_E_x_a_m_p_l_e_s:

     var(1:10)# 9.166667

     var(1:5,1:5)# 2.5

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

     ## var() & cov() are "really the same":
     stopifnot(var(1:5,0:4) == cov(1:5))

     ## 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

