Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test for life_exp() plot and gen_data_le() function #114

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
^README\.Rmd$
^\.Rproj\.user$
^\.github$
^\.pre-commit-config\.yaml$
^_pkgdown\.yml$
^docs$
^pkgdown$
Expand Down
2 changes: 0 additions & 2 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,3 @@ Fixes #<NUM>

- [ ] *Lorem ipsum dolor sit amet, consectetur adipiscing.*
- [ ] Everything looks ok?

Thank you!
33 changes: 33 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:

name: lint.yaml

permissions: read-all

jobs:
lint:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::lintr, local::.
needs: lint

- name: Lint
run: lintr::lint_package()
shell: Rscript {0}
env:
LINTR_ERROR_ON_LINT: true
85 changes: 85 additions & 0 deletions .github/workflows/pr-commands.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
issue_comment:
types: [created]

name: pr-commands.yaml

permissions: read-all

jobs:
document:
if: ${{ github.event.issue.pull_request && (github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER') && startsWith(github.event.comment.body, '/document') }}
name: document
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
permissions:
contents: write
steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/pr-fetch@v2
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::roxygen2
needs: pr-document

- name: Document
run: roxygen2::roxygenise()
shell: Rscript {0}

- name: commit
run: |
git config --local user.name "$GITHUB_ACTOR"
git config --local user.email "[email protected]"
git add man/\* NAMESPACE
git commit -m 'Document'

- uses: r-lib/actions/pr-push@v2
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

style:
if: ${{ github.event.issue.pull_request && (github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER') && startsWith(github.event.comment.body, '/style') }}
name: style
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
permissions:
contents: write
steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/pr-fetch@v2
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

- uses: r-lib/actions/setup-r@v2

- name: Install dependencies
run: install.packages("styler")
shell: Rscript {0}

- name: Style
run: styler::style_pkg()
shell: Rscript {0}

- name: commit
run: |
git config --local user.name "$GITHUB_ACTOR"
git config --local user.email "[email protected]"
git add \*.R
git commit -m 'Style'

- uses: r-lib/actions/pr-push@v2
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
76 changes: 0 additions & 76 deletions .pre-commit-config.yaml

This file was deleted.

41 changes: 26 additions & 15 deletions R/data-process.R
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#' Data Processing
#'
#' This file contains a set of functions designed to work together for processing the data.

Check warning on line 3 in R/data-process.R

View workflow job for this annotation

GitHub Actions / lint

file=R/data-process.R,line=3,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 91 characters.
#' Below is a description of how to use these functions in sequence.
#'
#' ## Step-by-Step Usage:
#'
#' 1. **Read the data**: This function reads the data from the location specified `data <- readRDS("data.rds")`.

Check warning on line 8 in R/data-process.R

View workflow job for this annotation

GitHub Actions / lint

file=R/data-process.R,line=8,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 113 characters.
#'
#' 1. **`gen_data_weighted`**: Calculates weighted mean values for various metrics over years `data_weighted <- gen_data_weighted(data)`.

Check warning on line 10 in R/data-process.R

View workflow job for this annotation

GitHub Actions / lint

file=R/data-process.R,line=10,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 137 characters.
#'
#' 1. **`gen_data_weighted_rf`**: Calculates the differences between intervention and baseline values for risk factors `data_weighted_rf_wide_collapse <- gen_data_weighted_rf(data_weighted)`.

Check warning on line 12 in R/data-process.R

View workflow job for this annotation

GitHub Actions / lint

file=R/data-process.R,line=12,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 191 characters.
#'
#' 1. **`gen_data_weighted_ds`**: Calculates the differences between intervention and baseline values for incidences `data_weighted_ds_wide_collapse <- gen_data_weighted_ds(data_weighted)`.

Check warning on line 14 in R/data-process.R

View workflow job for this annotation

GitHub Actions / lint

file=R/data-process.R,line=14,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 189 characters.
#'
#' 1. **`gen_data_weighted_burden`**: Calculates the differences between intervention and baseline values for burden of disease `data_weighted_burden_wide_collapse <- gen_data_weighted_burden(data_weighted)`.

Check warning on line 16 in R/data-process.R

View workflow job for this annotation

GitHub Actions / lint

file=R/data-process.R,line=16,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 208 characters.
#'
#' 1. **`gen_data_weighted_burden_spline`**: Performs data smoothing for burden of disease, when necessary. For instance, with only a few simulations, there can be positive values in difference in burden of disease `data_weighted_burden_spline <- gen_data_weighted_burden_spline(data_weighted_burden_wide_collapse)`.

Check warning on line 18 in R/data-process.R

View workflow job for this annotation

GitHub Actions / lint

file=R/data-process.R,line=18,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 316 characters.
#'
#' 1. **`gen_data_le`**: Calculates life expectancy for various age and groups `data_ple_wide <- gen_data_le(data_weighted)`.

Check warning on line 20 in R/data-process.R

View workflow job for this annotation

GitHub Actions / lint

file=R/data-process.R,line=20,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 125 characters.
#'
#' ## Examples
#' ```r
Expand All @@ -27,7 +27,7 @@
#' data_weighted_rf_wide_collapse <- gen_data_weighted_rf(data_weighted)
#' data_weighted_ds_wide_collapse <- gen_data_weighted_ds(data_weighted)
#' data_weighted_burden_wide_collapse <- gen_data_weighted_burden(data_weighted)
#' data_weighted_burden_spline <- gen_data_weighted_burden_spline(data_weighted_burden_wide_collapse)

Check warning on line 30 in R/data-process.R

View workflow job for this annotation

GitHub Actions / lint

file=R/data-process.R,line=30,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 101 characters.
#' data_ple_wide <- gen_data_le(data_weighted)
#' ```
#'
Expand All @@ -45,7 +45,7 @@
print("Loading the config file...")
config <- load_config("default")
print("Processing the data...")
colnames(data) <- gsub("^mean_", "", colnames(data)) # Clean the column names by removing 'mean_'

Check warning on line 48 in R/data-process.R

View workflow job for this annotation

GitHub Actions / lint

file=R/data-process.R,line=48,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 99 characters.
weight_column <- rlang::sym(config$weight)
data_weighted <- data |>
dplyr::group_by(dplyr::across(dplyr::all_of(config$grouping_vars))) |>
Expand Down Expand Up @@ -179,9 +179,9 @@
)

data_weighted_ds_wide <- tidyr::pivot_wider(data_weighted_ds,
names_from = config$names_from,
id_cols = config$id_cols,
values_from = config$weighted_ds
names_from = config$names_from,
id_cols = config$id_cols,
values_from = config$weighted_ds
)

data_weighted_ds_wide_diff <- data_weighted_ds_wide |>
Expand Down Expand Up @@ -359,9 +359,11 @@
config <- load_config("default")
print("Processing the data...")
config_file_path <- system.file("config", "config.yml", package = "healthgpsrvis")
burden_spline <- config::get(value = "burden_spline",
file = config_file_path,
use_parent = FALSE)
burden_spline <- config::get(
value = "burden_spline",
file = config_file_path,
use_parent = FALSE
)

## This function is data smoothing
## It is applied manually now in India project due to abnormal positive values in diff_daly or cumdiff_daly
Expand Down Expand Up @@ -391,25 +393,31 @@
## Fit spline and predict
spline_fit_mean <- splines::interpSpline(
as.numeric(unlist(data_weighted_burden_mean[config$group])),
as.numeric(unlist(data_weighted_burden_mean[burden_spline[[1]]$burden_mean])))
as.numeric(unlist(data_weighted_burden_mean[burden_spline[[1]]$burden_mean]))
)
data_weighted_burden_spline[burden_spline[[1]]$burden_mean] <- stats::predict(
spline_fit_mean,
as.numeric(unlist(data_weighted_burden_spline[config$group])))$y
as.numeric(unlist(data_weighted_burden_spline[config$group]))
)$y

spline_fit_min <- splines::interpSpline(
as.numeric(unlist(data_weighted_burden_min[config$group])),
as.numeric(unlist(data_weighted_burden_min[burden_spline[[2]]$burden_min])))
as.numeric(unlist(data_weighted_burden_min[burden_spline[[2]]$burden_min]))
)
data_weighted_burden_spline[burden_spline[[2]]$burden_min] <- stats::predict(
spline_fit_min,
as.numeric(unlist(data_weighted_burden_spline[config$group])))$y
as.numeric(unlist(data_weighted_burden_spline[config$group]))
)$y

## Use smooth.spline for ps4-low
spline_fit_max <- splines::interpSpline(
as.numeric(unlist(data_weighted_burden_max[config$group])),
as.numeric(unlist(data_weighted_burden_max[burden_spline[[3]]$burden_max])))
as.numeric(unlist(data_weighted_burden_max[burden_spline[[3]]$burden_max]))
)
data_weighted_burden_spline[burden_spline[[3]]$burden_max] <- stats::predict(
spline_fit_max,
as.numeric(unlist(data_weighted_burden_spline[config$group])))$y
as.numeric(unlist(data_weighted_burden_spline[config$group]))
)$y

