用到的还是mtcars数据集(数据集截图见上篇,自己在rstudio里看也可以):
题目:用相关性矩阵展示cyl,hp,drat,qsec,vs的关联,设置显著性级别为0.01,非显著性相关变量为空白。
1、首先调用数据集,找到对应的数据列进行命名。
# Task 2: correlation matrix
# use the mtcars data for this task.
data("mtcars")
# show the correlation matrix plot for the variables cyl, hp, drat, qsec, vs
# set significance level at 0.01, and leave non-significant coefficients blank
my_data <- mtcars[, c(2,4,5,7,8)] # select columns 第1列对应1,不是0
names(mtcars[, c(2,4,5,7,8)]) #给对应的数据列命名
2、计算关联参数,得到关联表。
res <- cor(my_data) #计算关联参数
round(res, 2) #保留小数点后2位
3、安装Hmisc包,用于计算p值。