The library offers a range of helpful services. All of our appointments are free of charge and confidential.
The Chi-Square Test is used to test whether two categorical (nominal) variables are associated with each other. This test assumes that the observations are independent, and that the expected frequencies for each category should be at least 1 (Note: no more than 20% of the categories should have expected frequencies less than 5).
Note that this is a non-parametric test. There is no parametric version of a Chi-Square Test of Independence.
To run a Chi-Square test of independence in R, start by creating a contingency table from your categorical data. If your data are in separate columns within the same dataframe, use the table()
function to generate this table, summarizing the frequency distribution of the variables. Create the table: TABLE_NAME <- table(NAMEOFDATAFRAME$NAMEOFCOLUMN1, NAMEOFDATAFRAME$NAMEOFCOLUMN2)
. Next, print the contingency table to verify it looks correct using: print(TABLE_NAME)
. Then perform the Chi-Square Test of Independence using the chisq.test()
function: CHI_TEST <- chisq.test(TABLE_NAME)
. Print the test result using print(CHI_TEST)
to view the Chi-Square statistic, degrees of freedom, and p-value. Pro Tip: it is important to check both the actual and expected frequencies, as the Chi-Square Test assumes sufficient data in each cell.
Running the above steps will generate the following output: a Pearson’s Chi-squared test table that tells you whether your categorical variables are independent (p > .05) or associated (p < .05), a crosstab table showing the actual combinations between the variables you selected (e.g., indicating how many observations of each combination were present in your data), and a crosstab table showing the expected combinations between the variables you selected (e.g., indicating how many of each combination would be expected in your data).
For the Pearson’s Chi-squared test table, the “X-squared” value is the test statistic (X2), and the p-value indicates statistical significance (p < .05 is generally considered statistically significant, which would indicate that the variables are associated).
The effect size for the Chi-square test is the Cramer’s V, which is a measure of association between two categorical variables.
To calculate Cramer’s V, use the cramersv()
function from the confintr
package to compute Cramer's V based on the result of your chi-square test: cramersv(CHI_TEST)
.
Spearman’s rank-order correlation is used to determine the strength and direction of a relationship of the rankings of two variables. The variables can be ordinal or continuous. This test does not assume the variables are normally distributed. However, the relationship between the ranked values should be monotonic (i.e., an increasing OR decreasing relationship; not increasing AND decreasing).
Note that this is a non-parametric test; you could / should use a Spearman’s rank-order correlation if the normality assumption has been violated for your Pearson correlation (i.e., the parametric equivalent). You can also use this test if you wish to conduct a correlation on ordinal data (note: Pearson’s would not be appropriate here).
To run a Spearman correlation, use the cor.test()
function, passing it the two columns you wish to correlate. By default, this function will run a Pearson’s correlation, but you can specify a Spearman correlation by specifying "spearman
" in the method argument. This test requires a dataframe with at least two columns of numeric data. Use: str(NAMEOFDATAFRAME)
to verify the data type for your variables (the <num>
label indicates a numeric variable).
There are two methods of providing the two columns: individually, or as a formula.
cor.test(NAMEOFDATAFRAME$NAMEOFCOLUMN1, NAMEOFDATAFRAME$NAMEOFCOLUMN2, method = "spearman")
. The test will then pair up the entries in each column in the order they appear and run the test of correlation. This method is especially helpful when you want to correlate two sets of numbers that are not stored in the same variable.cor.test(~NAMEOFCOLUMN1 + NAMEOFCOLUMN2, data = NAMEOFDATAFRAME, method = "spearman")
. This method is particularly helpful when the data you are correlating are in separate columns in the same dataframe.Running the above steps will generate the following output:
Spearman’s rho can range from -1 (perfect negative) to +1 (perfect positive), and indicates the strength and direction of the relationship between the two variables. Here, we see a non-significant, weak positive correlation.
The Wilcoxon signed-rank test is used to determine whether the median of a single continuous variable differs from a specified constant (similar to a one-sample t-test) AND / OR whether the median of two continuous variables from the same group of participants differ (similar to a paired-samples t-test). Both versions of this test do not assume that the data are normally distributed.
Note that this is a non-parametric test; you could / should use the Wilcoxon signed-rank test if the normality assumption has been violated for your one-sample t-test or a paired-samples t-test (i.e., the parametric equivalents).
This test requires a dataframe with at least one ordinal or continuous variable column in it. Use glimpse(NAMEOFDATAFRAME)
to see the structure of your data; the <dbl>
label indicates a continuous variable, and the <fct>
label indicates a categorical variable. If your column is categorical but isn't labeled as a factor (“<fct>
”), use: NAMEOFDATAFRAME$NAMEOFCOLUMN <- as.factor(NAMEOFDATAFRAME$NAMEOFCOLUMN)
to change it.
To run a one-sample Wilcoxon signed-rank test, use the wilcox.test()
function. This function requires two pieces of information: the data to be tested, and the constant to test them against. Provide the column that contains the data, and use the mu
parameter to define the constant: wilcox.test(NAMEOFDATAFRAME$NAMEOFCOLUMN, mu = CONSTANT)
.
The code above will determine whether the median of the specified column of data statistically differs from the constant.
You can customize the test further by adding additional arguments. For instance, specify the alternative hypothesis using: alternative
= “____”, selecting one of: "two.sided"
, "greater"
, or "less"
. Setting conf.int = TRUE
and conf.level = 0.95
will include a confidence interval at the specified level.
Running the above steps will generate the following output:
For non-parametric tests like the Wilcoxon signed-rank test, a commonly used effect size is r, which represents the strength of the association between the sample and the hypothesized median. Pro Tip: the effect size r for the Wilcoxon signed-rank test is not the same as Pearson's r.
The effect size r for the Wilcoxon signed-rank can be calculated in R by:
wilcox_effsize()
function from the rstatix
packageThe wilcox_effsize()
function will also give a 95% confidence interval on the effect size. When using the wilcox_effsize()
function with a one-sample comparison, provide the dataframe and column that contains the data, use the mu
parameter to define the constant, use alternative
to specify the alternative hypothesis, and use ci
to request confidence intervals for the effect size: wilcox_effsize(NAMEOFDATAFRAME, NAMEOFCOLUMN ~ 1, mu = 0, alternative = “two.sided”, ci = TRUE)
.
To perform a paired Wilcoxon Signed-Rank Test in R (equivalent to a paired-samples t-test), you can use the wilcox.test()
function, which is included in base R (i.e., you do not need to install or load any packages). There are two ways of conducting this test in RStudio, and the most appropriate test depends on the way your data are organized.
If the dependent variable data for each condition are in separate columns or separate variables in the same dataframe (i.e., wide format):
wilcox.test(NAMEOFDATAFRAME$CONDITION1, NAMEOFDATAFRAME$CONDITION2, paired = TRUE
Note: This test requires a dataframe with at least two columns of numeric data. Use str(NAMEOFDATAFRAME)
to see the structure of your data; the label <num>
indicates a numeric variable.
Note: it is important that TRUE
is written in all capital letters for this code to run properly.
Note: The order of groups is important. If you get zero as your V statistic, change the order of groups, e.g.: wilcox.test(NAMEOFDATAFRAME$CONDITION2, NAMEOFDATAFRAME$CONDITION1, paired = TRUE)
.
Running the above steps will generate the output above:
If the dependent variable data for both conditions are in the same column, with the condition labels in another column of the same data frame (i.e., long format):
wilcox_test(NAMEOFDVCOLUMN ~ NAMEOFIVCOLUMN, data = NAMEOFDATAFRAME , paired = TRUE)
Note: This test requires a dataframe with at least one column of numeric data (your DV) and one column of categorical data (your IV). Use str(NAMEOFDATAFRAME)
to see the structure of your data; the label <num>
indicates a numeric variable, the label <fct>
indicates a categorical variable (in this case, it should shows “Factor w/ 2 levels” because the factor has two levels).
Note: If you get zero as your V statistic, change the order of the groups using: NAMEOFDATAFRAME $NAMEOFIVCOLUMN <- relevel(NAMEOFDATAFRAME$NAMEOFIVCOLUMN, ref = "NAMEOFGROUP2")
.
Running the above steps will generate the following output:
In this tibble:
To calculate the effect size, use the wilcox_effsize()
function with the same formula and paired setting:
wilcox_effsize(NAMEOFDVCOLUMN ~ NAMEOFIVCOLUMN, data = NAMEOFDATAFRAME.long, paired = TRUE)
.
Running the above steps will generate the following output:
In this tibble:
The Mann-Whitney U test is used to determine whether two groups’ medians on the same continuous variable differ (similar to an independent samples t-test). This test does not assume that the data are normally distributed, but is does assume that the distributions are the same shape.
Note that this is a non-parametric test; you could / should use the Mann-Whitney U test if the normality assumption has been violated for your independent samples t-test (i.e., the parametric equivalent).
This test requires a dataframe with one column of numeric data (your DV) and one column of categorical data (your IV). Use str(NAMEOFDATAFRAME)
to see the structure of your data; the label <num>
indicates a numeric variable, the label <fct>
indicates a categorical variable (in this case, it should shows “Factor w/ 2 levels” because the factor has two levels). Your IV variable must be categorical; if your IV column isn’t a factor (“<fct>
”), use: NAMEOFDATAFRAME$NAMEOFCOLUMN <- as.factor(NAMEOFDATAFRAME$NAMEOFCOLUMN)
to change it.
To run a Mann-Whitney U test, use the wilcox_test()
function from the rstatix
package (Reminder: the data must be in long format for this test):
wilcox_test(NAMEOFDVCOLUMN ~ NAMEOFIVCOLUMN, data = NAMEOFDATAFRAME, paired = FALSE)
Note: it is important that FALSE
is written in all capital letters for this code to run properly.
Running the above steps will generate the following output:
In this tibble:
To calculate the effect size for the Mann-Whitney U test, use the wilcox_effsize()
function with the same formula and non-paired setting:
wilcox_effsize(NAMEOFDVCOLUMN ~ NAMEOFIVCOLUMN, data = NAMEOFDATAFRAME, paired = FALSE)
Running the above steps will generate the following output:
In this tibble:
Next, we can plot the data. The best visualization for a Man-Whitney U test is a boxplot for each group. We can plot this using the boxplot()
function from base R:
boxplot(NAMEOFDVCOLUMN ~ NAMEOFIVCOLUMN, data = NAMEOFDATAFRAME)
The dark bar in the middle of the box is the median, the edges of the box indicate the interquartile range (IQR; quartile 1 to quartile 3), the whiskers indicate the minimum and maximum (Q1 - 1.5*IQR and Q3 + 1.5*IQR, respectively), and any circles outside the whiskers indicate outliers in that group / condition.
The Kruskal-Wallis H test is used to determine whether three or more groups’ medians on the same continuous variable differ (similar to a one-way ANOVA, with independent groups). This test does not assume that the data are normally distributed, but it does assume the distributions are the same shape.
Note that this is a non-parametric test; you could / should use the Kruskal-Wallis H test if the normality assumption has been violated for your one-way ANOVA with independent groups (i.e., the parametric equivalent).
This test requires a dataframe with at one column of numeric data (your DV) and one column of categorical data (your IV). Use str(NAMEOFDATAFRAME)
to see the structure of your data; the label <num>
indicates a numeric variable, the label <fct>
indicates a categorical variable (in this case, it should shows “Factor w/ 4 levels” because the factor has four levels). Your IV variable must be categorical; If your IV column isn’t a factor (“<fct>
”), use: NAMEOFDATAFRAME$NAMEOFCOLUMN <- as.factor(NAMEOFDATAFRAME$NAMEOFCOLUMN)
to change it.
To run a Kruskal-Wallis H test, use the kruskal_test()
function from the rstatix
package (Reminder: the data must be in long format for this test):
kruskal_test(NAMEOFDVCOLUMN ~ NAMEOFIVCOLUMN, data = NAMEOFDATAFRAME)
Running the above steps will generate the following output:
In this tibble:
To calculate the effect size for the Kruskal-Wallis H test, use the kruskal_effsize()
function:
kruskal_effsize(NAMEOFDVCOLUMN ~ NAMEOFIVCOLUMN, data = NAMEOFDATAFRAME)
Running the above steps will generate the following output:
Typically, when a Kruskal-Wallis H test is non-significant, you would not run post hoc tests. If, however, the Kruskal-Wallis H test returned p < .05, this indicates a difference between groups somewhere. To determine where the difference(s) lies, we must run follow-up tests (generally post hoc tests). To perform post-hoc tests, use the wilcox_test()
function to run pairwise comparisons across all levels of your independent variable:
wilcox_test(NAMEOFDVCOLUMN ~ NAMEOFIVCOLUMN, data = NAMEOFDATAFRAME, p.adjust.method = "bonferroni")
The output from the wilcox_test()
function presents the results of all combinations of Wilcoxon rank-sum tests (also called Mann-Whitney U tests).
In this tibble:
To calculate the effect sizes for pairwise comparisons following a Kruskal-Wallis test, use the wilcox_effsize()
function from the rstatix
package:
wilcox_effsize(NAMEOFDVCOLUMN ~ NAMEOFIVCOLUMN, data = NAMEOFDATAFRAME)
Running the above steps will generate the following output:
In this tibble:
Next, we can plot the data. The best visualization for a Kruskal-Wallis H test is a boxplot for each group. We can plot this using the boxplot()
function from base R:
boxplot(NAMEOFDVCOLUMN ~ NAMEOFIVCOLUMN, data = NAMEOFDATAFRAME)
The dark bar in the middle of the box is the median, the edges of the box indicate the interquartile range (IQR; quartile 1 to quartile 3), the whiskers indicate the minimum and maximum (Q1 - 1.5*IQR and Q3 + 1.5*IQR, respectively), and any circles outside the whiskers indicate outliers in that group / condition.
The Friedman test is used to determine whether one groups’ ranking on three or more continuous or ordinal variables differ (similar to a repeated measures one-way ANOVA). This test does not assume that the data are normally distributed, but it does assume the distributions are the same shape.
Note that this is a non-parametric test; you could / should use the Friedman test if the normality assumption has been violated for your repeated measures one-way ANOVA (i.e., the parametric equivalent).
This test requires a dataframe with one column of numeric data (your DV), one column of categorical data (your IV), and one column of identification (ID). The ID column is used to match observations across conditions; for example, if the data belong to participants, the ID column would label which observations belong to participant 1, participant 2, et cetera. There should be one value for each participant in each condition. Use str(NAMEOFDATAFRAME)
to see the structure of your data; the label <num>
indicates a numeric variable, the label <fct>
indicates a categorical variable (in this case, the IV should show “Factor w/ 3 levels” because the factor has three levels). Your IV and ID variables must be categorical variables; If your IV and ID column aren’t factors (“<fct>
”), use: NAMEOFDATAFRAME$NAMEOFCOLUMN <- as.factor(NAMEOFDATAFRAME$NAMEOFCOLUMN)
to change them.
To run a Friedman test, use the friedman_test()
function from the rstatix
package (REMINDER: the data must be in long format for this test):
friedman_test(NAMEOFDVCOLUMN ~ NAMEOFIVCOLUMN | ID, data = NAMEOFDATAFRAME)
Running the above steps will generate the following output:
In this tibble:
To calculate the effect size for the Friedman test, use the friedman_effsize()
function:
friedman_effsize(NAMEOFDVCOLUMN ~ NAMEOFIVCOLUMN | ID, data = NAMEOFDATAFRAME)
Running the above steps will generate the following output:
Typically, when a Friedman’s test is non-significant, you would not run post hoc tests. If, however, the Friedman’s test returned p < .05, this indicates a difference between conditions somewhere. To determine where the difference(s) lies, we must run follow-up tests (generally post hoc tests). To perform post-hoc tests, use the wilcox_test()
function to run pairwise comparisons across all levels of your independent variable:
wilcox_test(NAMEOFDVCOLUMN ~ NAMEOFIVCOLUMN, data = NAMEOFDATAFRAME, paired = TRUE, p.adjust.method = "bonferroni")
The output from the wilcox_test()
function presents the results of all combinations of Wilcoxon signed-rank tests:
In this tibble:
Next, we can plot the data. The best visualization for a Kruskal-Wallis H test is a boxplot for each group. We can plot this using the boxplot()
function from base R:
boxplot(NAMEOFDVCOLUMN ~ NAMEOFIVCOLUMN, data = NAMEOFDATAFRAME)
The dark bar in the middle of the box is the median, the edges of the box indicate the interquartile range (IQR; quartile 1 to quartile 3), the whiskers indicate the minimum and maximum (Q1 - 1.5*IQR and Q3 + 1.5*IQR, respectively), and any circles outside the whiskers indicate outliers in that group / condition.
Please note that there is no non-parametric alternative to a factorial ANOVA. If your factorial ANOVA does not meet the assumptions, you could try transforming your dependent variable data and running all assumptions again.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.