Question

My program is meant to calculate the linear regression coefficients given the array "input" using least squares method. My issue is when attempting to calculate the slope of data. As you can see the array "input" is a 2 row array. I was able to separate the two rows as input[i] and input[j]. But in order to find the slope I need to use these two rows separately and increment them in the equation to solve for v. How do I use these two arrays to get the value of v?

Note: Using the input shown in the image below, v and be should be 2.795 and -3.891 respectively.

My assignment: http://i1306.photobucket.com/albums/s576/sammyr2011/assignment2question4_zps08a5464b.jpg

My code so far:

/*
*   Program: linreg.c
*   A program that calculates the linear regression coefficients
*   v and b using the least squares method.
*/

#include <stdio.h>
#include "forStatLib.h"

double input[][10] ={{0, 5, 10, 15, 20, 25, 30 ,35, 40, 45}, {0, 13, 22, 37, 54, 62, 64,     100, 112, 126}};

// Use the sum += when inputting array into equation

int main() {
int first, second, i, j, k;
double v, b, t, y;

//grab 1st row of the array
for(i=0; input[0][i] ; i++) {
    input[i]; 
}

//grab 2nd row of the array
for(i=0; input[1][i]; i++) {
    input[j];
}

/*calculate the average of both rows, first row is t and second is y. 
Then assign the averages of each as t_a and y_a. */
double t_a, y_a;
t_a = mean(10, input[i]);
y_a = mean(10, input[j]);

//calculate v (slope), Maybe use two loops with one nested?
// i need to increment
v = (  - t_a)(  - y_a) / ( )


} 
Was it helpful?

Solution

make these changes,

t_a = mean(10, input[0]);
y_a = mean(10, input[1]);

//calculate v (slope), Maybe use two loops with one nested?
// i need to increment
v1=0;
v2=0;
for(i=0; i< 10; i++)
{
 v1+ = ( input[0][i] - t_a)*( input[1][i] - y_a);
}
for(i=0; i< 10; i++)
{
 v2=v2+( input[0][i] - t_a)*( input[0][i] - t_a);
}
v=v1/v2;          //use float or double data type

input[][] is a 2D array so to access the elements use input[i][j] form.
and i dont know why you used,

for(i=0; input[0][i] ; i++) {
    input[i]; 
}

//grab 2nd row of the array
for(i=0; input[1][i]; i++) {
    input[j];               // j not initialized       
}

if to print the values use printf()

for(i=0; i<10 ; i++) {
        printf("%d",input[0][i]); 
    }

    //grab 2nd row of the array
    for(i=0; i<10; i++) {
        printf("%d",input[1][i]);
    }

OTHER TIPS

I wont give you the answer to your question or to your assignment, but something I think you will need a lot more at the moment: it seems you haven't really got a grasp on array manipulation. I'll try explain what your code is doing, so then you can modify / write / debug this and other code.

//grab 1st row of the array
for(i=0; input[0][i] ; i++) {
    input[i]; 
}

What you have done till here is to increment the values of of i step by step till input[0][i]=0. For loops are written as follows for(do_this_once_in_the_begining;Keep_looping_if_this_is_true;do_this_once_per_loop) In C false=0 and true=nonzero.

str="my string"; for(i=0; str[i] ; i++) will work because the last element of any string is the number 0 (not the ascii character'0'). This wont work for an array f integers.

In the body of the loop you just read each element of the array and did nothing to it. If you wanted to you might have put a printf("%f",input[0][j]) so you view the elements. However, this does not do anything else.

And you don't need to "grab" anything. Your array was all setup when you declared it.. nothing more needed to be done.

The last part:

Your formula has two separate sigmas that are calculated separately, so you will need to use two for loops (you can do it in one, try figure it out, it will go a long way in solving other problems) BUT, you are not required to take a sigma of a sigma, so you wont need to nest them.

For example just calculate the v_denominator like this:

double v_denom=0;
for(i=0;i<10;i++){
     v_denom+=( input[0][i] - t_a)*( input[0][i] - t_a);
}

try this

for(i=0 ; i < 10 ; i++)
v +=(input[0][i]-t_a)*(input[1][i]-t_y);//numerator

similarly calculate denominator and divide

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top