更新数据集 2 和 1 结构: 抱歉这次突然更新。我有两个数据集。我的第一个数据集的结构是(在 print(matr1)
中使用 R
时):
month_year income
[1,] "Jan 2000" "30000"
[2,] "Feb 2000" "12364"
[3,] "Mar 2000" "37485"
[4,] "Apr 2000" "2000"
[5,] "Jun 2000" "7573"
. . .
. . .
因此,第一个数据集具有 每年每个月的一个收入值 。
我的第二个数据集的结构是(在
print(matr2)
中使用 R
时): month_year value
[1,] "Jan 2000" "84737476"
[2,] "Jan 2000" "39450334"
[3,] "Jan 2000" "48384943"
[4,] "Feb 2000" "12345678"
[5,] "Feb 2000" "49595340"
. . .
. . .
所以在这第二个数据集中,我有每年每个月的
n
(比如 100 但不是一直恒定)数量的值。 这两个数据集在随后的许多年中都有每月明智的值(例如 2000、2001 年等的所有月份)。现在我想找到这两个数据集之间的相关性,但不是按月计算,而不是整体。当我使用 R 命令
cor(as.numeric(matr1[,"income"]),as.numeric(matr2[,"value"]))
时,我得到了整体相关性,但我想要每月的相关性而不是整体相关性。 我想要这样的相关性: Jan | Feb | Mar | Apr | May | .....
Correlation x | y | z | p | q | .....
我遇到的问题是:
注意: 我不确定是否应该在此处或
Cross Validated
上发布此问题。我发布了一个关于这个数据集的问题,只是关于获取相关性的错误,它从那里迁移到这里。所以如果我把这个贴在错误的地方,请原谅。UPDATE1: 经过一些建议,我修改了这篇文章以指向正确的维度。首先,截至目前的数据集是矩阵格式,因此是引号。我可以按照一些评论的建议将其转换为
data.frame
,但现在我一直在通过使用 as.numeric
转换列来计算相关性。
最佳答案
也许你可以尝试:
dat1 <- structure(list(year = c(2000L, 2000L, 2000L, 2000L, 2000L, 2001L,
2001L, 2001L, 2001L, 2001L), month = c(1L, 2L, 3L, 4L, 5L, 1L,
2L, 3L, 4L, 5L), income = c(30000L, 12364L, 37485L, 2000L, 7573L,
25000L, 14364L, 38485L, 4000L, 7873L)), .Names = c("year", "month",
"income"), class = "data.frame", row.names = c(NA, -10L))
dat2 <- structure(list(month_year = c("Jan 2000", "Feb 2000", "Mar 2000",
"Apr 2000", "May 2000", "Jan 2001", "Feb 2001", "Mar 2001", "Apr 2001",
"May 2001"), value = c(84737476L, 39450334L, 48384943L, 12345678L,
49595340L, 84337476L, 34450334L, 48984943L, 124545678L, 49525340L
)), .Names = c("month_year", "value"), class = "data.frame", row.names = c(NA,
-10L))
dat1$month_year <- paste(month.abb[dat1$month], dat1$year)
dat1$month <- gsub(" \\d+","", dat1$month_year)
dat2$month <- gsub(" \\d+","", dat2$month_year)
dat1$indx <- with(dat1, ave(month, month, FUN=seq_along))
dat2$indx <- with(dat2, ave(month, month, FUN=seq_along))
dat1 <- dat1[,c(2,3,5)]
dat2 <- dat2[,c(3,2,4)]
colnames(dat2)[2] <- "income"
library(reshape2)
dat2C <- dcast(dat2, indx~month, value.var="income")
dat1C <- dcast(dat1, indx~month, value.var="income")
m1 <- as.matrix(dat1C[,-1])
m2 <- as.matrix(dat2C[,-1])
cor(m1,m2)
diag(cor(m1,m2))
# Apr Feb Jan Mar May
#1 -1 1 1 -1
此外,如果您可以将两个数据集合并在一起,则可以使用
data.table
来完成。使用上面的 dput()
数据 library(data.table)
dat1$month_year <- paste(month.abb[dat1$month], dat1$year)
dat1 <- dat1[,c(4,3)]
setDT(dat1)
setDT(dat2)
setkey(dat2, month_year)
dat2[dat1, income := i.income]
dat2[,month:= gsub(" \\d+", "", month_year)][,cor(value, income), by=month]
# month V1
#1: Apr 1
#2: Feb -1
#3: Jan 1
#4: Mar 1
#5: May -1
更新
dat1 <- structure(list(month_year = structure(c(5L, 3L, 8L, 1L, 7L, 6L,
4L, 9L, 2L), .Label = c("Apr 2000", "Apr 2001", "Feb 2000", "Feb 2001",
"Jan 2000", "Jan 2001", "Jun 2000", "Mar 2000", "Mar 2001"), class = "factor"),
income = c(30000, 12364, 37485, 2000, 7573, 42000, 15764,
38465, 5000)), .Names = c("month_year", "income"), row.names = c(NA,
-9L), class = "data.frame")
dat2 <- structure(list(month_year = structure(c(5L, 5L, 5L, 3L, 3L, 7L,
7L, 7L, 1L, 1L, 6L, 6L, 4L, 4L, 8L, 8L, 2L, 2L, 2L, 2L), .Label = c("Apr 2000",
"Apr 2001", "Feb 2000", "Feb 2001", "Jan 2000", "Jan 2001", "Mar 2000",
"Mar 2001"), class = "factor"), value = c(84737476, 39450334,
48384973, 12345678, 49595340, 4534353, 43353325, 84333535, 35343232,
4334353, 3434353, 5355322, 5223345, 4523535, 345353, 32235, 423553,
233553, 423535, 884455)), .Names = c("month_year", "value"), row.names = c(NA,
-20L), class = "data.frame")
datN <- merge(dat1, dat2, all=T)
library(data.table)
DT <- data.table(datN)
DT[, month:= gsub(" \\d+", "", month_year)][,cor(value, income),by=month]
# month V1
#1: Apr -0.7136049
#2: Feb -0.7037676
#3: Jan -0.8637808
#4: Jun NA
#5: Mar -0.6484684
关于r - 在 R 中查找两个数据集之间的相关性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25226478/