NAG Library Function Document nag_partial_corr (g02byc)

Similar documents
NAG Library Function Document nag_corr_cov (g02bxc)

NAG Library Function Document nag_mv_distance_mat (g03eac)

NAG Library Function Document nag_mv_discrim (g03dac)

NAG Library Function Document nag_kruskal_wallis_test (g08afc)

NAG Library Function Document nag_mv_ordinal_multidimscale (g03fcc)

NAG Library Function Document nag_dtrmv (f16pfc)

NAG Library Function Document nag_dummy_vars (g04eac)

NAG Library Function Document nag_corr_cov (g02bxc)

NAG Library Function Document nag_rngs_copula_students_t (g05rbc)

NAG Library Function Document nag_tsa_multi_part_lag_corr (g13dnc)

NAG Library Function Document nag_prob_lin_non_central_chi_sq (g01jcc)

NAG Library Function Document nag_real_cholesky_skyline_solve (f04mcc)

NAG Library Function Document nag_dtr_load (f16qgc)

NAG Library Function Document nag_all_regsn (g02eac)

NAG Library Function Document nag_mv_cluster_indicator (g03ejc)

NAG Library Function Document nag_glm_tran_model (g02gkc)

NAG Library Function Document nag_2d_spline_eval (e02dec)

NAG Library Function Document nag_rgsn_matrix_multi_normal (g05lyc)

NAG Library Function Document nag_asian_geom_price (s30sac)

NAG Library Function Document nag_dsymv (f16pcc)

NAG Library Function Document nag_dgglse (f08zac)

NAG Library Function Document nag_rngs_varma_time_series (g05pcc)

NAG Library Function Document nag_smooth_spline_fit (g10abc)

NAG Library Function Document nag_tsa_cross_spectrum_bivar (g13cec)

NAG Library Function Document nag_anova_random (g04bbc)

NAG Library Function Document nag_dgetri (f07ajc)

NAG Library Function Document nag_friedman_test (g08aec)

NAG Library Function Document nag_rand_2_way_table (g05pzc)

NAG Library Function Document nag_3d_shep_interp (e01tgc)

NAG Library Function Document nag_mv_discrim (g03dac)

NAG Library Function Document nag_dtrsyl (f08qhc)

NAG Library Function Document nag_zhpr2 (f16ssc)

NAG Library Function Document nag_2_sample_ks_test (g08cdc)

NAG Library Function Document nag_ztbcon (f07vuc)

NAG Library Function Document nag_complex_gen_lin_solve (f04cac)

NAG Library Function Document nag_superlu_diagnostic_lu (f11mmc)

NAG Library Function Document nag_real_sym_posdef_tridiag_lin_solve (f04bgc)

NAG Library Function Document nag_mv_prin_comp (g03aac)

NAG Library Function Document nag_dpbtrf (f07hdc)

NAG Library Function Document nag_dsytrf (f07mdc)

NAG Library Function Document nag_zeros_complex_poly (c02afc)

NAG Library Function Document nag_zero_nonlin_eqns_deriv_1 (c05ubc)

NAG Library Function Document nag_1d_cheb_fit_constr (e02agc)

NAG Library Function Document nag_herm_posdef_tridiag_lin_solve (f04cgc)

NAG Library Function Document nag_sum_sqs_update (g02btc)

NAG Library Function Document nag_complex_tridiag_lin_solve (f04ccc)

NAG Library Function Document nag_asian_geom_greeks (s30sbc)

NAG Library Function Document nag_mesh2d_inc (d06aac)

NAG Library Function Document nag_bsm_greeks (s30abc)

NAG Library Function Document nag_regsn_mult_linear_upd_model (g02ddc)

NAG Library Function Document nag_tsa_varma_diagnostic (g13dsc)

NAG Library Function Document nag_real_symm_sparse_eigensystem_init (f12fac)

NAG Library Function Document nag_kruskal_wallis_test (g08afc)

NAG Library Function Document nag_rand_copula_clayton (g05rhc)

NAG Library Function Document nag_regsn_mult_linear_delete_var (g02dfc)

NAG Library Function Document nag_rand_arma (g05phc)

NAG Library Function Document nag_1d_cheb_intg (e02ajc)

NAG C Library Function Document nag_binary_factor_service (g11sbc)

NAG C Library Function Document nag_kruskal_wallis_test (g08afc)

NAG Library Function Document nag_regsn_mult_linear_addrem_obs (g02dcc)

NAG Library Function Document nag_kalman_sqrt_filt_info_var (g13ecc)

NAG Library Function Document nag_real_symm_sparse_eigensystem_sol (f12fcc)

