Skip to content

Commit

Permalink
Merge a167337 into a223c5a
Browse files Browse the repository at this point in the history
  • Loading branch information
harsharora21 authored Jun 27, 2022
2 parents a223c5a + a167337 commit 437c0b6
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions src/svyglm.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
mutable struct control
rtol
atol
maxiter
end

"""
```julia
svyglm(formula, design, dist, link)
Expand All @@ -13,7 +19,7 @@ julia> data(api);
julia> dclus1 = svydesign(id=:dnum, weights=:pw, data = apiclus1, fpc=:fpc);
julia> svyglm(@formula(ell~meals),dclus1,Normal(),IdentityLink())
svyglm(StatsModels.TableRegressionModel{GLM.GeneralizedLinearModel{GLM.GlmResp{Vector{Float64}, Normal{Float64}, IdentityLink}, GLM.DensePredChol{Float64, LinearAlgebra.Cholesky{Float64, Matrix{Float64}}}}, Matrix{Float64}}
StatsModels.TableRegressionModel{GLM.GeneralizedLinearModel{GLM.GlmResp{Vector{Float64}, Normal{Float64}, IdentityLink}, GLM.DensePredChol{Float64, LinearAlgebra.Cholesky{Float64, Matrix{Float64}}}}, Matrix{Float64}}
ell ~ 1 + meals
Expand All @@ -23,25 +29,7 @@ Coefficients:
────────────────────────────────────────────────────────────────────────
(Intercept) 6.86665 0.350512 19.59 <1e-84 6.17966 7.55364
meals 0.410511 0.00613985 66.86 <1e-99 0.398477 0.422545
────────────────────────────────────────────────────────────────────────, [6.866653500690286, 0.41051064115199803], 183×40 DataFrame
Row │ Column1 cds stype name sname snum dname dnum c ⋯
│ Int64 Int64 String1 String15 String Int64 String31 Int64 S ⋯
─────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ 1 1612910137588 H San Leandro Hig San Leandro High 236 San Leandro Unified 637 A ⋯
2 │ 2 1612916002372 E Garfield Elemen Garfield Elementary 237 San Leandro Unified 637 A
3 │ 3 1612916002398 E Jefferson Eleme Jefferson Elementary 238 San Leandro Unified 637 A
4 │ 4 1612916002414 E Madison (James) Madison (James) Elementary 239 San Leandro Unified 637 A
5 │ 5 1612916002422 E McKinley Elemen McKinley Elementary 240 San Leandro Unified 637 A ⋯
6 │ 6 1612916002430 E Monroe Elementa Monroe Elementary 241 San Leandro Unified 637 A
7 │ 7 1612916002448 E Roosevelt Eleme Roosevelt Elementary 242 San Leandro Unified 637 A
8 │ 8 1612916002455 E Washington Elem Washington Elementary 243 San Leandro Unified 637 A
⋮ │ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋱
177 │ 177 43733876047633 E Weller (Joseph) Weller (Joseph) Elementary 5444 Milpitas Unified 448 S ⋯
178 │ 178 43733876047641 E Pomeroy (Marsha Pomeroy (Marshall) Elementary 5445 Milpitas Unified 448 S
179 │ 179 43733876047666 M Rancho Milpitas Rancho Milpitas Junior High 5446 Milpitas Unified 448 S
182 │ 182 43733876047690 E Burnett (Willia Burnett (William) Elementary 5449 Milpitas Unified 448 S
183 │ 183 43733876067219 E Zanker (Pearl) Zanker (Pearl) Elementary 5450 Milpitas Unified 448 S
32 columns and 168 rows omitted, [33.846996307373, 33.846996307373, 33.846996307373, 33.846996307373, 33.846996307373, 33.846996307373, 33.846996307373, 33.846996307373, 33.846996307373, 33.846996307373 … 33.846996307373, 33.846996307373, 33.846996307373, 33.846996307373, 33.846996307373, 33.846996307373, 33.846996307373, 33.846996307373, 33.846996307373, 33.846996307373], 49195.421245741614, Normal{Float64}(μ=0.0, σ=1.0), 2, ell ~ 1 + meals, (ell = [22, 23, 27, 17, 27, 24, 10, 33, 36, 21 … 25, 17, 21, 26, 24, 29, 48, 13, 31, 30], meals = [19, 39, 39, 23, 43, 36, 17, 43, 48, 28 … 31, 18, 15, 41, 18, 33, 47, 23, 29, 26]), 1.0196009035970893e6, Float64[], [22.0, 23.0, 27.0, 17.0, 27.0, 24.0, 10.0, 33.0, 36.0, 21.0 … 25.0, 17.0, 21.0, 26.0, 24.0, 29.0, 48.0, 13.0, 31.0, 30.0], [14.66635568257825, 22.87656850561821, 22.87656850561821, 16.30839824718624, 24.518611070226203, 21.645036582162216, 13.845334400274252, 24.518611070226203, 26.57116427598619, 18.36095145294623 … 19.592483376402225, 14.255845041426252, 13.024313117970257, 23.697589787922205, 14.255845041426252, 20.41350465870622, 26.160653634834194, 16.30839824718624, 18.77146209409823, 17.539930170642236], [14.66635568257825, 22.87656850561821, 22.87656850561821, 16.30839824718624, 24.518611070226203, 21.645036582162216, 13.845334400274252, 24.518611070226203, 26.57116427598619, 18.36095145294623 … 19.592483376402225, 14.255845041426252, 13.024313117970257, 23.697589787922205, 14.255845041426252, 20.41350465870622, 26.160653634834194, 16.30839824718624, 18.77146209409823, 17.539930170642236], [33.846996307373, 33.846996307373, 33.846996307373, 33.846996307373, 33.846996307373, 33.846996307373, 33.846996307373, 33.846996307373, 33.846996307373, 33.846996307373 … 33.846996307373, 33.846996307373, 33.846996307373, 33.846996307373, 33.846996307373, 33.846996307373, 33.846996307373, 33.846996307373, 33.846996307373, 33.846996307373], [7.333644317421751, 0.12343149438179069, 4.123431494381791, 0.6916017528137601, 2.4813889297737965, 2.3549634178377836, -3.845334400274252, 8.481388929773797, 9.428835724013808, 2.6390485470537683 … 5.407516623597775, 2.7441549585737484, 7.975686882029743, 2.3024102120777954, 9.744154958573748, 8.58649534129378, 21.839346365165806, -3.30839824718624, 12.22853790590177, 12.460069829357764])
────────────────────────────────────────────────────────────────────────
```
"""
mutable struct svyglm
Expand All @@ -61,7 +49,11 @@ mutable struct svyglm
fitted_values
prior_weights #initial weights
residuals
function svyglm_cons(glm, data, weights)
converged
control
terms
contrasts
function svyglm_cons(glm, data, weights,rtol,atol,maxiter)
out = new()
out.glm = glm
out.coefficients = coef(glm)
Expand All @@ -79,11 +71,18 @@ mutable struct svyglm
out.fitted_values = fitted(glm)
out.prior_weights = weights
out.residuals = glm.model.rr.wrkresid
out.converged = true
out.control = control(rtol,atol,maxiter)
out.terms = glm.mf.f
out.contrasts = []
out
end

function svyglm(formula, design, dist, link)
data = design.data
rtol = 1e-8
atol = 1e-8
maxiter = 30
if design.probs != Symbol("")
weights = (1.0 ./ data[:,design.probs])
else
Expand All @@ -92,7 +91,11 @@ mutable struct svyglm
if design.weights != Symbol("")
weights .*= data[:,design.weights]
end
absglm = glm(formula, data, dist, link, wts = weights, rtol = 1e-8, atol = 1e-8)
svyglm_cons(absglm, data, weights)
glmout = glm(formula, data, dist, link, wts = weights, rtol = rtol, atol = atol, maxiter = maxiter)
svyglm_cons(glmout, data, weights,rtol,atol,maxiter)
end
end

function Base.show(io::IO, g::svyglm)
print(g.glm)
end

0 comments on commit 437c0b6

Please sign in to comment.