BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
afgdurrani0
Pyrite | Level 9

R^2 is unfortunately not a part of the output from the nonlinear regression (e.g. below) in the newer versions of SAS. But I need the adjusted r2 value or simple r2 (coefficient of determination) for my research manuscript. Please kindly modify my below proc programming that could also estimate the R2.

Many thanks

proc nlin data=small method=marquardt;

parms B=0.5 R=0.1;

delta=0.000001;

s=sow**(1-B)-R*(1-B)*time;

if s>0 then stx=s**(1/(1-B));

else stx=0;

model stw=stx;

sb=(sow**(1-B+delta)-R*(1-B+delta)*time);

if sb>0 then sdb=(stx-sb**(1/(1-B+delta)))/delta;

else sdb=0;

  1. der.B=sdb;

sr=(sow**(1-B)-(R-delta)*(1-B)*time);

if sr>0 then sdr=(stx-sr**(1/(1-B)))/delta;

else sdr=0;

  1. der.R=sdr;

output out=smallp p=pstw;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

change the line

output out=smallp p=pstw;


to

output out=smallp p=pstw r=stw_residual;


Then, this ought to work (untested code)

proc summary data=smallp;

     var stw_residual stw;

     output out=stats css(stw)=sstot uss(stw_residual)=ssres;

run;

data final;

     set stats;

     rsquared=1-(ssres/sstot);

run;

--
Paige Miller

View solution in original post

39 REPLIES 39
PaigeMiller
Diamond | Level 26

Using the residual values that your can output into your dataset smallp, the calculation of R-squared is pretty simple. Why, I believe even Wikipedia has the proper formulas to use.

--
Paige Miller
afgdurrani0
Pyrite | Level 9

Dearest Miller, I don't have very strong knowledge of these programming that's why I need someone expert like you to insert/modify my present programming that could also estimate the R^2.

PaigeMiller
Diamond | Level 26

change the line

output out=smallp p=pstw;


to

output out=smallp p=pstw r=stw_residual;


Then, this ought to work (untested code)

proc summary data=smallp;

     var stw_residual stw;

     output out=stats css(stw)=sstot uss(stw_residual)=ssres;

run;

data final;

     set stats;

     rsquared=1-(ssres/sstot);

run;

--
Paige Miller
afgdurrani0
Pyrite | Level 9

No result for R^2 Smiley Sad . It shows nothing about R^2

Here is the complete data, please use it if you want

data large;

input sow stw time;

datalines;

4 3.366 3

4 3.052 6

4 2.666 9

4 2.755 12

4 2.203 15

4 1.886 18

3.982 1.525 21

3.882 1.547 24

3.393 0.81 27

3.586 0.76 30

2.295 0.27 33

3.475 0.67 36

;

proc nlin data=large method=marquardt;

parms B=0.5 R=0.1;

delta=0.000001;

s=sow**(1-B)-R*(1-B)*time;

if s>0 then stx=s**(1/(1-B));

else stx=0;

model stw=stx;

sb=(sow**(1-B+delta)-R*(1-B+delta)*time);

if sb>0 then sdb=(stx-sb**(1/(1-B+delta)))/delta;

else sdb=0;

der.B=sdb;

sr=(sow**(1-B)-(R-delta)*(1-B)*time);

if sr>0 then sdr=(stx-sr**(1/(1-B)))/delta;

else sdr=0;

der.R=sdr;

output out=largep p=pstw;

run;

PaigeMiller
Diamond | Level 26

Is there an error in the SAS LOG?

--
Paige Miller
afgdurrani0
Pyrite | Level 9

No , nothing like that. But before it use to produce R^2 along with my desired parameters but now the newer version doesn't generate the R^2. that's why I want to modify my persent programming to make it capable to produce R^2 (or AIC at least)

PaigeMiller
Diamond | Level 26

Reading your messages, I don't see where you stated that you actually tried to run the code I provided. Did you do that? What happened?

