-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #316 from xKDR/0.3.1
0.4.0
- Loading branch information
Showing
23 changed files
with
786 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
name = "Survey" | ||
uuid = "c1a98b4d-6cd2-47ec-b9e9-69b59c35373c" | ||
authors = ["Ayush Patnaik <[email protected]>"] | ||
version = "0.3.0" | ||
version = "0.4.0" | ||
|
||
[deps] | ||
AlgebraOfGraphics = "cbdf2221-f076-402e-a563-3d30da359d67" | ||
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b" | ||
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0" | ||
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597" | ||
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" | ||
GLM = "38e38edf-8417-5370-95a0-9cbb8c7f171a" | ||
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | ||
Missings = "e1d29d7a-bbdc-5cf2-9ac0-f12de2c33e28" | ||
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# Generalized Linear Models in Survey | ||
The `glm()` function in the Julia Survey package is used to fit generalized linear models (GLMs) to survey data. It incorporates survey design information, such as sampling weights, stratification, and clustering, to produce valid estimates and standard errors that account for the type of survey design. | ||
|
||
As of June 2023, the [GLM.jl documentation](https://juliastats.org/GLM.jl/stable/) lists the supported distribution families and their link functions as: | ||
```txt | ||
Bernoulli (LogitLink) | ||
Binomial (LogitLink) | ||
Gamma (InverseLink) | ||
InverseGaussian (InverseSquareLink) | ||
NegativeBinomial (NegativeBinomialLink, often used with LogLink) | ||
Normal (IdentityLink) | ||
Poisson (LogLink) | ||
``` | ||
|
||
Refer to the GLM.jl documentation for more information about the GLM package. | ||
|
||
## Fitting a GLM to a Survey Design object | ||
|
||
You can fit a GLM to a Survey Design object the same way you would fit it to a regular data frame. The only difference is that you need to specify the survey design object as the second argument to the `glm()` function. | ||
|
||
```julia | ||
using Survey | ||
apisrs = load_data("apisrs") | ||
|
||
# Simple random sample survey | ||
srs = SurveyDesign(apisrs, weights = :pw) | ||
|
||
# Survey stratified by stype | ||
dstrat = SurveyDesign(apistrat, strata = :stype, weights = :pw) | ||
|
||
# Survey clustered by dnum | ||
dclus1 = SurveyDesign(apiclus1, clusters = :dnum, weights = :pw) | ||
``` | ||
|
||
Once you have the survey design object, you can fit a GLM using the `glm()` function. Specify the formula for the model and the distribution family. | ||
|
||
The `glm()` function supports all distribution families supported by GLM.jl, i.e. Bernoulli, Binomial, Gamma, Geometric, InverseGaussian, NegativeBinomial, Normal, and Poisson. | ||
|
||
For example, to fit a GLM with a Bernoulli distribution and a Logit link function to the `srs` survey design object we created above: | ||
```julia | ||
formula = @formula(api00 ~ api99) | ||
my_glm = glm(formula, srs, family = Normal()) | ||
|
||
# View the coefficients and standard errors | ||
my_glm.Coefficients | ||
my_glm.SE | ||
``` | ||
|
||
## Examples | ||
|
||
The examples below use the `api` datasets, which contain survey data collected about California schools. The datasets are included in the Survey.jl package and can be loaded by calling `load_data("name_of_dataset")`. | ||
|
||
### Bernoulli with Logit Link | ||
|
||
A school being eligible for the awards program (`awards`) is a binary outcome (0 or 1). Let's assume it follows a Bernoulli distribution. Suppose we want to predict `awards` based on the percentage of students eligible for subsidized meals (`meals`) and the percentage of English Language Learners (`ell`). We can fit this GLM using the code below: | ||
|
||
```julia | ||
using Survey | ||
apisrs = load_data("apisrs") | ||
srs = SurveyDesign(apisrs, weights = :pw) | ||
|
||
# Convert yes/no to 1/0 | ||
apisrs.awards = ifelse.(apisrs.awards .== "Yes", 1, 0) | ||
|
||
# Fit the model | ||
model = glm(@formula(awards ~ meals + ell), apisrs, Bernoulli(), LogitLink()) | ||
``` | ||
|
||
### Poisson with Log Link | ||
|
||
Let us assume that the number of students tested (`api_stu`) follows a Poisson distribution, which models the number of successes out of a fixed number of trials. Suppose we want to predict the number of students tested based on the percentage of students eligible for subsidized meals (`meals`) and the percentage of English Language Learners (`ell`). We can fit this GLM using the code below: | ||
|
||
```julia | ||
using Survey | ||
apisrs = load_data("apisrs") | ||
srs = SurveyDesign(apisrs, weights = :pw) | ||
|
||
# Rename api.stu to api_stu | ||
rename!(apisrs, Symbol("api.stu") => :api_stu) | ||
|
||
# Fit the model | ||
model = glm(@formula(api_stu ~ meals + ell), apisrs, Poisson(), LogLink()) | ||
``` | ||
|
||
### Gamma with Inverse Link | ||
|
||
Let us assume that the average parental education level (`avg_ed`) follows a Gamma distribution, which is suitable for modeling continuous, positive-valued variables with a skewed distribution. Suppose we want to predict the average parental education level based on the percentage of students eligible for subsidized meals (`meals`) and the percentage of English Language Learners (`ell`). We can fit this GLM using the code below: | ||
|
||
```julia | ||
using Survey | ||
apisrs = load_data("apisrs") | ||
srs = SurveyDesign(apisrs, weights = :pw) | ||
|
||
# Rename api.stu to api_stu | ||
rename!(apisrs, Symbol("avg.ed") => :avg_ed) | ||
|
||
# Fit the model | ||
model = glm(@formula(avg_ed ~ meals + ell), apisrs, Gamma(), InverseLink()) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.