python 方差分析

A common method in experimental psychology is within-subjects designs. One way to analysis the data collected using within-subjects designs are using repeated measures ANOVA. I recently wrote a post on how to conduct a repeated measures ANOVA using Python and rpy2. I wrote that post since the great Python package statsmodels do not include repeated measures ANOVA. However, the approach using rpy2 requires R statistical environment installed.  Recently, I found a python library called pyvttbl whith which you can do within-subjects ANOVAs.  Pyvttbl enables you to create multidimensional pivot tables, process data and carry out statistical tests. Using the method anova on pyvttbl’s DataFrame we can carry out repeated measures ANOVA using only Python.

实验心理学中的一种常见方法是受试者内部设计 。 分析使用受试者内部设计收集的数据的一种方法是使用重复测量方差分析 。 我最近写了一篇关于如何使用Python和rpy2进行重复测量方差分析的文章 。 我写这篇文章是因为出色的Python软件包statsmodels不包含重复测量方差分析。 但是,使用rpy2的方法需要安装R统计环境。 最近,我发现了一个名为pyvttbl的python库,您可以在主题内进行方差分析。 Pyvttbl使您可以创建多维数据透视表,处理数据并进行统计测试。 使用pyvttbl的DataFrame上的方差分析方法,我们可以仅使用Python进行重复测量方差分析。

为什么在主题设计中? (Why within subject designs?)

There are, at least, two of the advantages using within-subjects design. First, more information is obtained from each subject in a within-subjects design compared to a between-subjects design. Each subject is measured in all conditions, whereas in the between-subjects design, each subject is typically measured in one or more but not all conditions. A within-subject design thus requires fewer subjects to obtain a certain level of statistical power. In situations where it is costly to find subjects this kind of design is clearly better than a between-subjects design. Second, the variability in individual differences between subjects is removed from the error term. That is, each subject is his or her own control and extraneous error variance is reduced.

使用主题内设计至少有两个优点。 首先,与受试者之间的设计相比,受试者内部设计可从每个受试者获得更多信息。 在所有条件下对每个对象进行测量,而在对象间设计中,通常在一个或多个但不是所有条件下对每个对象进行测量。 因此,受试者内部的设计需要较少的受试者来获得一定水平的统计能力。 在找不到主题的成本很高的情况下,这种设计显然比主题间设计更好。 第二,从误差项中消除了受试者之间个体差异的变异性。 即,每个对象都是他或她自己的控制,并且减少了无关的误差方差。

Python中的重复测量方差分析 (Repeated measures ANOVA in Python)

安装pyvttbl (Installing pyvttbl)

pyvttbl can be installed using pip:

可以使用pip安装pyvttbl:

pip install pyvttblpip install pyvttbl 

If you are using Linux you may need to add ‘sudo’ before the pip command. This method installs pyvttbl and, hopefully, any missing dependencies.

如果您使用的是Linux,则可能需要在pip命令之前添加“ sudo”。 此方法将安装pyvttbl,并希望安装所有缺少的依赖项。

Python脚本 (Python script)

I continue with simulating a response time data set. If you have your own data set you want to do your analysis on you can use the method “read_tbl” to load your data from a CSV-file.

我继续模拟响应时间数据集。 如果您拥有自己的数据集,则可以使用“ read_tbl ”方法从CSV文件加载数据,以进行分析。

Conducting the repeated measures ANOVA with pyvttbl is pretty straight forward. You just take the pyvttbl DataFrame object and use the method anova. The first argument is your dependent variable (e.g. response time), and you specify the column in which the subject IDs are (e.g., sub=’Sub_id’). Finally, you add your within subject factor(s) (e.g., wfactors). wfactors take a list of column names containing your within subject factors. In my simulated data there is only one (e.g. ‘condition’).

用pyvttbl进行重复测量ANOVA非常简单。 您只需获取pyvttbl DataFrame对象并使用anova方法。 第一个参数是您的因变量(例如响应时间),并且您指定主题ID所在的列(例如sub ='Sub_id')。 最后,您添加您的主题因素(例如wfactor)。 wfactors列出包含您的主题内因素的列名列表。 在我的模拟数据中,只有一个(例如“条件”)。

