Logarithmic Regression

30 views
Skip to first unread message

Ben Hur Bahia do Nascimento

unread,
Nov 18, 2014, 1:37:54 PM11/18/14
to accor...@googlegroups.com

Hello!
Small question: is there a way to do logarithmic regression?

Thanks for the good work!

César

unread,
Nov 18, 2014, 3:00:20 PM11/18/14
to accor...@googlegroups.com
Hi Ben Hur!

Thanks for the question! Yes, there is; logarithmic regression is roughly the same as linear regression with log-transformed variables. In short, all that is necessary to do to perform logarithmic regression is to first transform your inputs into logarithms then fit a simple linear regression in this transformed data. 

An example implementation is given below:

// This is the same data from the example available at
// http://mathbits.com/MathBits/TISection/Statistics2/logarithmic.htm


// Declare your inputs and output data
double[] inputs = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
double[] outputs = { 6, 9.5, 13, 15, 16.5, 17.5, 18.5, 19, 19.5, 19.7, 19.8 };


// Transform inputs to logarithms
double[] logx = Matrix.Log(inputs);

// Compute a simple linear regression
var lr = new SimpleLinearRegression();

// Compute with the log-transformed data
double error = lr.Regress(logx, outputs);


// Get an expression representing the learned regression model
// We just have to remember that 'x' will actually mean 'log(x)'
string result = lr.ToString("N4", CultureInfo.InvariantCulture);

// Result will be "y(x) = 6.1082x + 6.0993"



Hope it helps!

Best regards,
Cesar


Ben Hur Bahia do Nascimento

unread,
Nov 18, 2014, 3:05:51 PM11/18/14
to accor...@googlegroups.com
Yes, it helps. Thanks very much!

Best regards,
Ben Hur
Reply all
Reply to author
Forward
0 new messages