Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Analytics
- /
- Stat Procs
- /
- plot the correlation between 2 functions

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 10-24-2017 05:53 AM
(1782 views)

Hello,

My problem is the following one:

I have 2 function A and B. The parameters m is in [0.001,30]. We have tau_1=1 and tau_2=1.

I wonder if there is a way plot the corrélation between my function A and B on SAS?

thank you for your help

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

The distribution of M determines the correlation between A(m) and B(m). If you choose M ~ lognormal you will get a different answer from if you choose M ~ gamma or M ~ exponential.

Since you didn't specify the distribution of M, the following DATA step chooses equally spaced values in [1,30] along with some small values. This should give the general idea of how to do the computation:

```
/* evaluate A(m) and B(m) for range of m
THE LOCATIONS OF m THAT YOU CHOOSE AFFECT THE CORRELATION */
data AB;
tau2 = 2;
do tau1 = 0.0001, 0.001, 0.01, 0.1, 1 to 30;
/* probably want a DISTRIBUTION for m */
do m = 0.0001, 0.001, 0.01, 0.1, 1 to 30;
c1 = -m/tau1;
c2 = -m/tau2;
A = (1 - exp(c1)) / c1 - exp(c1);
B = c2**2 * exp(c2);
output;
end;
end;
drop c1 c2;
run;
/* compute correlation matrices for each tau1 value */
proc corr data=AB noprint
outp=OutCorr(where=(_TYPE_="CORR") rename=(B=Corr));
by tau1;
var A B;
run;
/* we only want the correlation coefficient; extract the R[1,2] cell */
data Corr;
set OutCorr;
if mod(_N_,2)=1; /* Keep odd-numbered rows; delete even rows */
drop _TYPE_ _NAME_ A;
run;
title "Corr(A,B), tau2=2";
proc sgplot data=Corr;
series x=tau1 y=Corr;
run;
```

If you choose a distribution for M, then the inner loop will be

do i = 1 to 10000;

m = rand("disrib_name", params);

c1= ..; c2=..; etc;

end;

10 REPLIES 10

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

This might help:

I will move the post to stats section of forum also.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thank you for your reply.

I have a technical question about the correlation matrix:

Do you know how to keep only the value of the corr between C_0 and P_0, C_1 and P_1, C_2 and P_2... in a table?

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

@madix wrote:

Thank you for your reply.

I have a technical question about the correlation matrix:

Do you know how to keep only the value of the corr between C_0 and P_0, C_1 and P_1, C_2 and P_2... in a table?

It wouldn't hurt to show the code you used to generate that.

Assuming you used Proc Corr and want Pearson correlation statistics then the option OUTP=datassetname will place the statistics into a dataset named on the option.

I am not sure exactly what you mean by "between C_0 and P_0, C_1 and P_1, C_2 and P_2" but you should be able to transform the resulting dataset to have the contents and structure you want.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Why not show the **relation** between the two functions as:

```
proc fcmp outlib=sasuser.fcmp.math;
function A(m);
return ((1-exp(-m))/m - exp(-m));
endsub;
function B(m);
return (m**2 * exp(-m));
endsub;
run;
options cmplib=sasuser.fcmp;
data test;
do lm = log(.001) to log(30) by log(30/0.001)/100;
m = exp(lm);
A = A(m);
B = B(m);
output;
end;
run;
proc sgplot data=test;
series x=a y=b / markers
markerattrs=(symbol=circlefilled size=5)
lineattrs=(thickness=2);
run;
```

PG

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Interesting Relationship...

PG

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

First thank you for all your reply.

For the correlation I meant the cov(X,Y) by std_X and std_y.

I would like to plot the correlation of the function A(m) and B(m) for m in [0.000001,30], tau1 fixed equal to 2 and tau2 in [0.0001,30].

the x axis will be tau2 and the y axis will be corr(A,B)

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

So there are no random variables in your problem? The variable M is not random with a distribution?

Do you have SAS/IML software or do you require the solution to use Base SAS?

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

The distribution of M determines the correlation between A(m) and B(m). If you choose M ~ lognormal you will get a different answer from if you choose M ~ gamma or M ~ exponential.

Since you didn't specify the distribution of M, the following DATA step chooses equally spaced values in [1,30] along with some small values. This should give the general idea of how to do the computation:

```
/* evaluate A(m) and B(m) for range of m
THE LOCATIONS OF m THAT YOU CHOOSE AFFECT THE CORRELATION */
data AB;
tau2 = 2;
do tau1 = 0.0001, 0.001, 0.01, 0.1, 1 to 30;
/* probably want a DISTRIBUTION for m */
do m = 0.0001, 0.001, 0.01, 0.1, 1 to 30;
c1 = -m/tau1;
c2 = -m/tau2;
A = (1 - exp(c1)) / c1 - exp(c1);
B = c2**2 * exp(c2);
output;
end;
end;
drop c1 c2;
run;
/* compute correlation matrices for each tau1 value */
proc corr data=AB noprint
outp=OutCorr(where=(_TYPE_="CORR") rename=(B=Corr));
by tau1;
var A B;
run;
/* we only want the correlation coefficient; extract the R[1,2] cell */
data Corr;
set OutCorr;
if mod(_N_,2)=1; /* Keep odd-numbered rows; delete even rows */
drop _TYPE_ _NAME_ A;
run;
title "Corr(A,B), tau2=2";
proc sgplot data=Corr;
series x=tau1 y=Corr;
run;
```

If you choose a distribution for M, then the inner loop will be

do i = 1 to 10000;

m = rand("disrib_name", params);

c1= ..; c2=..; etc;

end;

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. **Registration is now open through August 30th**. Visit the SAS Hackathon homepage.

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.