Get columns of DataFrames.jl ModelMatrix object


Is there a way to extract column names of an object created from ModelMatrix() so that I can use those columns as row names for coefficients?


This isn’t standard Julia. Is this part of some package?


Actually, I am writing a Julia function for a stat method in which I need to use ModelMatrix and would like to obtain the levels of the covariates from the ModelMatrix so I can use them to name the coefficients in the output.



Oh, you should’ve mentioned that this is a DataFrames.jl thing.,-ModelFrame-and-ModelMatrix-Types-1

Edited for clarity.


Oh sorry, yes I was referring to ModelMatrix in DataFrame.jl. I cannot obtain the column names from an object created from ModelMatrix(). Any idea?


You don’t have access to the names at the ModelMatrix stage. You’d need the ModelFrame for that.


Thanks. Would ModelFrame give me columns by creating dummy indicators of levels of factor variables? Thanks,


No that is what ModelMatrix does but from a mf::ModelFrame you can get the ModelMatrix with ModelMatrix(mf) and the column names with coefnames(mf).


@andreasnoack @ChrisRackauckas : Below is an example of the same problem addressed in R language. Notice how levels (1s) of factor variables are attached to the column variables am and vs. I would like to do the same in Julia so that I can access the names (columns) from design matrix. Thanks.

> table(df)
am  0 1
  0 2 5
  1 2 1

> model.matrix(~., df)
   (Intercept) am1 vs1
1            1   1   0
2            1   1   0
3            1   1   1
4            1   0   1
5            1   0   0
6            1   0   1
7            1   0   0
8            1   0   1
9            1   0   1
10           1   0   1
[1] 0 1 2
[1] "contr.treatment"

[1] "contr.treatment"


@andreasnoack: Thanks a lot. That’s what I wanted.


@andreasnoack: Is there any way to boost the speed of Pkg.add() or using packages? These are too slow. Thanks.


Not really at this point but it might get better soon. See though it is not certain when it will arrive.

split this topic #13

12 posts were split to a new topic: How to start writing a new Julia package?


Is that a 1.0 feature?

split this topic #15

A post was merged into an existing topic: How to start writing a new Julia package?