### Example 1
set.seed(2024)
obs = 100
cte = rep(1, obs)
x2 = rnorm(obs, 5, 0.01) # related to intercept: non essential
x3 = rnorm(obs, 5, 10)
x4 = x3 + rnorm(obs, 5, 0.5) # related to x3: essential
x5 = rnorm(obs, -1, 3)
x6 = rnorm(obs, 15, 0.5)
y = 4 + 5*x2 - 9*x3 -2*x4 + 2*x5 + 7*x6 + rnorm(obs, 0, 2)
x = cbind(cte, x2, x3, x4, x5, x6)
multicollinearity(y, x)
### Example 2
### Effect of sample size
obs = 25 # by decreasing the number of observations affected to x4
cte = rep(1, obs)
x2 = rnorm(obs, 5, 0.01) # related to intercept: non essential
x3 = rnorm(obs, 5, 10)
x4 = x3 + rnorm(obs, 5, 0.5) # related to x3: essential
x5 = rnorm(obs, -1, 3)
x6 = rnorm(obs, 15, 0.5)
y = 4 + 5*x2 - 9*x3 -2*x4 + 2*x5 + 7*x6 + rnorm(obs, 0, 2)
x = cbind(cte, x2, x3, x4, x5, x6)
multicollinearity(y, x)
### Example 3
y = 4 - 9*x3 - 2*x5 + rnorm(obs, 0, 2)
x = cbind(cte, x3, x5) # independently generated
multicollinearity(y, x)
### Example 4
### Detection of multicollinearity in Wissel data
head(Wissel, n=5)
y = Wissel[,2]
x = Wissel[,3:6]
multicollinearity(y, x)
### Example 5
### Detection of multicollinearity in euribor data
head(euribor, n=5)
y = euribor[,1]
x = euribor[,2:5]
multicollinearity(y, x)
### Example 6
### Detection of multicollinearity in Cobb-Douglas production function data
head(CDpf, n=5)
y = CDpf[,1]
x = CDpf[,2:4]
multicollinearity(y, x)
### Example 7
### Detection of multicollinearity in number of employees of Spanish companies data
head(employees, n=5)
y = employees[,1]
x = employees[,3:5]
multicollinearity(y, x)
### Example 8
### Detection of multicollinearity in simple linear model simulated data
head(SLM1, n=5)
y = SLM1[,1]
x = SLM1[,2:3]
multicollinearity(y, x)
head(SLM2, n=5)
y = SLM2[,1]
x = SLM2[,2:3]
multicollinearity(y, x)
### Example 9
### Detection of multicollinearity in soil characteristics data
head(soil, n=5)
y = soil[,16]
x = soil[,-16]
x = cbind(rep(1, length(y)), x) # the design matrix has to have the intercept in the first column
multicollinearity(y, x)
multicollinearity(y, x[,-3]) # eliminating the problematic variable (SumCation)
### Example 10
### The intercept must be in the first column of the design matrix
set.seed(2025)
obs = 100
cte = rep(1, obs)
x2 = sample(1:500, obs)
x3 = sample(1:500, obs)
x4 = rep(4, obs)
x = cbind(cte, x2, x3, x4)
u = rnorm(obs, 0, 2)
y = 5 + 2*x2 - 3*x3 + 10*x4 + u
multicollinearity(y, x)
multicollinearity(y, x[,-4]) # the constant variable is removed
Run the code above in your browser using DataLab