int main(int argc, char* argv[]) {
  LinearRegression lr;
  LinearRegressionScoreTest lrst;
  LinearRegressionPermutationTest lrpt;

  Vector y;
  Matrix x;

  LoadVector("input.y", y);
  LoadMatrix("input.x", x);

  if (lr.FitLinearModel(x, y) == false) {
    fprintf(stderr, "Fitting failed!\n");
    return -1;
  }

  Vector& beta = lr.GetCovEst();
  Matrix& v = lr.GetCovB();
  Vector& pWald = lr.GetAsyPvalue();

  fprintf(stdout, "wald_beta\t");
  Print(beta);
  fputc('\n', stdout);

  fprintf(stdout, "wald_vcov\t");
  Print(v);
  fputc('\n', stdout);

  fprintf(stdout, "wald_p\t");
  Print(pWald[1]);
  fputc('\n', stdout);

  if (lrpt.FitLinearModel(x, 1, y, 200, 0.05) == false) {
    fprintf(stderr, "Fitting failed!\n");
    return -1;
  }

  fprintf(stdout, "permutation_p\t");
  double permu_p = lrpt.getPvalue();
  Print(permu_p);
  fputc('\n', stdout);

  if (lrst.FitLinearModel(x, y, 1) == false) {
    fprintf(stderr, "Fitting failed!\n");
    return -1;
  }

  fprintf(stdout, "score_p\t");
  double score_p = lrst.GetPvalue();
  Print(score_p);
  fputc('\n', stdout);

  return 0;
};
Exemple #2
0
int DataLoader::useResidualAsPhenotype() {
  if (binaryPhenotype) {
    logger->warn(
        "WARNING: Skip transforming binary phenotype, although you want to "
        "use residual as phenotype!");
    return 0;
  }

  LinearRegression lr;
  Vector pheno;
  Matrix covAndInt;
  const int numCovariate = covariate.ncol();

  copyPhenotype(phenotype, &pheno);
  copyCovariateAndIntercept(pheno.Length(), covariate, &covAndInt);
  if (!lr.FitLinearModel(covAndInt, pheno)) {
    if (numCovariate > 0) {
      logger->error(
          "Cannot fit model: [ phenotype ~ 1 + covariates ], now use the "
          "original phenotype");
    } else {
      logger->error(
          "Cannot fit model: [ phenotype ~ 1 ], now use the "
          "original phenotype");
    }
  } else {  // linear model fitted successfully
    copyVectorToMatrixColumn(lr.GetResiduals(), &phenotype, 0);
    // const int n = lr.GetResiduals().Length();
    // for (int i = 0; i < n; ++i) {
    //   // phenotypeInOrder[i] = lr.GetResiduals()[i];
    //   phenotype[i][0] = lr.GetResiduals()[i];
    // }
    covariate.clear();
    if (numCovariate > 0) {
      logger->info(
          "DONE: Fit model [ phenotype ~ 1 + covariates ] and model "
          "residuals will be used as responses");
    } else {
      logger->info("DONE: Use residual as phenotype by centerng it");
    }

    // store fitting results
    Vector& beta = lr.GetCovEst();
    Matrix& betaSd = lr.GetCovB();
    const int n = beta.Length();
    for (int i = 0; i < n; ++i) {
      addFittedParameter(covAndInt.GetColumnLabel(i), beta[i], betaSd[i][i]);
    }
    addFittedParameter("Sigma2", lr.GetSigma2(), NAN);
  }

#if 0
  if (covariate.ncol() > 0) {
    LinearRegression lr;
    Vector pheno;
    Matrix covAndInt;
    copyPhenotype(phenotype, &pheno);
    copyCovariateAndIntercept(covariate.nrow(), covariate, &covAndInt);
    if (!lr.FitLinearModel(covAndInt, pheno)) {
      logger->error(
          "Cannot fit model: [ phenotype ~ 1 + covariates ], now use the "
          "original phenotype");
    } else {
      const int n = lr.GetResiduals().Length();
      for (int i = 0; i < n; ++i) {
        // phenotypeInOrder[i] = lr.GetResiduals()[i];
        phenotype[i][0] = lr.GetResiduals()[i];
      }
      covariate.clear();
      logger->info(
          "DONE: Fit model [ phenotype ~ 1 + covariates ] and model "
          "residuals will be used as responses");
    }
    storeFittedModel(lr);
  } else {  // no covaraites
    // centerVector(&phenotypeInOrder);
    std::vector<double> v;
    phenotype.extractCol(0, &v);
    centerVector(&v);
    phenotype.setCol(0, v);

    logger->info("DONE: Use residual as phenotype by centerng it");
  }
#endif

  return 0;
}