# HG changeset patch # User nicolas # Date 1477420695 14400 # Node ID 40664d2d295fbb8ce5c21aa6dcd05a583d22af2d # Parent 203872c77910495646e932b60a8b7f4013997b19 Uploaded diff -r 203872c77910 -r 40664d2d295f computeR2.R --- a/computeR2.R Tue Oct 25 14:37:45 2016 -0400 +++ b/computeR2.R Tue Oct 25 14:38:15 2016 -0400 @@ -1,24 +1,26 @@ ######################################################## # # creation date : 27/06/16 -# last modification : 27/06/16 +# last modification : 22/10/16 # author : Dr Nicolas Beaume # owner : IRRI # ######################################################## -log <- file(paste(getwd(), "log_computeR2.txt", sep="/"), open = "wt") -sink(file = log, type="message") - -library("miscTools") -library(randomForest) - -computeR2 <- function(phenotype, prediction) { - return(rSquared(phenotype, (phenotype - prediction))[1,1]) +# compute r2 by computing the classic formula +# compare the sum of square difference from target to prediciton +# to the sum of square difference from target to the mean of the target +computeR2 <- function(target, prediction) { + sst <- sum((target-mean(target))^2) + ssr <- sum((target-prediction)^2) + return(1-ssr/sst) } ############################ main ############################# +# extract argument cmd <- commandArgs(trailingOnly = T) source(cmd[1]) +# load target and prediction phenotype <- read.table(phenotype, sep="\t", h=T)[,1] predicted <- read.table(predicted, sep = "\t", h=T)[,2] +# compute r2 cat(computeR2(phenotype, predicted), file=out) \ No newline at end of file