Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Monday, January 23, 2012

Bland - Altman plot in R

Not so long time ago I did a comparison study of two software packages, more precisely their ability to estimate random effects. The plain approach was to do correlations among the outcomes, and if they were high I assumed that both programs are OK. I presented this to my more experienced colleagues who suggested to use Bland-Altman plots to confirm the results. This is a reasonably simple technique to measure the agreement of the two outcomes. As they say in their paper Statistical methods for assessing agreement between two methods of clinical measurement (pdf linked, worthwile to read):

In clinical measurement comparison of a new measurement technique with an established one is often needed to see whether they agree sufficiently for the new to replace the old. Such investigations are often analysed inappropriately, notably by using correlation coefficients.The use of correlation is misleading. An alternative approach, based on graphical techniques and simple calculations, is described, together with the relation between this analysis and the assessment of repeatability.

 Simple, yet beautifull technique.

Naturally I was searching for implementartion in R. I quickly found and installed a package called ResearchMethods, but personally I thought it has very few soft tuning possibilities (e.g. scaling or color change). I went deeper and found a quite well documented page with a custom/modified R function. 

I used this one, as I could scale the y axis, so the final plot was nicer. On the other hand I also found that this particular function was written for demonstration purposes, but it had difficulties to run on other data.

For example the limits of the y axis were hard coded to -60, 60, which is quite problematic if you are interested in much higher or smaller differences (In my case on the 0.01 level...). Also the data set names were hard coded into the funtion.

So I modified the code to a more generic function like this:

BAplot <- function(x,y,yAxisLim=c(-1,1),xlab="Average", ylab="Difference") {
   d <- ((x + y)/2)
   diff <- x - y        
   plot(diff ~ d,pch=16,ylim=yAxisLim,xlab=xlab,ylab=ylab)
   abline(h=mean(diff)-c(-2,0,2)*sd(diff),lty=2)
}
 You call it as:
BAplot(testSet1,testSet2,yAxisLim=c(-0.1,0.1))

The "testSet"s are the datasets to compare, the yAxisLim modifies the scaling of the axis according to your needs. You might modify the labels of x and y axis if you wish with xlab and ylab.


Monday, November 28, 2011

R crash course

This is a follow up post for my R course held in Addis Ababa last week. You can find the description of the stay in the Inside Ethiopia (part 1) and Inside Ethiopia (part 2) blogposts, if interested. 

This post is a shorter and more practical one. I promised to multiple people to put the presentations online, so I thought I will make them available through my blog in combination with my Dropbox account. Here they are:

  1. Introduction to R
  2. Basic commands of R
  3. Statistics and programming in R

The other links I am referring to in the presentations are An Introduction to R, the R reference card and the other variations of cheat sheets.

You are free to re use and replicate the presenations, provided that the initial work is acknowledged.

Have fun with R! :)


Wednesday, July 20, 2011

Learning Python

Today I started to learn a new programming language, Python. The reason for this extra activity is that I am attending the Synbreed Summer School "Next Generation Sequence Analysis: Practice and Departure to New Frontiers". In the requrements they said that knowledge of Linux and a scripting language (e.g. Phyton) is required. I am so-so with Linux but had zero idea about Python until today. Also because it is not sure if I coud get away with my current knowledge, and might well be that we will be bombarded by Python for 8 days, I thought I should have a look at it...


First stop was of course the site-that-knows-everything and the Python wiki. I realized quite quickly that there is a lot of the stuff online as usual, so I used the wiki on hints of installing Python to my notebook and went right over to Youtube. There I have found last years Google Python class (the first video of the two days course below + support materials).
Also there was a longer series of short videos dealing with introduction to Python - video attached. Looks good as well. First I will go through this one and then see what the Google-guy says. A bit of repeating does not hurt, especially when one want to learn something...

Here are the introductory videos to the two series. By chance do you know about other sources or sites with some nice example codes?