Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • best books about data analytics
    Best Books About Data Analytics Course
  • Descriptive statistics vs Inferential statistics
    Descriptive statistics vs Inferential statistics: Guide Statistics
  • How do augmented analytics work
    How do augmented analytics work? R
  • Data Science Challenges in R Programming Language
    Data Science Challenges in R Programming Language Machine Learning
  • sorting in r
    Sorting in r: sort, order & rank R Functions R
  • How to Group and Summarize Data in R
    How to Group and Summarize Data in R R
  • Replace NA with Zero in R
    Replace NA with Zero in R R
  • Augmented Dickey-Fuller Test in R
    Augmented Dickey-Fuller Test in R R
How to Create an Interaction Plot in R

How to Create an Interaction Plot in R?

Posted on July 27July 27 By Jim No Comments on How to Create an Interaction Plot in R?
Tweet
Share
Share
Pin

How to Create an Interaction Plot in R?, To find out if the means of three or more independent groups that have been divided based on two factors differ, a two-way ANOVA is performed.

When we want to determine whether two distinct factors have an impact on a certain response variable, we employ a two-way ANOVA.

The interpretation of the link between the factors and the response variable may be affected, though, when there is an interaction effect between the two factors.

For instance, we might be interested in finding out how gender and exercise affect the response variable weight loss.

While both variables may have an impact on weight loss, it’s also likely that they will work in concert.

For instance, it’s likely that men and women lose weight through exercise at varying rates. Exercise and gender have an interaction effect in this situation.

An interaction plot is the most effective tool for spotting and comprehending the effects of interactions between two variables.

This particular plot style shows the values of the first factor on the x-axis and the fitted values of the response variable on the y-axis.

The values of the second component of interest are depicted by the lines in the plot.

An interaction plot in R can be made and read using the instructions in this tutorial.

Example: How to Create an Interaction Plot in R

Let’s say researchers want to know if gender and activity volume affect weight loss.

They enlist 30 men and 30 women to take part in an experiment where 10 of each gender are randomly assigned to follow a program of either no activity, light exercise, or severe exercise for one month in order to test this.

To see the interaction impact between exercise and gender, use the following procedures to generate a data frame in R, run a two-way ANOVA, and create an interactive graphic.

Step 1: Create the data.

The code below demonstrates how to make a data frame in R:

Make this illustration repeatable.

set.seed(123)

Now we can create a data frame

data <- data.frame(gender = rep(c("Male", "Female"), each = 30),
                   exercise = rep(c("None", "Light", "Intense"), each = 10, times = 2),
                   weight_loss = c(runif(10, -3, 3), runif(10, 0, 5), runif(10, 5, 9),
                                   runif(10, -4, 2), runif(10, 0, 3), runif(10, 3, 8)))

Let’s view the first six rows of the data frame

head(data)
  gender exercise weight_loss
1   Male     None  -1.2745349
2   Male     None   1.7298308
3   Male     None  -0.5461385
4   Male     None   2.2981044
5   Male     None   2.6428037
6   Male     None  -2.7266610

Step 2: Fit the two-way ANOVA model.

How to apply a two-way ANOVA to the data is demonstrated in the code below:

two-way ANOVA model fit

model <- aov(weight_loss ~ gender * exercise, data = data)

Now view the model summary

summary(model)
Df Sum Sq Mean Sq F value   Pr(>F)   
gender           1   43.7   43.72  19.032 5.83e-05 ***
exercise         2  438.9  219.43  95.515  < 2e-16 ***
gender:exercise  2    2.9    1.46   0.634    0.535   
Residuals       54  124.1    2.30                    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Note that the p-value (0.535) for the interaction term between exercise and gender is not statistically significant, which indicates that there is no significant interaction effect between the two factors.

Step 3: Create the interaction plot.

The code below demonstrates how to make an interaction plot for gender and exercise.

interaction.plot(x.factor = data$exercise, #x-axis variable
                 trace.factor = data$gender, #variable for lines
                 response = data$weight_loss, #y-axis variable
                 fun = median, #metric to plot
                 ylab = "Weight Loss",
                 xlab = "Exercise Intensity",
                 col = c("pink", "blue"),
                 lty = 1, #line type
                 lwd = 2, #line width
                 trace.label = "Gender")

Interaction plot in R

In general, there is no interaction effect if the two lines on the interaction plot are parallel. However, there will probably be an interaction effect if the lines cross.

As we can see in this plot, there is no intersection between the lines for men and women, suggesting that there is no interaction between the variables of exercise intensity and gender.

This is consistent with the p-value in the ANOVA table’s output, which indicated that the interaction term in the ANOVA model was not statistically significant.

Check your inbox or spam folder to confirm your subscription.

Tweet
Share
Share
Pin
R Tags:interaction

Post navigation

Previous Post: How to Create Summary Tables in R
Next Post: How to Standardize Data in R?

Related Posts

  • How to perform kruskal wallis test in r
    How to perform the Kruskal-Wallis test in R? R
  • Add new calculated variables to a data frame and drop all existing variables
    Add new calculated variables to a data frame and drop all existing variables R
  • Is R or Python Better for Data Science in Bangalore
    Is R or Python Better for Data Science in Bangalore R
  • Count Observations by Group in R
    Count Observations by Group in R R
  • best books about data analytics
    Best Books About Data Analytics Course
  • How to Join Data Frames for different column names in R
    How to Join Data Frames for different column names in R R

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • About Us
  • Contact
  • Disclaimer
  • Guest Blog
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Defensive Programming Strategies in R
  • Plot categorical data in R
  • Top Data Modeling Tools for 2023
  • Ogive Graph in R
  • Is R or Python Better for Data Science in Bangalore

Check your inbox or spam folder to confirm your subscription.

  • Data Scientist Career Path Map in Finance
  • Is Python the ideal language for machine learning
  • Convert character string to name class object
  • How to play sound at end of R Script
  • Pattern Searching in R
  • OLS Regression in R
    OLS Regression in R R
  • How to Replace Inf Values with NA in R
    How to Replace Inf Values with NA in R R
  • How to Use Mutate function in R
    How to Use Mutate function in R R
  • The Uniform Distribution in R
    The Uniform Distribution in R R
  • How to Avoid Overfitting
    How to Avoid Overfitting? Machine Learning
  • Calculate the p-Value from Z-Score in R
    Calculate the p-Value from Z-Score in R R
  • Descriptive statistics vs Inferential statistics
    Descriptive statistics vs Inferential statistics: Guide Statistics
  • Ad Hoc Analysis
    What is Ad Hoc Analysis? Statistics

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme