Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plotting the result #139

Open
Mutaz94 opened this issue Jan 8, 2020 · 10 comments · May be fixed by #180
Open

Plotting the result #139

Mutaz94 opened this issue Jan 8, 2020 · 10 comments · May be fixed by #180

Comments

@Mutaz94
Copy link

Mutaz94 commented Jan 8, 2020

Hello,

a quick question; after the estimation of parameters, is there a way to plot the curve fit estimation on the data set to visually inspect the model?

Thanks!
Mutaz

@pkofod
Copy link
Member

pkofod commented Jan 11, 2020

There is no syntactic sugar for it though. But it's a good idea if it's univariate data.

@Mutaz94
Copy link
Author

Mutaz94 commented Jan 12, 2020

So is it worth an issue? it could lead to a fancy function to do that. For instance, when talking about concentration versus time, a function like harmonic analysis or dual cosine that describes the circadian oscillation could fit the curve, and it is nice to plot it after the estimation.

@pkofod
Copy link
Member

pkofod commented Jan 13, 2020

Yeah, I'm keeping the issue open to remember :)

@pkofod
Copy link
Member

pkofod commented Jan 17, 2020

Feel free to post some functions here that you think does what you what. I think the approach would ultimately be a plot recipe of some sort (makie or plots, whichever wins the battle)

@Mutaz94
Copy link
Author

Mutaz94 commented Jan 17, 2020

Sure, I'll do that by the weekend, even we can add ggplot2.jl, I'm developing some functions in it.

@Mutaz94
Copy link
Author

Mutaz94 commented Feb 3, 2020

Hi @pkofod , sorry for being late on this. I'll try to elaborate more in the moment.

I was not able to figure out how to plot the fitting. I did the residuals using this

plot(time, m.resid, seriestype=:scatter)
plot!([0], seriestype = "hline")

image

can we use the jacobian for this purpose? I want to see the predicted. Thanks!

@pkofod
Copy link
Member

pkofod commented Feb 5, 2020

For the predicted, just evaluate the model function in the data points with your parameters.

@pkofod
Copy link
Member

pkofod commented Feb 5, 2020

using LsqFit

# a two-parameter exponential model
# x: array of independent variables
# p: array of model parameters
# model(x, p) will accept the full data set as the first argument `x`.
# This means that we need to write our model function so it applies
# the model to the full dataset. We use `@.` to apply the calculations
# across all rows.
@. model(x, p) = p[1]*exp(-x*p[2])

# some example data
# xdata: independent variables
# ydata: dependent variable
xdata = range(0, stop=10, length=20)
ydata = model(xdata, [1.0 2.0]) + 0.01*randn(length(xdata))
p0 = [0.5, 0.5]

fit = curve_fit(model, xdata, ydata, p0)

prediction = model.(xdata, Ref(fit.param))

using Plots 
scatter(xdata, ydata)
plot!(xdata, prediction)

for example

@Mutaz94
Copy link
Author

Mutaz94 commented Feb 5, 2020

Thanks for providing this nice example. trying to figure out things. My question is could we take the model function from inside the main function. For example: want to take the model outside the Fourier function to predict it. and compare all type according to harmonic listed.

function fourier!(y::AbstractArray,
                  x::AbstractArray,
                  h::Int64,
                  p0::Vector{Float64})

    if (h == 1)
        @. model1(t, p)= p[1] + p[2]*cos(2*π*t/24) + p[3]*sin(2*π*t/24)
        fit = curve_fit(model1,x, y, p0)
        return(fit)
    elseif (h==2)
        @. model2(t, p) = p[1] + p[2]*cos(2*π*t/24) + p[3]*sin(2*π*t/24) +
                          p[4]*cos(2*π*t/12) + p[5]*sin(2*π*t/12)
                fit = curve_fit(model2,x, y, p0)
            return(fit)
            return(model1)
    elseif (h==3)
        @. model3(t, p) = p[1] + p[2]*cos(2*π*t/24) + p[3]*sin(2*π*t/24) +
                          p[4]*cos(2*π*t/12) + p[5]*sin(2*π*t/12) +
                          p[6]*cos(2*π*t/8) + p[7]*sin(2*π*t/8)
                fit = curve_fit(model3,x, y, p0)
            return(fit)
        else
            error("Only  three harmonics allowed")
    end

end

@Magalame
Copy link
Contributor

Magalame commented Jun 23, 2020

For reference I really like how https://github.com/Spinmob/spinmob/wiki/4.-Fitting plots its results. Not the best formatting but I appreciate the quantity of information

@gustaphe gustaphe linked a pull request Apr 7, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants