Get columns of DataFrames.jl ModelMatrix object


#1

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?
Thanks.


#2

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


#3

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.

Thanks.


#4

Oh, you should’ve mentioned that this is a DataFrames.jl thing.

http://juliastats.github.io/DataFrames.jl/latest/man/formulas/#The-Formula,-ModelFrame-and-ModelMatrix-Types-1

Edited for clarity.


#5

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?
Thanks.
So


#6

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


#7

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


#8

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).


#9

@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)
   vs
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
attr(,"assign")
[1] 0 1 2
attr(,"contrasts")
attr(,"contrasts")$am
[1] "contr.treatment"

attr(,"contrasts")$vs
[1] "contr.treatment"

#10

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


#11

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


#12

Not really at this point but it might get better soon. See https://github.com/JuliaLang/Juleps/blob/master/Pkg3.md though it is not certain when it will arrive.


#13

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


#14

Is that a 1.0 feature?


#15

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