用于分析调查中不同排名问题的答案之间关系的代码

问题描述 投票:2回答:1

我的目标是找到更简单的代码,这些代码可以概括,显示两个调查问题的响应之间的关系。在MWE中,有一个问题要求受访者将8个营销选择从1到8排名,另一个要求他们对从1到9的9个属性选择进行排名。较高的排名表明受访者更倾向于选择。这是数据框。

structure(list(Email = c("a", "b", "c", "d", "e", "f", "g", "h", 
"i"), Ads = c(2, 1, 1, 1, 1, 2, 1, 1, 1), Alumni = c(3, 2, 2, 
3, 2, 3, 2, 2, 2), Articles = c(6, 4, 3, 2, 3, 4, 3, 3, 3), Referrals = c(4, 
3, 4, 8, 7, 8, 8, 6, 4), Speeches = c(7, 7, 6, 7, 4, 7, 4, 5, 
5), Updates = c(8, 6, 6, 5, 5, 5, 5, 7, 6), Visits = c(5, 8, 
7, 6, 6, 6, 6, 4, 8), `Business Savvy` = c(10, 6, 10, 10, 4, 
4, 6, 8, 9), Communication = c(4, 3, 8, 3, 3, 9, 7, 6, 7), Experience = c(7, 
7, 7, 9, 2, 8, 5, 9, 5), Innovation = c(2, 1, 4, 2, 1, 2, 2, 
1, 1), Nearby = c(3, 2, 2, 1, 5, 3, 3, 2, 2), Personal = c(8, 
10, 6, 8, 6, 10, 4, 3, 3), Rates = c(9, 5, 9, 6, 9, 7, 10, 5, 
4), `Staffing Model` = c(6, 8, 5, 5, 7, 5, 8, 7, 8), `Total Cost` = c(5, 
4, 3, 7, 8, 6, 9, 4, 6)), row.names = c(NA, -9L), class = c("tbl_df", 
"tbl", "data.frame"))

如果数字排名不能用于我的计算关系(相关性)的解决方案,请纠正我。

希望它们可以使用,我得到了以下的代码,我希望根据每个属性选择计算每个方法选择的相关矩阵。

library(psych)

dataframe2 <- psych::corr.test(dataframe[  , c(2, 9:17)])[[1]][1:10]  # the first method vs all attributes
dataframe3 <- psych::corr.test(dataframe[  , c(3, 9:17)])[[1]][1:10]  # the 2nd method vs all attributes and so on
dataframe4 <- psych::corr.test(dataframe[  , c(4, 9:17)])[[1]][1:10]  
dataframe5 <- psych::corr.test(dataframe[  , c(5, 9:17)])[[1]][1:10]
dataframe6 <- psych::corr.test(dataframe[  , c(6, 9:17)])[[1]][1:10]  
dataframe7 <- psych::corr.test(dataframe[  , c(7, 9:17)])[[1]][1:10]
dataframe8 <- psych::corr.test(dataframe[  , c(8, 9:17)])[[1]][1:10]

# create a dataframe from the rbinded rows
bind <- data.frame(rbind(dataframe2, dataframe3, dataframe4, dataframe5, dataframe6, dataframe7, dataframe8))

重命名行和列:

colnames(bind) <- c("Sel", colnames(dataframe[9:17]))
rownames(bind) <- colnames(dataframe[2:8])

如何更有效地完成上述工作?

顺便说一下,绑定数据框还允许用DataExplorer包生成热图。

library(DataExplorer)

DataExplorer::plot_correlation(bind)

heat map of ranking correlations

r correlation ranking survey
1个回答
3
投票

[摘要]

在我们讨论的范围内,有两种方法可以获取相关数据。

  1. 使用stats::cor,即cor(subset(dataframe, select = -Email))
  2. 使用psych::corr.test,即corr.test(subset(dataframe, select = -Email))[[1]]

然后,您可以将相关矩阵与所需的行和列进行子集化。

为了使用DataExplorer::plot_correlation,你可以简单地做plot_correlation(dataframe, type = "c")。注意:输出热图将包含所有列的相关性,因此您可以忽略不感兴趣的列。


[原始答案]

## Create data
dataframe <- structure(
  list(
    Email = c("a", "b", "c", "d", "e", "f", "g", "h",  "i"),
    Ads = c(2, 1, 1, 1, 1, 2, 1, 1, 1),
    Alumni = c(3, 2, 2, 3, 2, 3, 2, 2, 2),
    Articles = c(6, 4, 3, 2, 3, 4, 3, 3, 3),
    Referrals = c(4, 3, 4, 8, 7, 8, 8, 6, 4),
    Speeches = c(7, 7, 6, 7, 4, 7, 4, 5, 5),
    Updates = c(8, 6, 6, 5, 5, 5, 5, 7, 6),
    Visits = c(5, 8, 7, 6, 6, 6, 6, 4, 8),
    `Business Savvy` = c(10, 6, 10, 10, 4, 4, 6, 8, 9),
    Communication = c(4, 3, 8, 3, 3, 9, 7, 6, 7),
    Experience = c(7, 7, 7, 9, 2, 8, 5, 9, 5),
    Innovation = c(2, 1, 4, 2, 1, 2, 2, 1, 1),
    Nearby = c(3, 2, 2, 1, 5, 3, 3, 2, 2),
    Personal = c(8, 10, 6, 8, 6, 10, 4, 3, 3),
    Rates = c(9, 5, 9, 6, 9, 7, 10, 5, 4),
    `Staffing Model` = c(6, 8, 5, 5, 7, 5, 8, 7, 8),
    `Total Cost` = c(5, 4, 3, 7, 8, 6, 9, 4, 6)
  ),
  row.names = c(NA, -9L),
  class = c("tbl_df", "tbl", "data.frame")
)

严格遵循您的示例,我们可以执行以下操作:

## Calculate correlation
df2 <- subset(dataframe, select = -Email)
marketing_selections <- names(df2)[1:7]
attribute_selections <- names(df2)[8:16]
corr_matrix <- psych::corr.test(df2)[[1]]
bind <- subset(corr_matrix,
               subset = rownames(corr_matrix) %in% marketing_selections,
               select = attribute_selections)
DataExplorer::plot_correlation(bind)

Correlation Heatmap

警告

但是,这真的是你想要的吗? psych::corr.test生成相关矩阵,DataExplorer::plot_correlation再次计算相关性。这就像是相关性的相关性。

© www.soinside.com 2019 - 2024. All rights reserved.