Calculate slope and intercept value for logarithmic trendline as excel

0 votes

I'm creating logarithmic trendlines with the help of an Excel algorithm. However, I am unable to calculate the slope and intercept values as done in Excel. I believe my formula contained several errors.

This is my code

 var X= [10, 25, 30, 40]; Y= [5, 4, 7, 12];

 var Slope, Intercept, SX = 0, SY = 0,
            SXX = 0, SXY = 0, SYY = 0,
            SumProduct = 0, N = X.length;

            for (var i = 0; i < N; i++) {
                SX = SX + X[i];
                SY = SY + Y[i];
                SXY = SXY + X[i] * Y[i];
                SXX = SXX + X[i] * X[i];
                SYY = SYY + Y[i] * Y[i];
            }

            Slope = ((N * SXY) - (SX * SY)) / ((N * SXX) - (SX * SX));

            Intercept = (SY - (Slope * SX)) / N;

Fiddle link

Excel Formula:

Logarithmic Equation: y=(c*LN(x))+b

where:

c = INDEX(LINEST(y,LN(x)),1)
b = INDEX(LINEST(y,LN(x)),1,2)

Screenshot for Excel output

enter image description here

Please suggest how to derive the Excel formula in JavaScript.

Nov 8 in Others by Kithuzzz
• 20,660 points
45 views

1 answer to this question.

0 votes

You missed Math.log() for ln() in Excel. Edit for parts like this.

for (var i = 0; i < N; i++) {
    SX = SX + Math.log(X[i]);
    SY = SY + Y[i];
    SXY = SXY + Math.log(X[i]) * Y[i];
    SXX = SXX + Math.log(X[i]) * Math.log(X[i]);
    SYY = SYY + Y[i] * Y[i];
}

I've verified the output is the same as Excel.

> Slope

3.8860409979365333

> Intercept

-5.252238189415747
answered Nov 8 by narikkadan
• 37,660 points

Related Questions In Others

0 votes
0 answers

How is it possible for DynamoDB to support both Key-Value and Document database properties at the same time

As per DynamoDB's documentation, it supports both ...READ MORE

Apr 5 in Others by Kichu
• 19,040 points
200 views
0 votes
1 answer

Create Excel file and save as PDF.

Office 2013 still has the Interop library and ...READ MORE

answered Oct 11 in Others by narikkadan
• 37,660 points
109 views
0 votes
1 answer

How do you calculate the Quintile for groups of rows in Excel?

Use this formula: =MAX(1,ROUNDUP(10*PERCENTRANK($C:$C,$C2,4),0)) To divide into whichever many ...READ MORE

answered Oct 17 in Others by narikkadan
• 37,660 points
37 views
0 votes
1 answer

I want to make Excel read a value in Calc and copy it to my sheet in Excel

Here is the sample code that will allow ...READ MORE

answered Oct 27 in Others by narikkadan
• 37,660 points
39 views
0 votes
0 answers

Convert Rows to Columns with values in Excel using custom format

1 I having a Excel sheet with 1 ...READ MORE

Feb 17 in Others by Edureka
• 13,640 points
109 views
0 votes
1 answer

Remove formulas from all worksheets in Excel using VBA

Try this : Option Explicit Sub test1() ...READ MORE

answered Oct 3 in Others by narikkadan
• 37,660 points
93 views
0 votes
1 answer

Calculate monthly average from daily data without PivotTable

Assuming you have the months in column D enter ...READ MORE

answered Oct 3 in Others by narikkadan
• 37,660 points
69 views
0 votes
1 answer

Automate compound annual growth rate (CAGR) calculation

The following PowerPivot DAX formulas worked for ...READ MORE

answered Oct 7 in Others by narikkadan
• 37,660 points
89 views
0 votes
1 answer

Thousand and million formatting for negative numbers (excel/ googlesheets)

Its not possible. What you could do is ...READ MORE

answered Sep 25 in Others by narikkadan
• 37,660 points
64 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP