简体   繁体   中英

Is it possible to filter a corrplot/cormatrix in R?

I have a dataset that contains a number of different variables, each with it's own subset of groups. I've also got a target variable. I want to establish which groups are correlated with the response and have managed to create a series of correlation matrices for each variable and the response, and also, corresponding correlation plots using the cor function and corrplot function respsectively in R.

For the purpose of what I'm doing, I'm not interested in whether variables are correlated with each other, I'm just interested in whether they are correlated with the response variable. Is there a way of filtering the correlation matrix prior to plotting so as to only include the variables against the target variable

cor(x) function, when given one argument (matrix or a data.frame) computes correlations between all pairs of variables present in the columns. However the same function can accept two arguments: cor(x, y) , in which case it only computes correlations between pairs x and y.

So in your case you can provide all your group variables as x, and the response variable as y, and then plot the result (assuming "response" is in the last column):

cors <- cor(dat[,-ncol(dat)], dat[,ncol(dat)])
corrplot::corrplot(cors)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM