Skip to content

Commit

Permalink
new aggregation tweaks and testing, remove compind to appease CMD
Browse files Browse the repository at this point in the history
  • Loading branch information
bluefoxr committed Apr 11, 2024
1 parent c746008 commit 76e5fb1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 14 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ Suggests:
spelling,
knitr,
testthat (>= 3.0.0),
Compind,
matrixStats,
performance,
covr
Expand Down
1 change: 0 additions & 1 deletion R/aggregate.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ Aggregate.coin <- function(x, dset, f_ag = NULL, w = NULL, f_ag_para = NULL, dat
by_dfs <- by_df
}


# CHECK AND SET f_ag ------------------------------------------------------

# default and check
Expand Down
32 changes: 32 additions & 0 deletions tests/testthat/test-aggregate.R
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,35 @@ test_that("aggregation by level", {
expect_equal(X[["Index"]], 10:(nrow(X) + 9))

})

# test passing no weights to aggregation function...
test_that("sum_by_level", {

coin <- build_example_coin(up_to = "new_coin")

# test as sum of indicators in each group (weights NOT passed)
coin <- Aggregate(coin, dset = "Raw", f_ag = "sum",
f_ag_para = list(na.rm = TRUE), w = "none"
)

# checks - pick a selected value
CHN_phys <- get_data(coin, dset = "Aggregated", iCodes = "Physical", Level = 2, uCodes = "CHN", also_get = "none") |>
as.numeric()

CHN_phys_man <- get_data(coin, dset = "Raw", iCodes = "Physical", Level = 1, uCodes = "CHN", also_get = "none") |>
as.numeric() |>
sum(na.rm = TRUE)

expect_equal(CHN_phys, CHN_phys_man)

# another
IND_conn <- get_data(coin, dset = "Aggregated", iCodes = "Conn", Level = 3, uCodes = "IND", also_get = "none") |>
as.numeric()

IND_conn_man <- get_data(coin, dset = "Aggregated", iCodes = "Conn", Level = 2, uCodes = "IND", also_get = "none") |>
as.numeric() |>
sum(na.rm = TRUE)

expect_equal(IND_conn, IND_conn_man)

})
29 changes: 17 additions & 12 deletions vignettes/aggregate.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,16 @@ coin <- Aggregate(coin, dset = "Normalised",

All of the four aggregation functions mentioned above have the same format (try e.g. `?a_gmean`), and are built into the COINr package. But what if we want to use another type of aggregation function? The process is exactly the same.

*NOTE: the compind package has been disabled here from running the commands in this vignette because of changes to a dependent package which are causing problems with the R CMD check. The commands should still work if you run them, but the results will not be shown here.*

In this section we use some functions from other packages: the matrixStats package and the Compind package. These are not imported by COINr, so the code here will only work if you have these installed. If this vignette was built on your computer, we have to check whether these packages are installed:

```{r}
ms_installed <- requireNamespace("matrixStats", quietly = TRUE)
ms_installed
ci_installed <- requireNamespace("Compind", quietly = TRUE)
ci_installed
# ci_installed <- requireNamespace("Compind", quietly = TRUE)
# ci_installed
```

If either of these have returned `FALSE`, in the following code chunks you will see some blanks. See the online version of this vignette to see the results, or install the above packages and rebuild the vignettes.
Expand Down Expand Up @@ -195,18 +197,21 @@ Sometimes this may mean that we have to create a wrapper function to satisfy the

```{r, eval= F}
# RESTORE ABOVE eval= ci_installed
# load Compind
suppressPackageStartupMessages(library(Compind))
# wrapper to get output of interest from ci_bod
# also suppress messages about missing values
ci_bod2 <- function(x){
suppressMessages(Compind::ci_bod(x)$ci_bod_est)
}
# NOTE: this chunk disabled - see comments above.
# aggregate
coin <- Aggregate(coin, dset = "Normalised",
f_ag = "ci_bod2", by_df = TRUE, w = "none")
# load Compind
# suppressPackageStartupMessages(library(Compind))
#
# # wrapper to get output of interest from ci_bod
# # also suppress messages about missing values
# ci_bod2 <- function(x){
# suppressMessages(Compind::ci_bod(x)$ci_bod_est)
# }
#
# # aggregate
# coin <- Aggregate(coin, dset = "Normalised",
# f_ag = "ci_bod2", by_df = TRUE, w = "none")
```

The benefit of the doubt approach automatically assigns individual weights to each unit, so we need to specify `w = "none"` to stop `Aggregate()` from attempting to pass weights to the function. Importantly, we also need to specify `by_df = TRUE` which tells `Aggregate()` to pass a data frame to `f_ag` rather than a vector.
Expand Down

0 comments on commit 76e5fb1

Please sign in to comment.