コード例 #1
0
ファイル: dataobj.cpp プロジェクト: jfahren/BayesX
void outfilerun(dataobject & o)
  {

  unsigned nrwritten;
  ST::string path  = o.uwrite.getPath();
  list<ST::string> names = o.m.getModelVarnames();
  ST::string expression = o.methods[6].getexpression();

  if ( (o.uwrite.isexisting() == true) && (o.replace.getvalue() == false) )
	 o.errormessages.push_back(
	 "ERROR: file " + path + " is already existing\n");
  else
	 {
	 ofstream fout;
	 ST::open(fout,path);
	 if (expression.length() > 0)
		{
		realvar v = o.d.eval_exp(expression);
        #if defined(JAVA_OUTPUT_WINDOW)
		nrwritten = o.d.write(o.adminb_p,fout,names,o.header.getvalue(),v);
        #else
        nrwritten = o.d.write(fout,names,o.header.getvalue(),v);
        #endif
		}
	 else
       {
        #if defined(JAVA_OUTPUT_WINDOW)
	   nrwritten = o.d.write(o.adminb_p,fout,names,o.header.getvalue());
       #else
	   nrwritten = o.d.write(fout,names,o.header.getvalue());
       #endif
       }

     o.errormessages = o.d.geterrormessages();
     if (o.errormessages.empty())
       {
       o.out("NOTE: " + ST::inttostring(names.size()) + " variable(s) with " +
             ST::inttostring(nrwritten) +  " observations written to file\n");
       o.out("      " + path + "\n");
       }
     else
       {
       fout.close();
       remove(path.strtochar());
       }


	 }
  }
