JuliaStats / GLM.jl

Generalized linear models in Julia

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`predict(model, df)` fails if `df` does not contain dependent variable (ERROR: type NamedTuple has no field)

DilumAluthge opened this issue · comments

predict(model, df) works fine if df contains the dependent variable, but fails if df does not contain the dependent variable. I think this is a bug, because you should be able to predict with just the independent variables.

Minimum working example (MWE)

using GLM, RDatasets
df = RDatasets.dataset("MASS", "Boston")
f = @formula(MedV ~ 1 + Crim + Zn + Indus + Chas + NOx + Rm + Age + Dis + Rad + Tax + PTRatio + Black + LStat)
model = glm(f, df, Normal(), IdentityLink())
predict(model, df) # this works
deletecols!(df, [:MedV])
predict(model, df) # this gives an error

Running the last line gives this error:

ERROR: type NamedTuple has no field MedV
Stacktrace:
 [1] #134 at ./none:0 [inlined]
 [2] iterate(::Base.Generator{NTuple{14,Symbol},getfield(Base, Symbol("##134#137")){NamedTuple{(:Crim, :Zn, :Indus, :Chas, :NOx, :Rm, :Age, :Dis, :Rad, :Tax, :PTRatio, :Black, :LStat),Tuple{Array{Float64,1},Array{Float64,1},Array{Float64,1},Array{Int64,1},Array{Float64,1},Array{Float64,1},Array{Float64,1},Array{Float64,1},Array{Int64,1},Array{Int64,1},Array{Float64,1},Array{Float64,1},Array{Float64,1}}}}}) at ./generator.jl:47
 [3] append_any(::Any, ::Vararg{Any,N} where N) at ./essentials.jl:674
 [4] (::Type{NamedTuple{names,T} where T<:Tuple})(::NamedTuple) at ./namedtuple.jl:87
 [5] missing_omit(::NamedTuple{(:Crim, :Zn, :Indus, :Chas, :NOx, :Rm, :Age, :Dis, :Rad, :Tax, :PTRatio, :Black, :LStat),Tuple{Array{Float64,1},Array{Float64,1},Array{Float64,1},Array{Int64,1},Array{Float64,1},Array{Float64,1},Array{Float64,1},Array{Float64,1},Array{Int64,1},Array{Int64,1},Array{Float64,1},Array{Float64,1},Array{Float64,1}}}, ::FormulaTerm{ContinuousTerm{Float64},MatrixTerm{Tuple{InterceptTerm{true},ContinuousTerm{Float64},ContinuousTerm{Float64},ContinuousTerm{Float64},ContinuousTerm{Float64},ContinuousTerm{Float64},ContinuousTerm{Float64},ContinuousTerm{Float64},ContinuousTerm{Float64},ContinuousTerm{Float64},ContinuousTerm{Float64},ContinuousTerm{Float64},ContinuousTerm{Float64},ContinuousTerm{Float64}}}}) at /Users/dilum/.julia/packages/StatsModels/ZHkQE/src/modelframe.jl:77
 [6] #predict#74(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{,Tuple{}}}, ::Function, ::StatsModels.TableRegressionModel{GeneralizedLinearModel{GlmResp{Array{Float64,1},Normal{Float64},IdentityLink},DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}}, ::DataFrame) at /Users/dilum/.julia/packages/StatsModels/ZHkQE/src/statsmodel.jl:162
 [7] predict(::StatsModels.TableRegressionModel{GeneralizedLinearModel{GlmResp{Array{Float64,1},Normal{Float64},IdentityLink},DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}}, ::DataFrame) at /Users/dilum/.julia/packages/StatsModels/ZHkQE/src/statsmodel.jl:158
 [8] top-level scope at none:0

This is on Julia v1.1.1, GLM v1.1.1, and StatsModels v0.6.0.

This is a StatsModels issue, so I am closing this issue.