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

Conversation

SaranjeetKaur
Copy link
Collaborator

@SaranjeetKaur SaranjeetKaur commented Dec 10, 2024

This PR adds unit tests for life_exp() and gen_data_le() functions.

Fixes #79 #89

Some housekeeping checks

  • All ok when you run devtools::check() after the changes you have made?
  • Have you updated the .Rd files using devtools::document()?
  • If there were changes to README.Rmd, did you build and render it using devtools::build_readme()?
  • If you added a new function or changed an existing function, have you created a (or updated an existing) unit test for it using usethis::use_test()?
  • Everything looks ok?

@SaranjeetKaur SaranjeetKaur linked an issue Dec 10, 2024 that may be closed by this pull request
@SaranjeetKaur SaranjeetKaur requested a review from jzhu20 December 13, 2024 09:09
@SaranjeetKaur
Copy link
Collaborator Author

SaranjeetKaur commented Dec 13, 2024

@jzhu20 when computing gen_data_le() in data-process.R, it still uses the data_mean and looks for values like gender, age, etc.

Do you think this code needs to be modified? If yes, could you please suggest what needs to be changed?

gen_data_le <- function(data_mean) {
  data_le <- data_mean[, c(
    data_mean$source,
    data_mean$time,
    data_mean$gender,
    data_mean$age,
    data_mean$count,
    data_mean$deaths,
    data_mean$migrations
  )]
  data_le$timediff <- data_le$time - 2023
  # calculate the population considering both dead and alive
  # count is survivors, deaths = dead pop/count
  data_le$count_both <- data_le$count * data_le$deaths + data_le$count
  # calculate life expectancy px at each age
  data_le <- data_le[order(
    data_le$source,
    data_le$timediff,
    -data_le$age
  ), ]
  data_le <- data_le |>
    dplyr::group_by(
      data_le$source,
      data_le$timediff
    ) |>
    dplyr::mutate(
      tx = cumsum(data_le$count),
      px = data_le$tx / data_le$count
    )
  ## calculate period life expectancy as weighted sum of life expectancy at each age ##
  data_ple <- data_le |>
    dplyr::group_by(
      data_le$source,
      data_le$timediff
    ) |>
    dplyr::summarise(ple = stats::weighted.mean(data_le$px, data_le$count_both))

  ## calculate difference between baseline and intervention ##
  data_ple_wide <- tidyr::pivot_wider(data_ple,
    names_from = data_ple$source,
    id_cols = data_ple$timediff,
    values_from = c(data_ple$ple)
  )
  data_ple_wide$diff <- data_ple_wide$intervention - data_ple_wide$baseline

  return(data_ple_wide)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

test life_exp and if it is redundant test gen_data_le and check if it still required
1 participant