NAG Library Function Document nag_durbin_watson_stat (g02fcc)

NAG Library Function Document nag_rand_bivariate_copula_frank (g05rfc)

NAG Library Function Document nag_zero_cont_func_brent_rcomm (c05azc)

NAG Library Function Document nag_dummy_vars (g04eac)

NAG C Library Function Document nag_tsa_multi_part_lag_corr (g13dnc)

NAG Library Function Document nag_dger (f16pmc)

NAG Library Function Document nag_rand_copula_normal (g05rdc)

NAG Library Function Document nag_mv_promax (g03bdc)

NAG Library Function Document nag_dspr (f16pqc)

NAG C Library Function Document nag_rank_regsn (g08rac)

NAG Library Function Document nag_tsa_transf_filter (g13bbc)

NAG Library Function Document nag_regress_confid_interval (g02cbc)

NAG C Library Function Document nag_zhemv (f16scc)

NAG Library Function Document nag_opt_one_var_deriv (e04bbc)

NAG C Library Function Document nag_dgemv (f16pac)

NAG Library Function Document nag_prob_durbin_watson (g01epc)

NAG Library Function Document nag_robust_m_corr_user_fn (g02hlc)

NAG Library Function Document nag_full_step_regsn (g02efc)

NAG C Library Function Document nag_tsa_multi_cross_corr (g13dmc)

NAG C Library Function Document nag_zgemv (f16sac)

NAG Library Function Document nag_smooth_spline_fit (g10abc)

NAG Library Function Document nag_robust_m_corr_user_fn_no_derr (g02hmc)

NAG Library Function Document nag_binomial_ci (g07aac)

NAG C Library Function Document nag_zger (f16smc)

NAG Library Function Document nag_tsa_noise_spectrum_bivar (g13cgc)

NAG C Library Function Document nag_rngs_varma_time_series (g05pcc)

NAG Library Function Document nag_tsa_auto_corr_part (g13acc)

NAG Library Function Document nag_dtpttr (f01vcc)

NAG C Library Function Document nag_anova_row_col (g04bcc)

NAG C Library Function Document nag_linf_fit (e02gcc)

NAG Library Function Document nag_dtpsv (f16plc)

NAG C Library Function Document nag_surviv_risk_sets (g12zac)

NAG Library Function Document nag_2d_spline_eval (e02dec)

NAG Library Function Document nag_zsymv (f16tac)

NAG C Library Function Document nag_tsa_multi_auto_corr_part (g13dbc)

NAG Library Function Document nag_5d_shep_eval (e01tnc)

NAG Library Function Document nag_pde_parab_1d_euler_exact (d03pxc)

NAG Library Function Document nag_1d_spline_interpolant (e01bac)

Transcription:

g02 Correlation and Regression Analysis g02byc 1 Purpose NAG Library Function Document nag_partial_corr (g02byc) nag_partial_corr (g02byc) computes a partial correlation/variance-covariance matrix from a correlation or variance-covariance matrix computed by nag_corr_cov (g02bxc). 2 Specification #include <nag.h> #include <nagg02.h> void nag_partial_corr (Integer m, Integer ny, Integer nx, const Integer sz[], const double r[], Integer tdr, double p[], Integer tdp, NagError *fail) 3 Description Partial correlation can be used to explore the association between pairs of random variables in the presence of other variables. For three variables, y 1, y 2 and x 3 the partial correlation coefficient between y 1 and y 2 given x 3 is computed as: q r 12 r 13 r ffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffi 23, 1 r 2 13 1 r 2 23 where r ij is the product-moment correlation coefficient between variables with subscripts i and j. The partial correlation coefficient is a measure of the linear association between y 1 and y 2 having eliminated the effect due to both y 1 and y 2 being linearly associated with x 3. That is, it is a measure of association between y 1 and y 2 conditional upon fixed values of x 3. Like the full correlation coefficients the partial correlation coefficient takes a value in the range ð 1; 1Þ with the value 0 indicating no association. In general, let a set of variables be partitioned into two groups Y and X with n y variables in Y and n x variables in X and let the variance-covariance matrix of all n y þ n x variables be partitioned into, xx yx xy yy The variance-covariance of Y conditional on fixed values of the X variables is given by: yjx ¼ yy yx 1 xx xy The partial correlation matrix is then computed by standardizing yjx, 1 diag 2 1 yjx yjx diag 2 yjx. To test the hypothesis that a partial correlation is zero under the assumption that the data has an approximately Normal distribution a test similar to the test for the full correlation coefficient can be used. If r is the computed partial correlation coefficient then the appropriate t statistic is rffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffi n n r x 2 1 r 2 which has approximately a Student s t-distribution with n n x 2 degrees of freedom, where n is the number of observations from which the full correlation coefficients were computed. 4 References Krzanowski W J (1990) Principles of Multivariate Analysis Oxford University Press Morrison D F (1967) Multivariate Statistical Methods McGraw Hill g02byc.1

