polynomial regression in julia - glm

Viewed 1821

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.

3 Answers

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.

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.

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.

Related