Two-Way ANOVA in R Studio
Jessie Morrill
Author
11/07/2024
Added
3
Plays
Description
Dr. Jessie Morrill shares how to conduct a Two-Way ANOVA with pairwise comparisons in R Studio.
Searchable Transcript
Toggle between list and paragraph view.
- [00:00:00.000]Okay, so in this video, I am going to be showing you how to do a two-way ANOVA in R. So to get started, the first thing we're going to do is we are going to call two packages to our library.
- [00:00:19.060]So those are going to be ggpubr as well as dplyr. So to bring these in, I'm going to run the function library and those packages together.
- [00:00:30.780]So I'm going to run this really quickly. You can see that those lines of code have been run by looking down in your console.
- [00:00:37.920]So next, for this example, I am going to be using a preloaded R data set.
- [00:00:46.560]If you are going to be doing this on your own,
- [00:00:48.920]you need to make sure that you set your working directory and you read in your CSV file before moving forward.
- [00:00:57.620]But for the sake of this example, you're able to follow along on your own computer just by using the function "data"
- [00:01:04.820]and then in parentheses and quotes, put in "tooth growth."
- [00:01:09.320]Make sure that when you do this on your own computer that you are putting a capital T and a capital G to be able to successfully read that in.
- [00:01:18.320]If it's worked in your environment in the upper right hand corner, you should see an object called "tooth growth" show up.
- [00:01:27.320]It's always best practice to check out the headings on a new data set whenever you're working with it.
- [00:01:36.320]If you look down in your console, what you can see about this data set is it has three columns.
- [00:01:42.320]The first column is going to be the response variable,
- [00:01:46.320]which is the length of the response.
- [00:01:47.720]The second column is going to be the length of a tooth in response to various treatments.
- [00:01:52.720]In the second column, what you see is the column heading SUP,
- [00:01:57.720]which represents two different supplements that have been given to patients.
- [00:02:02.720]So one of those supplements is going to be vitamin C.
- [00:02:05.720]Another supplement is going to be orange juice.
- [00:02:08.720]So in the third column, you see the dose that was given to patients in this trial.
- [00:02:17.120]So there's three doses that happen to be represented.
- [00:02:21.120]Those happen to be 0.5, 1, and 2.
- [00:02:25.120]So next item of business that's always good practice is to check the structure of a data set when you're working with it.
- [00:02:34.120]So you can do that by using the function STR, and then we're putting tooth growth in parentheses.
- [00:02:40.120]So when we do that, what we see is that the first column length happens to be a numeric number.
- [00:02:46.520]So that's a numeric variable.
- [00:02:48.520]When we look at the second column, what we see is that it's a factor with two levels, those being OJ and VC.
- [00:02:57.520]When we look at the third column, we see that this is showing up as a numeric variable.
- [00:03:04.520]And that's not what we want to be looking at for our particular example where we're doing a two-way ANOVA today.
- [00:03:11.520]So we need to correct this.
- [00:03:13.520]We need to tell R that the dose column is a factor.
- [00:03:15.920]So we can make that correction by doing tooth growth dollar sign dose to tell R we're looking at the dose column in that data set.
- [00:03:26.920]And then we're going to put in as dot factor to correct that column to be a factor column rather than a numeric variable column.
- [00:03:36.920]So then in parentheses again, we put tooth growth dollar sign dose.
- [00:03:41.920]If we run that, you can see it's been successfully run.
- [00:03:45.320]In the console next, we're going to recheck the structure of that tooth growth data set by doing STR and tooth growth.
- [00:03:54.320]When we run that now, you can see that the dose column has been changed to be a factor that has three levels found within that column.
- [00:04:04.320]Those factors happen to be zero point five, one and two.
- [00:04:10.320]So next order of business is to get an idea of what we want.
- [00:04:14.720]So we want to get an idea of what this data set looks like by plotting it.
- [00:04:18.720]So I'm going to use the ggbarplot function that's a part of the ggr package, ggpubr package.
- [00:04:25.720]And when I run that code, you can see the bar plot that results from this data set on the lower right hand corner of your screen.
- [00:04:33.720]So on the y axis, you see the length of the tooth and on the x axis, you see the dose that was given and then the different supplements that were given,
- [00:04:44.120]orange juice and vitamin C are color coded as either orange or blue respectively.
- [00:04:51.120]So you just at first glance of this data set, you can see that something is happening, something is going on.
- [00:04:58.120]I also want to point out that each of the individual data points on this bar plot represent the experimental unit in this particular experiment.
- [00:05:09.120]So next, what we are going to do is we are going to summarize
- [00:05:13.520]this data. We're going to get the exact means and the exact standard error of the means that are plotted on this bar plot,
- [00:05:22.120]and we are going to get that by using the summarize function.
- [00:05:26.020]And so we are going to group our data set in the tooth growth data set by supplement as well as dose.
- [00:05:33.120]We are going to calculate the mean length for these responses,
- [00:05:37.520]and we are going to calculate the standard error of the mean, which, remember, is going to be the
- [00:05:42.920]standard deviation of our response variable, which is length, divided by the square root of n.
- [00:05:50.320]And again, remember that n is equal to the number of experimental units per treatment group.
- [00:05:57.060]So if I run this and I'm going to set it equal to tooth growth summary to make it an object that now shows up in the upper right hand corner of my environment.
- [00:06:07.070]So you can see the tooth growth summary has six observations of four variables.
- [00:06:12.970]And if I come back to my script and I want to display the result of my summary statistics, I can do that by just putting in tooth growth summary, clicking run.
- [00:06:25.870]And what you see come up is you see a data frame that has six rows and four columns.
- [00:06:34.690]And so the first column is going to represent the different supplements.
- [00:06:38.730]The second one represents the dose.
- [00:06:40.890]And you can see the mean link that was calculated for each of those treatment groups, as well as the standard error of the mean that was calculated for them.
- [00:06:50.410]So from there, what we can do is we can.
- [00:06:55.830]And move forward to conducting our two-way ANOVA.
- [00:06:59.010]So to be able to do that, we are going to use the AOV, or analysis of variance function, that's a part of base R.
- [00:07:06.770]And in our formula, we are going to look at length in response to supplement, as well as dose.
- [00:07:15.890]And when we add this multiplication symbol into our formula,
- [00:07:22.010]this is going to allow us to also look at the.
- [00:07:25.810]interaction between supplement and dose.
- [00:07:28.310]And then finally, what we need to add to our function is the argument for data to tell R to look at our tooth growth data set.
- [00:07:37.950]So if I run this individual line of code,
- [00:07:42.610]you can see that the tooth growth analysis of variance shows up in the environment as a list of 13 items.
- [00:07:52.230]So if we want to see those items, we can put summary.
- [00:07:55.530]And then in parentheses, tooth growth analysis of variance.
- [00:07:59.310]We can run that line of code.
- [00:08:01.310]And what we end up seeing is the degrees of freedom for the main effects as well as the residuals and the interaction.
- [00:08:11.430]We can see the sums of squares.
- [00:08:13.430]We can see the mean square error, the F value, as well as the P value.
- [00:08:18.430]And so when we look at this, what we see is there happens to be a significant main effect
- [00:08:24.430]where the P value is higher.
- [00:08:25.430]And so when we look at this, what we see is there happens to be a significant main effect
- [00:08:25.430]where the P value is higher.
- [00:08:25.430]And so when we look at this, what we see is there happens to be a significant main effect
- [00:08:25.430]where the P value is higher.
- [00:08:25.430]And so when we look at this, what we see is there happens to be a significant main effect
- [00:08:25.430]And so when we look at this, what we see is there happens to be a significant main effect
- [00:08:25.430]where the P value is less than 0.05.
- [00:08:28.230]And so what we can conclude from that is that on average tooth length is going to differ significantly
- [00:08:36.230]between the two supplement types regardless of the dose.
- [00:08:40.230]So next we can look at the dose line.
- [00:08:43.230]What we see is that there's a main effect of dose with a P value less than 0.05.
- [00:08:48.230]And this suggests the tooth length is going to differ significantly across the three levels of the dose
- [00:08:55.330]regardless of the supplement type.
- [00:08:58.230]Next we can look at the P value that's associated with the interaction.
- [00:09:05.230]And we also see that this P value is less than 0.05.
- [00:09:09.230]And what this means is that the effect of supplement type on the tooth length
- [00:09:14.230]is not consistent across all the dose levels.
- [00:09:17.230]In other words, this means that the difference between the orange juice
- [00:09:21.230]and the vitamin C treatments depends on the dose that was given
- [00:09:25.230]and the impact of the dose on the tooth length is going to differ
- [00:09:30.130]depending upon the supplement type that the patient received.
- [00:09:34.130]So to move one step forward now, because we've done our two-way ANOVA
- [00:09:40.130]and we have p-values that are less than 0.05,
- [00:09:43.130]we can then look at the pairwise comparisons
- [00:09:46.130]to be able to look at each of these individually.
- [00:09:50.130]So to do our pairwise comparisons, we're going to use the function called Tukey HSD
- [00:09:55.130]and then in our parentheses, we are going to put the results
- [00:10:00.130]of our tooth growth analysis of variance.
- [00:10:03.130]If I select that line of code and I click run,
- [00:10:06.130]we have to scroll up to be able to see this full result here.
- [00:10:10.130]But we can at very first see the main effective supplement
- [00:10:17.130]and you can see what that difference is between those two treatments.
- [00:10:23.130]And then if we go ahead and we look at
- [00:10:25.030]the main effective dose, you're able to see the individual pairwise comparisons.
- [00:10:30.930]So in the first row you see dose 1 versus 0.5.
- [00:10:37.930]You can see the difference between those two as well as the p-value,
- [00:10:43.930]which happens to be less than 0.05.
- [00:10:45.930]Next you can look at the pairwise comparison
- [00:10:48.930]between a dose that was two units compared to 0.5
- [00:10:54.930]and you can see that that p-value was also less than 0.05.
- [00:11:00.830]Moving forward, you can also look at the comparison
- [00:11:03.830]between the dose level of two versus one.
- [00:11:07.830]So we're able to do all the pairwise comparisons
- [00:11:10.830]in the dose effect area of our analysis.
- [00:11:16.830]If we scroll down, what we're then able to see
- [00:11:19.830]is the pairwise comparisons between each of the six
- [00:11:24.830]treatment groups.
- [00:11:26.730]And so with those, you can also see the pairwise differences
- [00:11:31.730]in mean, and you can also see the p-values,
- [00:11:35.730]and you can see some of those are significant
- [00:11:37.730]and you can see some of those are not.
- [00:11:39.730]And so anyways, I hope this was helpful in being able to see
- [00:11:44.730]how to do a two-way ANOVA in R, and I encourage you
- [00:11:49.730]to try this with the tooth growth data set
- [00:11:51.730]as well as the data set of your own.
The screen size you are trying to search captions on is too small!
You can always jump over to MediaHub and check it out there.
Log in to post comments
Embed
Copy the following code into your page
HTML
<div style="padding-top: 56.25%; overflow: hidden; position:relative; -webkit-box-flex: 1; flex-grow: 1;"> <iframe style="bottom: 0; left: 0; position: absolute; right: 0; top: 0; border: 0; height: 100%; width: 100%;" src="https://mediahub.unl.edu/media/23493?format=iframe&autoplay=0" title="Video Player: Two-Way ANOVA in R Studio" allowfullscreen ></iframe> </div>
Comments
0 Comments