7: p½ny tdpš double Output Note: the ði; jþth element of the matrix P is stored in p½ði 1Þtdp þ j 1Š. On exit: the strict upper triangle of p contains the strict upper triangular part of the n y by n y partial correlation matrix. The lower triangle contains the lower triangle of the n y by n y partial varianceg02byc NAG Library Manual Osborn J F (1979) Statistical Exercises in Medical Research Blackwell Snedecor G W and Cochran W G (1967) Statistical Methods Iowa State University Press 5 Arguments 1: m Integer Input On entry: the number of variables in the variance-covariance/correlation matrix given in r. Constraint: m 3. 2: ny Integer Input On entry: the number of Y variables, n y, for which partial correlation coefficients are to be computed. Constraint: ny 2. 3: nx Integer Input On entry: the number of X variables, n x, which are to be considered as fixed. Constraints: nx 1; ny þ nx m. 4: sz½mš const Integer Input On entry: indicates which variables belong to set X and Y. szðþ< i 0 The ith variable is a Y variable, for i ¼ 1; 2;...; m. szðþ> i 0 The ith variable is a X variable. szðþ¼0 i The ith variable is not included in the computations. Constraints: exactly ny elements of sz must be < 0, exactly nx elements of sz must be > 0. 5: r½m tdrš const double Input Note: the ði; jþth element of the matrix R is stored in r½ði 1Þtdr þ j 1Š. On entry: the variance-covariance or correlation matrix for the m variables as given by nag_corr_cov (g02bxc). Only the upper triangle need be given. Note: the matrix must be a full rank variance-covariance or correlation matrix and so be positivedefinite. This condition is not directly checked by the function. 6: tdr Integer Input On entry: the stride separating matrix row elements in r. Constraint: tdr m g02byc.2

g02 Correlation and Regression Analysis g02byc covariance matrix if the matrix given in r is a variance-covariance matrix. If the matrix given in r is a correlation matrix then the variance-covariance matrix is for standardized variables. 8: tdp Integer Input On entry: the stride separating matrix row elements in p. Constraint: tdp ny 9: fail NagError * Input/Output The NAG error argument (see Section 3.6 in the Essential Introduction). 6 Error Indicators and Warnings NE_2_INT_ARG_LT On entry, tdp ¼ hvaluei while ny ¼ hvaluei. These arguments must satisfy tdp ny. On entry, tdr ¼ hvaluei while m ¼ hvaluei. These arguments must satisfy tdr m. NE_3_INT_ARG_CONS On entry, ny ¼ hvaluei, nx ¼ hvaluei and m ¼ hvaluei. These arguments must satisfy ny þ nx m. NE_ALLOC_FAIL Dynamic memory allocation failed. NE_BAD_NX_SET On entry, nx ¼ hvaluei and there are not exactly nx values of sz < 0. NE_BAD_NY_SET On entry, ny ¼ hvaluei and there are not exactly ny values of sz < 0. Number of values of sz < 0 ¼ hvaluei. NE_COR_MAT_POSDEF Either a diagonal element of the partial variance-covariance matrix is zero and/or a computed partial correlation coefficient is greater than one. Both indicate that the matrix input in r was not positivedefinite. NE_COR_MAT_RANK On entry, either the variance-covariance matrix or the correlation matrix is not of full rank. removing some of the nx variables by setting the appropriate elements of sz to zero. Try NE_INT_ARG_LT On entry, m ¼ hvaluei. Constraint: m 3. On entry, nx ¼ hvaluei. Constraint: nx 1. On entry, ny ¼ hvaluei. Constraint: ny 2. NE_INTERNAL_ERROR An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance. g02byc.3

