BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
oriti
Fluorite | Level 6

Dear all,

I enclosed a program which simulate regressions

that you can try to run.

I succeeded in calculating the coefficient matrix of the regression (b).

but when I try to calculate the residuals I get just one column  in the matrix that is correct but the other columns are with ones (1).

so the std of the residuals is also is not correct. (bolded with blue colour in the program)

I don't understand why that happens..

Who can help?

Many thanks...

proc iml;

n=10;

b=j(6,n);


     do j=1 to n;                          *Number of simulations;

  resid=j(239,n);

  stdresid=j(n,1);

  xtl = j(240, 1);

  xsl=j(240, 1);

  y=j(240, 1);

  xtl[1,1]=0.926145577;

   xsl[1,1] = 1.367164758;

   y[1,1] = -27.99208409;

  c=0.24269;

  e=0.02106;

  d=0.97564;

  h=-0.24776;

  m=-0.00362;

  l=-0.00111;

  q=0.00091339;

  r=1.00339;

  /** specify the mean and covariance of the population **/

  Mean = {0, 0, 0};

        Cov = {0.0343693345 0.0005718713 -.0000330633,

    0.0005718713 0.0008609739 0.0000083048,

   -.0000330633 0.0000083048 0.0000022582};

  NumSamples = 240;

  call randseed(0);  /** set seed for the RandNormal module **/

  U = RandNormal(NumSamples, Mean, Cov);

            do i=2 to 240;

              Y[i,1]=y[1,1]+u[i,1];         

              xsl[i,1]=c+e*y[(i-1),1]+d*xsl[(i-1),1]+h*xtl[(i-1),1]+u[i,2];    

              xtl[i,1]=m+l*y[(i-1),1]+q*xsl[(i-1),1]+r*xtl[(i-1),1]+u[i,3];    

            end;    

    ylag=lag(y);

    xsl2=xsl##2;

    xtl2=xtl##2;

  

    /* Step 1: Compute X`X and X`y */

    x = j(nrow(y), 1, 1) || ylag || xsl|| xtl || xsl2||xtl2;            /* add intercept column */

  v = {1};                   /* specify rows to exclude             */

    idx = setdif(1:nrow(x), v);  /*  exclude values in v */

    xnew = x[idx, ];             /* extract submatrix                   */

    xpx = xnew`* xnew;                         /* cross-products       */

  v = {1};                   /* specify rows to exclude             */

    idy = setdif(1:nrow(y), v);  /*exclude values in v */

    ynew = y[idx, ];             /* extract submatrix*/

    xpy = xnew` * ynew;

  b[,j]= solve(xpx, xpy);

  resid[,j]=ynew-xnew*b[,j];

  stdresid[j,]=std(resid[,j]);

  end;

  print b;

  print resid;

  print stdresid;

  quit;

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

Move the initialization of RESID and STDRESID outside of the DO loop for J.

View solution in original post

2 REPLIES 2
Rick_SAS
SAS Super FREQ

Move the initialization of RESID and STDRESID outside of the DO loop for J.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 2 replies
  • 1001 views
  • 1 like
  • 2 in conversation