NAG Library Function Document nag_kruskal_wallis_test (g08afc)

Similar documents
NAG Library Function Document nag_kruskal_wallis_test (g08afc)

NAG C Library Function Document nag_kruskal_wallis_test (g08afc)

NAG Library Function Document nag_prob_lin_non_central_chi_sq (g01jcc)

NAG Library Function Document nag_friedman_test (g08aec)

NAG Library Function Document nag_partial_corr (g02byc)

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_smooth_spline_fit (g10abc)

NAG Library Function Document nag_dtrmv (f16pfc)

NAG Library Function Document nag_all_regsn (g02eac)

NAG Library Function Document nag_asian_geom_price (s30sac)

NAG Library Function Document nag_mv_cluster_indicator (g03ejc)

NAG Library Function Document nag_dtr_load (f16qgc)

NAG Library Function Document nag_2d_spline_eval (e02dec)

NAG Library Function Document nag_tsa_multi_part_lag_corr (g13dnc)

NAG Library Function Document nag_mv_ordinal_multidimscale (g03fcc)

NAG Library Function Document nag_2_sample_ks_test (g08cdc)

NAG Library Function Document nag_tsa_cross_spectrum_bivar (g13cec)

NAG Library Function Document nag_dgglse (f08zac)

NAG Library Function Document nag_dsymv (f16pcc)

NAG Library Function Document nag_real_cholesky_skyline_solve (f04mcc)

NAG Library Function Document nag_dgetri (f07ajc)

NAG Library Function Document nag_dummy_vars (g04eac)

NAG Library Function Document nag_real_sym_posdef_tridiag_lin_solve (f04bgc)

NAG Library Function Document nag_superlu_diagnostic_lu (f11mmc)

NAG Library Function Document nag_1d_cheb_intg (e02ajc)

NAG Library Function Document nag_glm_tran_model (g02gkc)

NAG Library Function Document nag_rand_2_way_table (g05pzc)

NAG Library Function Document nag_herm_posdef_tridiag_lin_solve (f04cgc)

NAG Library Function Document nag_rngs_varma_time_series (g05pcc)

NAG Library Function Document nag_zhpr2 (f16ssc)

NAG Library Function Document nag_zeros_complex_poly (c02afc)

NAG Library Function Document nag_3d_shep_interp (e01tgc)

NAG Library Function Document nag_rngs_copula_students_t (g05rbc)

NAG Library Function Document nag_1d_cheb_fit_constr (e02agc)

NAG Library Function Document nag_dtrsyl (f08qhc)

NAG Library Function Document nag_complex_gen_lin_solve (f04cac)

NAG Library Function Document nag_mesh2d_inc (d06aac)

NAG Library Function Document nag_rgsn_matrix_multi_normal (g05lyc)

NAG Library Function Document nag_anova_random (g04bbc)

NAG Library Function Document nag_complex_tridiag_lin_solve (f04ccc)

NAG Library Function Document nag_bsm_greeks (s30abc)

NAG Library Function Document nag_asian_geom_greeks (s30sbc)

NAG Library Function Document nag_ztbcon (f07vuc)

NAG Library Function Document nag_zero_nonlin_eqns_deriv_1 (c05ubc)

NAG Library Function Document nag_dsytrf (f07mdc)

NAG Library Function Document nag_dpbtrf (f07hdc)

NAG Library Function Document nag_rand_copula_clayton (g05rhc)

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_rand_arma (g05phc)

NAG Library Function Document nag_durbin_watson_stat (g02fcc)

NAG Library Function Document nag_opt_one_var_deriv (e04bbc)

NAG C Library Function Document nag_binary_factor_service (g11sbc)

NAG Library Function Document nag_real_symm_sparse_eigensystem_init (f12fac)

NAG Library Function Document nag_tsa_varma_diagnostic (g13dsc)

NAG Library Function Document nag_tsa_dickey_fuller_unit (g13awc)

NAG Library Function Document nag_outlier_peirce (g07gac)

NAG Library Function Document nag_tsa_transf_filter (g13bbc)

NAG Library Function Document nag_real_symm_sparse_eigensystem_sol (f12fcc)

NAG Library Function Document nag_sum_sqs_update (g02btc)

NAG Library Function Document nag_legendre_p (s22aac)

NAG Library Function Document nag_corr_cov (g02bxc)

NAG Library Function Document nag_summary_stats_onevar_combine (g01auc)

NAG Library Function Document nag_pde_parab_1d_euler_exact (d03pxc)

NAG Library Function Document nag_prob_durbin_watson (g01epc)

NAG Library Function Document nag_zaxpby (f16gcc)

NAG Library Function Document nag_quad_md_gauss (d01fbc)

