How to use the output of log_fit in abline plotted in a loglog plot in Julia

Dear community,
For log_fit, the equation is log y=a+b*log x, whose output coefficients are ‘a’ and ‘b’. Slope ‘b’ is applied in view of log x, so how can I plot the non-linear regression in a loglog plot on scale of x?

Another way is to use log x as the scale, but all other attributes of loglog plot need to be mannually set in this case, i.e. vertically asymmetric error bar and axis notation. It seems to be harder for me and clumsy.

using CurveFit
coef=log_fit(x,y)
using Plots
scatter(x, y, yerror=se, scale=:log10)
Plots.abline!(coef[2],coef[1],line=:dash)
Plots.abline!(coef[2],coef[1],line=:dash,scale=:log10)#made no difference
Plots.abline!(coef[2],coef[1],line=:dash,xaxis=:log10)#made no difference

You could just calculate that curve directly, with something like

xgrid = range(extrema(x)...; length = 100)
ygrid = exp.(coef[1] .+ coef[2] .* log.(xgrid)) # check the order, I did not test this

and then plot the xgrid, ygrid curve.

Thank you, your method should work but the coefficients output from log_fit are weird (making the ygrid infinite). To make it repeatable, I attached the data in codes.

#population size
x=[ 10 100 1000 10000]
#mean time of an allele to extinction or fixation
y= [23.06 245.48 2437.8 31245.775510204083]
se=[2.068502366645087 26.886795788176432 264.41256106505153 2811.6173148600947] #of mean y
using CurveFit
coef=log_fit(x,y)
using Plots
scatter(x, y, yerror=se, scale=:log10,legend=false)

xgrid=range(extrema(x)…;length=100)
ygrid = exp.(coef[1] .+ coef[2] .* log.(xgrid))
print(ygrid) inf
plot!(xgrid,ygrid)

Check the docs of log_fit, it is for y = a_1 + a_2 \log(x).

I get it. Thank you for your patience~

1 Like