NAG Library Function Document nag_mv_discrim (g03dac)

Similar documents
NAG Library Function Document nag_mv_discrim (g03dac)

NAG Library Function Document nag_mv_prin_comp (g03aac)

NAG Library Function Document nag_friedman_test (g08aec)

NAG Library Function Document nag_regsn_mult_linear_upd_model (g02ddc)

NAG Library Function Document nag_regsn_mult_linear_delete_var (g02dfc)

NAG Library Function Document nag_corr_cov (g02bxc)

NAG Library Function Document nag_dummy_vars (g04eac)

NAG Library Function Document nag_regsn_mult_linear_addrem_obs (g02dcc)

NAG Library Function Document nag_kruskal_wallis_test (g08afc)

NAG Library Function Document nag_mv_dendrogram (g03ehc)

NAG Library Function Document nag_mv_distance_mat (g03eac)

NAG Library Function Document nag_smooth_spline_fit (g10abc)

NAG Library Function Document nag_full_step_regsn (g02efc)

NAG Library Function Document nag_chi_sq_2_way_table (g11aac)

NAG Library Function Document nag_corr_cov (g02bxc)

NAG Library Function Document nag_partial_corr (g02byc)

NAG Library Function Document nag_dtpttr (f01vcc)

NAG Library Function Document nag_durbin_watson_stat (g02fcc)

NAG Library Function Document nag_frequency_table (g01aec)

NAG Library Function Document nag_dspr (f16pqc)

NAG Library Function Document nag_sum_sqs_update (g02btc)

NAG Library Function Document nag_kalman_sqrt_filt_info_var (g13ecc)

NAG Library Function Document nag_dtrttf (f01vec)

NAG Library Function Document nag_tsa_noise_spectrum_bivar (g13cgc)

NAG Library Function Document nag_dger (f16pmc)

NAG Library Function Document nag_dtfttp (f01vlc)

NAG Library Function Document nag_robust_m_corr_user_fn (g02hlc)

NAG Library Function Document nag_mv_promax (g03bdc)

NAG Library Function Document nag_robust_m_corr_user_fn_no_derr (g02hmc)

NAG Library Function Document nag_dtpsv (f16plc)

NAG Library Function Document nag_mv_ordinal_multidimscale (g03fcc)

NAG Library Function Document nag_tsa_dickey_fuller_unit (g13awc)

NAG Library Function Document nag_dgelsd (f08kcc)

NAG Library Function Document nag_summary_stats_onevar_combine (g01auc)

NAG Library Function Document nag_rngs_copula_students_t (g05rbc)

NAG Library Function Document nag_dummy_vars (g04eac)

NAG Library Function Document nag_regress_confid_interval (g02cbc)

NAG Library Function Document nag_2_sample_ks_test (g08cdc)

NAG Library Function Document nag_stable_sort (m01ctc)

NAG C Library Function Document nag_binary_factor_service (g11sbc)

NAG Library Function Document nag_bsm_price (s30aac)

NAG Library Function Document nag_outlier_peirce (g07gac)

NAG Library Function Document nag_zgelsy (f08bnc)

NAG Library Function Document nag_real_gen_matrix_exp (f01ecc)

NAG Library Function Document nag_all_regsn (g02eac)

NAG Library Function Document nag_lars_param (g02mcc)

NAG Library Function Document nag_tsa_auto_corr_part (g13acc)

NAG Library Function Document nag_dgetrf (f07adc)

NAG Library Function Document nag_rgsn_matrix_multi_normal (g05lyc)

NAG Library Function Document nag_search_vector (m01fsc)

NAG Library Function Document nag_prob_non_central_f_dist (g01gdc)

NAG Library Function Document nag_zgelss (f08knc)

NAG Library Function Document nag_dgglse (f08zac)

NAG Library Function Document nag_dstev (f08jac)

NAG Library Function Document nag_ztfttp (f01vmc)

NAG Library Function Document nag_mv_cluster_indicator (g03ejc)

NAG Library Function Document nag_glm_tran_model (g02gkc)

NAG Library Function Document nag_ztrttf (f01vfc)

NAG Library Function Document nag_zsymv (f16tac)

NAG Library Function Document nag_dspsv (f07pac)