aov = df.anova('rt', sub='Sub_id', wfactors=['condition'])
print(aov)
aov = df.anova('rt', sub='Sub_id', wfactors=['condition'])
print(aov)
 

主体内效应测试 (Tests of Within-Subjects Effects)

度量:rt (Measure: rt)
Source 资源    Type III Sum of Squares III型平方和 ε ε df df MS 多发性硬化症 F F Sig. 签名 η2G η2 G Obs. 观察 SE of x̄ x̄的SE ±95% CI ±95%CI λ λ Obs. Power 观察 功率
condition 健康)状况 Sphericity Assumed 假定的球形度 4209536.428 4209536.428 1.000 1.000 4209536.428 4209536.428 309.093 309.093 0.000 0.000 4.165 4.165 40.000 40.000 19.042 19.042 37.323 37.323 317.019 317.019 1.000 1.000
Greenhouse-Geisser 温室盖瑟 4209536.428 4209536.428 1.000 1.000 1.000 1.000 4209536.428 4209536.428 309.093 309.093 0.000 0.000 4.165 4.165 40.000 40.000 19.042 19.042 37.323 37.323 317.019 317.019 1.000 1.000
Huynh-Feldt 休恩·费尔德 4209536.428 4209536.428 1.000 1.000 1.000 1.000 4209536.428 4209536.428 309.093 309.093 0.000 0.000 4.165 4.165 40.000 40.000 19.042 19.042 37.323 37.323 317.019 317.019 1.000 1.000
Box 4209536.428 4209536.428 1.000 1.000 1.000 1.000 4209536.428 4209536.428 309.093 309.093 0.000 0.000 4.165 4.165 40.000 40.000 19.042 19.042 37.323 37.323 317.019 317.019 1.000 1.000
Error(condition) 错误(条件) Sphericity Assumed 假定的球形度 531140.646 531140.646 39.000 39.000 13618.991 13618.991                        
Greenhouse-Geisser 温室盖瑟 531140.646 531140.646 1.000 1.000 39.000 39.000 13618.991 13618.991                        
Huynh-Feldt 休恩·费尔德 531140.646 531140.646 1.000 1.000 39.000 39.000 13618.991 13618.991                        
Box 531140.646 531140.646 1.000 1.000 39.000 39.000 13618.991 13618.991                        

As can be seen in the output table the Sum of Squares used is Type III which is what common statistical software use when calculating ANOVA (the F-statistic) (e.g., SPSS or R-packages such as ‘afex’ or ‘ez’). The table further contains correction in case our data violates the assumption of Sphericity (which in the case of only 2 factors, as in the simulated data, is nothing to worry about). As you can see we also get generalized eta squared as effect size measure and 95 % Confidence Intervals. It is stated in the docstring for the class Anova that standard Errors and 95% confidence intervals are calculated according to Loftus and Masson (1994). Furthermore, generalized eta squared allows comparability across between-subjects and within-subjects designs (see, Olejnik & Algina, 2003).

从输出表中可以看出,使用的平方和是III型 ,这是计算ANOVA(F统计量)时常用的统计软件(例如SPSS或R-package,例如“ afex”或“ ez”) 。 该表还包含更正,以防我们的数据违反“球形性”的假设(在模拟数据中只有2个因素的情况下,无需担心)。 如您所见,我们还获得了广义的eta平方作为效果量度和95%的置信区间。 在Anova类的文档字符串中指出,标准误差和95%置信区间是根据Loftus和Masson(1994)计算的。 此外,广义eta平方可以实现对象间和对象内设计之间的可比性(请参阅Olejnik&Algina,2003)。

Conveniently, if you ever want to transform your data you can add the argument transform. There are several options here; log or log10, reciprocal or inverse, square-root or sqrt, arcsine or arcsin, and windsor10. For instance, if you want to use log-transformation you just add the argument “transform=’log’” (either of the previously mentioned methods can be used as arguments in string form):

