Predict fails for simple case of GLM?

I am trying a simple example using the ISLR dataset

ols = lm(@formula(sales ~ TV + radio + newspaper), adsData)

this works OK:

``StatsModels.TableRegressionModel{LinearModel{GLM.LmResp{Vector{Float64}}, GLM.DensePredChol{Float64, LinearAlgebra.CholeskyPivoted{Float64, Matrix{Float64}}}}, Matrix{Float64}}

sales ~ 1 + TV + radio + newspaper

Coefficients:
────────────────────────────────────────────────────────────────────────────
Coef. Std. Error t Pr(>|t|) Lower 95% Upper 95%
────────────────────────────────────────────────────────────────────────────
(Intercept) 2.93889 0.311908 9.42 <1e-16 2.32376 3.55402
TV 0.0457646 0.0013949 32.81 <1e-80 0.0430137 0.0485156
radio 0.18853 0.00861123 21.89 <1e-53 0.171547 0.205513
newspaper -0.00103749 0.00587101 -0.18 0.8599 -0.012616 0.010541
────────────────────────────────────────────────────────────────────────────``

but

predict(ols)
fails:

MethodError: no method matching predict(::StatsModels.TableRegressionModel{LinearModel{GLM.LmResp{Vector{Float64}}, GLM.DensePredChol{Float64, LinearAlgebra.CholeskyPivoted{Float64, Matrix{Float64}}}}, Matrix{Float64}}) Closest candidates are: predict(::Any, ::Any, ::Any) at In[8]:1

I would be grateful for any insights.

PS there could be missing values

Works fine for me

julia> using DataFrames, GLM;
julia> adsData = DataFrame(sales = rand(100), TV = rand(100), radio = rand(100), newspaper = rand(100));

julia> ols = lm(@formula(sales ~ TV + radio + newspaper), adsData);
julia> predict(ols);

can you provide an MWE?

Hi. Sorry, I don’t know that acronym
[thanks for taking a look, though]

Please read this post for advice on how to ask a better question.

Have you filtered out missing values? [there aren’t any]

In a good question, we should be able to copy-paste your code into the repl and get the same error that you are asking about.

Please provide code that reproduces the error you are getting. If the error happens due to missing values in the data, then provide code that generates data with missing values.

Thanks for this assurance. Knowing that it had worked for you helped me find (someone else’s overloading error).

Apologies. I will try to present questions in a better format next time.
Still, your input helped here.
Thanks!

This error is probably worth noting, because it is from a Notebook from the standard translation of ISLR into Julia - Chapter 3. In the notebook there is a macro defined called ‘predict’ which overloads the standard ‘predict’ function. If the name of the macro is changed, there is no overloading, and everything works fine.

1 Like