我一直在尝试在Julia
中复制这个矩阵(R
)。
data = read.csv("https://gist.githubusercontent.com/TJhon/158daa0c2dd06010d01a72dae2af8314/raw/61df065c98ec90b9ea3b8598d1996fb5371a64aa/rnd.csv")
head(model.matrix(y ~ (x1 + x2 + x3 + x4 + x5)^2, data), 3)
#> (Intercept) x1 x2 x3 x4 x5 x1:x2
#> 1 1 -0.3007225 -1.3710894 0.3423409 1.322547 2 0.41231744
#> 2 1 0.4674170 0.8728939 0.9534157 -1.007083 1 0.40800548
#> 3 1 0.2085316 -0.3657995 -0.3043694 -1.036938 4 -0.07628076
#> x1:x3 x1:x4 x1:x5 x2:x3 x2:x4 x2:x5 x3:x4
#> 1 -0.10294961 -0.3977198 -0.6014450 -0.4693799 -1.8133307 -2.7421787 0.4527620
#> 2 0.44564276 -0.4707279 0.4674170 0.8322308 -0.8790769 0.8728939 -0.9601690
#> 3 -0.06347064 -0.2162343 0.8341265 0.1113382 0.3793113 -1.4631979 0.3156121
#> x3:x5 x4:x5
#> 1 0.6846817 2.645095
#> 2 0.9534157 -1.007083
#> 3 -1.2174775 -4.147751
本文创建于2022年10月18日,使用reprex程序包(v2.0.1)
我尝试了
using CSV, DataFrames, StatsModels, StatsBase
data = CSV.read(download("https://gist.githubusercontent.com/TJhon/158daa0c2dd06010d01a72dae2af8314/raw/61df065c98ec90b9ea3b8598d1996fb5371a64aa/rnd.csv"), DataFrame)
ModelMatrix(ModelFrame(@formula(y ~ (x1 + x2 + x3 + x4 + x5) * (x1 + x2 + x3 + x4 + x5)), data)).m
9×31 Matrix{Float64}:
1.0 -0.300723 -1.37109 0.342341 1.32255 2.0 0.090434 0.412317 -0.10295 -0.39772 … 0.452762 1.74913 2.64509 -0.601445 -2.74218 0.684682 2.64509 4.0
1.0 0.467417 0.872894 0.953416 -1.00708 1.0 0.218479 0.408005 0.445643 -0.470728 -0.960169 1.01422 -1.00708 0.467417 0.872894 0.953416 -1.00708 1.0
1.0 0.395908 -1.15159 -0.204683 -0.207952 2.0 0.156743 -0.455924 -0.0810355 -0.0823297 0.0425641 0.0432439 -0.415903 0.791816 -2.30318 -0.409365 -0.415903 4.0
ModelMatrix(ModelFrame(@formula(y ~ (x1 + x2 + x3 + x4 + x5) & (x1 + x2 + x3 + x4 + x5)), data)).m
9×26 Matrix{Float64}:
1.0 0.090434 0.412317 -0.10295 -0.39772 -0.601445 0.412317 1.87989 -0.46938 -1.81333 … 0.452762 1.74913 2.64509 -0.601445 -2.74218 0.684682 2.64509 4.0
1.0 0.218479 0.408005 0.445643 -0.470728 0.467417 0.408005 0.761944 0.832231 -0.879077 -0.960169 1.01422 -1.00708 0.467417 0.872894 0.953416
1.0 0.156743 -0.455924 -0.0810355 -0.0823297 0.791816 -0.455924 1.32616 0.235711 0.239475 0.0425641 0.0432439 -0.415903 0.791816 -2.30318 -0.409365 -0.415903 4.0
ModelMatrix(ModelFrame(@formula(y ~ (x1 + x2 + x3 + x4 + x5)^2), data)).m
9×2 Matrix{Float64}:
1.0 3.97235
1.0 5.22874
: :
1.0 0.691696
我希望相同的数组和向量变量名称能够被转换为数据框以供日后使用。