What is interaction variable and how to calculate it and how is it used in model building in R

This recipe explains what is interaction variable and how to calculate it and how is it used in model building in R

Recipe Objective

Interaction effects occurs when the value of one independent variable depends on the effects of another variable. Interaction between independent variables increases the complexity of the model. It mainly indicates that a third variable affects the relationahip between a pair of independent and dependent variable. We cannot ignore these interation effects effects while modelling. ​

They are most common in regression analysis and designed experments. Interaction variable is a variable constructed which tries to represent some or all of the interation effects present in a set of independent variables. They introduce an additional level of analysis by allowing the user to explore it's effects on a deeper level. ​

In this recipe, we will learn how to create a interaction variable and model. We will demonstrate this by using regression analysis with interaction variable. ​

Step 1: Reading the dataset

Dataset Description: The company wants to predict the cost they should set for a new variant of the kinds of bags based on the attributes mentioned below using multiple linear regression model with Interation variable: ​

  1. Height – The height of the bag
  2. Width – The width of the bag
  3. Length – The length of the bag
  4. Weight – The weight the bag can carry
  5. Weight1 – Weight the bag can carry after expansion

data_1 = read.csv("R_250_Data_1.csv") # attach data variable attach(data_1) head(data_1)

Cost	Weight	Weight1	Length	Height	Width
242	23.2	25.4	30.0	11.5200	4.0200
290	24.0	26.3	31.2	12.4800	4.3056
340	23.9	26.5	31.1	12.3778	4.6961
363	26.3	29.0	33.5	12.7300	4.4555
430	26.5	29.0	34.0	12.4440	5.1340
450	26.8	29.7	34.7	13.6024	4.9274

Step 2: Creating an interaction variable

Two steps should be followed in creation of the interaction variable: ​

  1. The input variable must be centered to avoid multicollinearity
  2. The interaction variable is formed by the multiplication of two or more predictors or independent variables.

We will use the interaction between Weight and Weight1. ​

# centering the input variables Weightc <- Weight - mean(Weight) Weight1c <- Weight1 - mean(Weight1) # creating the interaction variable Weighti_Weight1 <- Weightc * Weight1c

Step 3: Creating an Interaction model

We use lm(FORMULA, data) function to create an interaction model where: ​

  1. Formula = y~x1+x2+x3+... (y ~ dependent variable; x1,x2 ~ independent variable)
  2. data = data variable

interactionModel <- lm(Cost ~ Weight1 + Weight + Length + Height + Width + Weighti_Weight1, data = data_1) #display summary information about the model summary(interactionModel)

Call:
lm(formula = Cost ~ Weight1 + Weight + Length + Height + Width + 
    Weighti_Weight1, data = data_1)

Residuals:
     Min       1Q   Median       3Q      Max 
-180.928  -35.323   -3.022   32.165  201.418 

Coefficients:
                  Estimate Std. Error t value Pr(>|t|)    
(Intercept)     -516.25092   14.36967 -35.926  < 2e-16 ***
Weight1          -24.44967   20.27981  -1.206  0.22984    
Weight            51.30138   19.51801   2.628  0.00946 ** 
Length           -18.99909    8.43269  -2.253  0.02569 *  
Height            36.24918    4.25092   8.527  1.4e-14 ***
Width             98.44557   10.45567   9.416  < 2e-16 ***
Weighti_Weight1    0.90253    0.04045  22.310  < 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 59.79 on 152 degrees of freedom
Multiple R-squared:  0.9732,	Adjusted R-squared:  0.9721 
F-statistic: 918.7 on 6 and 152 DF,  p-value: < 2.2e-16

Note: This is the complete interaction model. We would like to compare the model to others. ​

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Build Regression (Linear,Ridge,Lasso) Models in NumPy Python
In this machine learning regression project, you will learn to build NumPy Regression Models (Linear Regression, Ridge Regression, Lasso Regression) from Scratch.

Hands-On Approach to Causal Inference in Machine Learning
In this Machine Learning Project, you will learn to implement various causal inference techniques in Python to determine, how effective the sprinkler is in making the grass wet.

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.

End-to-End Snowflake Healthcare Analytics Project on AWS-2
In this AWS Snowflake project, you will build an end to end retraining pipeline by checking Data and Model Drift and learn how to redeploy the model if needed

Deploy Transformer BART Model for Text summarization on GCP
Learn to Deploy a Machine Learning Model for the Abstractive Text Summarization on Google Cloud Platform (GCP)

Learn Hyperparameter Tuning for Neural Networks with PyTorch
In this Deep Learning Project, you will learn how to optimally tune the hyperparameters (learning rate, epochs, dropout, early stopping) of a neural network model in PyTorch to improve model performance.

Llama2 Project for MetaData Generation using FAISS and RAGs
In this LLM Llama2 Project, you will automate metadata generation using Llama2, RAGs, and AWS to reduce manual efforts.

Mastering A/B Testing: A Practical Guide for Production
In this A/B Testing for Machine Learning Project, you will gain hands-on experience in conducting A/B tests, analyzing statistical significance, and understanding the challenges of building a solution for A/B testing in a production environment.

MLOps AWS Project on Topic Modeling using Gunicorn Flask
In this project we will see the end-to-end machine learning development process to design, build and manage reproducible, testable, and evolvable machine learning models by using AWS

Text Classification with Transformers-RoBERTa and XLNet Model
In this machine learning project, you will learn how to load, fine tune and evaluate various transformer models for text classification tasks.