如何在R中运行多个半方相关性?
我是R中的初学者。我想做的是计算R中的半部分相关性,同时控制一个变量。
例如,在MTCARS数据集的情况下,我想计算Cyl,Disp,Hp,Hp,Drat,wt,Qsec,vs,Am,Am,Gear,Carb,同时控制MPG之间的所有相关性。
从我观察到的内容来看,包括PPCOR在内的大多数软件包都会做类似的事情:
pcor.test(a, b, c)
这意味着在控制C时计算A和B之间的相关性。 但是我不知道在控制一个变量时将其扩展到多个变量的方法。我不确定如何扩展此语法以实现这一目标。在伪代码中,我想要的是类似的东西:
compute_correlations(cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb; control for = mpg)
然后在热图中可视化结果,标签上说明了我在控制“ mpg”时如何计算所有这些相关性。有什么方法可以在R中有效地做到这一点?我想我能做的就是我自己对每对变量进行所有这些相关性,但这将需要很长时间,而且效率低下。
这在SPSS中很容易实现,但我不知道如何在R中做到这一点。
I am a beginner in R. What I want to do is compute semi partial correlations in R, while controlling for one variable.
For example, in the case of the mtcars data set, I would like to compute all the correlations between cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb, while controlling for mpg.
From what I observed, most packages, including ppcor, do something like this:
pcor.test(a, b, c)
Which means computing the correlation between a and b while controlling for c.
But I do not know of a way to extend that to multiple variables while controlling for one. I am not sure how to extend this syntax to achieve that. In pseudocode, what I would want is something like:
compute_correlations(cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb; control for = mpg)
And then visualize the result in a heatmap, with labeling that says how I computed all these correlations while controlling for 'mpg'. Is there any way to do this efficiently in R? I guess what I could do is make all these correlations myself for each pair of variables, but that would take a long time and it would be inefficient.
This is quite easily achievable in SPSS but I do not know how to do this in R.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这样做的过程确实并不明显,也许这是创建使其更容易的软件包的机会。同时,这是一种解决方案,我们在每个变量组合中迭代:
编辑:创建热图:
”
The process to do this is indeed not evident, and perhaps this is an opportunity to create a package that makes it easier. In the meantime, here is one solution where we iterate through each combination of variables:
EDIT: To create a heatmap: