/** * @param ci * @param useWeighted * @param robust * @param cluster * @throws Exception */ public void coxphfitSCleanup( CoxInfo ci, boolean useWeighted, boolean robust, ArrayList<String> cluster) throws Exception { // Do cleanup found after coxfit6 is called in coxph.fit.S // infs <- abs(coxfit$u %*% var) // [ a1 b1] * [a1 b1] // [a2 b2] double[][] du = new double[1][ci.u.length]; du[0] = ci.u; double[] infs = Matrix.abs(Matrix.multiply(ci.u, ci.getVariance())); // StdArrayIO.print(infs); ArrayList<CoxCoefficient> coxCoefficients = new ArrayList<CoxCoefficient>(ci.getCoefficientsList().values()); for (int i = 0; i < infs.length; i++) { double inf = infs[i]; double coe = coxCoefficients.get(i).getCoeff(); if (inf > ci.eps && inf > (ci.toler * Math.abs(coe))) { ci.message = "Loglik converged before variable "; } } // sum(coef*coxfit$means) double sumcoefmeans = 0; for (CoxCoefficient cc : coxCoefficients) { sumcoefmeans = sumcoefmeans + cc.getCoeff() * cc.getMean(); } // coxph.fit.S line 107 // lp <- c(x %*% coef) + offset - sum(coef*coxfit$means) for (SurvivalInfo si : ci.survivalInfoList) { double offset = si.getOffset(); double lp = 0; for (CoxCoefficient cc : coxCoefficients) { String name = cc.getName(); double coef = cc.getCoeff(); double value = si.getVariable(name); lp = lp + value * coef; } lp = lp + offset - sumcoefmeans; si.setLinearPredictor(lp); si.setScore(Math.exp(lp)); // System.out.println("lp score " + si.order + " " + si.time + " " + si.getWeight() // + " " + si.getClusterValue() + " " + lp + " " + Math.exp(lp)); } // ci.dump(); // begin code after call to coxfit6 in coxph.fit.S // Compute the martingale residual for a Cox model // appears to be C syntax error for = - vs -= // (if (nullmodel) in coxph.fit double[] res = CoxMart.process(ci.method, ci.survivalInfoList, false); for (int i = 0; i < ci.survivalInfoList.size(); i++) { SurvivalInfo si = ci.survivalInfoList.get(i); si.setResidual(res[i]); } // this represents the end of coxph.fit.S code and we pickup // after call to fit <- fitter(X, Y, strats ....) in coxph.R if (robust) { ci.setNaiveVariance(ci.getVariance()); double[][] temp; double[][] temp0; if (cluster != null) { temp = ResidualsCoxph.process(ci, ResidualsCoxph.Type.dfbeta, useWeighted, cluster); // # get score for null model // if (is.null(init)) // fit2$linear.predictors <- 0*fit$linear.predictors // else // fit2$linear.predictors <- c(X %*% init) // Set score to 1 double[] templp = new double[ci.survivalInfoList.size()]; double[] tempscore = new double[ci.survivalInfoList.size()]; int i = 0; for (SurvivalInfo si : ci.survivalInfoList) { templp[i] = si.getLinearPredictor(); tempscore[i] = si.getScore(); si.setLinearPredictor(0); si.setScore(1.0); // this erases stored value which isn't how the R code does it i++; } temp0 = ResidualsCoxph.process(ci, ResidualsCoxph.Type.score, useWeighted, cluster); i = 0; for (SurvivalInfo si : ci.survivalInfoList) { si.setLinearPredictor(templp[i]); si.setScore(tempscore[i]); // this erases stored value which isn't how the R code does it i++; } } else { temp = ResidualsCoxph.process(ci, ResidualsCoxph.Type.dfbeta, useWeighted, null); // fit2$linear.predictors <- 0*fit$linear.predictors double[] templp = new double[ci.survivalInfoList.size()]; double[] tempscore = new double[ci.survivalInfoList.size()]; int i = 0; for (SurvivalInfo si : ci.survivalInfoList) { templp[i] = si.getLinearPredictor(); tempscore[i] = si.getScore(); si.setLinearPredictor(0); si.setScore(1.0); } temp0 = ResidualsCoxph.process(ci, ResidualsCoxph.Type.score, useWeighted, null); i = 0; for (SurvivalInfo si : ci.survivalInfoList) { si.setLinearPredictor(templp[i]); si.setScore(tempscore[i]); // this erases stored value which isn't how the R code does it i++; } } // fit$var<- t(temp) % * % temp double[][] ttemp = Matrix.transpose(temp); double[][] var = Matrix.multiply(ttemp, temp); ci.setVariance(var); // u<- apply(as.matrix(temp0), 2, sum) double[] u = new double[temp0[0].length]; for (int i = 0; i < temp0[0].length; i++) { for (int j = 0; j < temp0.length; j++) { u[i] = u[i] + temp0[j][i]; } } // fit$rscore <- coxph.wtest(t(temp0)%*%temp0, u, control$toler.chol)$test double[][] wtemp = Matrix.multiply(Matrix.transpose(temp0), temp0); double toler_chol = 1.818989e-12; // toler_chol = ci.toler; WaldTestInfo wti = WaldTest.process(wtemp, u, toler_chol); // not giving the correct value ci.setRscore(wti.getTest()); } if (ci.getNumberCoefficients() > 0) { double toler_chol = 1.818989e-12; // toler_chol = ci.toler; double[][] b = new double[1][ci.getNumberCoefficients()]; int i = 0; for (CoxCoefficient coe : ci.getCoefficientsList().values()) { b[0][i] = coe.getCoeff(); i++; } ci.setWaldTestInfo(WaldTest.process(ci.getVariance(), b, toler_chol)); } }