Question

In: Computer Science

Write a JavaScript program to implement the Least-Squares Linear Regression algorithm shown below, for an arbitrary...

Write a JavaScript program to implement the Least-Squares Linear Regression algorithm shown below, for an arbitrary set of ( x, y ) data points entered by the user.

Least-Squares Linear Regression: • Linear regression basically means finding the ( equation for the ) straight line that is the closest “fit” to a set of ( x, y ) data points.

• Least-squares is a means of identifying the “best” fit between the line and the data. For each data point, the distance between the point and the line is calculated and squared, and the total of these squared distances is added up over all the data points. The straight line that yields the minimum value for this sum of squares is defined as the best fit.

• calculate the correlation coefficient, r, which is a metric for how well the line fits the data, and if the correlation is positive or negative. This value always lies between -1.0 and 1.0 inclusive. The closer it is to 1.0, the stronger the positive correlation; The closer to -1.0 the stronger the negative correlation; and if the coefficient is close to zero, then it shows a weak correlation ( if any ) between x and y.

The Algorithm: 1. Read in a series of ( x, y ) data pairs. While doing so, calculate the following sums:

• N = number of data pairs.

• Sx = ∑ x = Summation ( total ) of all X values.

• Sy = ∑ y = Summation ( total ) of all Y values.

• Sxx = ∑ x2 = Summation ( total ) of all X2 values.

• Syy = ∑ y2 = Summation ( total ) of all Y2 values.

• Sxy = ∑ xy = Summation ( total ) of all X∙Y values.

2. Then calculate: • Slope, m = ( N ∙ Sxy – Sx ∙ Sy ) / ( N ∙ Sxx – Sx ∙ Sx ) • Intercept, b = ( Sy – m ∙ Sx ) / N

3. Report the best-fit line as y = m x + b

4. Finally calculate and report the correlation coefficient, r, as:

• ? = ? ∙ ???−?? ∙ ?? /√( ? ∙ ???−?? ∙ ?? ) ∙ ( ? ∙ ???−?? ∙ ?? )

Solutions

Expert Solution

Please find the working commented code below :

----------------------------------------------------------------------------------------------------------------------------------------------------

function leastSquareLinearRegression(x_array, y_array) {
    var sum_x = 0;
    var sum_y = 0;
    var sum_xy = 0;
    var sum_yy = 0;
    var sum_xx = 0;
    var count = 0;

    var x = 0;
    var y = 0;
    var input_length = x_array.length;

// find required sums
    for (let i = 0; i< input_length; i++) {
        x = x_array[i];
        y = y_array[i];
        sum_x+= x;
        sum_y+= y;
        sum_xx += x*x;
        sum_yy += y*y;
        sum_xy += x*y;
        count++;
    }

  
    // Find m and b:
   
    var m = (count*sum_xy - sum_x*sum_y) / (count*sum_xx - sum_x*sum_x);
    var b = (sum_y/count) - (m*sum_x)/count;

    document.write("Best fit line ( in the format y = mx+c ) is : <br>");
    document.write("y = " + m + "x + " + b + "<br>");

    // ? ∙ ???−?? ∙ ?? /√( ? ∙ ???−?? ∙ ?? ) ∙ ( ? ∙ ???−?? ∙ ?? )
var corr_coeff = input_length*(sum_xy)-(sum_x*sum_y);
corr_coeff = corr_coeff / Math.sqrt((input_length*sum_xx) - (sum_x*sum_x));
corr_coeff = corr_coeff / Math.sqrt((input_length*sum_yy) - (sum_y*sum_y));

    document.write("The correlation coefficient is : <br>");
    document.write(corr_coeff);
  
    return [ [], [] ];
}

var x = [1,2,3];
var y = [3,5,6];
document.write(leastSquareLinearRegression(x,y));

----------------------------------------------------------------------------------------------------------------------------------------------------

Please find the screenshot of working code below :


Related Solutions

What are Least Squares Assumptions for simple linear regression? For each least squares assumption, provide an...
What are Least Squares Assumptions for simple linear regression? For each least squares assumption, provide an example in which the assumption is valid, then provide an example in which the assumption fails.
A simple linear least squares regression of the heights (in feet) of a building on the...
A simple linear least squares regression of the heights (in feet) of a building on the number of stories in the building was performed using a random sample of 30 buildings. The associated ANOVA F statistic was 5.60. What is the P-value associated with this ANOVA F test? a.) greater than 0.10 b.) between 0.001 and 0.01 c.) between 0.01 and 0.025 d.) between 0.05 and 0.10 e.) between 0.025 and 0.05 f.) less than 0.001
In simple linear regression analysis, the least squares regression line minimizes the sum of the squared...
In simple linear regression analysis, the least squares regression line minimizes the sum of the squared differences between actual and predicted y values. True False
Prove that the least squares estimates in a simple linear regression model are unbiased. Be sure...
Prove that the least squares estimates in a simple linear regression model are unbiased. Be sure to state carefully the assumptions under which your proof holds.
Find the equation of the least-squares regression line ŷ and the linear correlation coefficient r for...
Find the equation of the least-squares regression line ŷ and the linear correlation coefficient r for the given data. Round the constants, a, b, and r, to the nearest hundredth. {(0, 10.8), (3, 11.3), (5, 11.2), (−4, 10.7), (1, 9.3)}
QUESTION 17 The process of creating a linear model of bivariate data. a. Least Squares Regression...
QUESTION 17 The process of creating a linear model of bivariate data. a. Least Squares Regression b. Variability c. Extrapolation d. Residual analysis QUESTION 18 The "Portion of Variability" is also known as the a. Correlation coefficient b. Regression line c. Fitted Value d. Coefficient of determination QUESTION 19 Linear regression models may not always acccurately reflect the pattern of data from which they are made a. TRUE b. FALSE QUESTION 20 The following data relates the time a student...
Write the below code to use HTML and JavaScript. 1. a) Write a JavaScript program to...
Write the below code to use HTML and JavaScript. 1. a) Write a JavaScript program to display the current day and time. b) Write a JavaScript program to print the contents of the current window.   c) Write a JavaScript program where the program takes a random integer between 1 to 10 d) Write a JavaScript program to calculate multiplication and division of two numbers (input from the user). e)Write a JavaScript program to create a new string from a given...
A least-squares simple linear regression model was fit predicting duration (in minutes) of a dive from...
A least-squares simple linear regression model was fit predicting duration (in minutes) of a dive from depth of the dive (in meters) from a sample of 45 penguins' diving depths and times. Calculate the F-statistic for the regression by filling in the ANOVA table. SS df MS F-statistic Regression Residual 1628.4056 Total 367385.9237
Q1. Write down the equation of the regression straight line (the least-squares line)
  Q1. Write down the equation of the regression straight line (the least-squares line) Q2. For an increase of 1 mg of fertiliser applied, what is the average change in the wet weight of maize plants? Q3.​​​​​​ ​How are the two variables associated with each other? (Answer in 1 or 2 sentences)Q4. Determine the average weight of plants grown with 100mg of fertiliser applied. (round up your answer to 2 decimal places)Q5. Determine the average weight of plants grown with...
what is the regression technique of ordinary least squares?
what is the regression technique of ordinary least squares?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT