Julia 中的虚拟变量

Dummy Variables in Julia

在 R 中,运行 有一个很好的功能,可以对分类变量的每个水平使用虚拟变量进行回归。例如Automatically expanding an R factor into a collection of 1/0 indicator variables for every factor level

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

如果您使用的是 DataFrames 包,在您 pool 数据之后,包会处理剩下的事情:

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.

您可以查看有关合并数据的其余文档here