0% found this document useful (0 votes)
6 views5 pages

Non-parametric Tests in R Guide

This document outlines the objectives and procedures for conducting non-parametric tests, specifically the Mann-Whitney-Wilcoxon test and Kruskal-Wallis test, using R. It includes detailed instructions on how to perform these tests, interpret outputs, and conduct hypothesis testing with example exercises. Additional exercises are provided to apply the concepts learned, focusing on real-world scenarios involving fuel additives, consumer preferences, and ecological studies.

Uploaded by

Gray Loren
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Non-parametric Tests in R Guide

This document outlines the objectives and procedures for conducting non-parametric tests, specifically the Mann-Whitney-Wilcoxon test and Kruskal-Wallis test, using R. It includes detailed instructions on how to perform these tests, interpret outputs, and conduct hypothesis testing with example exercises. Additional exercises are provided to apply the concepts learned, focusing on real-world scenarios involving fuel additives, consumer preferences, and ecological studies.

Uploaded by

Gray Loren
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

BES - LAB 6

Non-parametric Tests 1
1. Objectives
- Explain the Mann-Whitney-Wilcoxon test and Kruskal Wallis test procedure
- Provide instructions on how to use R to run the tests.
- Understand and interpret R outputs and make decisions based on p-value.
2. Procedure

We will use the function [Link] to perform the Mann-Whitney-Wilcoxon test to compare 2
independent samples. The format of the function is:

Ø [Link](x,y, data = name of data frame, alternative =


"[Link]", paired = FALSE, exact = NULL)

where x and y are numeric vectors of data values.

Here are meanings of other arguments:

data name of your data frame that contains the data for the test.
alternative a character string specifying the alternative hypothesis
paired a logical indicating whether you want a paired-samples test. We are considering
independent samples, so choose FALSE.

exact a logical indicating whether an exact p-value should be computed. If this


argument is not specified, an exact p-value is computed by default if the samples
contain less than 50 finite values and there are no ties. Otherwise, a normal
approximation is used.

To conduct Kruskal-Wallis test, we will use the function [Link]( ).

Ø [Link](outcome ~ grouping variable, data = name of data


frame)

Here are the meanings of these arguments:

outcome The outcome variable


Grouping variable Categorical variable that defines the groups
data Name of your dataframe that contains the variables for the test.

3. Exercises

Exercise 1. Two fuel additives are being tested to determine their effects on gasoline mileage. Ten (10)
cars were tested with additive 1 and 10 cars were tested with additive 2 (the cars are randomly assigned
to each group). The following data show the miles per gallon obtained with the two additives. Using α =
0.05, conduct a hypothesis test to see whether there is a significant difference between gasoline mileage
for the two additives. Data for this exercise are hypothetical.

Additive 1 Additive 2

1|Page
BES - LAB 6

17.3 18.7
18.4 17.8
19.1 21.3
16.7 21
18.2 22.1
18.6 18.7
17.5 19.8
15.4 20.7
16.2 20.2
15.1 26

Set up H! , H" : We want to see if significant differences exist between 2 additives:

H# : The two population distributions of gasoline mileage are the same


H" : The two population distributions are different

Check the assumptions to see whether the MWW test is appropriate.

Because the samples sizes are quite small (10), we produce a stem and leaf display for each sample. Note
that when sample sizes are small, it is difficult to determine whether the populations are normal or not.

Remember to create 2 data vectors of Additive 1 and Additive 2, called add1 and add2:

Ø add1<-c(17.3,18.4,19.1,16.7,18.2,18.6,17.5, 15.4, 16.2, 15.1)


Ø add2<-c(18.7,17.8,21.3,21,22.1,18.7,19.8,20.7,20.2, 26)

Stem and leaf display for Additive 1:

The decimal point is at the |

15 | 14
16 | 27
17 | 35
18 | 246
19 | 1

Stem and leaf display for Additive 2:

The decimal point is at the |

