Julia-线性回归-错误:DimensionMismatch

时间:2019-06-30 08:27:32

标签: dataframe julia

我希望在Julia中进行线性回归,但出现错误: DimensionMismatch(“ X列的列长度3000,与Y列的列长度1000不兼容”)

julia> x=rand(1000,3);

julia> x[:,1]=x[:,1] .+ 1;

julia> y = rand(1000,1) .+ 3;

julia> size(x)
(1000, 3)

julia> size(y)
(1000, 1)

julia> ols = lm(@formula(Y ~ X), DataFrame(X = x, Y = y))
ERROR: DimensionMismatch("column length 3000 for column(s) X, and is incompatible with column length 1000 for column(s) Y")
Stacktrace:
 [1] (::getfield(DataFrames, Symbol("##DataFrame#83#86")))(::Bool, ::Type, ::Array{Any,1}, ::DataFrames.Index) at /Users/henry/.julia/packages/DataFrames/CZrca/src/dataframe/dataframe.jl:118
 [2] Type at ./none:0 [inlined]
 [3] #DataFrame#94(::Base.Iterators.Pairs{Symbol,Array{Float64,2},Tuple{Symbol,Symbol},NamedTuple{(:X, :Y),Tuple{Array{Float64,2},Array{Float64,2}}}}, ::Type) at /Users/henry/.julia/packages/DataFrames/CZrca/src/dataframe/dataframe.jl:174
 [4] (::getfield(Core, Symbol("#kw#Type")))(::NamedTuple{(:X, :Y),Tuple{Array{Float64,2},Array{Float64,2}}}, ::Type{DataFrame}) at ./none:0
 [5] top-level scope at none:0

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

您的问题是,当您写:

DataFrame(X = x, Y = y)

您正在尝试将Matrix设置为DataFrame的一列。这是不允许的。您只能将向量添加为数据框的列。所以这会工作例如

DataFrame([x y], [:x1, :x2, :x3, :y])

因此,在您的原始代码中,您可以编写例如:

lm(@formula(y ~ x1+x2+x3), DataFrame([x y], [:x1, :x2, :x3, :y]))