コード例 #2
0
ファイル: FC_predict_mult.cpp プロジェクト: jfahren/BayesX
void FC_predict_mult::outresults(ofstream & out_stata, ofstream & out_R,
                            const ST::string & pathresults)
  {

  if (pathresults.isvalidfile() != 1)
    {

    FC::outresults(out_stata,out_R,"");

    FC_deviance.outresults(out_stata,out_R,"");

    optionsp->out("  PREDICTED VALUES: \n",true);
    optionsp->out("\n");

    optionsp->out("    Results for the predictor, mean are stored in file\n");
    optionsp->out("    " +  pathresults + "\n");
    optionsp->out("\n");
    out_R << "predict=" << pathresults << ";" <<  endl;

    ofstream outres(pathresults.strtochar());

    optionsp->out("\n");

    unsigned i,j;

    ST::string l1 = ST::doubletostring(optionsp->lower1,4);
    ST::string l2 = ST::doubletostring(optionsp->lower2,4);
    ST::string u1 = ST::doubletostring(optionsp->upper1,4);
    ST::string u2 = ST::doubletostring(optionsp->upper2,4);
    l1 = l1.replaceallsigns('.','p');
    l2 = l2.replaceallsigns('.','p');
    u1 = u1.replaceallsigns('.','p');
    u2 = u2.replaceallsigns('.','p');

    outres << "intnr" << "   ";

    for (i=0;i<varnames.size();i++)
      outres << varnames[i] << "   ";

      vector<double *> responsep;
      vector<double *> weightpmat;
      vector<double *> workmeanmat;
      vector<datamatrix *>   auxhelp;

     for (j=0;j<likep.size();j++)
       {
       workmeanmat.push_back((betamean.getV()+j));
       responsep.push_back(likep[j]->response.getV());
       weightpmat.push_back(likep[j]->weight.getV());
       auxhelp.push_back(likep[j]->get_auxiliary_parameter());
       }


    if (nosamplessave==false)
      {

      for (i=0;i<likep.size();i++)
        {

        if (likep[i]->outpredictor)
          {
          outres << "pmean_pred_" << likep[i]->predictor_name << "   ";

          if (optionsp->samplesize > 1)
            {
            outres << "pqu"  << l1  << "_pred_" << likep[i]->predictor_name << "   ";
            outres << "pqu"  << l2  << "_pred_" << likep[i]->predictor_name << "   ";
            outres << "pmed_pred_" << likep[i]->predictor_name << "   ";
            outres << "pqu"  << u1  << "_pred_" << likep[i]->predictor_name << "   ";
            outres << "pqu"  << u2  << "_pred_" << likep[i]->predictor_name << "   ";
            }
          }

        }

      for (i=0;i<likep.size();i++)
        {
        if (likep[i]->outexpectation)
          {
          outres << "pmean_E_" << likep[i]->predictor_name << "   ";

          if (optionsp->samplesize > 1)
            {
            outres << "pqu"  << l1  << "_E_" << likep[i]->predictor_name << "   ";
            outres << "pqu"  << l2  << "_E_" << likep[i]->predictor_name << "   ";
            outres << "pmed_E_" << likep[i]->predictor_name << "   ";
            outres << "pqu"  << u1  << "_E_" << likep[i]->predictor_name << "   ";
            outres << "pqu"  << u2  << "_E_" << likep[i]->predictor_name << "   ";
            }
          }
        }


      for (i=0;i<likep.size();i++)
        {

        if (likep[i]->outpredictor)
          {
          outres << "pmean_" << likep[i]->predictor_name << "   ";

          if (optionsp->samplesize > 1)
            {
            outres << "pqu"  << l1  << "_" << likep[i]->predictor_name << "   ";
            outres << "pqu"  << l2  << "_" << likep[i]->predictor_name << "   ";
            outres << "pmed_" << likep[i]->predictor_name << "   ";
            outres << "pqu"  << u1  << "_" << likep[i]->predictor_name << "   ";
            outres << "pqu"  << u2  << "_" << likep[i]->predictor_name << "   ";
            }
          }

        }

      outres << "quantile_res quadratic_score logarithmic_score spherical_score CRPS";

      outres << endl;

      double * workmean = betamean.getV();
      double * workbetaqu_l1_lower_p = betaqu_l1_lower.getV();
      double * workbetaqu_l2_lower_p = betaqu_l2_lower.getV();
      double * workbetaqu_l1_upper_p = betaqu_l1_upper.getV();
      double * workbetaqu_l2_upper_p = betaqu_l2_upper.getV();
      double * workbetaqu50 = betaqu50.getV();



      for(i=0;i<designmatrix.rows();i++)
        {

        outres << (i+1) << "   ";

        for (j=0;j<designmatrix.cols();j++)
          outres << designmatrix(i,j) << "   ";

        // predictor
        for (j=0;j<likep.size();j++,
            workmean++,
            workbetaqu_l1_lower_p++,
            workbetaqu_l2_lower_p++,
            workbetaqu50++,
            workbetaqu_l1_upper_p++,
            workbetaqu_l2_upper_p++)
          {

          if (likep[j]->outpredictor)
            {

            outres << *workmean << "   ";

            if (optionsp->samplesize > 1)
              {
              outres << *workbetaqu_l1_lower_p << "   ";
              outres << *workbetaqu_l2_lower_p << "   ";
              outres << *workbetaqu50 << "   ";
              outres << *workbetaqu_l2_upper_p << "   ";
              outres << *workbetaqu_l1_upper_p << "   ";
              }
            }
          }
        // end: predictor

        // begin: mu
        for (j=0;j<likep.size();j++,
            workmean++,
            workbetaqu_l1_lower_p++,
            workbetaqu_l2_lower_p++,
            workbetaqu50++,
            workbetaqu_l1_upper_p++,
            workbetaqu_l2_upper_p++)
          {

          if (likep[j]->outexpectation)
            {
            outres << *workmean << "   ";


            if (optionsp->samplesize > 1)
              {
              outres << *workbetaqu_l1_lower_p << "   ";
              outres << *workbetaqu_l2_lower_p << "   ";
              outres << *workbetaqu50 << "   ";
              outres << *workbetaqu_l2_upper_p << "   ";
              outres << *workbetaqu_l1_upper_p << "   ";
              }
            }
          }
        // end: mu

        // parameter
        for (j=0;j<likep.size();j++,
            workmean++,
            workbetaqu_l1_lower_p++,
            workbetaqu_l2_lower_p++,
            workbetaqu50++,
            workbetaqu_l1_upper_p++,
            workbetaqu_l2_upper_p++)


          {

          if (likep[j]->outpredictor)
            {
            outres << *workmean << "   ";
            workmeanmat[j] = workmean;

            if (optionsp->samplesize > 1)
              {
              outres << *workbetaqu_l1_lower_p << "   ";
              outres << *workbetaqu_l2_lower_p << "   ";
              outres << *workbetaqu50 << "   ";
              outres << *workbetaqu_l2_upper_p << "   ";
              outres << *workbetaqu_l1_upper_p << "   ";
              }
            }
          }
        // end: parameter

          outres << likep[likep.size()-1]->compute_quantile_residual_mult(responsep,workmeanmat,weightpmat,auxhelp) << "   ";
          outres << likep[likep.size()-1]->compute_quadr_mult()    << "   ";
          outres << likep[likep.size()-1]->compute_log_mult()    << "   ";
          outres << likep[likep.size()-1]->compute_spherical_mult()    << "   ";
          outres << likep[likep.size()-1]->compute_CRPS_mult()    << "   ";

        if (i < designmatrix.rows()-1)
          {
          for (j=0;j<likep.size();j++)
            {
            weightpmat[j]++;
            responsep[j]++;
            }
          }

          outres << endl;
        }

      } // end: if (nosamplessave==false)
    else
      {

      for (i=0;i<likep.size();i++)
        {
        if (likep[i]->outpredictor)
          outres << "pmean_pred_" << likep[i]->predictor_name << "   ";
        }

      for (i=0;i<likep.size();i++)
        {
        if (likep[i]->outexpectation)
          outres << "pmean_E_" << likep[i]->predictor_name << "   ";
        }

      for (i=0;i<likep.size();i++)
        {
        if (likep[i]->outpredictor)
          outres << "pmean_param_" << likep[i]->predictor_name << "   ";
        }

      outres << "quantile_res quadratic_score logarithmic_score spherical_score CRPS";

      outres << endl;

      double * workmean = betamean.getV();

      for(i=0;i<designmatrix.rows();i++)
        {

        outres << (i+1) << "   ";

        for (j=0;j<designmatrix.cols();j++)
          outres << designmatrix(i,j) << "   ";

        for (j=0;j<likep.size();j++,workmean++)
          {
          if (likep[j]->outpredictor)
            outres << *workmean << "   ";
          }

        for (j=0;j<likep.size();j++,workmean++)
          {
          if (likep[j]->outexpectation)
            outres << *workmean << "   ";
          }

        for (j=0;j<likep.size();j++,workmean++)
          {
          if (likep[j]->outpredictor)
            outres << *workmean << "   ";
            workmeanmat[j] = workmean;
          }


        outres << likep[likep.size()-1]->compute_quantile_residual_mult(responsep,workmeanmat,weightpmat,auxhelp) << "   ";
        outres << likep[likep.size()-1]->compute_quadr_mult()    << "   ";
        outres << likep[likep.size()-1]->compute_log_mult()    << "   ";
        outres << likep[likep.size()-1]->compute_spherical_mult()    << "   ";
        outres << likep[likep.size()-1]->compute_CRPS_mult()    << "   ";

//        std::ofstream out;
//  // helpmat1.prettyPrint(out);
//    out.open ("C:\\tmp\\res.raw", std::ofstream::out | std::ofstream::app);
//    out << *responsep[1] ;
//    out << " " ;
//    out << *responsep[0] ;
//    out << " " ;
//    out << *workmeanmat[1] ;
//    out << " " ;
//    out << *workmeanmat[0] ;
//    out << " " ;
//    out << *weightpmat[1] ;
//    out << " " ;
//    out << *weightpmat[0] ;
//    out << " " ;
//    out << likep[likep.size()-1]->compute_quantile_residual_mult(responsep,workmeanmat,weightpmat,auxhelp) << endl;


        outres << endl;

        if (i < designmatrix.rows()-1)
          {
          for (j=0;j<likep.size();j++)
            {
            weightpmat[j]++;
            responsep[j]++;
            }
          }

        }

      }

     outresults_deviance();
     outresults_DIC(out_stata,out_R,pathresults);


    }   // end if (pathresults.isvalidfile() != 1)

  }
コード例 #3
0
ファイル: FC_predict_mult.cpp プロジェクト: jfahren/BayesX
void FC_predict_mult::outresults_DIC(ofstream & out_stata, ofstream & out_R,
                                     const ST::string & pathresults)
  {

  ST::string pathresultsdic = pathresults.substr(0,pathresults.length()-4) + "_DIC.res";
  ofstream out(pathresultsdic.strtochar());

  out_R << "DIC=" << pathresultsdic << ";" <<  endl;

  optionsp->out("    Results for the DIC are stored in file\n");
  optionsp->out("    " +  pathresultsdic + "\n");
  optionsp->out("\n");

  double deviance2=0;

  double devhelp;

  vector<double *> worklinp;
  vector<double *> workresponse;
  vector<double *> workweight;
  vector<datamatrix *>   aux;

  unsigned j;

  for (j=0;j<likep.size();j++)
    {

    worklinp.push_back(betamean.getV()+j);

    workresponse.push_back(likep[j]->response.getV());

    workweight.push_back(likep[j]->weight.getV());

    aux.push_back(likep[j]->get_auxiliary_parameter(auxpostmean));

    }

  unsigned i;

  for (i=0;i<likep[0]->nrobs;i++)
    {

    likep[likep.size()-1]->compute_deviance_mult(workresponse,
                                                 workweight,worklinp,
                                                 &devhelp,aux);

    deviance2 += devhelp;

    int s = likep.size();
    int bs = betamean.cols();

    for (j=0;j<s;j++)
      {
      worklinp[j]+=bs;
      workresponse[j]++;
      workweight[j]++;
      }

    }


  double devhelpm = FC_deviance.betamean(0,0);

  unsigned d;
  if (devhelpm > 1000000000)
    d = 14;
  else if (devhelpm > 1000000)
    d = 11;
  else
    d = 8;

  out << "deviance   pd dic" << endl;

  optionsp->out("  ESTIMATION RESULTS FOR THE DIC: \n",true);
  optionsp->out("\n");

  optionsp->out("    Deviance(bar_mu):           " +
  ST::doubletostring(deviance2,d) + "\n");
  out << deviance2 << "   ";

  optionsp->out("    pD:                         " +
  ST::doubletostring(devhelpm-deviance2,d) + "\n");
  out << (devhelpm-deviance2) << "   ";

  optionsp->out("    DIC:                        " +
  ST::doubletostring(2*devhelpm-deviance2,d) + "\n");
  optionsp->out("\n");
  out << (2*devhelpm-deviance2) << "   " << endl;

  optionsp->out("\n");

  }
