Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

regression to the mean using javascript

function linearRegression(y,x){
        var lr = {};
        var n = y.length;
        var sum_x = 0;
        var sum_y = 0;
        var sum_xy = 0;
        var sum_xx = 0;
        var sum_yy = 0;

        for (var i = 0; i < y.length; i++) {

            sum_x += x[i];
            sum_y += y[i];
            sum_xy += (x[i]*y[i]);
            sum_xx += (x[i]*x[i]);
            sum_yy += (y[i]*y[i]);
        } 

        lr['slope'] = (n * sum_xy - sum_x * sum_y) / (n*sum_xx - sum_x * sum_x);
        lr['intercept'] = (sum_y - lr.slope * sum_x)/n;
        lr['r2'] = Math.pow((n*sum_xy - sum_x*sum_y)/Math.sqrt((n*sum_xx-sum_x*sum_x)*(n*sum_yy-sum_y*sum_y)),2);

        return lr;
}
Comment

regression to the mean using javascript

var known_y = [1, 2, 3, 4];
var known_x = [5.2, 5.7, 5.0, 4.2];

var lr = linearRegression(known_y, known_x);
// now you have:
// lr.slope
// lr.intercept
// lr.r2
Comment

regression to the mean using javascript

const regress = (x, y) => {
    const n = y.length;
    let sx = 0;
    let sy = 0;
    let sxy = 0;
    let sxx = 0;
    let syy = 0;
    for (let i = 0; i < n; i++) {
        sx += x[i];
        sy += y[i];
        sxy += x[i] * y[i];
        sxx += x[i] * x[i];
        syy += y[i] * y[i];
    }
    const mx = sx / n;
    const my = sy / n;
    const yy = n * syy - sy * sy;
    const xx = n * sxx - sx * sx;
    const xy = n * sxy - sx * sy;
    const slope = xy / xx;
    const intercept = my - slope * mx;
    const r = xy / Math.sqrt(xx * yy);
    const r2 = Math.pow(r,2);
    let sst = 0;
    for (let i = 0; i < n; i++) {
       sst += Math.pow((y[i] - my), 2);
    }
    const sse = sst - r2 * sst;
    const see = Math.sqrt(sse / (n - 2));
    const ssr = sst - sse;
    return {slope, intercept, r, r2, sse, ssr, sst, sy, sx, see};
}
regress([1, 2, 3, 4, 5], [1, 2, 3, 4, 3]);
Comment

PREVIOUS NEXT
Code Example
Javascript :: createelement and set prop attr jquery 
Javascript :: argument and parameter 
Javascript :: disable button without losing value 
Javascript :: silk carousel jquery 
Javascript :: Refresh page after dialoge closes 
Javascript :: acender lampada javascript 
Javascript :: Detect when the BACKSPACE is pressed 
Javascript :: React Rendering Movies 
Javascript :: manifest.json background service worker vs scripts 
Javascript :: React Native - Trigger Media Scanner 
Javascript :: how to use moment in angular 8 
Javascript :: mengakses gambar didalam asset angular 
Javascript :: slick.js pb 
Javascript :: import lodash react 
Javascript :: javascript set contains 
Javascript :: how to create a snake game in html css js 
Javascript :: Reactjs exemple function component 
Javascript :: online validator json schema 2020-12/schema 
Javascript :: js check if field in database is true or false 
Javascript :: Zoho Creator Javascript Loop through more than 200 records 
Javascript :: Logical Assignment Operator null coalescing 
Javascript :: modify a string in javascript 
Javascript :: how to get last index of array in javascript 
Javascript :: ASPxGridView - How to trigger the CustomButtonCallback event 
Javascript :: string format javascript 
Javascript :: &lt;xsl:apply-templates select node text subnodes all 
Javascript :: javascript ignore a function if viewed in mobile 
Javascript :: node_modulesexpresslib outerindex.js:508 this.stack.push(layer); 
Javascript :: Private slots are new and can be created via Instance and static private fields 
Javascript :: typeorm sqlite Using async/await syntax 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =