Press "Enter" to skip to content

Erik Marsja

R from Python – an rpy2 tutorial

rpy2 Tutorial

Recently I found the Python module rpy2. This module offers a Python interface to R. That is, rpy2 enables us to use the power of R in Python!

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 ‘emmeans‘, Python, and rpy2. The post is now updated and you will find a YouTube video going through the rpy2 examples found in this blog post. You will also find another YouTube Video in which you will learn two methods to show R plots inline in Jupyter Notebooks. Make sure you check them out!

R books for Psychologists

R is a free and open-source statistical programming environment. Being open-source and free it has a large and helpful online community (for instance, see StackOverflow).  When I went from carrying out analysis in SPSS to do them in R, I searched for good books targeted to Psychologists.   The following 4 R books are useful and good for Psychologists that want to learn R.
The first book, Discovering Statistics Using R, may be a really good start if you are an undergraduate and have no experience of programming or statistics. The next two books are from intermediate to advanced level. The last book is, at the moment,  free and is also a great introduction to statistics.

Papaja – APA manuscripts made easy

Do you also find it time-consuming to make your manuscripts follow American Psychological Associations (APA) guidelines? Have you searched the internet for a good .docx/.doc APA template? After reading this post, you might not have to search any more. Papaja is an r-package that makes your manuscript conform to APA guidelines! In this post I am briefly going to describe the package and what I think of it.

Some good Python Blogs and Resources

In this post you will find some really good Python Blogs and Resources. Most of these really helped me when moving from proprietary software to free and Open Source software (e.g., PsychoPy written in Python).  The links are divided into two categories: general and research. In the ‘general’ category you will find good Python resources that might be more general and introducing. That is, more helpful for general problems and you might want to start here. In the ‘research’ category there are links to good Python resources that are more specific to use in research (e.g., PsychoPy and data analysis). The links in the ‘research’ category mainly contains links to Python blogs.

Python apps and libraries for creating experiments

In this post, I will describe the existing free Python applications and libraries for creating experiments.  So far, I have only used PsychoPy but I plan to test most of them. At least the ones that seem to still be maintained. All applications and libraries are open-source which makes it possible to download the source code and add your own stuff to it.

Reverse Scoring using R Statistical Environment

This post is my first on R and it will describe a method to reverse scores using R.

Reverse scoring in R

Many instruments (i.e., questionnaires) contain items are phrased so that a strong agreement indicates something negative (e.g., “When there is music in the room I find it hard to concentrate on reading”). These items need to be reversed so that the data will be correct later for statistical analysis.

For more information on reverse scoring, please see my earlier post: Reverse scoring in Python.  Since I was more  familiar with Python  compared to R, and I had no a clue on how to do this in SPSS, I wrote a Python script. The Python script used a function that used Pandas DataFrame and it reversed the scores nice and quickly.

Questionnaire data to be reversed

However, in R was pretty much as simple to do reverse scoring as in Python. In the following script a data frame is generated with column names (i.e., columnNames,’Q1′ to ‘Q6’) and some data is generated using replicate and sample (100 responses, on the 6 questions). After that you will find two methods, that are pretty much the same, for reversing the can be found. The methods only differ in how the columns are selected. The first are select based on the index of the column and the second select based on the column names to be reversed (might be preferable if you know the names of columns but not the indices).

R Script for Reverse Scoring

Free & Useful Software – PsychoPy

PsychoPy

This post will describe why I prefer using PsychoPy before other software. When I started getting involved in research (i.e., when doing my Bachelor’s and Masters theses) I used the software that was accessible for me. That is, some of the most commonly used software at the department I was studying at. After starting my Ph.D. studies I got more and more annoyed with one of the software we used to build experimental tasks  in (i.e., E-prime).

First, I was not familiar with the scripting language (called ‘e-basic’) that was needed, for instance use, to solve pseudo-randomization, and for sending signals to a specially built device I was going to use in my experiments. Second, I prefer to use Linux systems and e-prime is limited to Windows. In fact, e-prime seemed to be working best Windows Vista or earlier but our lab computers were, and are, running Windows 7 (might have changed since back then). Some time into my Ph.D. studies I found PsychoPy which is written in Python, a programming language that I did know, and it seemed like a good alternative to e-prime, and other similar software, to create experiments in.

Another Python script for randomization with constraints

Here you will find a Python function for randomization with constrains. This was written, specifically, for a psychological experiment (a shifting/task-switching task) I was planning to conduct. For this experiment, I needed a list of stimuli names that were quasi-randomized.  Fifty percent of the items in the list are followed by a stimulus in the same colour and the remaining 50 % are followed by a stimulus in a different colour.

Python script