NAG Library Function Document nag_dgttrs (f07cec)

NAG Library Function Document nag_1d_spline_evaluate (e02bbc)

NAG Library Function Document nag_5d_shep_eval (e01tnc)

NAG Library Function Document nag_zhpev (f08gnc)

NAG Library Function Document nag_2d_spline_eval (e02dec)

NAG Library Function Document nag_real_cholesky_skyline_solve (f04mcc)

NAG Library Function Document nag_dsf_norm (f16rkc)

NAG C Library Function Document nag_rank_regsn (g08rac)

NAG Library Function Document nag_rand_copula_normal (g05rdc)

NAG Library Function Document nag_tsa_cp_binary (g13ndc)

NAG Library Function Document nag_dwt (c09cac)

NAG Library Function Document nag_ode_bvp_fd_lin_gen (d02gbc)

NAG Library Function Document nag_zhpr2 (f16ssc)

NAG Library Function Document nag_zpbequ (f07htc)

NAG Library Function Document nag_2d_panel_sort (e02zac)

nag mv kmeans cluster analysis (g03efc)

NAG Library Function Document nag_4d_shep_eval (e01tlc)

NAG Library Function Document nag_1d_spline_fit_knots (e02bac)

NAG Library Function Document nag_legendre_p (s22aac)

NAG Library Function Document nag_zhfrk (f16zqc)

NAG Library Function Document nag_prob_durbin_watson (g01epc)

NAG Library Function Document nag_wfilt (c09aac)

NAG Library Function Document nag_mldwt_2d (c09ecc)

NAG C Library Function Document nag_tsa_multi_auto_corr_part (g13dbc)

NAG Library Function Document nag_matop_real_gen_matrix_fun_num (f01elc)

NAG Library Function Document nag_matop_complex_gen_matrix_fun_num (f01flc)

NAG Library Function Document nag_nd_shep_eval (e01znc)

NAG Library Function Document nag_anova_random (g04bbc)

NAG Library Function Document nag_zaxpby (f16gcc)

NAG Library Function Document nag_dgbmv (f16pbc)

NAG Library Function Document nag_2d_spline_eval_rect (e02dfc)

NAG C Library Function Document nag_surviv_risk_sets (g12zac)

NAG Library Function Document nag_1d_spline_deriv (e02bcc)

NAG Library Function Document nag_superlu_lu_factorize (f11mec)

NAG Library Function Document. nag_matop_real_gen_matrix_cond_num

NAG Library Function Document nag_tsa_spectrum_univar_cov (g13cac)

NAG Library Function Document nag_kruskal_wallis_test (g08afc)

NAG Library Function Document nag_dgb_norm (f16rbc)

NAG Library Function Document nag_opt_check_deriv (e04hcc)

NAG Library Function Document nag_dtrmv (f16pfc)

Transcription:

g03 Multivariate Methods g03dac NAG Library Function Document nag_mv_discrim (g03dac) 1 Purpose nag_mv_discrim (g03dac) computes a test statistic for the equality of within-group covariance matrices and also computes matrices for use in discriminant analysis. 2 Specification #include <nag.h> #include <nagg03.h> void nag_mv_discrim (Integer n, Integer m, const double x[], Integer tdx, const Integer isx[], Integer nvar, const Integer ing[], Integer ng, const double wt[], Integer nig[], double gmean[], Integer tdg, double det[], double gc[], double *stat, double *df, double *sig, NagError *fail) 3 Description Let a sample of n observations on p variables come from n g groups with n j observations in the jth group and P n j ¼ n. If the data is assumed to follow a multivariate Normal distribution with the variance-covariance matrix of the jth group j, then to test for equality of the variance-covariance matrices between groups, that is, 1 ¼ 2 ¼¼ ng ¼, the following likelihood-ratio test statistic, G, can be used; ( ) X n g G ¼ C n n g log jsj n j 1 log Sj ; j¼1 where! C ¼ 1 2p2 þ 3p 1 Xn g 1 1 ; 6ðp þ 1Þ n g 1 n j 1 n n g j¼1 and S j are the within-group variance-covariance matrices and S is the pooled variance-covariance matrix given by X n g j¼1 n j 1 Sj S ¼ : n n g For large n, G is approximately distributed as a 2 variable with 1 2 ppþ ð 1Þ n g 1 degrees of freedom, see Morrison (1967) for further comments. If weights are used, then S and S j are the weighted pooled and within-group variance-covariance matrices and n is the effective number of observations, that is, the sum of the weights. Instead of calculating the within-group variance-covariance matrices and then computing their determinants in order to calculate the test statistic, nag_mv_discrim (g03dac) uses a QR decomposition. The group means are subtracted from the data and then for each group, a QR decomposition is computed to give an upper triangular matrix R j. This matrix can be scaled to give a matrix R j such that S j ¼ R T j R j. The pooled R matrix is then computed from the R j matrices. The values of jsj and the S j can then be calculated from the diagonal elements of R and the R j. This approach means that the Mahalanobis squared distances for a vector observation x can be computed as z T z,wherer j z ¼ x x j, xj being the vector of means of the jth group. These distances can be Mark 25 g03dac.1

