Set random seed for reproducibility

set.seed(123)

Generate 100 random observations for x1 and x2

x1 <- runif(100, 4, 25) x2 <- runif(100, 3, 10)

Define coefficients

beta0 <- 5 beta1 <- 2 beta2 <- -3

Add random noise

epsilon <- rnorm(100, mean = 0, sd = 1)

Generate y based on the different models

y1 <- beta0 + beta1 * x1 + beta2 * x2 + epsilon y2 <- beta0 + beta2 * x2 + epsilon y3 <- beta0 + beta1 * x1 + epsilon

Create data frames

data1 <- data.frame(x1, x2, y1) data2 <- data.frame(x2, y2) data3 <- data.frame(x1, y3)

Fit linear models

model1 <- lm(y1 ~ x1 + x2, data = data1) model2 <- lm(y2 ~ x2, data = data2) model3 <- lm(y3 ~ x1, data = data3)

Check for multi-collinearity using Variance Inflation Factor (VIF)

install.packages(“carData”) library(car) vif(model1)

No Multi-collinearity

Define new x2 as x1 + x2

data1\(new_x2 <- data1\)x1 + data1$x2

Fit the new linear model

new_model <- lm(y1 ~ x1 + new_x2, data = data1)

Summary of the new model

summary(new_model)

Check for multi-collinearity in the new model

vif_new_model <- vif(new_model) print(vif_new_model)