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

Add plot recipe for fit result #180

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

gustaphe
Copy link

@gustaphe gustaphe commented Apr 7, 2021

(might close #139)

Implements a plot recipe for fit results. Uses the delta method (as described here) to form confidence and prediction bands.

My statistics is shaky, it's been a couple of years, so this might be a terrible way to do this. Which is why I'm asking for input before I go through the trouble of writing docs and tests.

Is this desirable?

plot(tdata, ydata; seriestype=:scatter, label="Samples")
plot!(model, fit; significance=0.05, purpose=:both)

:

plot

@pkofod
Copy link
Member

pkofod commented Sep 2, 2022

We could maybe do this. I'm not sure I understand the prediction part.

@gustaphe
Copy link
Author

gustaphe commented Sep 3, 2022

The confidence interval is likely to contain the true mean. The prediction interval is likely to contain any new observation.

@gustaphe
Copy link
Author

gustaphe commented Sep 3, 2022

Sorry about the low quality illustration, here's a better showcase of the feature, generated by the test code. (Note that the first two series in every subplot are generated by separate code calls, this PR adds the rest)

plots

function margin_error(model::Function, x, fit::LsqFitResult, alpha=0.05; purpose=:confidence)
g = p -> ForwardDiff.gradient(p -> model(x, p), fit.param)
c = g(fit.param)' * estimate_covar(fit) * g(fit.param)
if purpose === :prediction
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this was in the original formulation on stackexchange, but where does the 1 come from?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My statistics textbook has a derivation for this, but it's sadly in Swedish. I'll see later today if I can find a good reference. Essentially the difference between prediction and confidence coincidentally comes down to 1 dof.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. You can post the picture of the Swedish text, I'll be able to read it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Here's the derivation in Seber, G.A.F & Wild, C.J 1989: Statistical inference (My textbook only had it for the linear case). My statistics isn't quite strong enough that I could convert this directly to working code, but I think it contains the source of the 1.

Intuitively, your next value is likely to land in an interval that is the confidence interval plus one "standard deviation" to either side, because that's where it's likely to land given the position of the true mean is inside the confidence interval. I hope this makes sense, otherwise it might be an idea to hold off on this part of the PR.

@pkofod
Copy link
Member

pkofod commented Sep 5, 2022

Sorry about the low quality illustration, here's a better showcase of the feature, generated by the test code. (Note that the first two series in every subplot are generated by separate code calls, this PR adds the rest)

Yeah, I like it. Just want to understand the prediction part first, I've never used that myself :)

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 this pull request may close these issues.

Plotting the result
2 participants