方便地,如果您要转换数据,可以添加参数transform。 这里有几种选择。 log或log10,倒数或倒数,平方根或sqrt,反正弦或反正弦以及windsor10。 例如,如果要使用对数转换,则只需添加参数“ transform ='log'”(前面提到的两种方法都可以用作字符串形式的参数):

Using pyvttbl we can also analyse mixed-design/split-plot (within-between) data. Doing a split-plot is easy; just add the argument “bfactors=” and a list of your between-subject factors. If you are interested in one-way ANOVA for independent measures see my newer post: Four ways to conduct one-way ANOVAS with Python.

使用pyvttbl,我们还可以分析混合设计/分割图(中间)数据。 进行分割图很容易; 只需添加参数“ bfactors =”和主题间因素列表即可。 如果您对单向ANOVA的独立测量感兴趣,请参阅我的新文章: 用Python进行单向ANOVAS的四种方法。

Finally, I created a function that extracts the F-statistics, Mean Square Error, generalized eta squared, and the p-value the results obtained with the anova method.  It takes a factor as a string, a ANOVA object, and the values you want to extract. Keys for your different factors can be found using the key-method (e.g., aov.keys()).

最后,我创建了一个函数,该函数提取F统计量,均方误差,广义eta平方和p值(使用方差分析方法获得的结果)。 它采用一个因子作为字符串,ANOVA对象和要提取的值。 可以使用密钥方法找到不同因素的密钥(例如aov.keys())。

def extract_for_apa(factor, aov, values = ['F', 'mse', 'eta', 'p']):
results = {}
for key,result in aov[(factor,)].iteritems():
if key in values:
results[key] = result
return results
def extract_for_apa(factor, aov, values = ['F', 'mse', 'eta', 'p']):
results = {}
for key,result in aov[(factor,)].iteritems():
if key in values:
results[key] = result
return results
 

Note, the table with the results in this post was created with the private method _within_html. To create an HTML table you will have to import SimpleHTML:

请注意,本文中包含结果的表是使用_within_html私有方法创建的。 要创建HTML表,您将必须导入SimpleHTML:

That was all. There are at least one downside with using pyvttbl for doing within-subjects analysis in Python (ANOVA). Pyvttbl is not compatible with Pandas DataFrame which is commonly used. However, this may not be a problem since pyvttbl, as we have seen, has its own DataFrame method. There are also a some ways to aggregate and visualizing data using Pyvttbl. Another downside is that it seems like Pyvttbl no longer is maintained. You can find Pyvttbl documentation here.

这就是全部了。 使用pyvttbl在Python(ANOVA)中进行对象内分析至少有一个缺点。 Pyvttbl与常用的Pandas DataFrame不兼容。 但是,由于pyvttbl具有自己的DataFrame方法,因此这可能不是问题。 还有一些使用Pyvttbl聚合和可视化数据的方法。 另一个缺点是,似乎Pyvttbl不再被维护。 您可以在此处找到Pyvttbl文档。

参考资料 (References)

翻译自: https://www.pybloggers.com/2016/02/repeated-measures-anova-using-python/

python 方差分析