g03dac NAG Library Manual calculated by nag_mv_discrim_mahaldist (g03dbc). The distances are used in discriminant analysis and nag_mv_discrim_group (g03dcc) uses the results of nag_mv_discrim (g03dac) to perform several different types of discriminant analysis. The differences between the discriminant methods are, in part, due to whether or not the within-group variance-covariance matrices are equal. 4 References Aitchison J and Dunsmore I R (1975) Statistical Prediction Analysis Cambridge Kendall M G and Stuart A (1976) The Advanced Theory of Statistics (Volume 3) (3rd Edition) Griffin Krzanowski W J (1990) Principles of Multivariate Analysis Oxford University Press Morrison D F (1967) Multivariate Statistical Methods McGraw Hill 5 Arguments 1: n Integer Input On entry: the number of observations, n. Constraint: n 1. 2: m Integer Input On entry: the number of variables in the data array x. Constraint: m nvar. 3: x½n tdxš const double Input On entry: x½ðk 1Þtdx þ l 1Š must contain the kth observation for the lth variable, for k ¼ 1; 2;...;n and l ¼ 1; 2;...; m. 4: tdx Integer Input On entry: the stride separating matrix column elements in the array x. Constraint: tdx m. 5: isx½mš const Integer Input On entry: isx½l 1Š indicates whether or not the lth variable in x is to be included in the variancecovariance matrices. If isx½l 1Š > 0thelth variable is included, for l ¼ 1; 2;...; m; otherwise it is not referenced. Constraint: isx½l 1Š > 0fornvar values of l. 6: nvar Integer Input On entry: the number of variables in the variance-covariance matrices, p. Constraint: nvar 1. 7: ing½nš const Integer Input On entry: ing½k 1Š indicates to which group the kth observation belongs, for k ¼ 1; 2;...;n. Constraint: 1 ing½k 1Š ng, fork ¼ 1; 2;...;n The values of ing must be such that each group has at least nvar members g03dac.2 Mark 25

g03 Multivariate Methods g03dac 8: ng Integer Input On entry: the number of groups, n g. Constraint: ng 2. 9: wt½nš const double Input On entry: the elements of wt must contain the weights to be used in the analysis and the effective number of observations for a group is the sum of the weights of the observations in that group. If wt½k 1Š ¼0:0 then the kth observation is excluded from the calculations. If weights are not provided then wt must be set to NULL and the effective number of observations for a group is the number of observations in that group. Constraints: if wt is not NULL, wt½k 1Š 0:0, for k ¼ 1; 2;...;n; the effective number of observations for each group must be greater than 1. 10: nig½ngš Integer Output On exit: nig½j 1Š contains the number of observations in the jth group, for j ¼ 1; 2;...;n g. 11: gmean½ng tdgš double Output Note: theði; jþth element of the matrix is stored in gmean½ði 1Þtdg þ j 1Š. On exit: thejth row of gmean contains the means of the p selected variables for the jth group, for j ¼ 1; 2;...;n g. 12: tdg Integer Input On entry: the stride separating matrix column elements in the array gmean. Constraint: tdg nvar. 13: det½ngš double Output On exit: the logarithm of the determinants of the within-group variance-covariance matrices. 14: gc½dimš double Output Note: the dimension, dim, ofthearraygc must be at least ðng þ 1Þnvar ðnvar þ 1Þ=2. On exit: thefirstppþ ð 1Þ=2 elementsofgc contain R and the remaining n g blocks of ppþ ð 1Þ=2 elements contain the R j matrices. All are stored in packed form by columns. 15: stat double * Output On exit: the likelihood-ratio test static, G. 16: df double * Output On exit: the degrees of freedom for the distribution of G. 17: sig double * Output On exit: the significance level for G. 18: fail NagError* Input/Output The NAG error argument (see Section 3.6 in the Essential Introduction). Mark 25 g03dac.3

