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
答案 0 :(得分:2)
为了估计线性模型,您可以使用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]))