This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Linear Regression Of Interacting Genotypes In Java

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

I will greatly appreciate your help. Thanks in advance.

java r

1 answer

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);
    }

}

Thank you very much for your answer.

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

Log in to answer this question.