Note that there are some explanatory texts on larger screens.

plurals
  1. POOdd Pearson correlation coefficient results
    text
    copied!<p>I have an assignment in my C programming class to write a program to get the correlation coefficient of 2 sets of real numbers. I've been given the equations, and it referenced wikipedia so I double checked the equations there. Here is a link to the equation, which seems to be pretty standard from my research:</p> <p><img src="https://upload.wikimedia.org/math/0/4/e/04e3ee493ddb1f01e03d8bf024fbd0a5.png" alt="alt text"></p> <p>I've written the program, but when I ran it I was getting numbers greater than 1 for my results, which I knew wasn't correct. I looked over my code several times but couldn't find anything out of place, so I tried dividing by n at the end instead of n-1, this gave me values with the -1 to 1 range that I expected, so i tested it against data values that I found online as well as a correlation coefficient calculator ( <a href="http://easycalculation.com/statistics/correlation.php" rel="nofollow noreferrer">http://easycalculation.com/statistics/correlation.php</a> ) and I'm now getting correct results for all of the numbers I input. I can't figure out why this is, so thought I might be able to get a little help with it here. Here is my code for the program, If there is anything else that stands out that I have done wrong here I would love to hear some advice, but mostly I'm trying to figure out why I'm getting the right results with what appears to be the wrong equation.</p> <p>It will then read in the values for both arrays(x and y), and then computes the correlation coefficient between the 2 sets of numbers.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;math.h&gt; int main(void) { int n; /* value to determine array length */ /* declare variables to hold results for each equation for x and y initialize all to zero to prepare for summation */ float r = 0.0, xbar = 0.0, ybar = 0.0, sx = 0.0, sy = 0.0; /*get number n input from user */ printf("Please enter a number n: "); scanf("%d", &amp;n); if( n &lt; 1) { printf("n must be a positive number.\nPlease enter a new value: "); scanf("%d", &amp;n); if( n &lt; 1) { printf("Invalid input, exiting...\n"); return 0; } } /*initialize arrays x and y with length of n */ float x[n], y[n]; /*use for loop to read in values of x*/ int i; for(i = 0; i &lt; n; ++i) { printf("Please enter a number for x: "); scanf("%f", &amp;x[i]); } /*use for loop to read in values of y*/ for(i = 0; i &lt; n; ++i) { printf("Please enter a number for y: "); scanf("%f", &amp;y[i]); } /*compute xbar */ for(i = 0; i &lt; n; ++i) { xbar += x[i]; } xbar /= n; /*compute ybar*/ for(i = 0; i &lt; n; ++i) { ybar += y[i]; } ybar /= n; /* compute standard deviation of x*/ for(i = 0; i &lt; n; ++i) { sx += (x[i] - xbar) * (x[i] - xbar); } sx = sqrt((sx / n)); /* compute standard deviation of y */ for(i = 0; i &lt; n; ++i) { sy += (y[i] - ybar) * (y[i] - ybar); } sy = sqrt((sy / n)); /*compute r, the correlation coefficient between the two arrays */ for( i = 0; i &lt; n; ++i ) { r += (((x[i] - xbar)/sx) * ((y[i] - ybar)/sy)); } r /= (n); /* originally divided by n-1, but gave incorrect results dividing by n instead produces the desired output */ /* print results */ printf("The correlation coefficient of the entered lists is: %6.4f\n", r); return 0; } </code></pre> <p>(it looks like my code formatting isn't working, very sorry about this. Tried using tags and the button but can't figure it out. It looks like I got it working somewhat, better than before.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload