-
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.
Rearrange sections and other changes.
- Loading branch information
1 parent
4b9c8c1
commit be38de7
Showing
9 changed files
with
175 additions
and
247 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
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,98 +1,121 @@ | ||
# Comparison with R | ||
# Moving from R to Julia | ||
This sections presents examples to help move from R to Julia. Examples show R and Julia code for common operations in survey analysis. <br> | ||
For the same operation, first the R and then the Julia code is presented. | ||
|
||
In the following examples, we'll compare Julia's performance to R's on the same set of operations. | ||
## Simple random sample | ||
|
||
## Installing and loading the package | ||
**R** | ||
The `apisrs` data, which is provided in both `survey` and `Survey`, is used as an example. It's a simple random sample of the Academic Performance Index of Californian schools. | ||
|
||
```r | ||
install.package("survey") | ||
### 1. Creating a design object | ||
The following example shows how to construct a design object for a simple random sample. | ||
|
||
```R | ||
library(survey) | ||
data(api) | ||
dsrs = svydesign(id = ~1, data = apisrs, weights = ~pw, fpc = ~fpc) | ||
``` | ||
|
||
**Julia** | ||
```julia | ||
using Pkg | ||
Pkg.add(url = "https://github.com/xKDR/Survey.jl.git") | ||
using Survey | ||
srs = load_data("apisrs") | ||
dsrs = SimpleRandomSample(srs; popsize = :fpc) | ||
``` | ||
|
||
The following command in the Pkg REPL may also be used to install the package. | ||
### 2. Mean | ||
In the following example the mean of the variable `api00` is calculated. | ||
|
||
```R | ||
svymean(~api00, dsrs) | ||
``` | ||
add "https://github.com/xKDR/Survey.jl.git" | ||
```julia | ||
mean(:api00, dsrs) | ||
``` | ||
|
||
## API data | ||
### 3. Total | ||
In the following example the sum of the variable `api00` is calculated. | ||
|
||
The Academic Performance Index is computed for all California schools based on standardised testing of students. The [data sets](https://cran.r-project.org/web/packages/survey/survey.pdf) contain information for all schools with at least 100 students and for various probability samples of the data. apiclus1 is a cluster sample of school districts, apistrat is a sample stratified by stype. | ||
```R | ||
svytotal(~api00, dsrs) | ||
``` | ||
```julia | ||
total(:api00, dsrs) | ||
``` | ||
|
||
In the following examples, we'll use the apiclus1 data from the api dataset. | ||
### 4. Quantile | ||
In the following example the median of the variable `api00` is calculated. | ||
```R | ||
svyquantile(~api00, dsrs, 0.5) | ||
``` | ||
```julia | ||
quantile(:api00, dsrs, 0.5) | ||
``` | ||
|
||
The api dataset can be loaded using the following command: | ||
### 5. Domain estimation | ||
In the following example the mean of the variable `api00` is calculated grouped by the variable `cname`. | ||
|
||
**R** | ||
```r | ||
data(api) | ||
```R | ||
svyby(~api00, ~cname, dsrs, svymean) | ||
``` | ||
|
||
**Julia** | ||
```julia | ||
apiclus1 = load_data("apiclus1") | ||
by(:api00, :cname, dsrs, mean) | ||
``` | ||
|
||
## svydesign | ||
[The ```svydesign``` object combines a data frame and all the survey design information needed to analyse it.](https://www.rdocumentation.org/packages/survey/versions/4.1-1/topics/svydesign) | ||
## Stratified sample | ||
|
||
A ```design``` object can be constructed with the following command: | ||
The `apistrat` data, which is provided in both `survey` and `Survey`, is used as an example. It's a stratified sample of the Academic Performance Index of Californian schools. | ||
|
||
**R** | ||
```r | ||
dclus1 <-svydesign(id = ~1, weights = ~pw, data = apiclus1, fpc = ~fpc) | ||
### 1. Creating a design object | ||
The following example shows how to construct a design object for a stratified sample. | ||
|
||
```R | ||
library(survey) | ||
data(api) | ||
dstrat = svydesign(id = ~1, data = apistrat, strata = ~stype, weights = ~pw, fpc = ~fpc) | ||
``` | ||
|
||
**Julia** | ||
```julia | ||
dclus1 = design(id = :1, weights = :pw, data = apiclus1, fpc = :fpc) | ||
using Survey | ||
strat = load_data("apistrat") | ||
dstrat = StratifiedSample(strat, :stype; popsize = :fpc) | ||
``` | ||
|
||
## by | ||
The `by` function can be used to generate stratified estimates. | ||
|
||
### Mean | ||
Weighted mean of a variable by strata can be computed using the following command: | ||
### 2. Mean | ||
In the following example the mean of the variable `api00` is calculated. | ||
|
||
**R** | ||
```r | ||
svyby(~api00, by = ~cname, design = dclus1, svymean) | ||
```R | ||
svymean(~api00, dstrat) | ||
``` | ||
|
||
**Julia** | ||
```julia | ||
by(:api00, :cname, dclus1, mean) | ||
mean(:api00, dstrat) | ||
``` | ||
|
||
### Sum | ||
Weighted sum of a variable by strata can be computed using the following command: | ||
### 3. Total | ||
In the following example the sum of the variable `api00` is calculated. | ||
|
||
**R** | ||
```r | ||
svyby(~api00, by = ~cname, design = dclus1, svytotal) | ||
```R | ||
svytotal(~api00, dstrat) | ||
``` | ||
```julia | ||
total(:api00, dstrat) | ||
``` | ||
|
||
**Julia** | ||
### 4. Quantile | ||
In the following example the median of the variable `api00` is calculated. | ||
```R | ||
svyquantile(~api00, dstrat, 0.5) | ||
``` | ||
```julia | ||
by(:api00, :cname, dclus1, total) | ||
quantile(:api00, dstrat, 0.5) | ||
``` | ||
|
||
### Quantile | ||
Weighted quantile of a variable by strata can be computed using the following command: | ||
### 5. Domain estimation | ||
In the following example the mean of the variable `api00` is calculated grouped by the variable `cname`. | ||
|
||
**R** | ||
```r | ||
svyby(~api00, by = ~cname, design = dclus1, svyquantile, quantile = 0.63) | ||
```R | ||
svyby(~api00, ~cname, dstrat, svymean) | ||
``` | ||
|
||
**Julia** | ||
```julia | ||
by(:api00, :cname, dclus1, quantile, 0.63) | ||
``` | ||
by(:api00, :cname, dstrat, mean) | ||
``` |
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,32 @@ | ||
# API | ||
|
||
## Index | ||
|
||
```@index | ||
Module = [Survey] | ||
Order = [:type, :function] | ||
Private = false | ||
``` | ||
|
||
```@docs | ||
AbstractSurveyDesign | ||
SimpleRandomSample | ||
StratifiedSample | ||
load_data | ||
mean(x::Symbol, design::SimpleRandomSample) | ||
total(x::Symbol, design::SimpleRandomSample) | ||
quantile | ||
by | ||
colnames(design::AbstractSurveyDesign) | ||
dim(design::AbstractSurveyDesign) | ||
dimnames(design::AbstractSurveyDesign) | ||
plot(design::AbstractSurveyDesign, x::Symbol, y::Symbol; kwargs...) | ||
boxplot(design::AbstractSurveyDesign, x::Symbol, y::Symbol; kwargs...) | ||
hist(design::AbstractSurveyDesign, var::Symbol, | ||
bins::Union{Integer, AbstractVector} = freedman_diaconis(design, var); | ||
normalization = :density, | ||
kwargs... | ||
) | ||
freedman_diaconis | ||
sturges | ||
``` |
This file was deleted.
Oops, something went wrong.
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.