I can’t figure out how to fit a model without the intercept
The documentatioin just says the intercept is fitted by default but doesn’t show how too do it without the intercept? is that even possible?
I can’t figure out how to fit a model without the intercept
The documentatioin just says the intercept is fitted by default but doesn’t show how too do it without the intercept? is that even possible?
using StatsModels
# add term(-1)
#e.g.
formula = Term(:target) ~ term(-1) + sum(Term.(Symbol.(names(last_2_years4[:, Not(:target)]))))
I answered in the other thread - here are two options with or without the @formula
macro:
julia> using GLM
julia> df = (x = rand(10), y = rand(Bool, 10));
julia> glm(@formula(y ~ 0 + x), df, Bernoulli(), LogitLink())
StatsModels.TableRegressionModel{GeneralizedLinearModel{GLM.GlmResp{Vector{Float64}, Bernoulli{Float64}, LogitLink}, GLM.DensePredChol{Float64, LinearAlgebra.CholeskyPivoted{Float64, Matrix{Float64}, Vector{Int64}}}}, Matrix{Float64}}
y ~ 0 + x
Coefficients:
──────────────────────────────────────────────────────────────
Coef. Std. Error z Pr(>|z|) Lower 95% Upper 95%
──────────────────────────────────────────────────────────────
x -1.32166 1.47523 -0.90 0.3703 -4.21306 1.56975
──────────────────────────────────────────────────────────────
julia> glm(reshape(df.x, 10, 1), df.y, Bernoulli(), LogitLink())
GeneralizedLinearModel{GLM.GlmResp{Vector{Float64}, Bernoulli{Float64}, LogitLink}, GLM.DensePredChol{Float64, LinearAlgebra.CholeskyPivoted{Float64, Matrix{Float64}, Vector{Int64}}}}:
Coefficients:
───────────────────────────────────────────────────────────────
Coef. Std. Error z Pr(>|z|) Lower 95% Upper 95%
───────────────────────────────────────────────────────────────
x1 -1.32166 1.47523 -0.90 0.3703 -4.21306 1.56975
───────────────────────────────────────────────────────────────
I want the terms to be constructed programmatically
Ah, that wasn’t clear from this question or the other thread, where the discussion was around using the lm
function.
If you want to translate something you know how to write in @formula
syntax into code, expanding the macro is helpful:
julia> @macroexpand(@formula(y ~ 0 + x))
:(StatsModels.Term(:y) ~ StatsModels.ConstantTerm(0) + StatsModels.Term(:x))
Also, term
is the preferred function for construcing Term
objects, as it will accept strings so you can do term.(names(df[!, Not(:target)]))