forked from Northbreeze/Multicollinearity
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmulticollinearity.Rmd
More file actions
126 lines (84 loc) · 1.63 KB
/
multicollinearity.Rmd
File metadata and controls
126 lines (84 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
---
title: "Multicollinearity"
output:
md_document:
variant: markdown_github
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Longley
```{r cars}
library(datasets)
library(tidyverse)
library(broom)
library(viridis)
library(GGally)
library(car)
library(gridExtra)
library(knitr)
library(corrplot)
```
```{r}
df <- longley
str(df)
```
```{r}
#ggpairs(df)
```
```{r}
#now fitting a linear model
model1 <- lm(Employed ~ GNP + Unemployed + Armed.Forces + Population + Year, data = df)
vif(model1)
```
```{r}
#now fitting a linear model again
model2 <- lm(Employed ~ Unemployed + Armed.Forces +Population + Year, data = df)
vif(model2)
```
```{r}
#now fitting a linear model again
model3 <- lm(Employed ~ Unemployed + Armed.Forces +Population, data = df)
vif(model3)
```
## Blood Pressure
```{r}
bloodpress <- read.table("bloodpress.txt", header=T)
```
```{r}
str(bloodpress)
```
```{r}
cor(bloodpress)
```
```{r}
corrplot(cor(bloodpress), type = "upper", order = "hclust",
tl.col = "black", tl.srt = 45)
```
```{r}
model.1 <- lm(BP ~ Age + Weight + BSA + Dur + Pulse + Stress, data = bloodpress)
car::vif(model.1)
```
```{r}
model.2 <- lm(BP ~ Age + BSA + Dur + Pulse + Stress, data = bloodpress)
car::vif(model.2)
```
```{r}
head(bloodpress)
```
```{r}
#plot(bloodpress[, -1])
corrplot(cor(bloodpress[, -1]), method = "number", type = "upper", diag = FALSE)
```
```{r}
summary(bloodpress.lm <- lm(BP ~ . - Pt, data = bloodpress))
```
```{r}
round(vif(bloodpress.lm),2)
```
```{r}
summary(bloodpress.lm2 <- lm(BP ~ . - Pt - BSA, data = bloodpress))
```
```{r}
round(vif(bloodpress.lm2),2)
```