NAG Library Function Document nag_prob_non_central_f_dist (g01gdc)

NAG Library Function Document nag_2d_panel_sort (e02zac)

NAG Library Function Document nag_dger (f16pmc)

NAG C Library Function Document nag_zgemv (f16sac)

NAG Library Function Document nag_mv_prin_comp (g03aac)

NAG Library Function Document nag_mv_discrim (g03dac)

NAG Library Function Document nag_5d_shep_eval (e01tnc)

NAG Library Function Document nag_dspr (f16pqc)

NAG Library Function Document nag_regress_confid_interval (g02cbc)

NAG Library Function Document nag_sum_convcorr_real (c06fkc)

NAG Library Function Document nag_wfilt (c09aac)

NAG Library Function Document nag_binomial_ci (g07aac)

NAG Library Function Document nag_1d_quad_wt_alglog (d01apc)

NAG Library Function Document nag_smooth_spline_fit (g10abc)

NAG C Library Function Document nag_dgemv (f16pac)

NAG C Library Function Document nag_cp_stat (g02ecc)

NAG C Library Function Document nag_tsa_multi_part_lag_corr (g13dnc)

NAG C Library Function Document nag_zger (f16smc)

NAG Library Function Document nag_1d_quad_wt_alglog_1 (d01spc)

NAG Library Function Document nag_opt_simplex_easy (e04cbc)

NAG C Library Function Document nag_tsa_multi_cross_corr (g13dmc)

NAG Library Function Document nag_regsn_mult_linear_upd_model (g02ddc)

NAG Library Function Document nag_real_gen_matrix_exp (f01ecc)

NAG Library Function Document nag_dtpttr (f01vcc)

NAG Library Function Document nag_search_vector (m01fsc)

NAG C Library Function Document nag_zhpmv (f16sec)

NAG Library Function Document nag_rand_copula_normal (g05rdc)

NAG Library Function Document nag_stable_sort (m01ctc)

NAG Library Function Document nag_opt_check_deriv (e04hcc)

NAG Library Function Document nag_4d_shep_eval (e01tlc)

NAG C Library Function Document nag_zhemv (f16scc)

Transcription:

1 Purpose NAG Library Function Document nag_kruskal_wallis_test () nag_kruskal_wallis_test () performs the Kruskal Wallis one-way analysis of variance by ranks on k independent samples of possibly unequal sizes. 2 Specification #include <nag.h> #include <nagg08.h> void nag_kruskal_wallis_test (Integer k, const Integer l[], const double x[], Integer lx, double *h, double *p, NagError *fail) 3 Description The Kruskal Wallis test investigates the differences between scores from k independent samples of unequal sizes, the ith sample containing l i observations. The hypothesis under test, H 0, often called the null hypothesis, is that the samples come from the same population, and this is to be tested against the alternative hypothesis H 1 that they come from different populations. The test proceeds as follows: (a) The pooled sample of all the observations is ranked. Average ranks are assigned to tied scores. (b) The ranks of the observations in each sample are summed, to give the rank sums R i, for i ¼ 1; 2;...;k. (c) The Kruskal Wallis test statistic H is computed as: 12 H ¼ NNþ ð 1Þ X k i¼1 R 2 i 3ðN þ 1Þ, wheren ¼ Xk l l i, i i¼1 i.e., N is the total number of observations. If there are tied scores, H is corrected by dividing by: P t 3 t 1 N 3 N where t is the number of tied scores in a group and the summation is over all tied groups. nag_kruskal_wallis_test () returns the value of H, and also an approximation, p, to the probability of a value of at least H being observed, H 0 is true. (H approximately follows a 2 k 1 distribution). H 0 is rejected by a test of chosen size if p<. The approximation p is acceptable unless k ¼ 3 and l 1, l 2 or l 3 5 in which case tables should be consulted (e.g., O of Siegel (1956)) or k ¼ 2 (in which case the Median test (see nag_median_test (g08acc)) or the Mann Whitney U test (see nag_mann_whitney (g08amc)) is more appropriate). 4 References Moore P G, Shirley E A and Edwards D E (1972) Standard Statistical Calculations Pitman Siegel S (1956) Non-parametric Statistics for the Behavioral Sciences McGraw Hill.1