g03dac NAG Library Manual 6 Error Indicators and Warnings NE_2_INT_ARG_LT On entry, m ¼ hvaluei while nvar ¼ hvaluei. These arguments must satisfy m nvar. On entry, tdg ¼ hvaluei while nvar ¼ hvaluei. These arguments must satisfy tdg nvar. On entry, tdx ¼ hvaluei while m ¼ hvaluei. These arguments must satisfy tdx m. NE_ALLOC_FAIL Dynamic memory allocation failed. NE_GROUP_OBSERV On entry, group hvaluei has hvaluei effective observations. Constraint: in each group the effective number of observations must be 1. NE_GROUP_VAR On entry, group hvaluei has hvaluei members, while nvar ¼ hvaluei. Constraint: number of members in each group nvar. NE_GROUP_VAR_RANK The variables in group NE_INT_ARG_LT On entry, n ¼ hvaluei. Constraint: n 1. On entry, ng ¼ hvaluei. Constraint: ng 2. On entry, nvar ¼ hvaluei. Constraint: nvar 1. hvaluei are not of full rank. NE_INTARR_INT On entry, ing½hvalueiš ¼ hvaluei, ng ¼ hvaluei. Constraint: 1 ing½i 1Š ng, fori ¼ 1; 2;...;n. 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. NE_NEG_WEIGHT_ELEMENT On entry, wt½hvalueiš ¼ hvaluei. Constraint: when referenced, all elements of wt must be non-negative. NE_VAR_INCL_INDICATED The number of variables, nvar in the analysis ¼ hvaluei, while number of variables included in the analysis via array isx ¼ hvaluei. Constraint: these two numbers must be the same. NE_VAR_RANK The variables are not of full rank. 7 Accuracy The accuracy is dependent on the accuracy of the computation of the QR decomposition. g03dac.4 Mark 25

g03 Multivariate Methods g03dac 8 Parallelism and Performance Not applicable. 9 Further Comments The time will be approximately proportional to np 2. 10 Example The data, taken from Aitchison and Dunsmore (1975), is concerned with the diagnosis of three types of Cushing s syndrome. The variables are the logarithms of the urinary excretion rates (mg/24hr) of two steroid metabolites. Observations for a total of 21 patients are input and the statistics computed by nag_mv_discrim (g03dac). The printed results show that there is evidence that the within-group variance-covariance matrices are not equal. 10.1 Program Text /* nag_mv_discrim (g03dac) Example Program. * * Copyright 2014 Numerical Algorithms Group. * * Mark 5, 1998. * Mark 8 revised, 2004. * */ #include <nag.h> #include <stdio.h> #include <nag_stdlib.h> #include <nagg03.h> #define GMEAN(I, J) gmean[(i) *tdgmean + J] #define X(I, J) x[(i) *tdx + J] int main(void) Integer exit_status = 0, i, *ing = 0, *isx = 0, j, m, n, ng, *nig = 0, nvar, tdgmean; Integer tdx; NagError fail; char weight[2]; double *det = 0, df, *gc = 0, *gmean = 0, sig, stat, *wt = 0, *wtptr = 0; double *x = 0; INIT_FAIL(fail); printf("nag_mv_discrim (g03dac) Example Program Results\n\n"); /* Skip headings in data file */ scanf_s("%*[^\n]"); scanf("%*[^\n]"); scanf_s("%"nag_ifmt"", &n); scanf("%"nag_ifmt"", &n); scanf_s("%"nag_ifmt"", &m); scanf("%"nag_ifmt"", &m); scanf_s("%"nag_ifmt"", &nvar); Mark 25 g03dac.5

