Julia GLM-使用devresid进行绘图

Julia GLM-使用devresid进行绘图,julia,glm,Julia,Glm,我想对GLM做一些残差分析 我的模型在表格中 using GLM model = glm(@formula(y ~ x), data, Binomial(), LogitLink()) 我的教科书建议使用偏差残差进行GLM中的残差分析。我很高兴看到Julia的GLM有一个devresid()函数,它建议如何使用它进行绘图(符号(y-μ)*sqrt(devresid(D,y,μ)))。然而,我完全不知道输入参数应该是什么。查看文档字符串: ?devresid devresid(D, y, μ::

我想对GLM做一些残差分析

我的模型在表格中

using GLM
model = glm(@formula(y ~ x), data, Binomial(), LogitLink())
我的教科书建议使用偏差残差进行GLM中的残差分析。我很高兴看到Julia的GLM有一个
devresid()
函数,它建议如何使用它进行绘图(
符号(y-μ)*sqrt(devresid(D,y,μ))
)。然而,我完全不知道输入参数应该是什么。查看文档字符串:

?devresid
devresid(D, y, μ::Real)

Return the squared deviance residual of μ from y for distribution D

The deviance of a GLM can be evaluated as the sum of the squared deviance residuals. This is the principal use for these values. The actual deviance residual, say for plotting, is the signed square root of this value

sign(y - μ) * sqrt(devresid(D, y, μ))

Examples

julia> devresid(Normal(), 0, 0.25) ≈ abs2(0.25)
true

julia> devresid(Bernoulli(), 1, 0.75) ≈ -2*log(0.75)
true

julia> devresid(Bernoulli(), 0, 0.25) ≈ -2*log1p(-0.25)
true
  • D:我猜在我的例子中是二项式的
  • y:我猜这是单个案例的指标变量,即1或0
  • 这是什么
我如何使用此函数生成正常概率标度上的偏差残差图和拟合值?

这是我在CSV表单中使用的数据

x,y
400,0
220,1
490,0
210,1
500,0
270,0
200,1
470,0
480,0
310,1
240,1
490,0
420,0
330,1
280,1
210,1
300,1
470,1
230,0
430,0
460,0
220,1
250,1
200,1
390,0

我知道这是你想要的:

julia> data = DataFrame(X=[1,1,1,2,2], Y=[1,1,0,0,1])
5×2 DataFrame
│ Row │ X     │ Y     │
│     │ Int64 │ Int64 │
├─────┼───────┼───────┤
│ 1   │ 1     │ 1     │
│ 2   │ 1     │ 1     │
│ 3   │ 1     │ 0     │
│ 4   │ 2     │ 0     │
│ 5   │ 2     │ 1     │

julia> model = glm(@formula(Y ~ X), data, Binomial(), LogitLink())
StatsModels.TableRegressionModel{GeneralizedLinearModel{GLM.GlmResp{Array{Float64,1},Binomial{Float64},LogitLink},GLM.DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}}

Y ~ 1 + X

Coefficients:
─────────────────────────────────────────────────────────────────────────────
              Estimate  Std. Error    z value  Pr(>|z|)  Lower 95%  Upper 95%
─────────────────────────────────────────────────────────────────────────────
(Intercept)   1.38629      2.82752   0.490286    0.6239   -4.15554    6.92813
X            -0.693146     1.87049  -0.37057     0.7110   -4.35923    2.97294
─────────────────────────────────────────────────────────────────────────────

julia> p = predict(model)
5-element Array{Float64,1}:
 0.6666664218508201
 0.6666664218508201
 0.6666664218508201
 0.5
 0.5

julia> y = data.Y
5-element Array{Int64,1}:
 1
 1
 0
 0
 1

julia> @. sign(y - p) * sqrt(devresid(Bernoulli(), y, p))
5-element Array{Float64,1}:
  0.9005170462928523
  0.9005170462928523
 -1.4823033118905455
 -1.1774100225154747
  1.1774100225154747
(这是在R中调用
残差(model,type=“deviance”)
得到的结果)

请注意,在最后一行中,我使用
@.
对整行进行矢量化。或者,您可以将其写成:

julia> sign.(y .- p) .* sqrt.(devresid.(Bernoulli(), y, p))
5-element Array{Float64,1}:
  0.9005170462928523
  0.9005170462928523
 -1.4823033118905455
 -1.1774100225154747
  1.1774100225154747