分类变量和二元变量之间的相关性 R

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

我想使用随附的数据来了解学生参加的训练营与他们最终获得的工作之间是否存在相关性。例如,参加软件工程训练营的人最终会找到一份软件工作,还是参加数据科学训练营的人会找到一份数据工作吗?我尝试过这样做,但我认为这是不对的。我已附上数据的屏幕截图。请帮忙提供正确的代码

data <- data[rowSums(is.na(data)) == 0,]
summary(data)
data <- as.data.frame.matrix(data)
sapply(data,class)
data$Bootcamp <- as.numeric(factor(data$Bootcamp))
sapply(data,class)
data <- data[rowSums(is.na(data)) == 0,]
r variables statistics correlation
1个回答
0
投票

以下是计算相关性的方法(记住相关性不是因果关系,可能存在混杂因素)。由于我无法访问您的数据,因此我首先生成一些随机数据,如下所示(您可以将其替换为您的实际数据)。

head(data)
#       Bootcamp software web data security engineer developer analyst
#1  Data Science        0   1    0        0        0         1       1
#2  Data Science        1   1    1        0        1         1       1
#3 Cybersecurity        1   1    0        1        0         0       1
#4 Cybersecurity        0   0    0        1        1         0       1
#5 Cybersecurity        0   1    0        1        0         0       0
#6  Data Science        0   1    0        1        0         0       1

现在,使用函数

model.matrix()
创建设计(或模型)矩阵,例如,通过将因子扩展到一组虚拟变量,从分类列创建虚拟二元变量。

bootcamp <- as.data.frame(model.matrix(~ Bootcamp + 0, data)) # with no intercept term
head(bootcamp)
#  BootcampCybersecurity BootcampData Science BootcampSoftware Engineering
#1                     0                    1                            0
#2                     0                    1                            0
#3                     1                    0                            0
#4                     1                    0                            0
#5                     1                    0                            0
#6                     0                    1                            0

请注意,它只为我生成了 3 个虚拟列变量,因为我只有 3 个扩展的相应因子变量级别。您将获得列数作为因子变量中的级别数。

现在,计算相关性:

job <- data[,2:ncol(data)]
corr <- cor(bootcamp, job)

如果您愿意,您可以使用奇特的绘图来更好地可视化/解释,如下所示:

library(ggcorrplot)
ggcorrplot(corr, lab = TRUE)

从上面的可视化中请注意,对于我的数据,表示数据作业的二进制变量与表示数据科学训练营的二进制变量的相关性是 0.1

您可以进行线性回归来确定特定的预测因素(例如训练营培训)对于预测响应(例如工作类型)是否重要。希望它能解答您的问题。

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