Linear Regression Of Interacting Genotypes In Java
1
2
Entering edit mode
11.5 years ago
samsara ▴ 630

I have a code in R for linear regression of two interacting genotypes geno_A and geno_B. I have to implement the same thing in Java. I tried Apache Commons Math Library. However, I am unable to regress test_trait neither on geno_A and geno_B nor on their interaction. I was only able to regress test_trait on either geno_A or geno_B which really does not solve the problem. I also tried Weka, but the case was the same.

R Code:

test_trait <- c( -0.48812477 , 0.33458213, -0.52754476, -0.79863471, -0.68544309, -0.12970239,  0.02355622, -0.31890850,0.34725819 , 0.08108851)
geno_A <- c(1, 0, 1, 2, 0, 0, 1, 0, 1, 0)
geno_B <- c(0, 0, 0, 1, 1, 0, 0, 0, 0, 0)
fit <- lm(test_trait ~ geno_A*geno_B)
fit


java r • 3.8k views
3
Entering edit mode
11.5 years ago
Johan ▴ 890

Here's a example replicating your R code in Java using Apache Commons Math Library. The key is the data transformation (which I did manually in the code below) to representing each data point as (A,B,A*B). Hope this helps. :)

import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression;

public class RegressionSandbox {

public static void main(String[] args) {

OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();

// Instantiate data
double[] y = new double[] { -0.48812477, 0.33458213, -0.52754476,
-0.79863471, -0.68544309, -0.12970239, 0.02355622, -0.31890850,
0.34725819, 0.08108851 };
double[][] x = new double[10][];
// Note that I have converted so that each data point is made up of
// Genotype A, Genotype B, Genotype A * Genotype B
x[0] = new double[] { 1, 0, 0 };
x[1] = new double[] { 0, 0, 0 };
x[2] = new double[] { 1, 0, 0 };
x[3] = new double[] { 2, 1, 2 };
x[4] = new double[] { 0, 1, 0 };
x[5] = new double[] { 0, 0, 0 };
x[6] = new double[] { 1, 0, 0 };
x[7] = new double[] { 0, 0, 0 };
x[8] = new double[] { 1, 0, 0 };
x[9] = new double[] { 0, 0, 0 };

// Add the data to the regression model
regression.newSampleData(y, x);

// Get the regression parameters and R-square value and print it.
double[] beta = regression.estimateRegressionParameters();
double rSquared = regression.calculateRSquared();

System.out.println("Regression parameters: ");
for (int i = 0; i < beta.length; i++) {
System.out.println(beta[i]);
}

System.out.println("rSquared: " + rSquared);
}

}

0
Entering edit mode

0
Entering edit mode

Do you have any idea, how can i calcualte Java equavalent of R code anova(fit) ?

0
Entering edit mode

I've never done it. But a quick google for ANOVA and java shows that there seems to be some libraries out there for doing it. For example this: http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/stat/inference/OneWayAnova.html Further than that I'm afraid I can't help.