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.
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.
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."
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;
The link in my response requires only SAS/STAT and the DATA step. Check out the link.
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;
For more on this topic, including a complete example, see
"Find points where a regression curve has zero slope."
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!
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.