options pageno=1 linesize=80;
goptions reset=all;
title "Model for digestive process in striped bass";
title2 "Wetzel and Kohler unpublished data";
data fishguts;
input weight time;
datalines;
1.000 0.0
0.965 0.5
0.723 3.0
0.458 5.5
0.245 8.0
0.095 10.5
0.065 13.0
0.030 15.5
0.012 18.0
0.008 20.5
0.000 23.0
;
run;
* Print data set;
proc print data=fishguts;
run;
* Sort x-axis data for plots;
proc sort data=fishguts;
by time;
run;
* Plot data and fit smooth line;
proc gplot data=fishguts;
plot weight*time;
symbol1 i=sm50 v=star;
run;
* Nonlinear regression using joined linear + exponential model;
proc nlin data=fishguts;
* Models are joined at time = theta;
if time<theta then
model weight=1+b*time; * Linear part of model;
else
model weight=(1+b*theta)*exp(d*(time-theta)); * Exponential part;
* Initial guesses for parameter values;
parms b=-0.15 to -0.05 by 0.02 theta=10 d=-0.30 to -0.10 by 0.02;
* Boundaries for model parameters;
bounds b d < 0;
bounds theta >= 0;
output out=resids p=pred r=resid;
run;
* Print data, fitted values, and residuls;
proc print data=resids;
run;
* Plot data and fitted model;
proc gplot data=resids;
plot weight*time=1 pred*time=2 / overlay;
symbol1 i=none v=star c=black;
symbol2 i=j v=none c=blue;
run;
goptions reset=all;
title "Diagnostic plots for nonlinear regression assumptions";
* Plot residuals vs. predicted values;
proc gplot data=resids;
plot resid*pred;
run;
* Normal quantile plot of residuals;
proc univariate noprint data=resids;
qqplot resid / normal;
run;
quit;
Greeting All
I am trying to use nonlinear regression segmented linear models for the Day with temp and RH
I have this example and I need help to make it fit to my data to predict the Day
My Data is
RH | temp | Day |
94 | 10 | 65 |
62 | 26 | 11 |
75 | 22 | 13 |
93 | 10 | 51 |
62 | 25 | 10 |
75 | 22 | 14 |
94 | 10 | 70 |
62 | 26 | 10 |
75 | 22 | 14 |
93 | 9 | 78 |
62 | 26 | 11 |
75 | 22 | 11 |
95 | 27 | 14 |
75 | 30 | 8 |
80 | 24 | 8 |
95 | 27 | 13 |
75 | 30 | 6 |
80 | 24 | 8 |
94 | 27 | 12 |
75 | 30 | 7 |
80 | 24 | 6 |
95 | 27 | 14 |
That must be a very old program! In modern SAS you can just turn on ODS graphics and add PLOTS(UNPACK) to the PROC NLIN statement to get the fit plots, residual plots, and diagnostic plots created for you automatically!
The example has one explanatory variable (time) and one response variable (weight). Thus it makes sense to fit a piecewise model to the growth/decay curve. In your day, you've provided two regressors (RH and Temp). How do you intend to fit a piecewise model? Also, DAY is a discrete variable. Do you intend to model it as a continuous variable, a count variable, or something else?
If your goal is to model the response as a nonlinear function of two variables, I suggest a nonparametric regression procedure such as PROC GAMPL or PROC ADAPTIVEREG.
Many thanks for your quick reply and help.
I am trying to get an equation Day = X + or - Xtemp + or - XRH
When I run multiple linear regression and I plot it, it showed it was not linear (between 10 and 22 it was shifted) that way I am trying to use nonlinear regression segmented linear models.
Is there a code for thatI can use it.
You can explore the shape of the response (day) with a 2d spline fit:
data have;
input RH temp Day;
datalines;
94 10 65
62 26 11
75 22 13
93 10 51
62 25 10
75 22 14
94 10 70
62 26 10
75 22 14
93 9 78
62 26 11
75 22 11
95 27 14
75 30 8
80 24 8
95 27 13
75 30 6
80 24 8
94 27 12
75 30 7
80 24 6
95 27 14
;
proc g3grid data=have out=haveg;
grid temp*rh=day / axis1=9 to 30 by 1 axis2=60 to 95 by 1 spline smooth=0.01;
run;
proc template;
define statgraph contourplotparm;
begingraph;
entrytitle "Contour Plot of Day by temp and RH";
layout overlay / xaxisopts=(griddisplay=on) yaxisopts=(griddisplay=on) ;
contourplotparm x=temp y=RH z=day /
contourtype=labeledline nhint=12 gridded=true;
endlayout;
endgraph;
end;
run;
proc sgrender data=haveg template=contourplotparm;
run;
Thanks for your help.
This code just shows the shape, but did not show the relationship, is it linear or nonlinear.
It's hard to tell from the data above. The design points are clustered, leaving vast areas of the design space uncovered.. It may look linear or not, depending on how much smoothing you apply
proc g3grid data=have out=haveg;
grid temp*rh=day / axis1=9 to 30 by 1 axis2=60 to 95 by 1 spline smooth=0.01 1;
run;
proc sql;
create table haveGraph as
select unique
a.*,
b.RH as RHpoint,
b.temp as tempPoint,
b.day as dayPoint
from haveg as a left join
have as b on a.rh=b.rh and a.temp=b.temp
order by _smth_, rh, temp;
quit;
proc template;
define statgraph contourplotparm;
begingraph;
entrytitle "Contour Plot of Day by temp and RH";
layout overlay / xaxisopts=(griddisplay=on) yaxisopts=(griddisplay=on) ;
contourplotparm x=temp y=RH z=day /
contourtype=labeledline nhint=12 gridded=true;
scatterplot x=tempPoint y=rhPoint / datalabel=dayPoint
markerattrs=(symbol=circlefilled) jitter=auto;
endlayout;
endgraph;
end;
run;
ods graphics / height=700 width=700;
proc sgrender data=haveGraph template=contourplotparm;
by _smth_;
run;
Shoothing = 0.01:
Smoothing = 1.0:
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!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.