16 | 8
18 | 778
20 | 2703
22 | 1
24 |
26 | 0
These figures show that normality should not be assumed here. Also, the samples are independent and
random samples (why?).

2|Page
BES - LAB 6

Then if you write the [Link]() function:

Ø ex1<-[Link](add1, add2, alternative="[Link]",


paired=FALSE)

you will have the following ouput:

Wilcoxon rank sum test with continuity correction

data: add1 and add2


W = 6, p-value = 0.001004
alternative hypothesis: true location shift is not equal to 0

Question 1. What is the difference between the choice of exact = TRUE and exact = FALSE?
Question 2. Let’s calculate the test statistic by hand and compare with the value given in R output. Why
are they different?

Type in ?[Link] to see the note about test statistic given by R. It states that R produces the
same value as Mann-Whitney test statistic, which equals the sum of ranks of the first sample with the
minimum value subtracted of 𝐦(𝐦 + 𝟏)/𝟐 (m is sample size of the first one).

Then if you want to express test statistic as sum of ranks, you must add 𝒎(𝒎 + 𝟏)/𝟐 to the value given
in R output. If you want this sum of ranks to appear in the output at the end, try the following
commands:

Ø n1<-length(add1)
Ø ex1$statistic<-ex1$statistic+n1*(n1+1)/2
Ø names(ex1$statistic)<- "T.W"
Ø ex1

The new output will appear:

Wilcoxon rank sum test with continuity correction

data: add1 and add2


T.W = 61, p-value = 0.001004
alternative hypothesis: true location shift is not equal to 0

Important note: We should not use R to run Mann-Whitney-Wilcoxon test in case a sample size is
smaller than 7 and there are ties.

Exercise 2. In the consumer preference study, one group of consumers was “primed” to images that
would appear on product labels, while the other group of consumers was not “primed.” All consumers
were then asked their attitude toward a product on a seven-point scale (from 1 = dislike very much to 7
= like very much). Carry out the Mann-Whitney-Wilcoxon procedure to test if “primed” consumers are
associated with systematically higher preference scores. What do you conclude?

What type of hypothesis is this? What is the type of the data we wish to compare?

3|Page
BES - LAB 6

At first, we must import file [Link] into R:

Ø brand<-[Link]("[Link]",header=TRUE,sep = ",")
Ø brand

The second command is to check how the data are presented. You will see 2 groups of primed and not
primed customers presented by index number 0 and 1. So you must separate these 2 groups first, using
subset() function:

Ø primed<-subset(brand, Primed == 1)
Ø notprimed<-subset(brand, Primed == 0)

Note that the samples are independent and we do not need to consider normality assumption (why?).

Then, you’re expected to produce the following output:

Wilcoxon rank sum test with continuity correction

data: primed$Preference and notprimed$Preference


T.W = 600.5, p-value = 0.00042
alternative hypothesis: true location shift is greater than 0
95 percent confidence interval:
0.99994 Inf
sample estimates:
difference in location
1.00004

The code to produce the above output is:

Ø ex2 <- [Link](primed$Preference, notprimed$Preference,


alternative="greater", paired=FALSE, exact=FALSE)
Ø ex2

Question 3: Try setting exact = TRUE in the above code and see what happens.

ADDITIONAL EXERCISES

Exercise 3. “Conservationists have despaired over destruction of tropical rainforest by logging, clearing,
and burning”. These words begin a report on a statistical study of the effects of logging in [Link].
Does logging significantly reduce the number of species in a plot after 8 years?

The R output for the test is provided below. Try to demonstrate that the populations are not normal and
that the assumptions of the Mann-Whitney-Wilcoxon test are satisfied.

Wilcoxon rank sum test with continuity correction

data: logged$Species and unlogged$Species


T.W = 72, p-value = 0.029
alternative hypothesis: true location shift is less than 0

4|Page
BES - LAB 6

Exercise 4. A sample of 20 engineers employed with a company for three years has been rank ordered
with respect to managerial potential. Some of the engineers attended the company’s management-
development course, others attended an off-site management-development program at a local university,
and the remainder did not attend any program. Use the following rankings and α = 0.025 to test for a
significant difference in the managerial potential of the three groups. Remember to state the hypotheses,
and carry out other necessary steps for the test.

