Tried using vif() function but how to get the correlation between pairs of predictors?
fit<-lm(y ~ X1 + X2 + X3, data=data1) vif(fit)
You can easily find correlations between varibles with the cor
function and plot it with the package corrplot
library(corrplot)
df <-
data.frame(
x1 = round(runif(100, min = 100, max=170)),
x2 = sample(1:10, 100, replace = T),
x3 = round(runif(100), 2),
y = round(runif(100), 2)
)
cor(df)
corrplot::corrplot(cor(df),method = "color")
If you just want the correlation coefficients, you can just use cor
. If you want them all in a go, use sapply
:
sapply(data[, c("X1", "X2")], function(x) cor(x, data$X3))
There are many ways..
Create dummy data
df <-
data.frame(
x1 = round(runif(100, min = 100, max=170)),
x2 = sample(1:10, 100, replace = T),
x3 = round(runif(100), 2),
y = round(runif(100), 2)
)
Using psych
package
library(psych)
pairs.panels(df,
method = "pearson", # correlation method
hist.col = "#00AFBB",
density = TRUE, # show density plots
ellipses = TRUE # show correlation ellipses
)
Using GGally package
library(GGally)
##GGally package
## Correlation matirx
ggcorr(df,
nbreaks = 6,
label = TRUE,
label_size = 3,
color = "grey50")
## Correlation Plots
ggpairs(df, columns = 1: ncol(df), title = NULL,
upper = list(continuous = "cor"),
lower = list(continuous = "smooth"),
mapping = NULL)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.