--
Paige Miller
afgdurrani0
Pyrite | Level 9

Yes sir of course I did but no result. I try your both suggested but no result. You can also copy past my data in sas program and can see the result.

Many thanks

Reeza
Super User

@umarkhan  You didn't add the r=stw_residual to the PROC NLIN program in the output statement.

The PROC summary should point to this data set instead of the smallp dataset in code.

With these changes it runs fine for me with your sample data.

I'll assume you can make the changes on your system.

PS, Thank you for the sample data/code but It's helpful to include the full code you ran.

afgdurrani0
Pyrite | Level 9

I added r=stw_residual in the output statement but no result about R^2. The one I give is the original form of my programming without any addition. But I modified the output statement and inserted r=stw_residual in the output as I was suggested, but result was similar as without this modification.

Reeza
Super User

Post your full code. The code above doesn't include the portion Paige suggested.

afgdurrani0
Pyrite | Level 9

Here is the full code .

data large;

input sow stw time;

datalines;

4 3.366 3

4 3.052 6

4 2.666 9

4 2.755 12

4 2.203 15

4 1.886 18

3.982 1.525 21

3.882 1.547 24

3.393 0.81 27

3.586 0.76 30

2.295 0.27 33

3.475 0.67 36

;

proc nlin data=large method=marquardt;

parms B=0.5 R=0.1;

delta=0.000001;

s=sow**(1-B)-R*(1-B)*time;

if s>0 then stx=s**(1/(1-B));

else stx=0;

model stw=stx;

sb=(sow**(1-B+delta)-R*(1-B+delta)*time);

if sb>0 then sdb=(stx-sb**(1/(1-B+delta)))/delta;

else sdb=0;

der.B=sdb;

sr=(sow**(1-B)-(R-delta)*(1-B)*time);

if sr>0 then sdr=(stx-sr**(1/(1-B)))/delta;

else sdr=0;

der.R=sdr;

output out=largep p=pstw r=stw_residual;

run;

Reeza
Super User

Go back and read Paiges answer. Where's the proc summary? Proc NLIN does not calculate the R-squared directly.

change the line

output out=smallp p=pstw;


to

output out=smallp p=pstw r=stw_residual;


Then, this ought to work (untested code)

proc summary data=smallp;

     var stw_residual stw;

     output out=stats css(stw)=sstot uss(stw_residual)=ssres;

run;

data final;

     set stats;

     rsquared=1-(ssres/sstot);

run;

afgdurrani0
Pyrite | Level 9

I also run like that too: but same result nothing about R^2.

data large;

input sow stw time;

datalines;

4 3.366 3

4 3.052 6

4 2.666 9

4 2.755 12

4 2.203 15

4 1.886 18

3.982 1.525 21

3.882 1.547 24

3.393 0.81 27

3.586 0.76 30

2.295 0.27 33

3.475 0.67 36

;

proc nlin data=large method=marquardt;

parms B=0.5 R=0.1;

delta=0.000001;

s=sow**(1-B)-R*(1-B)*time;

if s>0 then stx=s**(1/(1-B));

else stx=0;

model stw=stx;

sb=(sow**(1-B+delta)-R*(1-B+delta)*time);

if sb>0 then sdb=(stx-sb**(1/(1-B+delta)))/delta;

else sdb=0;

der.B=sdb;

sr=(sow**(1-B)-(R-delta)*(1-B)*time);

if sr>0 then sdr=(stx-sr**(1/(1-B)))/delta;

else sdr=0;

der.R=sdr;

output out=largep p=pstw r=stw_residual;

run;

proc summary data=largep;

     var stw_residual stw;

     output out=stats css(stw)=sstot uss(stw_residual)=ssres;

run;

data final;

     set stats;

     rsquared=1-(ssres/sstot);

run;

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!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 39 replies
  • 3728 views
  • 20 likes
  • 4 in conversation