带有 Julia 的线性模型,错误

linear model with Julia,error

 using RDatasets
 using GLM
 housing = dataset("Ecdat", "Housing")
 plot(housing, x="LotSize", y="Price", Geom.point)
 log_housing = DataFrame(LotSize=log(housing[:,2]), Price=log(housing[:,1]))
 plot(log_housing, x="LotSize", y="Price", 
 Geom.point,Guide.xlabel("LotSize(log)"), Guide.ylabel("Price(log)"))
 lm = fit(LinearModel, Price ~ LotSize, log_housing)
 #UndefVarError: Price not defined

我 运行 使用 Julia 的线性模型,但我不明白为什么它有错误 This is what I do

为了估计线性模型你可以使用 lm 函数(你的代码实际上会覆盖这个名字),所以最好写:

julia> lm_model = lm(@formula(Price ~ LotSize), log_housing)
StatsModels.DataFrameRegressionModel{GLM.LinearModel{GLM.LmResp{Array{Float64,1}},GLM.DensePredChol{Float64,Base.LinAlg.Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}}

Formula: Price ~ 1 + LotSize

Coefficients:
             Estimate Std.Error t value Pr(>|t|)
(Intercept)   6.46853  0.276741  23.374   <1e-83
LotSize      0.542179 0.0326501 16.6057   <1e-49

附带说明 - 不推荐将 log 函数应用于向量,您应该使用 log.(广播):

log_housing = DataFrame(LotSize=log.(housing[:,2]), Price=log.(housing[:,1]))