示例#1
0
double regression(int Nind, int Nmark, cvector cofactor, MQMMarkerMatrix marker, vector y,
                  vector *weight, ivector ind, int Naug, double *variance,
                  vector Fy, bool biasadj, bool fitQTL, bool dominance, bool verbose) {
  debug_trace("regression IN\n");
  /*
  cofactor[j] at locus j:
  MNOCOF: no cofactor at locus j
  MCOF: cofactor at locus j
  MSEX: QTL at locus j, but QTL effect is not included in the model
  MQTL: QTL at locu j and QTL effect is included in the model
  */

  //Calculate the dimensions of the designMatrix
  int dimx=designmatrixdimensions(cofactor,Nmark,dominance);
  int j, jj;
  const int dimx_alloc = dimx+2;
  //Allocate structures
  matrix  XtWX = newmatrix(dimx_alloc, dimx_alloc);
  cmatrix Xt   = newcmatrix(dimx_alloc, Naug);
  vector  XtWY = newvector(dimx_alloc);
  //Reset dimension designmatrix
  dimx = 1;
  for (j=0; j<Nmark; j++){
    if ((cofactor[j]==MCOF)||(cofactor[j]==MQTL)) dimx+= (dominance ? 2 : 1);
  }
  cvector xtQTL = newcvector(dimx);
  int jx=0;
  for (int i=0; i<Naug; i++) Xt[jx][i]= MH;
  xtQTL[jx]= MNOCOF;

  for (j=0; j<Nmark; j++)
    if (cofactor[j]==MCOF) { // cofactor (not a QTL moving along the chromosome)
      jx++;
      xtQTL[jx]= MCOF;
      if (dominance) {
        for (int i=0; i<Naug; i++)
          if (marker[j][i]==MH) {
            Xt[jx][i]=48;  //ASCII code 47, 48 en 49 voor -1, 0, 1;
            Xt[jx+1][i]=49;
          } else if (marker[j][i]==MAA) {
            Xt[jx][i]=47;  // '/' stands for -1
            Xt[jx+1][i]=48;
          } else {
            Xt[jx][i]=49;
            Xt[jx+1][i]=48;
          }
        jx++;
        xtQTL[jx]= MCOF;
      } else {
        for (int i=0; i<Naug; i++) {
          if (marker[j][i]==MH) {
            Xt[jx][i]=48;  //ASCII code 47, 48 en 49 voor -1, 0, 1;
          } else if (marker[j][i]==MAA) {
            Xt[jx][i]=47;  // '/' stands for -1
          } else                        {
            Xt[jx][i]=49;
          }
        }
      }
    } else if (cofactor[j]==MQTL) { // QTL
      jx++;
      xtQTL[jx]= MSEX;
      if (dominance) {
        jx++;
        xtQTL[jx]= MQTL;
      }
    }

  //Rprintf("calculate xtwx and xtwy\n");
  /* calculate xtwx and xtwy */
  double xtwj, yi, wi, calc_i;
  for (j=0; j<dimx; j++) {
    XtWY[j]= 0.0;
    for (jj=0; jj<dimx; jj++) XtWX[j][jj]= 0.0;
  }
  if (!fitQTL){
    for (int i=0; i<Naug; i++) {
      yi= y[i];
      wi= (*weight)[i];
      //in the original version when we enable Dominance , we crash around here
      for (j=0; j<dimx; j++) {
        xtwj= ((double)Xt[j][i]-48.0)*wi;
        XtWY[j]+= xtwj*yi;
        for (jj=0; jj<=j; jj++) XtWX[j][jj]+= xtwj*((double)Xt[jj][i]-48.0);
      }
    }
  }else{ // QTL is moving along the chromosomes
    for (int i=0; i<Naug; i++) {
      wi= (*weight)[i]+ (*weight)[i+Naug]+ (*weight)[i+2*Naug];
      yi= y[i];
      //Changed <= to < to prevent chrashes, this could make calculations a tad different then before
      for (j=0; j<dimx; j++){
        if (xtQTL[j]<=MCOF) {
          xtwj= ((double)Xt[j][i]-48.0)*wi;
          XtWY[j]+= xtwj*yi;
          for (jj=0; jj<=j; jj++)
            if (xtQTL[jj]<=MCOF) XtWX[j][jj]+= xtwj*((double)Xt[jj][i]-48.0);
            else if (xtQTL[jj]==MSEX) // QTL: additive effect if QTL=MCOF or MSEX
            {  // QTL==MAA
              XtWX[j][jj]+= ((double)(Xt[j][i]-48.0))*(*weight)[i]*(47.0-48.0);
              // QTL==MBB
              XtWX[j][jj]+= ((double)(Xt[j][i]-48.0))*(*weight)[i+2*Naug]*(49.0-48.0);
            } else // (xtQTL[jj]==MNOTAA)  QTL: dominance effect only if QTL=MCOF
            {  // QTL==MH
              XtWX[j][jj]+= ((double)(Xt[j][i]-48.0))*(*weight)[i+Naug]*(49.0-48.0);
            }
        } else if (xtQTL[j]==MSEX) { // QTL: additive effect if QTL=MCOF or MSEX
          xtwj= -1.0*(*weight)[i]; // QTL==MAA
          XtWY[j]+= xtwj*yi;
          for (jj=0; jj<j; jj++) XtWX[j][jj]+= xtwj*((double)Xt[jj][i]-48.0);
          XtWX[j][j]+= xtwj*-1.0;
          xtwj= 1.0*(*weight)[i+2*Naug]; // QTL==MBB
          XtWY[j]+= xtwj*yi;
          for (jj=0; jj<j; jj++) XtWX[j][jj]+= xtwj*((double)Xt[jj][i]-48.0);
          XtWX[j][j]+= xtwj*1.0;
        } else { // (xtQTL[j]==MQTL) QTL: dominance effect only if QTL=MCOF
          xtwj= 1.0*(*weight)[i+Naug]; // QTL==MCOF
          XtWY[j]+= xtwj*yi;
          // j-1 is for additive effect, which is orthogonal to dominance effect
          for (jj=0; jj<j-1; jj++) XtWX[j][jj]+= xtwj*((double)Xt[jj][i]-48.0);
          XtWX[j][j]+= xtwj*1.0;
        }
      }
    }
  }
  for (j=0; j<dimx; j++){
    for (jj=j+1; jj<dimx; jj++){
      XtWX[j][jj]= XtWX[jj][j];
    }
  }

  int d;
  ivector indx= newivector(dimx);
  /* solve equations */
  ludcmp(XtWX, dimx, indx, &d);
  lusolve(XtWX, dimx, indx, XtWY);

  double* indL = (double *)R_alloc(Nind, sizeof(double));
  int newNaug       = ((!fitQTL) ? Naug : 3*Naug);
  vector fit        = newvector(newNaug);
  vector resi       = newvector(newNaug);
  debug_trace("Calculate residuals\n");
  if (*variance<0) {
    *variance= 0.0;
    if (!fitQTL)
      for (int i=0; i<Naug; i++) {
        fit[i]= 0.0;
        for (j=0; j<dimx; j++)
          fit[i]+=((double)Xt[j][i]-48.0)*XtWY[j];
        resi[i]= y[i]-fit[i];
        *variance += (*weight)[i]*pow(resi[i], 2.0);
      }
    else
      for (int i=0; i<Naug; i++) {
        fit[i]= 0.0;
        fit[i+Naug]= 0.0;
        fit[i+2*Naug]= 0.0;
        for (j=0; j<dimx; j++)
          if (xtQTL[j]<=MCOF) {
            calc_i =((double)Xt[j][i]-48.0)*XtWY[j];
            fit[i]+= calc_i;
            fit[i+Naug]+= calc_i;
            fit[i+2*Naug]+= calc_i;
          } else if (xtQTL[j]==MSEX) {
            fit[i]+=-1.0*XtWY[j];
            fit[i+2*Naug]+=1.0*XtWY[j];
          } else
            fit[i+Naug]+=1.0*XtWY[j];
        resi[i]= y[i]-fit[i];
        resi[i+Naug]= y[i]-fit[i+Naug];
        resi[i+2*Naug]= y[i]-fit[i+2*Naug];
        *variance +=(*weight)[i]*pow(resi[i], 2.0);
        *variance +=(*weight)[i+Naug]*pow(resi[i+Naug], 2.0);
        *variance +=(*weight)[i+2*Naug]*pow(resi[i+2*Naug], 2.0);
      }
    *variance/= (!biasadj ? Nind : Nind-dimx); // to compare results with Johan; variance/=Nind;
    if (!fitQTL)
      for (int i=0; i<Naug; i++) Fy[i]= Lnormal(resi[i], *variance);
    else
      for (int i=0; i<Naug; i++) {
        Fy[i]       = Lnormal(resi[i], *variance);
        Fy[i+Naug]  = Lnormal(resi[i+Naug], *variance);
        Fy[i+2*Naug]= Lnormal(resi[i+2*Naug], *variance);
      }
  } else {
    if (!fitQTL)
      for (int i=0; i<Naug; i++) {
        fit[i]= 0.0;
        for (j=0; j<dimx; j++)
          fit[i]+=((double)Xt[j][i]-48.0)*XtWY[j];
        resi[i]= y[i]-fit[i];
        Fy[i]  = Lnormal(resi[i], *variance); // ????
      }
    else
      for (int i=0; i<Naug; i++) {
        fit[i]= 0.0;
        fit[i+Naug]= 0.0;
        fit[i+2*Naug]= 0.0;
        for (j=0; j<dimx; j++)
          if (xtQTL[j]<=MCOF) {
            calc_i =((double)Xt[j][i]-48.0)*XtWY[j];
            fit[i]+= calc_i;
            fit[i+Naug]+= calc_i;
            fit[i+2*Naug]+= calc_i;
          } else if (xtQTL[j]==MSEX) {
            fit[i]+=-1.0*XtWY[j];
            fit[i+2*Naug]+=1.0*XtWY[j];
          } else
            fit[i+Naug]+=1.0*XtWY[j];
        resi[i]= y[i]-fit[i];
        resi[i+Naug]= y[i]-fit[i+Naug];
        resi[i+2*Naug]= y[i]-fit[i+2*Naug];
        Fy[i]       = Lnormal(resi[i], *variance);
        Fy[i+Naug]  = Lnormal(resi[i+Naug], *variance);
        Fy[i+2*Naug]= Lnormal(resi[i+2*Naug], *variance);
      }
  }
  /* calculation of logL */
  debug_trace("calculate logL\n");
  double logL=0.0;
  for (int i=0; i<Nind; i++) {
    indL[i]= 0.0;
  }
  if (!fitQTL) {
    for (int i=0; i<Naug; i++) indL[ind[i]]+=(*weight)[i]*Fy[i];
  } else {
    for (int i=0; i<Naug; i++) {
      indL[ind[i]]+=(*weight)[i]*       Fy[i];
      indL[ind[i]]+=(*weight)[i+Naug]*  Fy[i+Naug];
      indL[ind[i]]+=(*weight)[i+2*Naug]*Fy[i+2*Naug];
    }
  }
  for (int i=0; i<Nind; i++) { //Sum up log likelihoods for each individual
    logL+= log(indL[i]);
  }
  return (double)logL;
}
示例#2
0
double multivariateregression(uint nvariables, uint nsamples, dmatrix x, dvector w, dvector y, dvector Fy){
  
  int d=0;
  double xtwj;
  dmatrix Xt   = newdmatrix(nvariables,nsamples);
  dmatrix XtWX = newdmatrix(nvariables, nvariables);
  dvector XtWY = newdvector(nvariables);

  ivector indx = newivector(nvariables);

  //cout << "calculating Xt" << endl;
  for(uint i=0; i<nsamples; i++){
    for(uint j=0; j<nvariables; j++){
      Xt[j][i] = x[i][j];
    }
  }

  //cout << "calculating XtWX and XtWY" << endl;
  for(uint i=0; i<nsamples; i++){
    for(uint j=0; j<nvariables; j++){
      xtwj     = Xt[j][i] * w[i];
      XtWY[j] += xtwj    * y[i];
      for(uint jj=0; jj<=j; jj++){
        XtWX[j][jj] += xtwj * Xt[jj][i];
      }
    }
  }
  
  LUdecomposition(XtWX, nvariables, indx, &d);
  LUsolve(XtWX, nvariables, indx, XtWY);

  //cout << "Estimated parameters:" << endl;
  //for (uint i=0; i < nvariables; i++){
  //  cout << "Parameter " << i << " = " << XtWY[i] << endl;
  //}

  dvector fit = newdvector(nsamples);
  dvector residual = newdvector(nsamples);
  dvector indL = newdvector(nsamples);
  
  double variance= 0.0;
  double logL=0.0;

  for (uint i=0; i<nsamples; i++){
    fit[i]= 0.0;
    for (uint j=0; j<nvariables; j++){
      fit[i]       += Xt[j][i] * XtWY[j];
      residual[i]   = y[i]-fit[i];
      variance     += w[i]*pow(residual[i],2.0);
    }
    Fy[i]     = Lnormal(residual[i],variance);
    indL[i]  += w[i]*Fy[i];
    logL     += log(indL[i]);
  }
  
  //cout << "Estimated response:" << endl;
  //printdvector(fit,nsamples);

  //cout << "Residuals:" << endl;
  //printdvector(residual,nsamples);

  //cout << "Estimated Fy:" << endl;
  //printdvector(Fy,nsamples);

  //cout << "Variance: " << variance << endl;
  //cout << "Loglikelihood: " << logL << endl;
  freematrix((void**)Xt,nvariables);
  freematrix((void**)XtWX, nvariables);
  freevector((void*)XtWY);
  freevector((void*)fit);
  freevector((void*)residual);
  freevector((void*)indL);
  return logL;
}