传递和计算皮尔森相关性

时间:2018-02-13 09:26:59

标签: r correlation transpose na pearson-correlation

我真的很喜欢编码,我需要在数据集中运行一些统计数据,例如pearson相关性,但是我在操作数据时遇到了一些麻烦。

根据我的理解,我需要转置我的数据以计算皮尔逊相关性,但这里我遇到了一些问题。对于初学者,列名称将变为新行,而不是成为新的列名称。然后我收到一条消息,表明我的值不是数字。

我也有一些NA,我正在尝试计算与此代码的相关性

cor(cr, use = "complete.obs", method = "pearson")
Error in cor(cr1, use = "complete.obs", method = "pearson") : 
  'x' must be numeric

我需要知道Victoria和Nuria之间的相关性,它应该产生0.3651484

这是我的数据集的输入:

> dput(cr)
structure(list(User = structure(c(8L, 10L, 2L, 17L, 11L, 1L, 
18L, 9L, 7L, 5L, 3L, 14L, 13L, 4L, 20L, 6L, 16L, 12L, 15L, 19L
), .Label = c("Ana", "Anton", "Bernard", "Carles", "Chris", "Ivan", 
"Jim", "John", "Marc", "Maria", "Martina", "Nadia", "Nerea", 
"Nuria", "Oriol", "Rachel", "Roger", "Sergi", "Valery", "Victoria"
), class = "factor"), Star.Wars.IV...A.New.Hope = c(1L, 5L, NA, 
NA, 4L, 2L, NA, 4L, 5L, 4L, 2L, 3L, 2L, 3L, 4L, NA, NA, 4L, 5L, 
1L), Star.Wars.VI...Return.of.the.Jedi = c(5L, 3L, NA, 3L, 3L, 
4L, NA, NA, 1L, 2L, 1L, 5L, 3L, NA, 4L, NA, NA, 5L, 1L, 2L), 
    Forrest.Gump = c(2L, NA, NA, NA, 4L, 4L, 3L, NA, NA, NA, 
    5L, 2L, NA, 3L, NA, 1L, NA, 1L, NA, 2L), The.Shawshank.Redemption = c(NA, 
    2L, 5L, NA, 1L, 4L, 1L, NA, 4L, 5L, NA, NA, 5L, NA, NA, NA, 
    NA, 5L, NA, 4L), The.Silence.of.the.Lambs = c(4L, 4L, 2L, 
    NA, 4L, NA, 1L, 3L, 2L, 3L, NA, 2L, 4L, 2L, 5L, 3L, 4L, 1L, 
    NA, 5L), Gladiator = c(4L, 2L, NA, 1L, 1L, NA, 4L, 2L, 4L, 
    NA, 5L, NA, NA, NA, 5L, 2L, NA, 1L, 4L, NA), Toy.Story = c(2L, 
    1L, 4L, 2L, NA, 3L, NA, 2L, 4L, 4L, 5L, 2L, 4L, 3L, 2L, NA, 
    2L, 4L, 2L, 2L), Saving.Private.Ryan = c(2L, NA, NA, 3L, 
    4L, 1L, 5L, NA, 4L, 3L, NA, NA, 5L, NA, NA, 2L, NA, NA, 1L, 
    3L), Pulp.Fiction = c(NA, NA, NA, 4L, NA, 4L, 2L, 3L, NA, 
    4L, NA, 1L, NA, NA, 3L, NA, 2L, 5L, 3L, 2L), Stand.by.Me = c(3L, 
    4L, 1L, NA, 1L, 4L, NA, NA, 1L, NA, NA, NA, NA, 4L, 5L, 1L, 
    NA, NA, 3L, 2L), Shakespeare.in.Love = c(2L, 3L, NA, NA, 
    5L, 5L, 1L, NA, 2L, NA, NA, 3L, NA, NA, NA, 5L, 2L, NA, 3L, 
    1L), Total.Recall = c(NA, 2L, 1L, 4L, 1L, 2L, NA, 2L, 3L, 
    NA, 3L, NA, 2L, 1L, 1L, NA, NA, NA, 1L, NA), Independence.Day = c(5L, 
    2L, 4L, 1L, NA, 4L, NA, 3L, 1L, 2L, 2L, 3L, 4L, 2L, 3L, NA, 
    NA, NA, NA, NA), Blade.Runner = c(2L, NA, 4L, 3L, 4L, NA, 
    3L, 2L, NA, NA, NA, NA, NA, 2L, NA, NA, NA, 4L, NA, 5L), 
    Groundhog.Day = c(NA, 2L, 1L, 5L, NA, 1L, NA, 4L, 5L, NA, 
    NA, 2L, 3L, 3L, 2L, 5L, NA, NA, NA, 5L), The.Matrix = c(4L, 
    NA, 1L, NA, 3L, NA, 1L, NA, NA, 2L, 1L, 5L, NA, 5L, NA, 2L, 
    4L, NA, 2L, 4L), Schindler.s.List = c(2L, 5L, 2L, 5L, 5L, 
    NA, NA, 1L, NA, 5L, NA, NA, NA, 1L, 3L, 2L, NA, 2L, NA, 3L
    ), The.Sixth.Sense = c(5L, 1L, 3L, 1L, 5L, 3L, NA, 3L, NA, 
    1L, 2L, NA, NA, NA, NA, 4L, NA, 1L, NA, 5L), Raiders.of.the.Lost.Ark = c(NA, 
    3L, 1L, 1L, NA, NA, 5L, 5L, NA, NA, 1L, NA, 5L, NA, 3L, 3L, 
    NA, 2L, NA, 3L), Babe = c(NA, NA, 3L, 2L, NA, 2L, 2L, NA, 
    5L, NA, 4L, 2L, NA, NA, 1L, 4L, NA, 5L, NA, NA)), .Names = c("User", 
"Star.Wars.IV...A.New.Hope", "Star.Wars.VI...Return.of.the.Jedi", 
"Forrest.Gump", "The.Shawshank.Redemption", "The.Silence.of.the.Lambs", 
"Gladiator", "Toy.Story", "Saving.Private.Ryan", "Pulp.Fiction", 
"Stand.by.Me", "Shakespeare.in.Love", "Total.Recall", "Independence.Day", 
"Blade.Runner", "Groundhog.Day", "The.Matrix", "Schindler.s.List", 
"The.Sixth.Sense", "Raiders.of.the.Lost.Ark", "Babe"), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:2)