Firstly, you must import file [Link] as program data frame into R. Then just apply the
function [Link]. You’re expected to produce the following output:

Kruskal-Wallis rank sum test

data: RankOfManagerialPotential by Program


Kruskal-Wallis chi-squared = 12.611, df = 2, p-value = 0.001826

Exercise 5. In Exercise 3 you compared the number of tree species in plots of land in a tropical rain
forest that had never been logged (Group 1) with similar plots nearby that had been logged 8 years
earlier (Group 3). The researchers also counted species in plots that had been logged just 1 year earlier
(Group 2). Check the normal assumptions, are there features that might prevent use of ANOVA? Use
the Kruskal-Wallis test to compare the distributions of tree counts. State hypotheses, the test statistic and
its p-value, and your conclusions.

Import file [Link] as borneo3 data frame into R.

The R output for the test is provided below:

Kruskal-Wallis rank sum test

data: species by groups


Kruskal-Wallis chi-squared = 9.4382, df = 2, p-value = 0.008923

5|Page

Common questions

Powered by AI

The Mann-Whitney-Wilcoxon test is appropriate for comparing the gasoline mileage of the two additives because the sample sizes are small and independence of samples is assumed. Normality should not be assumed due to the small sample size, as indicated by the stem-and-leaf plots, which also suggest sampling independence. Therefore, this non-parametric test is preferred over a parametric one since it does not require the assumption of normal distribution .

The Wilcoxon rank sum test's reliance on ranks rather than means allows detecting changes driven by central tendency shifts, robust to non-normal distributions or unequal variances. This provides sensitive insights into ecological variations from logging, which ANOVA might overlook due to its parametric assumptions .

Setting exact = TRUE in wilcox.test() requests the computation of an exact p-value, suitable for small samples less than 50 observations, with no ties. Exact = FALSE implies using a normal approximation, appropriate for larger samples or when the data contain ties. Using the correct setting ensures the validity of the p-value interpretation, relevant for hypothesis testing .

Log transformations or rank-based tests like the Kruskal-Wallis test help by normalizing variance or by ranking data, thus eliminating sensitivity to non-normal distributions. This approach provides valid test results and inferences even when parametric conditions are breached, as evidenced by the application to species distribution .

Stem-and-leaf plots visually present data distribution shapes, revealing skewness or outliers, which inform test selection. Non-normality or asymmetrical distributions observed in the plots suggest non-parametric tests like Mann-Whitney-Wilcoxon over parametric tests, ensuring valid inferential statistics .

Normality assumptions are unimportant for the Mann-Whitney-Wilcoxon test as it is non-parametric, focusing on the ranking rather than distribution. Essential characteristics include independent samples and ordinal-scaled data, which indicate applicability without violating test requirements .

A low p-value in the Kruskal-Wallis test indicates statistically significant differences in median ranks between groups, such as managerial potential across development programs. This result guides decisions to explore further analysis or intervention, supporting selective program allocation .

In R, the wilcox.test() function adjusts the test statistic for the Mann-Whitney-Wilcoxon test by adding \( m(m+1)/2 \) to the statistic provided by R, where \( m \) is the sample size of the first sample. This adjustment is necessary because the R output provides a rank sum that needs to be expressed as the sum of ranks plus the adjustment. This process ensures consistency with the Mann-Whitney test statistic .

Manual verification requires calculating the sum of ranks for individual samples, comparing with R's output adjusted by \( m(m+1)/2 \). Discrepancies may arise from continuity corrections or computational variations. Verifying ensures methodological clarity and enhances understanding of result derivation .

The alternative hypothesis is set greater than zero because the hypothesis tests if 'primed' consumers have systematically higher preference scores, implying a directional expectation of increased preference. This directional hypothesis allows testing for a specific increase, rather than just any difference, in preference levels between groups .

You might also like