Code Monkey home page Code Monkey logo

demokin's People

Contributors

alburezg avatar ivanwilli avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

demokin's Issues

Status of `get_HMDHFD`

This is rather a question instead of an issue: I noticed that you moved get_HMDHFD from the /R folder to the /dev folder. Is there a reason for this? The function seems to work correctly for me, and I appreciated it for conveniently getting data into R.

kins() returns duplicate rows?

When running the non-stable kin estimates:

y <- kins(ego_age = 35, year = 2015, P = swe_surv, asfr = swe_asfr, N = swe_pop, stable = F)

I would expect y[["kins"]] there have 35x101 = 3535 rows. However, it has ``y[["kins"]] == 3737` rows, 101 more than expected. It seems like there are duplicates for ego age 0:

count(y[["kins"]], x)
x n
1 0 202
2 1 101
3 2 101
4 3 101
5 4 101
6 5 101
7 6 101
8 7 101
9 8 101
10 9 101
11 10 101
12 11 101
13 12 101
14 13 101
15 14 101
16 15 101
17 16 101
18 17 101
19 18 101
20 19 101
21 20 101
22 21 101
23 22 101
24 23 101
25 24 101
26 25 101
27 26 101
28 27 101
29 28 101
30 29 101
31 30 101
32 31 101
33 32 101
34 33 101
35 34 101
36 35 101

`kin(..., time_invariant=F)` breaks when loading dtplyr

I consistently get an error whenever I load dtplyr previous to running a kin(..., time_invariant=F) function:

library(DemoKin)
library(dtplyr)

out <- kin(p = swe_px
      , f = swe_asfr
      , n = swe_pop
      , time_invariant = F
      , output_period = 1950
      , output_kin = "m")

Error in dt_squash_across(x, env, data, j = j) :
Problem while evaluating dplyr::all_of(agrupar).
Caused by error in as_indices_impl():
! object 'agrupar' not found

The function works perfectly if I don't load dtplyr before running kin.

`get_HMDHFD` does not return data retrieved from Human Fertility Database [false alarm]

Update

Issue is non-existing. I just overlooked fx in the returned object

Description of non-existing issue (ignore)

First, I'd like to thank you for your excellent work on this great package. While testing the library today, I noticed that the very useful get_HMDHFD()-function does not return the data retrieved from the Human fertility database. Hence, one, for instance, cannot extract the asfr required as an input for the kin()-function.

This is a code chunk from the get_HMDHFD()-function

 asfr <- HMDHFDplus::readHFDweb(country, "asfrRR", user_HFD, 
                                 pass_HFD, fixup = TRUE) %>% dplyr::filter(Year >= min_year, 
                                                                           Year <= max_year)

You could add the following code chunk to reshape the data into a format equivalent to the other objects returned by your function

asfr <- dplyr::tibble(Year = rep(unique(asfr$Year), each = OAG+1)) |>
    transform(Age = 0:OAG) |> 
    dplyr::left_join(dplyr::select(asfr, 1:3), by = c("Year", "Age")) |> 
    dplyr::mutate(ASFR = tidyr::replace_na(.data$ASFR, 0.00000)) |> 
    tidyr::pivot_wider(names_from = "Year", 
                       values_from = "ASFR") |>  
    dplyr::select(-Age) |>  
    as.matrix()

and then of course add it to the return call:

return(list(asfr = asfr, px = px, Sx = Sx, fx = fx, Nx = Nx))

Approximate two-sex kin using GKP factors?

Caswell (2022) notes that "GKP factors" can provide good-enough approximations of two-sex kin given female-only kin estimates. For example, the number of parents is the number of mothers * 2, grandparents are grandmothers * 4, etc. Is it desirable to add an argument approximate_male_kin or something similar to DemoKin::kin?

If approximate_male_kin == TRUE, then something like this could be used to approximate female+male kin:

factors <- c("coa" = 8, "cya" = 8, "d" = 2, "gd" = 4, "ggd" = 8, "ggm" = 8, "gm" = 4, "m" = 2, "nos" = 4, "nys" = 4, "oa" = 4, "ya" = 4, "os" = 2, "ys" = 2)
kin_full$living <- kin_full$living*factors[kin_full$kin]
kin_full$dead <- kin_full$dead*factors[kin_full$kin]

The function could also print something like Using GKP factors to approximate two-sex kin from female only kin age distributions.

References

Caswell, H. (2022). The formal demography of kinship IV: Two-sex models and their approximations. Demographic Research 47:359–396. doi:10.4054/DemRes.2022.47.13.

Suggested improvements to package

  • The logical parameter "stable" will be replaced by "time-invariant".
  • We will change parameters "focal_year" and "focal_cohort" by "output_period" and "output_cohort": those arguments would only apply for selecting which data wants the user as output in the "time-variant" case.
  • Both previous parameters would only apply for the "time-variant" case, and would not be "heard" in other case.
  • When no pi or N is supplied as an argument in the "time-variant" case, we would use the stable distribution for mothers´s age and return a message like this "The stable assumption was used for the age distribution of mothers in each input year".

`kin2sex` returns only '202' values for age_kin

Running the following code:

library(DemoKin)

years <- ncol(swe_px)
ages <- nrow(swe_px)
swe_surv_f_matrix <- swe_px
swe_surv_m_matrix <- swe_px ^ 1.5 # artificial perturbation for this example
swe_fert_f_matrix <- swe_asfr
swe_fert_m_matrix <- rbind(matrix(0, 5, years),  
                           swe_asfr[-((ages-4):ages),]) * 1.05 # artificial perturbation for this example

output_period <- 1950

output_kin <- c("gm", "m")

kin_out <- kin2sex(
  pf = swe_surv_f_matrix 
  , pm = swe_surv_m_matrix
  , ff = swe_fert_f_matrix 
  , fm = swe_fert_m_matrix
  , sex_focal = "f"
  , time_invariant = FALSE
  , birth_female = .5
  , output_period = output_period
  , output_kin = output_kin
)

returns only value '202' for age_kin:

table(kin_out$kin_full$age_kin)

202
404

Non-independent sex subsidy when focal is male

Incosistency when focal is male: mixing $pi$ female with $k$ male in some types. Options:

  • make subsidy credited to mother or father depending of focal´s sex ($m(0)=\sum{\pi_m*a}$), or
  • keep relating always to mother´s reproduction and having a female kinship network as reference even if focal is a male ($m(0)=\sum{\pi_f*a}$) with $a$ from female focal case.

Error in relative codes

DemoKin::demokin_codes[4, ] gives:

  DemoKin Caswell Labels_female Labels_male Labels_2sex
4       d       a     Daughters    Brothers    Siblings

which is wrong, since it should be daughters, sons, and children.'

sex_focal default

Add "sex=sex_focal" in line 124 of "kin_time_variant_2sex", because by default is female.

Release DemoKin 1.0.1

First release:

Prepare for release:

  • git pull
  • devtools::build_readme()
  • urlchecker::url_check()
  • devtools::check(remote = TRUE, manual = TRUE)
  • devtools::check_win_devel()
  • rhub::check_for_cran()
  • git push

Submit to CRAN:

  • usethis::use_version('patch')
  • devtools::submit_cran()
  • Approve email

Wait for CRAN...

  • Accepted 🎉
  • git push
  • usethis::use_github_release()
  • usethis::use_dev_version()
  • git push

`kin` error caused by `pivot_longer`

I receive an error message when running kin caused by two pivot_longer calls including the snippet names_to = "indicator", "value". If I replace this with `names_to = "indicator"`` everything works as it used to.

