Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that calculates the linear regression coefficients v and b using

ID: 642528 • Letter: W

Question

Write a program that calculates the linear regression coefficients v and b using the least squares method. Your program must import statlib.c to calculate the mean of t and y. The first dimension contains the t values and the second contains the y values

//statlib.c program

#include

#include

#include


//calculate mean
double mean(int totnum, double input[])

{

double sum=0;

int i=0;

for(i=0;i

{

sum=sum+input[i];

}

return sum/totnum;

}


//calculate variance
double variance(int totnum,double input[])

{

double sum=0;

double meanValue=mean(totnum,input);

int i=0;

for(i;i

{

sum=sum+(input[i]-meanValue)*(input[i]-meanValue);

}


return sum/totnum;

}


//calculate standard deviance
double stdDev(int totnum,double input[])

{

double varianceValue=variance(totnum,input);

return sqrt(varianceValue);

}


//sort the data
void sort(int totnum,double input[])

{
int c, d;

double t;

//find smallest remanding part of array
for (c = 0 ; c < ( totnum - 1 ); c++)
{
for (d = 0 ; d < totnum - c - 1; d++)

{
if (input[d] > input[d+1])

{

/* Swapping */

t = input[d];

input[d] = input[d+1];

input[d+1] = t;

}

}

}

}


//calculate minimum value of the data
double minValue(int totnum,double input[])

{

//assuming input input is sorted

// find if sorting is ascending/descending?

if(input[0] > input[1]) return input[totnum-1];

else return input[0];

}


// calculate the median of the data
double median(int totnum,double input[])

{

if(totnum%2 ==0) //even elements

{

return ((input[totnum/2-1]+input[totnum/2 ])/2);

}

else return input[totnum/2];

}


// find the max value of the data
double maxValue(int totnum,double input[])

{

//assuming input input is sorted

//lets find if sorting is ascending/descending?

if(input[0] > input[1]) return input[0];

else return input[totnum-1];

}

: (linreg.c) Write a program that calculates the linear regression coefficients v and b using the least squares method. Your program must import statlib.c to calculate the mean of t and y. The first dimension contains the t values and the second contains the y values y = b + vt In put array: double input [][10] = {{0, 5, 10, 15, 20, 25, 30, 35, 40, 45}, {0, 13, 22, 37, 54, 62, 64, 100, 112, 126}}; Sample output:

Explanation / Answer

Linear regression coefficients:

Linear regression is an approach for modelling the relationship between a scalar dependent variable y and one or more explantary variables denoted X.In linear regression,data are modelled using linear predictor functions,and unknown modelled parameters are estimated from the data.Such models are called linear models.

#include<stdio.h>

#include<conio.h>

#include<math.h>

calculate mean

double mean(int totnum,double input[])

{

double sum=0;

int i=0;

for(i=0;i<totnum;i++)

{

sum=sum+input[i];

}

return sum/totnum;

}

//calculate variance

double variance(int totnum,double input[])

{

double sum=0:

double mean value=mean(totnum,input);

int i=0;

for(i=0;i<totnum;i++)

{

sum=sum+(input[i]-meanvalue)*(input[i]-meanvalue);

}

return sum/totnum;

}

//calculate standard deviance

double stdDeviation(int totnum,double input[])

{

double variancevalue=Variance(totnum,input);

sdt=sqrt(var);

return sqrt(varianceValue);

}

//sort the data

void sort(int totnum,double input[])

{

int c,d;

double t;

//find smallest remainding part of array

for (c=0;c<(totnum-1);c++)

{

for (d=0;d<totnum-c-1;d++)

{

if (input[d]>input[d+1])

{

/*Swapping*/

(t)=input[d];

{

input[d]=input[d+1];

input[d+1]=t;

}

}

}

}

//calculate minimum value of the data

double minValue(int totnum,double input[])

{

//assuming input input is sorted

// find if sorting is ascending/descending?

for(i=1;i<totnum;i++)

if(input[0]>input[1])return input

[totnum-1];

else return input[0];

}

//calculate the median of the data

double median(int totnum,double input[])

{

if (totnum%2==0)//even elements

{

return ((input[totnum/2-1]+input[totnum/2])/2;

}

else return input [totnum/2];

}

//find the max value of the data

double maxvalue(int totnum,double input[])

{

//assuming input input is sorted

//lets find if sorting is ascending/descending?

for(i=1;i<totnum;i++)

if(input[0]>input[1]) return input[0];

else return input[totnum-1];

}

#include<stdio.h>

/*add code for statlib.c*/

#pragma importf<statlib.c>

/*declare extern functions so that the can be used*/

extern double mean(int totnum,double data[]);

//extern double variance(int totnum,double data[]);

//extern double stdDeviation(int totnum,double data[]);

//extern void sort(int totnum,double data[]);

//extern double minvalue(int totnum,double data[]);

//extern double median(int totnum,double data[]);

//extern double maxvalue(int totnum,double data[]);

int main()

{

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

int totnum=sizeof(input)/sizeof (double);

printf("The unsorted array is %d.",input);

printf("The mean is ")

mean(totnum, input);

}

Squares method:

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<string.h>

float mean(float*a,int n);

void deviation(float*a,float mean,int n,float*d,float*s);

void main()

{

float a[10],b[10],dx[10],dy[10];

float sy=0,st=0,mean_t=0,mean_y=0,sum_ty=0;

float corr_coff=0,reg_coff_ty=0,reg_coff_ty=0;

char type_coff[7];

int n=0,i=0;

clrscr();

printf("enter the value of n:");

scanf("%d",&n);

printf("enter the values of t and y: ");

for(i=0;i<n;i++)

scanf("%f%f",&a[i],&b[i]);

mean_t=mean(a,n);

mean_y=mean(b,n);

deviation(a,mean_t,n,dt,&st);

deviation(b,mean_y,n,dy,&sy);

for(i=0;i<n;i++)

sum_ty=sum_ty+dt[i]*dy[i];

corr_coff=sum_ty/(n*st*sy);

printf("enter the type of regression coefficient as 't on y' or 'y on t':");

fflush(stdin);

gets(type_coff);

if(strcmp(type_coff,"t on y")==1)

{

reg_coff_xy=corr_coff*(st/sy);

printf(" The value of linear regression coefficient is %f",reg_coff_ty);

}

else if (strcmp(type_coff,"y on t")==1)

{

reg_coff_yx=corr_coff*(sy/st);

printf(" The value of linear regression coefficient is %f",reg_coff_yt);

}

else

printf(" Enter the correct type of regression coefficient.");

getch();

}

float mean(float*a,int n)

{

float sum=0,i=0;

for(i=0;i<n;i++)

sum=sum+a[i];

sum=sum/n;

return(sum);

}

void deviation(float*a,float mean,int n,float*d,float*s)

{

float sum=0,t=0:

int i=0;

for(i=0;i<n;i++)

{

d[i]=a[i]-mean;

t=d[i]*d[i];

sum=sum+t;

}

sum=sum/n;

*s=sqrt(sum);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote