Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Julia 朱莉娅:无法将数组{Number,1}类型的对象“转换”为GLM.LmResp类型的对象_Julia_Linear Regression - Fatal编程技术网

Julia 朱莉娅:无法将数组{Number,1}类型的对象“转换”为GLM.LmResp类型的对象

Julia 朱莉娅:无法将数组{Number,1}类型的对象“转换”为GLM.LmResp类型的对象,julia,linear-regression,Julia,Linear Regression,我正在逐行构建一个数据帧,然后在其上运行回归。为简单起见,代码为: using DataFrames using GLM df = DataFrame(response = Number[]) for i in 1:10 df = vcat(df, DataFrame(response = rand())) end fit(LinearModel, @formula(response ~ 1), df) 我得到一个错误: ERROR: LoadError: MethodError:

我正在逐行构建一个数据帧,然后在其上运行回归。为简单起见,代码为:

using DataFrames
using GLM

df = DataFrame(response = Number[])
for i in 1:10
    df = vcat(df, DataFrame(response = rand()))
end

fit(LinearModel, @formula(response ~ 1), df)
我得到一个错误:

ERROR: LoadError: MethodError: Cannot `convert` an object of type Array{Number,1} to an object of type GLM.LmResp
This may have arisen from a call to the constructor GLM.LmResp(...),
since type constructors fall back to convert methods.
Stacktrace:
 [1] fit(::Type{GLM.LinearModel}, ::Array{Float64,2}, ::Array{Number,1}) at ~/.julia/v0.6/GLM/src/lm.jl:140
 [2] #fit#44(::Dict{Any,Any}, ::Array{Any,1}, ::Function, ::Type{GLM.LinearModel}, ::StatsModels.Formula, ::DataFrames.DataFrame) at ~/.julia/v0.6/StatsModels/src/statsmodel.jl:72
 [3] fit(::Type{GLM.LinearModel}, ::StatsModels.Formula, ::DataFrames.DataFrame) at ~/.julia/v0.6/StatsModels/src/statsmodel.jl:66
 [4] include_from_node1(::String) at ./loading.jl:576
 [5] include(::String) at ./sysimg.jl:14
while loading ~/test.jl, in expression starting on line 10
对线性回归的调用非常类似于:


问题出在哪里?

几个小时后,我意识到GLM需要具体类型,数字是一种抽象类型(尽管在撰写本文时很少提到这一点,只是“封装了线性模型的响应”)。解决方案是将声明更改为具体类型,例如Float64:

using DataFrames
using GLM

df = DataFrame(response = Float64[])
for i in 1:10
    df = vcat(df, DataFrame(response = rand()))
end

fit(LinearModel, @formula(response ~ 1), df)
输出:

StatsModels.DataFrameRegressionModel{GLM.LinearModel{GLM.LmResp{Array{Float64,1}},GLM.DensePredChol{Float64,Base.LinAlg.Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}}

Formula: response ~ +1

Coefficients:
             Estimate Std.Error t value Pr(>|t|)
(Intercept)  0.408856 0.0969961 4.21518   0.0023
该类型必须是具体的,例如,抽象类型
Real
带有
df=DataFrame(response=Real[])
失败,并显示更有用的错误消息:

ERROR: LoadError: `float` not defined on abstractly-typed arrays; please convert to a more specific type
或者,您可以在构建数据帧后转换为
Real

using DataFrames
using GLM

df = DataFrame(response = Number[])
for i in 1:10
    df = vcat(df, DataFrame(response = rand()))
end

df2 = DataFrame(response = map(Real, df[:response]))

fit(LinearModel, @formula(response ~ 1), df2)
这是因为转换为实数实际上会转换为Float64:

julia> typeof(df2[:response])
Array{Float64,1}
我提交了一份报告以改进错误消息

julia> typeof(df2[:response])
Array{Float64,1}