在我问到这个问题之后,这是一个跟进问题:Is there an equivalent function for anova.lm() in Java?。在这些答案的帮助下,当使用拦截进行两个线性模型的Anova时,我可以在Java中获得与R相同的结果。但是,当我从线性模型中删除截距时,残差平方和是相同的,但Java和R中的p值是不同的。
当截距被移除时,是否应该计算FDistribution的不同方式?
R代码
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)
fit2 <- lm(test_trait ~ geno_A + geno_B + geno_A:geno_B)
anova(fit, fit2)
# Res.Df RSS Df Sum of Sq F Pr(>F)
# 1 7 0.77982
# 2 6 0.77053 1 0.0092897 0.0723 0.797
fit <- lm(test_trait ~ geno_A+geno_B -1 )
fit2 <- lm(test_trait ~ geno_A + geno_B + geno_A:geno_B-1)
anova(fit, fit2)
# Res.Df RSS Df Sum of Sq F Pr(>F)
# 1 8 0.78539
# 2 7 0.77080 1 0.014593 0.1325 0.7266
爪哇
double[] y = {-0.48812477, 0.33458213, -0.52754476, -0.79863471, -0.68544309, -0.12970239, 0.02355622, -0.31890850, 0.34725819, 0.08108851};
double[][] x = {{1,0}, {0,0}, {1,0}, {2,1}, {0,1}, {0,0}, {1,0}, {0,0}, {1,0}, {0,0}};
double[][] xb = {{1,0,0}, {0,0,0}, {1,0,0}, {2,1,2}, {0,1,0}, {0,0,0}, {1,0,0}, {0,0,0}, {1,0,0}, {0,0,0}};
OLSMultipleLinearRegression regr = new OLSMultipleLinearRegression();
regr.newSampleData(y, x);
double sumOfSquaresModelA = regr.calculateResidualSumOfSquares();
regr.newSampleData(y, xb);
double sumOfSquaresModelB = regr.calculateResidualSumOfSquares();
int degreesOfFreedomA = y.length - (x[0].length + 1);
int degreesOfFreedomB = y.length - (xb[0].length + 1);
double MSE = sumOfSquaresModelB / degreesOfFreedomB;
System.out.printf("RSS intercept: %f\n",sumOfSquaresModelB);
int degreesOfFreedomDifference = Math.abs(degreesOfFreedomB - degreesOfFreedomA);
double MSEdiff = Math.abs((sumOfSquaresModelB - sumOfSquaresModelA) / (degreesOfFreedomDifference));
double Fval = MSEdiff / MSE;
FDistribution Fdist = new FDistribution(degreesOfFreedomDifference, degreesOfFreedomB);
double pval = 1 - Fdist.cumulative(Fval);
System.out.printf("pval with intercept: %f\n",pval);
regr.setNoIntercept(true);
regr.newSampleData(y, x);
double sumOfSquaresNoInterceptA = regr.calculateResidualSumOfSquares();
regr.newSampleData(y, xb);
double sumOfSquaresNoInterceptB = regr.calculateResidualSumOfSquares();
MSE = sumOfSquaresNoInterceptB / degreesOfFreedomB;
System.out.printf("RSS no intercept: %f\n",sumOfSquaresNoInterceptB);
degreesOfFreedomDifference = Math.abs(degreesOfFreedomB - degreesOfFreedomA);
MSEdiff = Math.abs((sumOfSquaresNoInterceptB - sumOfSquaresNoInterceptA) / (degreesOfFreedomDifference));
Fval = MSEdiff / MSE;
Fdist = new FDistribution(degreesOfFreedomDifference, degreesOfFreedomB);
pval = 1 - Fdist.cumulative(Fval);
System.out.printf("pval without intercept: %f",pval);
结果
RSS intercept: 0.770528 //correct
pval with intercept: 0.796973 //correct
RSS no intercept: 0.770799 //correct
pval without intercept: 0.747564 //wrong
答案 0 :(得分:1)
删除拦截增加了额外的自由度,我没有将其包含在Java代码中。以下确实给出了相同的结果
double[] y = {-0.48812477, 0.33458213, -0.52754476, -0.79863471, -0.68544309, -0.12970239, 0.02355622, -0.31890850, 0.34725819, 0.08108851};
double[][] x = {{1,0}, {0,0}, {1,0}, {2,1}, {0,1}, {0,0}, {1,0}, {0,0}, {1,0}, {0,0}};
double[][] xb = {{1,0,0}, {0,0,0}, {1,0,0}, {2,1,2}, {0,1,0}, {0,0,0}, {1,0,0}, {0,0,0}, {1,0,0}, {0,0,0}};
OLSMultipleLinearRegression regr = new OLSMultipleLinearRegression();
int degreesOfFreedomA = y.length - (x[0].length); // no + 1
int degreesOfFreedomB = y.length - (xb[0].length); // no + 1
regr.setNoIntercept(true);
regr.newSampleData(y, x);
double sumOfSquaresNoInterceptA = regr.calculateResidualSumOfSquares();
regr.newSampleData(y, xb);
double sumOfSquaresNoInterceptB = regr.calculateResidualSumOfSquares();
double MSE = sumOfSquaresNoInterceptB / degreesOfFreedomB;
System.out.printf("RSS no intercept: %f\n",sumOfSquaresNoInterceptB);
int degreesOfFreedomDifference = Math.abs(degreesOfFreedomB - degreesOfFreedomA);
double MSEdiff = Math.abs((sumOfSquaresNoInterceptB - sumOfSquaresNoInterceptA) / (degreesOfFreedomDifference));
double Fval = MSEdiff / MSE;
FDistribution Fdist = new FDistribution(degreesOfFreedomDifference, degreesOfFreedomB);
double pval = 1 - Fdist.cumulative(Fval);
System.out.printf("pval without intercept: %f",pval);
结果
pval without intercept: 0.726572