Search code examples
juliadistributionglm

How to extract data distribution type (e.g. Poisson, binomial) from fitted GLM model object in Julia?


How do I extract the data distribution specified in a general linear model in Julia? For example, below I fit a toy example Poisson GLM. I want to extract a character string saying 'Poisson' from the model. Similarly, if the model were specified with a data distribution = Binomial() instead, I would like the character string to return 'Binomial'.

### Load packages required 
using DataFrames
using GLM

### Simulate some data for a dummy GLM 
data = DataFrame(X=[1,2,3,4,5,6,7,8,9,10], Y=[2,4,7,3,1,6,3,2,5,1])

### Fit Poisson GLM
m1 = fit(GeneralizedLinearModel,
         @formula(Y ~ X),
         data,
         Poisson(),
         LogLink())

Ultimately, I want to write a function that takes an if statement, where if == Poisson then do X, else do Y.


Solution

  • This was discussed on the Julia Discourse a while ago here, you can do it with:

    julia> typeof(m1).parameters[1].parameters[1].parameters[2]
    Poisson{Float64}