BookmarkSubscribeRSS Feed
viollete
Calcite | Level 5

Hello,

 

I have data made from two variables: adm_date and total_nr (example below):

adm_date total_nr

1 200

2 265

3 245

...

69 340

70 356

I want to do two things:

1. I want to use first 59 observations and forecast the rest.

2. I want to plot regression line and real data in same graph, similar to example below (I do not need that grey block):

 

 

8 REPLIES 8
Ksharp
Super User
%let n=100;
data have;
 set sashelp.air;
 retain group 1;
 if _n_ gt &n then do;group=2;air=.;end;
run;

proc reg data=have outest=est(keep=date rename=(date=slope)) noprint;
model air=date;
output out=pred predicted=pred;
quit;


data want;
 merge sashelp.air pred(keep=date pred group rename=(date=_date));
run;

ods graphics / attrpriority=none;
proc sgplot data=want noautolegend;
styleattrs datalinepatterns=(solid dash);
series x=_date y=pred/group=group lineattrs=(color=red thickness=2);
scatter x=date y=air;
run;

x.png

viollete
Calcite | Level 5

Thanks!

 

And maybe you know how i could add another regression line only for the time period I forecasted, but this time to use the real data points?

rselukar
SAS Employee
Is it some other regression line, or you simply do not want to see the historical region in your plot? If it is the latter, the default forecast that is produced already does that. If it is another regression line, you will have to use the SGPLOT procedure with the UCM output data set (that is the simplest way I can think of). Use the LINEPARM, SERIES and SCATTER statements in PROC SGPLOT.
rselukar
SAS Employee

There are many was to do this (one already shown).  This is yet another (see the back= and lead= options in the ESTIMATE and FORECAST statements):

 

proc ucm data=sashelp.air;

   id date interval=month; *optional;

   model air;

   irregular;

   level variance=0 noest;

   slope variance=0 noest;

   estimate back=20; *sasy;

   forecast back=20 lead=20 plot=(decomp) outfor=for;

run;

This will produce a plot that is based on first 124 observations (out of 144).  The plot has confidence bands around them.  If you don't want them, you can use sgplot  with the columns in the output data set, for, (s_level has the values of the fitted line).  See the UCM doc for more info: http://support.sas.com/documentation/cdl/en/etsug/68148/HTML/default/viewer.htm#etsug_ucm_toc.htm

 

viollete
Calcite | Level 5

I am sorry if i am not clear, I would like to get a graph similar to (I want to see grapphically if there is any change due to policy implementation at specific time):

Ksharp
Super User
%let n=100;
data have;
 set sashelp.air;
 retain group 1;
 if _n_ gt &n then do;group=2;air=.;end;
run;
data have2;
 set sashelp.air;
 if _n_ gt &n ;
run;

proc reg data=have noprint;
model air=date;
output out=pred predicted=pred;
quit;
proc reg data=have2 noprint;
model air=date;
output out=pred2 predicted=pred;
quit;


data want;
 merge sashelp.air pred(keep=date pred group rename=(date=_date))
       pred2(keep=date pred rename=(date=_date2 pred=pred2));
run;

ods graphics / attrpriority=none;
proc sgplot data=want noautolegend;
styleattrs datalinepatterns=(solid dash);
series x=_date y=pred/group=group lineattrs=(color=red thickness=2);
series x=_date2 y=pred2/lineattrs=graphdata3(thickness=2 pattern=dash);
scatter x=date y=air;
run;
viollete
Calcite | Level 5

Thanks!

 

What is the best way in this case to put legend in graph?

Ksharp
Super User
%let n=100;
data have;
 set sashelp.air;
 retain group 'Trend Before                            ';
 if _n_ gt &n then do;group='Predicted based on pre-trend';air=.;end;
run;
data have2;
 set sashelp.air;
 if _n_ gt &n ;
run;

proc reg data=have noprint;
model air=date;
output out=pred predicted=pred;
quit;
proc reg data=have2 noprint;
model air=date;
output out=pred2 predicted=pred;
quit;


data want;
 merge sashelp.air pred(keep=date pred group rename=(date=_date))
       pred2(keep=date pred rename=(date=_date2 pred=pred2));
run;

ods graphics / attrpriority=none;
proc sgplot data=want ;
styleattrs datalinepatterns=(solid dash);
series x=_date y=pred/group=group lineattrs=(color=red thickness=2)
 name='a' legendlabel=' ';
series x=_date2 y=pred2/lineattrs=graphdata3(thickness=2 pattern=dash) 
name='b' legendlabel='Trend after';
scatter x=date y=air;
keylegend 'a' 'b'/ location=inside position=bottomright across=1;
run;

x.png

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Discussion stats
  • 8 replies
  • 1287 views
  • 0 likes
  • 3 in conversation