library(DemoKin)

swe_surv_2015 <- swe_px[,"2015"]
swe_asfr_2015 <- swe_asfr[,"2015"]
# Run kinship models
swe_2015 <- kin(p = swe_surv_2015, f = swe_asfr_2015)
#> Error in `tidyr::pivot_longer()`:
#> ! Arguments in `...` must be used.
#> ✖ Problematic argument:
#> • ..1 = "value"

#> Backtrace:
#>      ▆
#>   1. ├─DemoKin::kin(p = swe_surv_2015, f = swe_asfr_2015)
#>   2. │ ├─... %>% ...
#>   3. │ ├─dplyr::bind_rows(...)
#>   4. │ │ └─rlang::list2(...)
#>   5. │ └─... %>% ...
#>   6. ├─tidyr::pivot_wider(., names_from = indicator, values_from = value)
#>   7. ├─dplyr::ungroup(.)
#>   8. └─tidyr::pivot_longer(., count_living:sd_age, names_to = "indicator", "value")
#>   9.   └─rlang (local) `<fn>`()
#>  10.     └─rlang:::check_dots(env, error, action, call)
#>  11.       └─rlang:::action_dots(...)
#>  12.         ├─base (local) try_dots(...)
#>  13.         └─rlang (local) action(...)

Created on 2023-03-09 with reprex v2.0.2

Issues related to the `kin2sex vignette`

First, I would like to congratulate you on the package and for the initiative. I leave two comments/questions.

  • Suggested text change in DemoKin/vignettes/Reference_TwoSex.Rmd: At line 37 "female fertility" is repeated. I imagine the first mention is related to "male fertility".

  • [Question] About approaches for using the two-sex model in DemoKin: based on the results of the accuracy assessment between the approximations suggested by Caswell (2022) and estimates generated with the data for each sex, it seems possible to use the approximations in a scenario where there is no data by sex. However, in the presence of reliable mortality data by sex -- as is more common -- and the absence of male fertility data, what do you think of the possibility of combining the approaches suggested in DemoKin/vignettes/Reference_TwoSex.Rmd as an adaptation to Caswell's (2022) androgynous approach? For example, I used, for exploratory analysis purposes, male mortality and, instead of using also male fertility, in a "hypothetical" scenario of its non-existence, I used female fertility -- which would be the same as assuming the GKP factors, as suggested by Caswell (2022).

Next steps

  • collapse in one function
  • always receive a df
  • diagramm dinamycs
  • manage pkg dependencies
  • data loaded
  • comment funs
  • document
  • improve vignette writting

Select relative type in `kins` function

Sometimes, we don't need DemoKin::kins to estimate all relative types. We might be interested only on mothers, for example. I suggest adding an argument 'relatives' to kins. Something like this:

swe50_2015_stable <- 
  kins(
    ego_age = 50
    , year = 2015
    , P = swe_surv
    , asfr = swe_asfr
    , stable = TRUE
    , relatives = "all"
    )

or

swe50_2015_stable <- 
  kins(
    ego_age = 50
    , year = 2015
    , P = swe_surv
    , asfr = swe_asfr
    , stable = TRUE
    , relatives = c("m", "d")
    )

If you think this is a good, idea, I'll be happy to implement it and send a pull request!

Small improvements

HIGH PRIORITY (BEFORE CRAN)

  • Convert demokin_codes from function to object. Update kin names to include female and male relatives (save them in separate columns 'label_female', 'label_male'. Include "sisters", "cousins", etc (irrespective of whether they are older or younger).
  • Remove argument consolidate_relatives from rename_kin function. It's not very useful. Also, see next suggestion:
  • Allow kin* functions to take "s", "c", "a", "n" as options in kin_output argument without specifying whether they are through older or younger sister
  • Update Readme to remove typos and add new kinship diagram
  • Remove HMDHFD dependencies?
  • Update kin_multi_stage so that default of the argument parity is F.
  • Let multistate function return results by state of Focal in addition to state of kin
  • Improve error message when f does not have column names and is passed to kin function. The first error message can be "Make sure that p and f are matrices and have the same column names." and the second: "The years given as column names in the p and f matrices must be equally spaced."
  • clarify timing of deaths

AFTER CRAN UPDATE

  • plot_diagram doesn't work with "s", "c", "a", "n" : maybe give error message saying that it is not possible to create plot unless ALL the relevant kin types are given?

LOW PRIORITY (BEFORE CRAN?)

  • In kin* functions, add argument output_age_focal
  • In kin*(time_invariant==F), add argument sex_ratio`

AFTER CRAN

  • Create package website
  • Add an argument kin_summary to kin functions to determine whether a kin_summary should be created or not.

Incorrect `lifecycle` warning?

Running this code:

# First, get vectors for a given year
swe_surv_2015 <- DemoKin::swe_px[,"2015"]
swe_asfr_2015 <- DemoKin::swe_asfr[,"2015"]
# Run kinship models
swe_2015 <- kin(U = swe_surv_2015, f = swe_asfr_2015, time_invariant = TRUE)

Gives the warning:

Warning message:
The `stable` argument of `kin()` is deprecated as of DemoKin 0.0.0.9000.
ℹ Used time_invariant
This warning is displayed once every 8 hours.
Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated. 

which is not needed, since we are not using the stable=T argument?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.