g03dac NAG Library Manual scanf("%"nag_ifmt"", &nvar); scanf_s("%"nag_ifmt"", &ng); scanf("%"nag_ifmt"", &ng); scanf_s("%1s", weight, _countof(weight)); scanf("%1s", weight); if (n >= 1 && nvar >= 1 && m >= nvar && ng >= 2) if (!(det = NAG_ALLOC(ng, double))!(gc = NAG_ALLOC((ng+1)*nvar*(nvar+1)/2, double))!(gmean = NAG_ALLOC(ng*nvar, double))!(wt = NAG_ALLOC(n, double))!(x = NAG_ALLOC(n*m, double))!(ing = NAG_ALLOC(n, Integer))!(isx = NAG_ALLOC(m, Integer))!(nig = NAG_ALLOC(ng, Integer))) printf("allocation failure\n"); exit_status = -1; goto END; tdgmean = nvar; tdx = m; else printf("invalid n or nvar or m or ng.\n"); exit_status = 1; return exit_status; if (*weight == W ) for (i = 0; i < n; ++i) for (j = 0; j < m; ++j) scanf_s("%lf", &X(i, j)); scanf("%lf", &X(i, j)); scanf_s("%"nag_ifmt"", &ing[i]); scanf("%"nag_ifmt"", &ing[i]); #ifdef _WIN32 scanf_s("%lf", &wt[i]); scanf("%lf", &wt[i]); wtptr = wt; else for (i = 0; i < n; ++i) for (j = 0; j < m; ++j) scanf_s("%lf", &X(i, j)); #ifdef _WIN32 scanf("%lf", &X(i, j)); g03dac.6 Mark 25

g03 Multivariate Methods g03dac scanf_s("%"nag_ifmt"", &ing[i]); scanf("%"nag_ifmt"", &ing[i]); for (j = 0; j < m; ++j) scanf_s("%"nag_ifmt"", &isx[j]); scanf("%"nag_ifmt"", &isx[j]); /* nag_mv_discrim (g03dac). * Test for equality of within-group covariance matrices */ nag_mv_discrim(n, m, x, tdx, isx, nvar, ing, ng, wtptr, nig, gmean, tdgmean, det, gc, &stat, &df, &sig, &fail); if (fail.code!= NE_NOERROR) printf("error from nag_mv_discrim (g03dac).\n%s\n", fail.message); exit_status = 1; goto END; printf("\ngroup means\n\n"); for (i = 0; i < ng; ++i) for (j = 0; j < nvar; ++j) printf("%10.4f", GMEAN(i, j)); printf("\n"); printf("\nlog of determinants\n\n"); for (j = 0; j < ng; ++j) printf("%10.4f", det[j]); printf("\n\n%s%7.4f\n", "stat = ", stat); printf("%s%7.4f\n", " df = ", df); printf("%s%7.4f\n", " sig = ", sig); END: NAG_FREE(det); NAG_FREE(gc); NAG_FREE(gmean); NAG_FREE(wt); NAG_FREE(x); NAG_FREE(ing); NAG_FREE(isx); NAG_FREE(nig); return exit_status; 10.2 Program Data nag_mv_discrim (g03dac) Example Program Data 21 2 2 3 U 1.1314 2.4596 1 1.0986 0.2624 1 0.6419-2.3026 1 1.3350-3.2189 1 1.4110 0.0953 1 0.6419-0.9163 1 2.1163 0.0000 2 1.3350-1.6094 2 1.3610-0.5108 2 2.0541 0.1823 2 2.2083-0.5108 2 2.7344 1.2809 2 2.0412 0.4700 2 1.8718-0.9163 2 1.7405-0.9163 2 2.6101 0.4700 2 Mark 25 g03dac.7

g03dac NAG Library Manual 2.3224 1.8563 3 2.2192 2.0669 3 2.2618 1.1314 3 3.9853 0.9163 3 2.7600 2.0281 3 1 1 10.3 Program Results nag_mv_discrim (g03dac) Example Program Results Group means 1.0433-0.6034 2.0073-0.2060 2.7097 1.5998 LOG of determinants -0.8273-3.0460-2.2877 stat = 19.2410 df = 6.0000 sig = 0.0038 g03dac.8 (last) Mark 25