Search code examples
graphgnuplotlogarithm

semi-log scale linear regression [Gnuplot]


I have an experimental data set that I linearize on a semi-log scale and of which I want to run a linear regression.

The problem is that when I set a line as a function for the fit, it, and the subsequent plot, do not give valid results since I have the y in logarithmic scale.

set logscale y
plot 'file.txt' u 1:2
f(x)=m*x+q
fit f(x) 'file.txt' u 1:2 via m,q
plot 'file.txt' u 1:2, f(x)

And the result is as shown in the image, clearly the angular coefficient is off, and I'd like to figure out how to do a linear regression when I'm on a semi-log scale.

My bad fit

#EDIT 1: As suggested I did the fit in this way

fit f(x) 'file.txt' u 1:(log($2)) via m,q

the angular coefficient seems ok now, the problem is that when I plot the fit now the resulting image is not correct. enter image description here

#EDIT 2: Solved

plot 'file.txt' u 1:(log($2)), f(x) 

That's the result enter image description here


Solution

  • set logscale y only set the y axis of the graph, not your data. You need to fit versus log(y):

    fit f(x) 'file.txt' u 1:(log($2)) via m,q