python rpy2教程_来自Python的R – rpy2教程

python rpy2教程

rpy2教程 (rpy2 tutorial)

Recently I found the Python module rpy2. This module offers a Python interface to R. Obviously; rpy2 requires that you have both R (version 3.2+) and Python (versions 2.7 and 3.3) installed.  There are pre-compiled binaries available for Linux and Windows (unsupported and unofficial, however). In this short tutorial, I will show you how to do carry out a repeated measures ANOVA (rmANOVA) using the r-packages ‘afex‘ and ‘lsmeans‘, Python, and rpy2.

最近,我找到了Python模块rpy2 。 该模块为R提供了Python接口。 明显; rpy2要求您同时安装R(版本3.2+)和Python(版本2.7和3.3)。 有适用于Linux和Windows的预编译二进制文件(但是不受支持和非正式)。 在这个简短的教程中,我将向您展示如何使用r包“ afex ”和“ lsmeans ”,Python和rpy2进行重复测量ANOVA(rmANOVA)。

First, we need to install rpy2. I installed rpy2 on Ubuntu 14.04 using pip:

首先,我们需要安装rpy2。 我使用pip在Ubuntu 14.04上安装了rpy2:


sudo pip install rpy2

sudo pip install rpy2
 

When we have a working installation, we start to import the methods that we need to use. In this example we are going to use ‘afex’ to do the rmANOVA and ‘lsmeans’ to do the follow-up analysis.

当安装正常时,我们将开始导入需要使用的方法。 在此示例中,我们将使用“ afex”进行rmANOVA并使用“ lsmeans”进行后续分析。

Now we check if the packages we want to use are installed. Note that the following script will install the r-package if needed. However, it might be good to know how to carry out a rmANOVA using the function ez_aov.

现在,我们检查我们要使用的软件包是否已安装。 请注意,如果需要,以下脚本将安装r-package。 但是,最好知道如何使用ez_aov函数执行rmANOVA。


packageNames = ('afex', 'lsmeans')

if all(rpackages.isinstalled(x) for x in packageNames):

    have_packages = True

else:

   have_packages = False

if not have_packages:

    utils = rpackages.importr('utils')
    utils.chooseCRANmirror(ind=1)

    packnames_to_install = [x for x in packageNames if not rpackages.isinstalled(x)]

    if len(packnames_to_install) > 0:

        utils.install_packages(StrVector(packnames_to_install))

packageNames = ('afex', 'lsmeans')

if all(rpackages.isinstalled(x) for x in packageNames):

    have_packages = True

else:

   have_packages = False

if not have_packages:

    utils = rpackages.importr('utils')
    utils.chooseCRANmirror(ind=1)

    packnames_to_install = [x for x in packageNames if not rpackages.isinstalled(x)]

    if len(packnames_to_install) > 0:

        utils.install_packages(StrVector(packnames_to_install))
 

We borrow a data set from the package Psych. In this case, we use the r-function read.table to get the data.

我们从Psych包中借用了一个数据集。 在这种情况下,我们使用r函数read.table来获取数据。

 

重复测量方差分析 (repeated measures ANOVA)

Before conducting our rmANOVA, we need to import the r-package (i.e., afex). After importing the r-package, we will use the function aov_ez to conduct the analysis.

在进行rmANOVA之前,我们需要导入r-package(即afex)。 导入r-package后,我们将使用函数aov_ez进行分析。


afex = importr('afex') 
model = afex.aov_ez('Subject', 'Recall', data, within='Valence')
print model

afex = importr('afex') 
model = afex.aov_ez('Subject', 'Recall', data, within='Valence')
print model
 

The last line above prints the results. A main effect of Valence was found.

上方的最后一行将打印结果。 发现价的主要作用。

后续分析 (Follow-up analysis)

If we are interested in following up the main effect we can do that using the package ‘lsmeans’. First we need to import the package and then we do a pairwise contrast and adjust for familywise error using Holm-Bonferroni correction.

如果我们有兴趣跟进主要效果,可以使用“ lsmeans”包来实现。 首先,我们需要导入包,然后进行成对对比,并使用Holm-Bonferroni校正来调整家庭误差。


lsm  = importr('lsmeans')
pairwise = lsm.lsmeans(model, "Valence", contr="pairwise", adjust="holm")

lsm  = importr('lsmeans')
pairwise = lsm.lsmeans(model, "Valence", contr="pairwise", adjust="holm")
 

That was easy, right. Particularly, if you are used to doing analysis in R. Although, rpy2 is relatively easy to use I don’t think it will replace learning R. That is, you will have to know some R to make use of it. However, if you are a Python programmer and want to use available R-scripts, it might be useful. Noteworthy, I am not aware of any Python implementations of rmANOVA (except for the linear-mixed effects approach maybe). In fact, that is why I learned how to use rpy2 in the first place; to use Python, and R, to conduct the analysis.

那很容易,对。 特别是,如果您习惯于在R中进行分析。尽管rpy2相对易于使用,但我认为它不会代替学习R。也就是说,您必须知道一些R才能使用它。 但是,如果您是Python程序员,并且想使用可用的R脚本,则可能会很有用。 值得注意的是,我不知道rmANOVA的任何Python实现(也许是线性混合效果方法除外)。 实际上,这就是为什么我首先学习如何使用rpy2的原因。 使用Python和R进行分析。

Update: In this rpy2 tutorial you learned how to do a repeated measures ANOVA with Python and R. I have now found a Python package that allows Python ANOVA for within-subjects design (i.e., Python native); see my tutorial Repeated Measures ANOVA using Python.

更新 :在本rpy2教程中,您学习了如何使用Python和R进行重复测量ANOVA。我现在发现了一个Python软件包,该软件包允许Python ANOVA进行主题内设计(即Python本机); 请参阅我的使用Python的重复测量方差分析教程

 

翻译自: https://www.pybloggers.com/2015/12/r-from-python-an-rpy2-tutorial/

python rpy2教程

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值