除了@Niek的回答外,还有一个摘要。首先通过排除第一列(包含名称而不是数字,因此不能用于相关计算)将数据帧转换为t();在同一步骤中将这些名称分配给新列。然后计算具体的相关性。一个解决方案是:

cr2 <- setNames(as.data.frame(t(cr[, -1])), cr[, 1])
with(cr2, cor(Victoria, Nuria, use = "complete.obs"))
[1] 0.3651484

或整个相关矩阵:

cor(cr2, use = "pairwise.complete.obs")

答案 1 :(得分:2)

此代码应为您提供所有用户之间的关联矩阵。

cr2<-t(cr[,2:21]) # Transpose (first column contains names)
colnames(cr2)<-cr[,1] # Assign names to columns

cor(cr2,use="complete.obs") # Gives an error because there are no complete obs
# Error in cor(cr2, use = "complete.obs") : no complete element pairs

cor(cr2,use="pairwise.complete.obs") # use pairwise deletion

Victoria和Nuria之间的相关性为0.36514837(使用成对缺失)

编辑:要通过列表删除获得Victoria和Nuria之间的相关性,请运行上面的内容然后

cr2<-as.data.frame(cr2)
with(cr2, cor(Victoria, Nuria, use = "complete.obs", method = "pearson"))
[1] 0.3651484