| 
71
 | 
     1 ########################################################
 | 
| 
 | 
     2 #
 | 
| 
 | 
     3 # creation date : 27/06/16
 | 
| 
 | 
     4 # last modification : 22/10/16
 | 
| 
 | 
     5 # author : Dr Nicolas Beaume
 | 
| 
 | 
     6 # owner : IRRI
 | 
| 
 | 
     7 #
 | 
| 
 | 
     8 ########################################################
 | 
| 
 | 
     9 
 | 
| 
 | 
    10 # compute r2 by computing the classic formula
 | 
| 
 | 
    11 # compare the sum of square difference from target to prediciton
 | 
| 
 | 
    12 # to the sum of square difference from target to the mean of the target
 | 
| 
 | 
    13 computeR2 <- function(target, prediction) {
 | 
| 
 | 
    14   sst <- sum((target-mean(target))^2)
 | 
| 
 | 
    15   ssr <- sum((target-prediction)^2)
 | 
| 
 | 
    16   return(1-ssr/sst)
 | 
| 
 | 
    17 }
 | 
| 
 | 
    18 ############################ main #############################
 | 
| 
 | 
    19 # extract argument
 | 
| 
 | 
    20 cmd <- commandArgs(trailingOnly = T)
 | 
| 
 | 
    21 source(cmd[1])
 | 
| 
 | 
    22 # load target and prediction
 | 
| 
 | 
    23 phenotype <- read.table(phenotype, sep="\t", h=T)[,1] 
 | 
| 
 | 
    24 predicted <- read.table(predicted, sep = "\t", h=T)[,2]
 | 
| 
 | 
    25 # compute r2
 | 
| 
 | 
    26 cat(computeR2(phenotype, predicted), file=out) |