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.
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 )
The above problem can be solved by following the steps mentioned below-
STEP 1- We will define the js function that calculates the slope using least square linear regression method.
STEP 2- The function prompts the user to enter value of x data point or enter E to leave entering the data points. And if the user enters the value x, prompt him to enter the value y.
STEP 3- Check if the user has entered E, if yes then do not prompt user to enter any value again.
STEP 4- Steps 2 and 3 are coded in an infinite loop which breaks when user enters E.
STEP 5- After each iteration, value of summation of x, summation of y, summation of x2, summation of y2, summation of x.y is updated and stored in a variable. Also, count N keeps on getting incremented by 1.
STEP 6 - Using the formula to calculate the value of slope m, ( N ∙ Sxy – Sx ∙ Sy ) / ( N ∙ Sxx – Sx ∙ Sx ), calculate the value and display it to the user.
The code has been run on Google Chrome developer tools console and user input is taken by prompt.
JavaScript Code-
//define the javascript function
function LeastSquareLR(){
//define and initialize the variables whereever
needed
var x; //to store x value of data point
var y ; //to store y value of data
point
var Sx = 0, Sy = 0, Sxx = 0, Syy = 0, Sxy = 0,N =
0,i;
//infinite loop to keep on entering data points until
user wants to end input
while(true){
//prompt the user to enter
x = prompt("Enter the value of x or
E to exit");
//if input is E, break the
loop
if(x == 'E')
break;
x = parseInt(x);
//user enters y value
y = parseInt(prompt("Enter the
value of y"));
Sx = Sx + x;
//summation of x
Sy = Sy + y;
//summation of y
Sxx = Sxx + (x*x);
//summation of x^2
Syy = Syy + (y*y);
//summation of y^2
Sxy = Sxy + (x*y);
//summation of x*y
//increment N
N++;
}
//user formula to calculate slope m
var m = ((N*Sxy -(Sx*Sy))/(N*Sxx - (Sx*Sx)));
//display the result upto 3 decimal places
console.log(m.toFixed(3));
}
//call the above function
LeastSquareLR();
Image of
Code-
Output-
User inputs-
Console Output-
If this answer helps, please give an up vote. Feel free to comment for any query.
Write a JavaScript program to implement the Least-Squares Linear Regression algorithm shown below, for an arbitrary...
Using the data below find the slope and intercept for the least squares regression line. (SHOW ALL STEPS/WORK) x y xy x2 y2 16 109 1744 256 11881 25 122 3050 625 14884 39 143 5577 1521 20449 45 132 5940 2025 17424 49 199 9751 2401 39601 64 185 11840 4096 34225 70 199 13930 4900 39601 29 130 3770 841 16900 57 175 9975 3249 30625 22 118 2596 484 13924 416 1512 68173 20398 239514
Pleass write Java Program with comments. For this peoblem it
supposed to create ur own list of points, then use that list to
find the closest point. A list of points and the points have x and
y values such as [(1,2),(3,5),(6,7),....] that’s just example.
Implement the algorithm to meet the following requirements Define a class named Pair with data fields pl and p2 to represent two points and a method named getDistance) that returns the distance between the two...
2. If n points are connected to form a closed polygon as shown below, the area A of the polygon can be computed as n-2 Notice that although the illustrated polygon has only six distinct corners, n for this polygon is 7 because the algorithm expects that the last point, (es yo), wil be a repeat of the initial point, (ox Yo) (x0,y0) = (xi.y1) x2,y2) (x3.V3) (x5-%) (x4 Y4) a. Name your program file lab9p4 b. Do not write...
MATLAB only please
I am trying to get my getdatafuntion to work. I am also trying
to get all my x's, y's, and v's to popup in the command window so I
can put in any value and it will find the value for me using
the equation I put in.
function project_9_sjl()
% PROJECT_9_SJL project_9_sjl() is the driver function for the
program.
%
% Name: Scott Lawrence
% Date: 3/27/2019
% Class: CMPSC 200
% Description: Determine the optimal...
A group of physics students collected data from a test of the projectile motion problem that was analyzed in a previous lab exercise (L5). In their test, the students varied the angle and initial velocity Vo at which the projectile was launched, and then measured the resulting time of flight (tright). Note that tright was the dependent variable, while and Vo were independent variables. The results are listed below. (degrees) Time of Flight (s) Initial Velocity V. (m/s) 15 20...
Consider a cylindrical capacitor like that shown in Fig. 24.6. Let d = rb − ra be the spacing between the inner and outer conductors. (a) Let the radii of the two conductors be only slightly different, so that d << ra. Show that the result derived in Example 24.4 (Section 24.1) for the capacitance of a cylindrical capacitor then reduces to Eq. (24.2), the equation for the capacitance of a parallel-plate capacitor, with A being the surface area of...