Skip to content
Snippets Groups Projects
glm.cc 1.36 KiB
Newer Older
Stephen Smith's avatar
Stephen Smith committed
/*  glm.cc

    Mark Woolrich, FMRIB Image Analysis Group

    Copyright (C) 1999-2000 University of Oxford  */

/*  CCOPYRIGHT  */

#include "glm.h"
#include "miscmaths.h"
Christian Beckmann's avatar
Christian Beckmann committed
#include "log.h"
Stephen Smith's avatar
Stephen Smith committed

using namespace MISCMATHS;
Mark Woolrich's avatar
Mark Woolrich committed

namespace FILM {
Stephen Smith's avatar
Stephen Smith committed

  void Glm::setData(Matrix& p_y, Matrix& p_x, Matrix& p_contrasts)
    {
      y = &p_y;
      x = &p_x;
      numTS = p_y.Ncols();
      sizeTS = p_y.Nrows();
      contrasts = &p_contrasts;
      SetContrast(1);
    }
  
  const Matrix& Glm::ComputeResids()
    {
      Tracer ts("ComputeResids");

      Matrix& d = *x;
      Computeb();

      // r = (I - x(x'x)-1x')y
      Matrix I(sizeTS, sizeTS);
      Identity(I);      
      r = (I-d*inv_xx*d.t())*(y->Column(1));
	
      return r;
    }

  const float Glm::Computecb()
    { 
      Tracer ts("Computecb");

      cb = (c.t()*b).AsScalar();
      return cb;
    }

  const ColumnVector& Glm::Computeb()
    {
      Tracer ts("Computeb");

      Matrix& d = *x;
      
      // inv_xx = inv(x'x)
      inv_xx = (d.t()*d).i();

      b = inv_xx*d.t()*(y->Column(1));

      return b;
    }

  const float Glm::ComputeVar()
    {
      Tracer ts("ComputeVar");
   
      // var = e*var_on_e
      // e is the estimate of the variance of the timeseries, sigma^2
      var = (r.Column(1).t()*r.Column(1)*c.t()*inv_xx*c).AsScalar()/sizeTS;

      return var;
    }

#ifndef NO_NAMESPACE
}
#endif