JuliaStats / GLM.jl

Generalized linear models in Julia
Other
584 stars 114 forks source link

Feature request: Allow coef and p-value extraction by variable name #491

Open eirikbrandsaas opened 1 year ago

eirikbrandsaas commented 1 year ago

Hi,

It would be great (and safer?) if one could extract coeffecients by variable names:

df = DataFrame(y=rand(3),x=rand(3))
out = reg(df,@formula(y~x))
out.coef[findfirst(isequal("x"),out.coefnames)] # hard to do
out.coef["x"] # would be great
out.coef[:x] # would be great 

Or really any thing like that.

See e.g., https://discourse.julialang.org/t/how-to-obtain-the-pvalues-of-the-coefficients-in-glm-jl/9531/4

ararslan commented 1 year ago

I guess in addition to directly delegating coef to the model object in https://github.com/JuliaStats/StatsModels.jl/blob/61de82aa23fb562697fe0f750f6f83ca7be79506/src/statsmodel.jl#L128 we could define e.g.

coef(model, term) = coef(model)[findfirst(==(term), coefnames(model))]
model = fit(Whatever, @formula(y ~ 1 + x), data)
coef(model, :x)  # coefficient for `x`

That could only sensibly support table-based models though, since those are the only ones for which you know the coefficient names. (e.g. this wouldn't work for models fit with an explicit design matrix rather than a formula)

xgdgsc commented 1 year ago

This should be added or at least documented.