python 方差分析_使用Python的重复测量方差分析相关推荐

  1. 使用Statsmodels在Python中进行重复测量方差分析

    In this brief Python data analysis tutorial we will learn how to carry out a repeated measures ANOVA ...

  2. 三因素方差分析_重复测量方差分析的操作教程及结果解读

    内容来自:"小白学统计"微信公众号,感谢作者授权. 重复测量数据是指对同一个体在不同时间点的测量,这种数据在医学研究中较为常见,比较典型的数据形式如: 对一组人群分别在干预前后不同 ...

  3. R语言单因素重复测量方差分析(one-way repeated measures ANOVA)实战

    R语言单因素重复测量方差分析(one-way repeated measures ANOVA)实战 目录 R语言单因素重复测量方差分析(one-way repeated measures ANOVA) ...

  4. spss方差分析_SPSS单因素重复测量方差分析

    什么是重复测量方差分析? 重复测量是指在不同的时间点上对同一对象的同一观察指标进行多次测量,重复测量设计是科研工作中常见的设计方法,常用来分析在不同时间点上某指标的差异. 重复测量设计的优缺点是什么? ...

  5. 跟着这篇柳叶刀的文章来学会重复测量方差分析

    重复测量资料的统计分析与生存分析,堪称当前临床试验两类唱主角的方法.相比生存分析,重复测量资料的分析方法更为复杂. 重复测量方差分析是一种最基本的方法之一了,不过它也不简单.能真正掌握重复测量方差分析 ...

  6. 如何进行SPSS的重复测量方差分析

    IBM SPSS Statistics的重复测量方差分析,为重复测量的数据提供了单变量与多变量的方差分析,其分析的是多次测量数据之间存在的相关关系,与单因素或多元素方差分析不同的是,其不同测量数据之间 ...

  7. R语言重复测量方差分析

    一.概念 重复测量指的是对同一观察对象的同一观察指标在不同的时间点上进行多次测量.用于分析观察指标在不同时间上的变化规律.这类资料在医学研究中比较常见. 在实际工作当中,重复测量资料常被误认为是配对设 ...

  8. 解读SPSS重复测量方差分析的检验结果

    重复测量方差分析是对一个因变量重复测量,并分析测量值之间相关关系的分析方法.在<如何进行SPSS的重复测量方差分析>一文中,我们已经详细学习了IBM SPSS Statistics重复测量 ...

  9. RSPSS重复测量方差分析

    最近在做重复测量方差分析,真的是走了很多弯路,足足花费了我两周的时间,因此在此写一篇博文,希望能给其他人提供一些参考. 先说建议: 建议使用SPSS,不要使用R,会省很多精力,我用R做了3天,失败了, ...

最新文章

  1. PostGIS之路——线性参考
  2. Spring Boot 之路(一):一个简单的Spring Boot应用
  3. lotus表单域怎么获取视图结果_翻译案例 | 商业推广类文章怎么翻?如何修改译文?...
  4. ios 请求头设置token_HTTP中的OPTIONS请求
  5. java 运行环境注册表_Java运行环境与Windows注册表
  6. 「分块」数列分块入门1 – 9
  7. matlab中GUIDE的UItable居中方法
  8. python数据分析实况_Python数据分析实战:降雨量统计分析报告分析
  9. tp3.2 批量插入
  10. Vue.js身份证读卡器阅读器谷歌chrome火狐Firefox网页实现读取方法
  11. 项目智能化工程资格预审公告
  12. 七月刚入职字节跳动的测试开发面试题,附答案
  13. 微信网页开发(3)--微信网页授权
  14. 【PIMF】OpenHarmony啃论文俱乐部:拼音输入法原理_从触摸事件到汉字
  15. mysql fprintf_matlab中fprintf函数的用法详解
  16. python安装pip之后镜像源配置
  17. Ansible Inventory详细使用介绍
  18. 【表面缺陷检测】基于yolov5的布匹表面缺陷检测(附代码和数据集)
  19. MobaXterm在4K屏幕下的兼容性设置
  20. python地址怎么写_python文件地址(文件路径)怎么写

热门文章

  1. 米转经纬度_经纬度换算米(经纬度精度换算米数)
  2. 高效记忆/形象记忆(10)英语单词记忆-音标法
  3. paper read - 01- 2004 - 语码转换之结构研究述评
  4. JavaSE探赜索隐之乾坤袋(集合)
  5. Elasticsearch:创建属于自己的 Ingest processor
  6. php 微信表情存储,轻松处理PHP开发中微信emoji表情mysql存储的问题
  7. jacob java excel_Java使用jacob将微软office中word、excel、ppt转成pdf
  8. ERDAS将DN值转换为TOA光谱辐射亮度时 出现建模错误原因
  9. 蓬莱小课:零基础应届生如何转行学数据分析师?
  10. 使用Python进行交易策略和投资组合分析