Skip to content

Commit

Permalink
return tibble only if loaded, not if only installed
Browse files Browse the repository at this point in the history
  • Loading branch information
elbersb committed Jun 13, 2018
1 parent cfbd032 commit f0f7514
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 44 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
^cran-comments\.md$
^codecov\.yml$
^\.travis\.yml$
^render_README\.R$
2 changes: 1 addition & 1 deletion R/segregation.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ globalVariables(c(
"sumcond2", "unit1", "unit2"))

as_tibble_or_df <- function(data) {
if (requireNamespace("tibble", quietly = TRUE)) {
if ("package:tibble" %in% search()) {
tibble::as_tibble(data)
} else {
df <- as.data.frame(data, stringsAsFactors = FALSE)
Expand Down
3 changes: 2 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ on the Mutual Information Index (M) and Theil's Information Index (H).
- calculate total, between, within, and local segregation
- decompose differences in total segregation over time
- estimate standard errors via bootstrapping
- every method returns a [tidy](http://vita.had.co.nz/papers/tidy-data.html) data frame (or [tibble](http://tibble.tidyverse.org), if the package is installed) for easy post-processing and plotting
- every method returns a [tidy](http://vita.had.co.nz/papers/tidy-data.html) data frame (or [tibble](http://tibble.tidyverse.org), if the package is loaded) for easy post-processing and plotting
- it's fast, because it uses the [`data.table`](https://github.com/Rdatatable/data.table/wiki) package internally

## Usage
Expand All @@ -39,6 +39,7 @@ The package provides an easy way to calculate segregation measures, based on the

```{r}
library(segregation)
# example dataset with fake data provided by the package
mutual_total(schools00, "race", "school", weight = "n")
```
Expand Down
74 changes: 32 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

<!-- README.md is generated from README.Rmd. Please edit that file -->

# segregation
Expand All @@ -17,8 +18,8 @@ and Theil’s Information Index (H).
- estimate standard errors via bootstrapping
- every method returns a
[tidy](http://vita.had.co.nz/papers/tidy-data.html) data frame (or
[tibble](http://tibble.tidyverse.org), if the package is installed)
for easy post-processing and plotting
[tibble](http://tibble.tidyverse.org), if the package is loaded) for
easy post-processing and plotting
- it’s fast, because it uses the
[`data.table`](https://github.com/Rdatatable/data.table/wiki)
package internally
Expand All @@ -30,25 +31,22 @@ based on the Mutual Information Index (M) and Theil’s Entropy Index (H).

``` r
library(segregation)

# example dataset with fake data provided by the package
mutual_total(schools00, "race", "school", weight = "n")
#> # A tibble: 2 x 2
#> stat est
#> * <chr> <dbl>
#> 1 M 0.426
#> 2 H 0.419
#> stat est
#> M 0.426
#> H 0.419
```

Standard errors in all functions can be estimated via boostrapping:

``` r
mutual_total(schools00, "race", "school", weight = "n", se = TRUE)
#> ..........
#> # A tibble: 2 x 3
#> stat est se
#> * <chr> <dbl> <dbl>
#> 1 M 0.429 0.000935
#> 2 H 0.422 0.000985
#> stat est se
#> M 0.429 0.000935
#> H 0.422 0.000985
```

Decompose segregation into a between-state and a within-state term (the
Expand All @@ -57,19 +55,15 @@ sum of these equals total segregation):
``` r
# between states
mutual_total(schools00, "race", "state", weight = "n")
#> # A tibble: 2 x 2
#> stat est
#> * <chr> <dbl>
#> 1 M 0.0992
#> 2 H 0.0977
#> stat est
#> M 0.0992
#> H 0.0977

# within states
mutual_total(schools00, "race", "school", within = "state", weight = "n")
#> # A tibble: 2 x 2
#> stat est
#> * <chr> <dbl>
#> 1 M 0.326
#> 2 H 0.321
#> stat est
#> M 0.326
#> H 0.321
```

Local segregation (`ls`) is a decomposition by units (here racial
Expand All @@ -81,14 +75,12 @@ M:
(local <- mutual_local(schools00, group = "school", unit = "race", weight = "n",
se = TRUE, wide = TRUE))
#> ..........
#> # A tibble: 5 x 5
#> race ls ls_se p p_se
#> <fct> <dbl> <dbl> <dbl> <dbl>
#> 1 asian 0.667 0.00674 0.0226 0.000124
#> 2 black 0.885 0.00259 0.190 0.000465
#> 3 hisp 0.782 0.00258 0.152 0.000317
#> 4 white 0.184 0.000725 0.628 0.000687
#> 5 native 1.53 0.0229 0.00745 0.000135
#> race ls ls_se p p_se
#> asian 0.667 0.006736 0.02261 0.000124
#> black 0.885 0.002595 0.19005 0.000465
#> hisp 0.782 0.002582 0.15179 0.000317
#> white 0.184 0.000725 0.62810 0.000687
#> native 1.528 0.022868 0.00745 0.000135

sum(local$p * local$ls)
#> [1] 0.429
Expand All @@ -103,18 +95,16 @@ mutual_difference(schools00, schools05, group = "race", unit = "school",
weight = "n", method = "ipf")
#> ........
#> .........
#> # A tibble: 9 x 2
#> stat est
#> * <chr> <dbl>
#> 1 M1 0.426
#> 2 M2 0.413
#> 3 diff -0.0122
#> 4 additions -0.00341
#> 5 removals -0.0114
#> 6 unit_marginal -0.0202
#> 7 group_marginal 0.0172
#> 8 interaction 0.00245
#> 9 structural 0.00318
#> stat est
#> M1 0.42554
#> M2 0.41339
#> diff -0.01215
#> additions -0.00341
#> removals -0.01141
#> unit_marginal -0.02020
#> group_marginal 0.01723
#> interaction 0.00245
#> structural 0.00318
```

## How to install
Expand Down
8 changes: 8 additions & 0 deletions render_README.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
if ("package:tibble" %in% search()) {
detach("package:tibble", unload=TRUE)
}

rmarkdown::render("README.Rmd",
output_format = rmarkdown::github_document(
df_print = function(...) { print(..., row.names=FALSE) },
html_preview = FALSE))
11 changes: 11 additions & 0 deletions tests/testthat/test_mutual_total.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ test_that("mutual M works both ways around", {
)
})

test_that("as tibble works", {
ret <- mutual_total(test_data, "u", "g", weight = "n")
expect_true("data.frame" %in% class(ret))

if(requireNamespace("tibble", quietly = TRUE)) {
library(tibble)
ret <- mutual_total(test_data, "u", "g", weight = "n")
expect_true("tbl_df" %in% class(ret))
}
})

test_that("between + within = total", {
expect_equal(
mutual_total(test_data, "u", "g", weight = "n")[["M", "est"]],
Expand Down

0 comments on commit f0f7514

Please sign in to comment.