[prev in list] [next in list] [prev in thread] [next in thread] 

List:       sas-l
Subject:    Re: Forcing a linear regression through an arbitrary point - SAS new? restrict condition
From:       "Keintz, Mark" <mkeintz () WHARTON ! UPENN ! EDU>
Date:       2016-03-28 18:40:02
Message-ID: SN1PR10MB0478C4A6F2091E2DC3A3FEA4FD860 () SN1PR10MB0478 ! namprd10 ! prod ! outlook ! com
[Download RAW message or body]

And I now realize I was reading a truncated version of Roger's note, and have sent a \
totally superfluous note.

Regards,
Mark

-----Original Message-----
From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Keintz, Mark
Sent: Monday, March 28, 2016 2:32 PM
To: SAS-L@LISTSERV.UGA.EDU
Subject: Re: Forcing a linear regression through an arbitrary point - SAS new? \
restrict condition

Roger:

If you know the coordinates, say (X,Y)=(1,21), in advance, why not just subtract 1 \
from x and 21 from y?  

Then run the regression with the NOINT option, of the translated Y1 (=y-21)  on X1 \
(=x-1).  The regression line will run through the origin in the space of the \
translated X1 and Y1, equivalent to running through (1,21) in the original space.

Then if you want, you can express the estimate in original terms as

              Y1=   beta * X1 
 <==>  Y-21 = beta*(X-1) 
 <==>  Y =   21-beta  + beta*X 


data need / view=need; 
    set have; 
    Y1=y-21; 
    X1=x-1; 
run; 

proc reg data=need; 
   model y1=x1 / noint; 
quit; 



regards,
Mark

-----Original Message-----
From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Roger DeAngelis
Sent: Monday, March 28, 2016 9:06 AM
To: SAS-L@LISTSERV.UGA.EDU
Subject: Forcing a linear regression through an arbitrary point - SAS new? restrict \
condition

%let pgm=utl_reg_restrict;

* Forcing a linear regression through an arbitrary point - SAS new? restrict \
condition;

I could not find a nice intuitive R package to do restricted regression, At anyrate, \
I prefer SAS for tried and true stats, and R for new or odd designs.

Suppose we have infant ages and heights and we know the 50% norm for height at 1 year \
is 21 inches.

We want to force the regression through (X(AGE)=1 year, Y(HEIGHT)=21 inches);

/*
HERE is our data - SQL create below;
Up to 40 obs WORK.NEWINT total obs=19

Obs    AGE    HEIGHT

  1     4      27.60
  2     3      22.60
  3     3      26.12
  4     4      25.12
  5     4      25.40
  6     2      22.92
  7     2      23.92
  8     5      25.00
  9     3      25.00
 10     2      23.60
 11     1      20.52
 12     4      25.72
 13     2      22.52
 14     5      26.60
 15     6      28.80
 16     2      25.92
 17     5      26.80
 18     1      23.00
 19     5      26.60
*/

model height= intercept + beta*age

* SAS solution;
  Note because we are interested in height at 1 yr of age,
  the slopes contribution to height is beta*1 or just beta.
  If we force the regression age=1.
  Thus the restriction is 21 = intercept + beta ;

proc reg data=newint;
  model height=age;

  restrict intercept+age=21;

  output out=p predicted=p;
;run;quit;

/*
Examples of valid RESTRICT statements include the following:
   restrict x1;
   restrict a+b=l;
   restrict a=b=c;
   restrict a=b, b=c;
   restrict 2*f=g+h, intercept+f=0;
   restrict f=g=h=intercept;
*/
/*
                        Parameter Estimates

                     Parameter       Standard
Variable     DF       Estimate          Error    t Value    Pr > |t|

Intercept     1       19.46592        0.11815     164.75     <.0001
AGE           1        1.53408        0.11815      12.98     <.0001
RESTRICT     -1        7.26028        3.26154       2.23     0.0211*

note 21=19.46592 + 1.54308 * 1 year

* Probability computed using beta distribution.
*/


* Here is the edata;

options validvarname=upcase;
proc sql;
Create table newint(age float, height float); Insert into newint(age, height) \
Values(4, 27.60); Insert into newint(age, height) Values(3, 22.60); Insert into \
newint(age, height) Values(3, 26.12); Insert into newint(age, height) Values(4, \
25.12); Insert into newint(age, height) Values(4, 25.40); Insert into newint(age, \
height) Values(2, 22.92); Insert into newint(age, height) Values(2, 23.92); Insert \
into newint(age, height) Values(5, 25.00); Insert into newint(age, height) Values(3, \
25.00); Insert into newint(age, height) Values(2, 23.60); Insert into newint(age, \
height) Values(1, 20.52); Insert into newint(age, height) Values(4, 25.72); Insert \
into newint(age, height) Values(2, 22.52); Insert into newint(age, height) Values(5, \
26.60); Insert into newint(age, height) Values(6, 28.80); Insert into newint(age, \
height) Values(2, 25.92); Insert into newint(age, height) Values(5, 26.80); Insert \
into newint(age, height) Values(1, 23.00); Insert into newint(age, height) Values(5, \
26.60); ;quit;

* SAS  the hard way;

* Basically transform X-X0 and y-y0 with
  noint(no intercept) option.  SAS.(forces the line throug (0,0).

* transform input;
data hardway;
  set newint;
  set newint;
  age=age-1;
  height=height-21;
;run;quit;

proc reg data=hardway;
  model height=age/noint;
  output out=p predicted=p;
;run;quit;

* transform back;
data addbac;
  set p;
  age=age+1;
  p=p+21;
;run;quit;


* R solution;
* haven handles compressed 64bit SAS datasets?; data  "c:\temp\newint.sas7bdat";
  set newint;
;run;quit;

* R solution - the hard way only?;
* corresponding SAS code below;
%utl_submit_r(%nrbquote(
  library(haven);
  newint<-read_sas("c:\\temp\\newint.sas7bdat");
  attach(newint);
  nmod <- (lm(I(HEIGHT-21)~I(AGE-1) + 0, newint));
  tbl<-cbind(HEIGHT, AGE, predict(nmod)+21,residuals(nmod));
  tbl;
  coef(nmod);
  res<- 21 + coef(nmod)*0;
  res;
));

Note the 1 year olds;

   HEIGHT AGE   Predicted
1   27.60   4   25.60225
2   22.60   3   24.06817
3   26.12   3   24.06817
4   25.12   4   25.60225
5   25.40   4   25.60225
6   22.92   2   22.53408
7   23.92   2   22.53408
8   25.00   5   27.13634
9   25.00   3   24.06817
10  23.60   2   22.53408

11  20.52   1   21.00000

12  25.72   4   25.60225
13  22.52   2   22.53408
14  26.60   5   27.13634
15  28.80   6   28.67042
16  25.92   2   22.53408
17  26.80   5   27.13634

18  23.00   1   21.00000

19  26.60   5   27.13634


Slope for No intercept
I(AGE - 1)
  1.534085

* Predicted height at age=1 year - regression goes through the point we wanted;

I(AGE - 1)
        21


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic