来自RScript中csv文件的Bland Altman图

时间:2012-12-08 17:35:42

标签: r rscript

我有一个简单的csv文件,其中包含2列数字,标题为“Colli_On”和“Colli_Off”。我写了一个简单的Rscript,它传递了3个参数 - 文件名和列名 - 并希望产生一个Bland Altman图。但是我收到以下错误消息

> Error in plot.window(...) : need finite 'xlim' values
Calls: baplot ... do.call -> plot -> plot.default -> localWindow -> plot.window
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning -Inf

我哪里错了?

   #!/usr/bin/Rscript
# -*- mode: R =*-

#script passes 3 arguments filename and 2 columns and does bland altman analysis
#Example BA /home/moadeep/Data/sehcat.csv Colli_on Colli_off

args <- commandArgs(TRUE)
mydata <- read.csv(file=args[1],head=TRUE,sep="\t")

baplot = function(x,y){

  bamean = (x+y)/2
  badiff = (y-x)

  plot(badiff~bamean, pch=20, xlab="mean", ylab="difference")
# in the following, the deparse(substitute(varname)) is what retrieves the
# name of the argument as data
  title(main=paste("Bland-Altman plot of collimator x and y\n",
    deparse(substitute(x)), "and", deparse(substitute(y)),
    "standardized"), adj=".5")
#construct the reference lines on the fly: no need to save the values in new 
# variable names
  abline(h = c(mean(badiff), mean(badiff)+1.96 * sd(badiff),
    mean(badiff)-1.96 * sd(badiff)), lty=2)
} 

pdf(file="test.pdf")
baplot(mydata$args[2],mydata$argss[3])
dev.off()

1 个答案:

答案 0 :(得分:2)

问题在于这一行:

baplot(mydata$args[2],mydata$argss[3])

我们甚至没有提到拼写错误...当您要求mydata$args[2]时,R会查找名为&#34; args&#34;的列。在您的data.frame中。显然,没有这样的列,所以你得到NULL。从data.frame中提取列的编程方法是使用[。正确的语法应该是:

baplot(mydata[args[2]],mydata[args[3]])

那应该解决你的问题。

(另请注意,[运算符与$不同,如果您尝试提取不存在的列,则会抛出错误:更好的功能恕我直言。)