## Keep 0 values in the first two years, before policy implementation
group <- config$group
Expand All @@ -419,7 +427,8 @@
data_weighted_burden_spline[[burden_sp]] <- ifelse(
data_weighted_burden_spline[[group]] < 2024,
0,
data_weighted_burden_spline[[burden_sp]])
data_weighted_burden_spline[[burden_sp]]
)
}
print("Data processing complete.")
return(data_weighted_burden_spline)
Expand All @@ -433,6 +442,7 @@
#' @return A data frame with life expectancy.
#' @export
gen_data_le <- function(data_mean) {
print("Processing the data...")
data_le <- data_mean[, c(
data_mean$source,
data_mean$time,
Expand Down Expand Up @@ -476,7 +486,7 @@
values_from = c(data_ple$ple)
)
data_ple_wide$diff <- data_ple_wide$intervention - data_ple_wide$baseline

print("Data processing complete.")
return(data_ple_wide)
}

Expand All @@ -485,4 +495,5 @@
"incidence_intracerebralhemorrhage", "incidence_ischemicstroke",
"incidence_subarachnoidhemorrhage", "prevalence_intracerebralhemorrhage",
"prevalence_ischemicstroke", "prevalence_subarachnoidhemorrhage",
"diff_energyintake", "diff_inc_diabetes"))
"diff_energyintake", "diff_inc_diabetes"
))
5 changes: 4 additions & 1 deletion tests/testthat/test-data-process.R
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ test_that("Columns in the generated dataframe", {

# Generate a data frame with spline smoothing applied for burden of disease
data_weighted_burden_spline <- gen_data_weighted_burden_spline(
data_weighted_bd_wide_collapse)
data_weighted_bd_wide_collapse
)

# Check if the data has the expected number of columns
expect_equal(ncol(data_weighted_burden_spline), 4)
Expand All @@ -204,3 +205,5 @@ test_that("Columns in the generated dataframe", {
c("time", "cumdiff_daly_mean", "cumdiff_daly_min", "cumdiff_daly_max")
)
})

# Testing gen_data_le() function
14 changes: 14 additions & 0 deletions tests/testthat/test-health-plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,20 @@ test_that("life_exp function works correctly", {
expect_equal(plot_le$labels$title, "Increase in life expectancy under intervention")
})

# Testing life_exp() function
test_that("life_exp function works correctly", {
# Create sample data
data_ple_wide <- data.frame(
timediff = seq(-9, 21, by = 1),
diff = runif(31, -3, 0)
)

# Test for valid input
plot_le <- life_exp(data_ple_wide$diff, data_ple_wide)
expect_s3_class(plot_le, "ggplot")
expect_equal(plot_le$labels$title, "Increase in life expectancy under intervention")
})

# Testing combine_plots() function
test_that("combine_plots function works correctly", {
# Create sample metrics
Expand Down
Loading