r中的关联矩阵

编程入门 行业动态 更新时间:2024-10-13 22:24:32
本文介绍了r中的关联矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

corrplot允许您在R中绘制相关矩阵的方式

任何想法我如何在R中绘制关联矩阵关联方法使用的是任何用户指定的方法,例如Cramer's V

解决方案

问题的答案在很大程度上取决于您所获得的数据和特定的关联方法.我假设您有一堆标称变量,并想查看它们是否在相关图上使用Cramer的V进行了相关.在这种情况下,可以采用以下方法:

  • 为每一对计算Cramer的V相关系数变量.我使用了 vcd 库,因为它具有计算Cramer V的方法.
  • 将这些系数放在一起,基本上可以得到相关矩阵
  • 可视化矩阵
  • 下面列出了执行此操作的难看但有效的代码.我玩过

    The way corrplot allows you to plot a correlation matrix in R

    Any idea how i can plot a association matrix in R where the method of association is using any user specified method like Cramer's V

    解决方案

    The answer to your question strongly depends on the data you've got and specific correlation method. I assume you have a bunch of nominal variables and want to see whether they are correlated using Cramer's V on the correlation plot. In this case, a way to do this is following:

  • Calculate Cramer's V correlation coefficient for every pair of variables.I used vcd library, as it has method to calculate Cramer's V.
  • Put these coefficients together and basically get correlation matrix
  • Visualize the matrix
  • Ugly but working code to do this is listed below. I played around outer - the clearest and most precise way to work with row and column indexes, but encountered problems with indexing columns in df using row and column index from m: for some reason it just didn't want to get variable from df.

    install.packages("vcd") library(vcd) # Simulate some data or paste your own df <- data.frame(x1 = sample(letters[1:5], 20, replace = TRUE), x2 = sample(letters[1:5], 20, replace = TRUE), x3 = sample(letters[1:5], 20, replace = TRUE)) # Initialize empty matrix to store coefficients empty_m <- matrix(ncol = length(df), nrow = length(df), dimnames = list(names(df), names(df))) # Function that accepts matrix for coefficients and data and returns a correlation matrix calculate_cramer <- function(m, df) { for (r in seq(nrow(m))){ for (c in seq(ncol(m))){ m[[r, c]] <- assocstats(table(df[[r]], df[[c]]))$cramer } } return(m) } cor_matrix <- calculate_cramer(empty_m ,data) corrplot(cor_matrix)

    更多推荐

    r中的关联矩阵

    本文发布于:2023-11-30 17:24:40,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1650883.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:矩阵

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!