NAG Library Manual 5 Arguments 1: k Integer Input On entry: the number of samples, k. Constraint: k 2. 2: l½kš const Integer Input On entry: l½i 1Š must contain the number of observations l i in sample i, for i ¼ 1; 2;...;k. Constraint: l½i 1Š > 0, for i ¼ 1; 2;...;k. 3: x½lxš const double Input On entry: the elements of x must contain the observations in the k groups. The first l 1 elements must contain the scores in the first group, the next l 2 those in the second group, and so on. 4: lx Integer Input On entry: the total number of observations, N. Constraint: lx ¼ P k i¼1l½i 1Š. 5: h double * Output On exit: the value of the Kruskal Wallis test statistic, H. 6: p double * Output On exit: the approximate significance, p, of the Kruskal Wallis test statistic. 7: fail NagError * Input/Output The NAG error argument (see Section 3.6 in the Essential Introduction). 6 Error Indicators and Warnings NE_ALLOC_FAIL Dynamic memory allocation failed. NE_ARRAY_CONS The contents of array l are not valid. Constraint: l½i 1Š > 0, for i ¼ 1; 2;...;k. NE_INT On entry, lx ¼ hvaluei. Constraint: lx ¼ P k i¼1l½i 1Š, for i ¼ 1; 2;...;k. NE_INT_ARG_LT On entry, k ¼ hvaluei. Constraint: k 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. NE_X_IDEN On entry, all elements of x are equal..2

7 Accuracy For estimates of the accuracy of the significance p, see nag_prob_chi_sq (g01ecc). The 2 approximation is acceptable unless k ¼ 3 and l 1 ;l 2 or l 3 5. 8 Further Comments The time taken by nag_kruskal_wallis_test () is small, and increases with N and k. If k ¼ 2, the Median test (see nag_median_test (g08acc)) or the Mann Whitney U test (see nag_mann_whitney (g08amc)) is more appropriate. 9 Example This example is taken from Moore et al. Moore et al. (1972). There are 5 groups of sizes 5, 8, 6, 8 and 8. The data represent the weight gain, in pounds, of pigs from five different litters under the same conditions. 9.1 Program Text /* nag_kruskal_wallis_test () Example Program. * * Copyright 2000 Numerical Algorithms Group. * * Mark 6, 2000. */ #include <stdio.h> #include <nag.h> #include <nagx04.h> #include <nag_stdlib.h> #include <nagg08.h> int main(int argc, char *argv[]) FILE *fpin, *fpout; Integer count, exit_status = 0, i, ii, k, *l = 0, lx, nhi, ni, nlo; NagError fail; double h, p, *x = 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_kruskal_wallis_test () Example Program Results\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n]"); k = 5; if (!(l = NAG_ALLOC(k, Integer))) fprintf(fpout, "Allocation failure\n"); exit_status = -1; for (i = 1; i <= k; i++) fscanf(fpin, "%ld", &l[i-1]); fprintf(fpout, "%s\n", "Kruskal-Wallis test"); fprintf(fpout, "%s\n", "Data values"); fprintf(fpout, "%s\n", " Group Observations"); lx = 0; for (i = 1; i <= 5; ++i).3

NAG Library Manual lx += l[i - 1]; if (!(x = NAG_ALLOC(lx, double))) fprintf(fpout, "Allocation failure\n"); exit_status = -1; for (i = 1; i <= lx; ++i) fscanf(fpin, "%lf", &x[i - 1]); nlo = 1; for (i = 1; i <= k; ++i) ni = l[i - 1]; nhi = nlo + ni - 1; fprintf(fpout, " %5ld ", i); count = 1; for (ii = nlo; ii <= nhi; ++ii) fprintf(fpout, "%4.0f%s", x[ii - 1], count%10?"":"\n"); count++; nlo += ni; /* nag_kruskal_wallis_test (). * Kruskal-Wallis one-way analysis of variance on k samples * of unequal size */ nag_kruskal_wallis_test(k, l, x, lx, &h, &p, &fail); if (fail.code!= NE_NOERROR) fprintf(fpout, "Error from nag_kruskal_wallis_test ().\n%s\n", fail.message); exit_status = 1; fprintf(fpout, "%s%9.3f\n", "Test statistic ", h); fprintf(fpout, "%s%9ld\n", "Degrees of freedom ", k-1); fprintf(fpout, "%s%9.3f\n", "Significance ", p); END: if (fpin!= stdin) fclose(fpin); if (fpout!= stdout) fclose(fpout); if (l) NAG_FREE(l); if (x) NAG_FREE(x); return exit_status; 9.2 Program Data nag_kruskal_wallis_test () Example Program Data 5 8 6 8 8 23 27 26 19 30 29 25 33 36 32 28 30 31 38 31 28 35 33 36 30 27 28 22 33 34 34 32 31 33 31 28 30 24 29 30 9.3 Program Results nag_kruskal_wallis_test () Example Program Results Kruskal-Wallis test Data values Group Observations 1 23 27 26 19 30 2 29 25 33 36 32 28 30 31.4

3 38 31 28 35 33 36 4 30 27 28 22 33 34 34 32 5 31 33 31 28 30 24 29 30 Test statistic 10.537 Degrees of freedom 4 Significance 0.032.5 (last)