BookmarkSubscribeRSS Feed
rameezroshan
Calcite | Level 5

Can someone help me to understand what is going wrong with the following code? 

The code is to estimate the covariance parameters employing mixed model analysis with the classical animal model used in breeding. There are 3 fixed effects F1=2 levels, F2=11 levels and F3=2 levels. Individual animals (1413 Nos) are considered as random effects. am is the additive genetic relationship matrix or numerator relationship matrix. The same procedure from proc mixed using REML is tried to implement through the following code.  The output from MIVQUE0 module is the starting value for the minimization of -2loglikelihood. I cannot understand why the following error happens. 

 
1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
72
73 proc iml;
NOTE: IML Ready
74 USE MAGUR;
75 READ ALL VAR {Fixed1} INTO F1;
76 READ ALL VAR {Fixed2} INTO F2;
77 READ ALL VAR {Fixed3} INTO F3;
78 READ ALL VAR {Weight} INTO y;
79 x1=DESIGN(F1);
79 x2=DESIGN(F2);
79 x3=DESIGN(F3);
80 xns=x1||x2||x3;
80 /** non-singular (full column rank)
81 design matrix for fixed effects **/
82 x=J (NROW (xns),1,1)||xns;
82 /** singular
83 (over parameterized) design matrix for fixed effects **/
84 START matrank (x);
84  /** matrank computes matrix rank **/
85 matrank=ROUND (TRACE (x*GINV(x)));
86 RETURN (matrank);
87 FINISH;
NOTE: Module MATRANK defined.
88 a=matrank (x);
89 Z=I(1413);
89 /** design
90 matrix for random effects **/
91 USE AMTRIX;
92 READ ALL VAR _all_ INTO am;
92 /**Numerator relationship
93 matrix from pedigree**/
94 n = NROW (x);
95 ie = I(1413);
96 pi = CONSTANT ("PI");
97 START reml (theta) GLOBAL (z,y,xns,am,ie,pi,n,a);
98 /** Defining the function -2 (Log Likelihood) **/
99 sigma_a =theta[1, 1]*am;
100 sigma_e = theta[1, 2]*ie;
101 v = z*sigma_a*z` + sigma_e;
102 /** Covariance matrix **/
103 vinv=inv(v);
104 r=y-xns*GINV(xns`*vinv*xns)*xns`*vinv*y;
105 prod=logabsdet(v)+logabsdet(xns`*vinv*xns)+
106 r`*vinv*r+(n-a)*LOG(2*pi);
107 RETURN (prod);
108 FINISH reml;
NOTE: Module REML defined.
109 /** MIVQUE (0) **/
110 p=ie-x*GINV(x`*x)*x`;
110 ! /** projection onto x **/;
111 pz1=p*z*z`;
112 pz2=p*ie;
113 mat_a=(TRACE(pz1*pz1)||trace(pz1*pz2))//(TRACE(pz2*pz1)||trace(pz2*pz2));
114 mat_b=(y`*pz1*p`*y)//(y`*pz2*p*y);
115 mivque0=(GINV(mat_a)*mat_b)`;
116 print mivque0;
117 optn = {0 1};
118 con={ 0 0 };
119 CALL NLPNRR (rc, covs,"reml", mivque0,optn,con);
ERROR: NLPNRR call: Module REML must return a scalar.
120 PRINT covs;
121
122
123 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
136

 

1 REPLY 1
PeterClemmensen
Tourmaline | Level 20

This happens because your Function Module REML does not return a 1x1 matrix ie. a scalar. The NLPNRR Subroutine requires that. From the NLPNRR Documentation, you can see the small example program

 

proc iml;
start F_BETTS(x); 
   f = .01 * x[1] * x[1] + x[2] * x[2] - 100.; 
   return(f); 
finish F_BETTS; 

con = {  2. -50.  .   ., 
        50.  50.  .   ., 
        10.  -1. 1. 10.}; 
x = {-1. -1.}; 
optn = {0 2}; 
call nlpnrr(rc,xres,"F_BETTS",x,optn,con); 
quit;

If you change the BETTS Function Module to return a (2x1) vector instead of a scalar, you get that exact error:

 

proc iml;
start F_BETTS(x); 
   f = .01 * x[1] * x[1] + x[2] * x[2] - 100.; 
   return(f//f); 
finish F_BETTS; 

con = {  2. -50.  .   ., 
        50.  50.  .   ., 
        10.  -1. 1. 10.}; 
x = {-1. -1.}; 
optn = {0 2}; 
call nlpnrr(rc,xres,"F_BETTS",x,optn,con); 
quit;

Capture.PNG

 

 

 

 

 

So debug and rewrite your REML Function module to return a scalar 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 1 reply
  • 828 views
  • 2 likes
  • 2 in conversation