我有用 R 编写的线性回归代码,我必须在 Java 中做同样的事情。我为此使用了Apache Commons 数学库。我在 R 代码和 Java 代码中使用了相同的数据,但我得到了不同的截距值。我无法弄清楚我在代码中做了什么愚蠢的事情。
代码:
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 <- as.factor(c("Sub_0001"=1, "Sub_0002"=0, "Sub_0003"=1, "Sub_0004"=2, "Sub_0005"=0, "Sub_0006"=0, "Sub_0007"=1, "Sub_0008"=0, "Sub_0009"=1, "Sub_0010"=0))
geno_B <- as.factor(c("Sub_0001"=0, "Sub_0002"=0, "Sub_0003"=0, "Sub_0004"=1, "Sub_0005"=1, "Sub_0006"=0, "Sub_0007"=0, "Sub_0008"=0, "Sub_0009"=0, "Sub_0010"=0) )
fit <- lm(test_trait ~ geno_A*geno_B)
fit
输出:
Call:
lm(formula = test_trait ~ geno_A * geno_B)
Coefficients:
(Intercept) geno_A1 geno_A2 geno_B1
-0.008235 -0.152979 -0.113192 -0.677208
geno_A1:geno_B1 geno_A2:geno_B1
NA NA
Java 代码:
package linearregression;
import org.apache.commons.math3.stat.regression.SimpleRegression;
public class LinearRegression {
public static void main(String[] args) {
double[][] x = {{1,0},
{0,0},
{1,0},
{2,1},
{0,1},
{0,0},
{1,0},
{0,0},
{1,0},
{0,0}
};
double[]y = { -0.48812477,
0.33458213,
-0.52754476,
-0.79863471,
-0.68544309,
-0.12970239,
0.02355622,
-0.31890850,
0.34725819,
0.08108851
};
SimpleRegression regression = new SimpleRegression(true);
regression.addObservations(x,y);
System.out.println("Intercept: \t\t"+regression.getIntercept());
}
}
Java 输出:
Intercept: -0.08732359363636362
我将非常感谢您的帮助。谢谢 !