Dummy Variables in Julia

五迷三道 提交于 2020-01-14 07:58:13

问题


In R there is nice functionality for running a regression with dummy variables for each level of a categorical variable. e.g. Automatically expanding an R factor into a collection of 1/0 indicator variables for every factor level

Is there an equivalent way to do this in Julia.

x = randn(1000)
group = repmat(1:25 , 40)
groupMeans = randn(25)
y = 3*x + groupMeans[group]

data = DataFrame(x=x, y=y, g=group)
for i in levels(group)
    data[parse("I$i")] = data[:g] .== i
end
lm(y~x+I1+I2+I3+I4+I5+I6+I7+I8+I9+I10+
    I11+I12+I13+I14+I15+I16+I17+I18+I19+I20+
    I21+I22+I23+I24, data)

回答1:


If you are using the DataFrames package, after you pool the data, the package will take care of the rest:

Pooling columns is important for working with the GLM package When fitting regression models, PooledDataArray columns in the input are translated into 0/1 indicator columns in the ModelMatrix - with one column for each of the levels of the PooledDataArray.

You can see the rest of documentation on pooled data here



来源:https://stackoverflow.com/questions/29158626/dummy-variables-in-julia

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!