g02byc NAG Library Manual 7 Accuracy nag_partial_corr (g02byc) computes the partial variance-covariance matrix, yjx, by computing the Cholesky factorization of xx. If xx is not of full rank the computation will fail. 8 Further Comments Models that represent the linear associations given by partial correlations can be fitted using the multiple regression function nag_regsn_mult_linear (g02dac). 9 Example Data, given by Osborn (1979), on the number of deaths, smoke mg=m 3 and sulphur dioxide (parts/ million) during an intense period of fog is input. The correlations are computed using nag_corr_cov (g02bxc) and the partial correlation between deaths and smoke given sulphur dioxide is computed using nag_partial_corr (g02byc). 9.1 Program Text /* nag_partial_corr (g02byc) Example Program. * * Copyright 2000 Numerical Algorithms Group. * * Mark 6, 2000. */ #include <stdio.h> #include <nag.h> #include <nagx04.h> #include <nag_stdlib.h> #include <nagg02.h> #define X(I, J) x[((i) -1)*m + ((J) -1)] #define R(I, J) r[((i) -1)*m + ((J) -1)] int main(int argc, char *argv[]) FILE *fpin, *fpout; Integer exit_status = 0, j, k, m, n, nx, ny, *sz = 0; NagError fail; double *r = 0, *std = 0, sw, *v, *x = 0, *xbar = 0; INIT_FAIL(fail); /* Check for command-line IO options */ fpin = nag_example_file_io(argc, argv, "-data", NULL); fpout = nag_example_file_io(argc, argv, "-results", NULL); fprintf(fpout, "nag_partial_corr (g02byc) Example Program Results\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n]"); fscanf(fpin, "%ld %ld", &n, &m); if (!(r = NAG_ALLOC(m*m, double))!(std = NAG_ALLOC(m, double))!(v = NAG_ALLOC(m*m, double))!(x = NAG_ALLOC(n*m, double))!(xbar = NAG_ALLOC(m, double))!(sz = NAG_ALLOC(m, Integer))) fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; for (j = 1; j <= n; ++j) for (k = 1; k <= m; ++k) g02byc.4

g02 Correlation and Regression Analysis g02byc fscanf(fpin, "%lf", &X(j, k)); /* Calculate correlation matrix */ /* nag_corr_cov (g02bxc). * Product-moment correlation, unweighted/weighted * correlation and covariance matrix, allows variables to be * disregarded */ nag_corr_cov(n, m, x, m, 0, 0, &sw, xbar, std, r, m, v, m, &fail); if (fail.code == NE_NOERROR) /* Print the correlation matrix */ fprintf(fpout, "\ncorrelation Matrix\n\n"); for (j = 1; j <= m; j++) for (k = 1; k <= m; k++) if (j > k) fprintf(fpout, "%11s", ""); else fprintf(fpout, "%7.4f%4s", R(j, k), ""); fprintf(fpout, "\n"); fscanf(fpin, "%ld %ld", &ny, &nx); for (j = 1; j <= m; ++j) fscanf(fpin, "%ld", &sz[j - 1]); /* Calculate partial correlation matrix */ /* nag_partial_corr (g02byc). * Computes partial correlation/variance-covariance matrix * from correlation/variance-covariance matrix computed by * nag_corr_cov (g02bxc) */ nag_partial_corr(m, ny, nx, sz, v, m, r, m, &fail); if (fail.code!= NE_NOERROR) fprintf(fpout, "Error from nag_partial_corr (g02byc).\n%s\n", fail.message); exit_status = 1; goto END; /* Print partial correlation matrix */ fprintf(fpout, "\n"); fprintf(fpout, "\npartial Correlation Matrix\n\n"); for (j = 1; j <= ny; j++) for (k = 1; k <= ny; k++) if (j > k) fprintf(fpout, "%11s", ""); else if (j == k) fprintf(fpout, "%7.4f%4s", 1.0, ""); else fprintf(fpout, "%7.4f%4s", R(j, k), ""); fprintf(fpout, "\n"); else fprintf(fpout, "Error from nag_corr_cov (g02bxc).\n%s\n", fail.message); exit_status = 1; goto END; END: if (fpin!= stdin) fclose(fpin); if (fpout!= stdout) fclose(fpout); if (r) NAG_FREE(r); if (std) NAG_FREE(std); if (v) NAG_FREE(v); g02byc.5

g02byc NAG Library Manual if (x) NAG_FREE(x); if (xbar) NAG_FREE(xbar); if (sz) NAG_FREE(sz); return exit_status; 9.2 Program Data nag_partial_corr (g02byc) Example Program Data 15 3 112 0.30 0.09 140 0.49 0.16 143 0.61 0.22 120 0.49 0.14 196 2.64 0.75 294 3.45 0.86 513 4.46 1.34 518 4.46 1.34 430 1.22 0.47 274 1.22 0.47 255 0.32 0.22 236 0.29 0.23 256 0.50 0.26 222 0.32 0.16 213 0.32 0.16 2 1-1 -1 1 9.3 Program Results nag_partial_corr (g02byc) Example Program Results Correlation Matrix 1.0000 0.7560 0.8309 1.0000 0.9876 1.0000 Partial Correlation Matrix 1.0000-0.7381 1.0000 g02byc.6 (last)