polynomial regression in julia - glm
Asked Answered
A

3

6

Is it possible to do polynomial regression in the GLM-package within Julia? Considering the similarity with R syntax, I had hoped that

 fit(LinearModel, @formula(y ~ poly(x,5)), dataset)

would work (for fitting a degree 5 polynomial). It does not.

Amimia answered 7/10, 2019 at 7:34 Comment(0)
H
3

StatsModels.jl does not provide a poly() function, but their documentation does provide a complete example that shows how you can add your own poly() function that will work inside the @formula macro.

Hanks answered 8/10, 2019 at 3:16 Comment(3)
Maybe file an issue for exporting such a poly function? Polynomials are common enough for this to make sense, no?Gluteal
Ok, that resolves it. I agree that it would be very useful to have this exported. Quite honestly, don't know exactly how to file this issue.Amimia
This is currently shown only to illustrate the flexibility of @formula. A proper implementation aimed at end users still needs to be finished and exports.Neau
G
6

Although you are explicitly asking for a GLM.jl solution, let me nonetheless point out the straightforward solution using Polynomials.jl:

using Polynomials
polyfit(x, 5)

See polyfit for more information.

Gluteal answered 7/10, 2019 at 10:13 Comment(1)
Thanks for pointing out. I am also interested in standard-errors, so that's why I included GLM. It is not terribly hard to code this myself, but I am a bit surprised that it seems not to be available within GLM.Amimia
E
3

This isn't an issue with GLM directly, but with the underlying @formula macro from StatsModels.jl - as far as I know, there's currently no syntax for generating polynomials up to a prespecified order.

You can however apply Julia functions columnwise within the formula macro, so up to a limited order it's probably easiest to just type it out like

using GLM, DataFrames

y = [0.1*x^2 - 0.5*x for x ∈ 1:100]
x = collect(1:100)
df = (y = y, x = x)

fit(LinearModel, @formula(y ~ x + x^2), df)

Note that this doesn't require having x squared as a column in the dataframe - the @formula can generate the required regressors on the fly in the model matrix.

You can also generate formulas programmatically, as described in the docs here - the problem is that this doesn't yet work for passing Julia functions as shown above in the @formula macro. I believe work is ongoing on this, so stay tuned.

Extinguisher answered 7/10, 2019 at 13:16 Comment(1)
Thanks! Very useful to know.Amimia
H
3

StatsModels.jl does not provide a poly() function, but their documentation does provide a complete example that shows how you can add your own poly() function that will work inside the @formula macro.

Hanks answered 8/10, 2019 at 3:16 Comment(3)
Maybe file an issue for exporting such a poly function? Polynomials are common enough for this to make sense, no?Gluteal
Ok, that resolves it. I agree that it would be very useful to have this exported. Quite honestly, don't know exactly how to file this issue.Amimia
This is currently shown only to illustrate the flexibility of @formula. A proper implementation aimed at end users still needs to be finished and exports.Neau

© 2022 - 2024 — McMap. All rights reserved.