0

I have a file with multiple columns and I would like to make multiple linear regressions (to be all plotted in same graph after regression) over the file using a loop as follows:

do for [i=1:6] {
g(i,x)= 'm'.i*x + 'b'.i
fit g(i,x) 'Dens.dat' using 1:(column(i+1)) via 'm'.i,'b'.i    
}

But I get an error: could not read parameter-file "m1"

How can I fix this?

0

1 Answer 1

1

If your version of Gnuplot doesn't support arrays, then you could adapt an alternative solution, for example:

vGet(name, i) = value(sprintf("%s%i", name, i)) 
vSet(name, i, val) = sprintf("%s%i = %.16e", name, i, val)

N = 2
#array m[N]
#array b[N]

do for [i=1:N] {
    g(x)= alpha*x + beta
    fit g(x) 'Dens.dat' using 1:(column(i+1)) via alpha,beta

    eval vSet('m', i, alpha);
    eval vSet('b', i, beta);

    #m[i-1] = alpha
    #b[i-1] = beta
}

set key top left
set terminal pngcairo
set output 'fig.png'

plot \
    for [i=1:N] vGet('m', i)*x + vGet('b',i) w l t '', \
    for [i=1:N] 'Dens.dat' u 1:(column(i+1)) w p t sprintf("column %d", i)

in combination with Dens.dat as:

1   4   5
2   8   10
3   12  15
4   16  20

produces: enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.