BookmarkSubscribeRSS Feed
drshashlik
Fluorite | Level 6

Hi all,

I have time series data of daily observations for an event.  There is one observation per day, and it is always >=0.

I am using proc loess ad/or proc sgplot with a penalised b spline to fit a smooth curve.

I want to identify the points where the curve turns.  Ideally, I need to see where the slope=0 and where the slope becomes negative.

Any help greatly appreciated!

thank you.

8 REPLIES 8
Ksharp
Super User

Check the current point and previous point, next point 's slope .

If 

(Yt-Yt-1)/(t- t-1)    and  (Yt+1-Yt)/(t+1 - t)

have different sign (+ or -) , then t is the point which have 0 slope .

 

@Rick_SAS wrote a blog about it before.

Rick_SAS
SAS Super FREQ

You can use finite-difference approximations to estimate the slope of a nonparametric regression model. See the discussion and example in the article "Compute derivatives for nonparametric regression models."

 

PGStats
Opal | Level 21

Here is an example using proc loess and proc expand to process consecutive time series observations:

 

proc loess data=sashelp.ENSO plots=none;
   model Pressure=Month;
   score ;
   ods output ScoreResults=sr;
run;

proc expand data=sr out=srd;
convert p_pressure=y / transformout=(dif sign movsum 2 = 0 + 1 lead);
run;

/* Show the locations of the slope changes (where y = 1) on a graph */ data graph; set srd; if y then pp_pressure = p_pressure; run; title "Slope changes"; proc sgplot data=graph noautolegend; scatter x=month y=pressure; series x=month y=p_pressure; scatter x=month y=pp_pressure / markerattrs=(symbol=circlefilled color=red); run;

image.png

PG
drshashlik
Fluorite | Level 6
Thank you, i think this looks exactly like what I want. BUT, I don't have access to proc expand. (I'm using EG, but even though proc expand comes up in the options, i get the error message that the procedure was not found.
Is there a workaround for this problem?
thanks again!
Rick_SAS
SAS Super FREQ

The link in my response requires only SAS/STAT and the DATA step. Check out the link.

drshashlik
Fluorite | Level 6
Thank you Rick_SAS. I am working through your method, and get this error from this step:
/* score the model at each time point */
proc plm restore=SplineModel noprint;
score data=ScoreData out=ScoreOut; /* Predicted column contains f(x+h) and f(x-h) */
run;
WARNING: At least one variable used in the model is not present or has incompatible type in the WORK.SCOREDATA score data set. All computed score variables are set to missing in WORK.SCOREOUT.
I can't figure out which step has caused this issue.
Rick_SAS
SAS Super FREQ

Sorry for the confusion. All I meant is that you don't need to use PROC EXPAND to compute the locations where the derivative of the smoother is zero. I intended for you to use PGStats program, but use a DATA step to compute the derivative. Like this:

 

data Have;
set Sashelp.Enso;
x = Month;
y = Pressure;
run;

/* Put min and max into macro variables */
proc sql noprint;
  select min(x), max(x) into :min_x, :max_x 
  from Have;
quit;

/* compute derivatives at specified points in interval [min(x), max(x)] */
data ScoreData;
do x = &min_x to &max_x by 0.05;   /* choose the step size wisely */
   output;
end;
run;

ods select none;
proc loess data=Have plots=none;
    model y = x;
    score data=ScoreData;
    ods output ScoreResults=ScoreOut;
run;
ods select all;

/* compute first derivative by using finite difference formula.
  Output locations where the derivative is approximately zero  */
data Deriv0;
set ScoreOut;
Slope = dif(p_y) / dif(x);  /* [f(x) - f(x-dx)] / dx */
SlopePrev = lag(Slope);
if n(SlopePrev) AND sign(SlopePrev) ^= sign(Slope) then output; 
run;

data Combine;
merge Have
      ScoreOut(rename=(x=t p_y=p_t))
      Deriv0(rename=(x=xx p_y=yy));
run;

title "Loess Smoother";
title2 "Values with Zero Derivatives Are Marked";
proc sgplot data=Combine noautolegend;
   scatter x=x y=y;
   series x=t y=P_t / lineattrs=GraphData2;
   scatter x=xx y=yy / markerattrs=(symbol=circlefilled color=red);
   yaxis grid; 
run;
Rick_SAS
SAS Super FREQ

For more on this topic, including a complete example, see 

"Find points where a regression curve has zero slope."

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 1152 views
  • 1 like
  • 4 in conversation