Julia 中的虚拟变量 [英] Dummy Variables in Julia
问题描述
在 R 中,有一个很好的功能可以为分类变量的每个级别运行带有虚拟变量的回归.例如自动将 R 因子扩展为每个因子水平的 1/0 指标变量的集合
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
在 Julia 中是否有等效的方法.
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)
推荐答案
如果你使用的是DataFrames包,在你pool
数据之后,包会处理剩下的事情:
If you are using the DataFrames package, after you pool
the data, the package will take care of the rest:
合并列对于使用 GLM 包很重要 拟合回归模型时,输入中的 PooledDataArray 列被转换为 ModelMatrix 中的 0/1 指标列 - PooledDataArray 的每个级别都有一个列.
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.
您可以在这里查看其余文档
这篇关于Julia 中的虚拟变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!