コード例 #4
0
ファイル: FC_nonp_variance.cpp プロジェクト: jfahren/BayesX
void FC_nonp_variance::outresults(ofstream & out_stata,ofstream & out_R,
                                  const ST::string & pathresults)
  {

  FC::outresults(out_stata,out_R,"");

  ST::string l1 = ST::doubletostring(optionsp->lower1,4);
  ST::string l2 = ST::doubletostring(optionsp->lower2,4);
  ST::string u1 = ST::doubletostring(optionsp->upper1,4);
  ST::string u2 = ST::doubletostring(optionsp->upper2,4);

  ST::string nl1 = ST::doubletostring(optionsp->lower1,4);
  ST::string nl2 = ST::doubletostring(optionsp->lower2,4);
  ST::string nu1 = ST::doubletostring(optionsp->upper1,4);
  ST::string nu2 = ST::doubletostring(optionsp->upper2,4);
  nl1 = nl1.replaceallsigns('.','p');
  nl2 = nl2.replaceallsigns('.','p');
  nu1 = nu1.replaceallsigns('.','p');
  nu2 = nu2.replaceallsigns('.','p');

  ST::string vstr;


  if (optionsp->samplesize > 1)
    {

    vstr = "    Mean:         ";
    optionsp->out(vstr + ST::string(' ',20-vstr.length()) +
    ST::doubletostring(betamean(0,0),6) + "\n");

    vstr = "    Std. dev.:    ";

    optionsp->out(vstr + ST::string(' ',20-vstr.length()) +
    ST::doubletostring(sqrt(betavar(0,0)),6) + "\n");

    vstr = "    " + l1 + "% Quantile: ";
    optionsp->out(vstr + ST::string(' ',20-vstr.length()) +
    ST::doubletostring(betaqu_l1_lower(0,0),6) + "\n");

    vstr = "    " + l2 + "% Quantile: ";
    optionsp->out(vstr + ST::string(' ',20-vstr.length()) +
    ST::doubletostring(betaqu_l2_lower(0,0),6) + "\n");

    vstr = "    50% Quantile: ";
    optionsp->out(vstr + ST::string(' ',20-vstr.length()) +
    ST::doubletostring(betaqu50(0,0),6) + "\n");

    vstr = "    " + u1 + "% Quantile: ";
    optionsp->out(vstr + ST::string(' ',20-vstr.length()) +
    ST::doubletostring(betaqu_l2_upper(0,0),6) + "\n");

    vstr = "    " + u2 + "% Quantile: ";
    optionsp->out(vstr + ST::string(' ',20-vstr.length()) +
    ST::doubletostring(betaqu_l1_upper(0,0),6) + "\n");

    optionsp->out("\n");

    }
  else
    {
    optionsp->out("    Smoothing parameter: " +
    ST::doubletostring(betamean(0,1),6) + "\n");

    optionsp->out("\n");
    }

//  out_R << "term=" << title <<  ";" << endl;

  if (pathresults.isvalidfile() != 1)
    {

    optionsp->out("    Results for the variance component are also stored in file\n");
    optionsp->out("    " +  pathresults + "\n");
    optionsp->out("\n");

    ST::string paths = pathresults.substr(0,pathresults.length()-4) +
                                 "_sample.raw";

    out_R << "pathvarsample=" << paths << endl; 
//    out_R << "filetype=param; path=" << pathresults << ";" <<  endl;

    ofstream ou(pathresults.strtochar());

    if (optionsp->samplesize > 1)
      {
      ou << "pmean  pstd  pqu"  << nl1 << "   pqu" << nl2 << "  pmed pqu" <<
      nu1 << "   pqu" << nu2 << endl;
      }
    else
      {
      ou << "pmean" << endl;
      }

    ou << betamean(0,0) << "  ";
    if (optionsp->samplesize > 1)
      {
      if (betavar(0,0) < 0.0000000000001)
        ou << 0 << "  ";
      else
        ou << sqrt(betavar(0,0)) << "  ";
      ou << betaqu_l1_lower(0,0) << "  ";
      ou << betaqu_l2_lower(0,0) << "  ";
      ou << betaqu50(0,0) << "  ";
      ou << betaqu_l2_upper(0,0) << "  ";
      ou << betaqu_l1_upper(0,0) << "  " << endl;
      }

    optionsp->out("\n");
    }

  }