GLM linear regression. How to extract the coefficients?

I fit a simple linear regression using GLM. How do I extract the calculated slope and intercept?

I looked through the documentation but didn’t see an example of it. If such an example exists, feel free to point there.

There may be a better way, but this one works.

reg = GLM.lm(GLM.@formula(y ~ x), data)
slope = GLM.coef(reg)[2]

Also, check

GLM.coeftable(reg)

You can use that, for example, to extract p value for the slope.

pval = GLM.coeftable(reg).cols[4][2]
3 Likes

See for example Listing 8.5 here: https://people.smp.uq.edu.au/YoniNazarathy/julia-stats/StatisticsWithJulia.pdf#page289

4 Likes

These are exactly what I needed! I marked curious as the solution because he answered first, but really both of you answered it. This could definitely be improved in the GLM docs; once I knew the name of the function I was able to find it, but it’s mentioned quickly in the “Manual” section and never appears in any of the examples… I may try to do a PR.

2 Likes