ivanwilli / demokin Goto Github PK
View Code? Open in Web Editor NEWDemoKin estimates kinship distribution by type and age
License: Other
DemoKin estimates kinship distribution by type and age
License: Other
In Demokin::demokin_codes, the labels for 'd' are incorrect. Labels_female says Daughter, but Labels_male: says Brothers, and Labels_2sex: says Sibling. Males for 'd' should be sons and for two-sex children or offspring.
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.
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!
HIGH PRIORITY (BEFORE CRAN)
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).consolidate_relatives
from rename_kin
function. It's not very useful. Also, see next suggestion:kin_output
argument without specifying whether they are through older or younger sisterkin_multi_stage
so that default of the argument parity
is F.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."AFTER CRAN UPDATE
LOW PRIORITY (BEFORE CRAN?)
output_age_focal
kin*(time_invariant==F), add argument
sex_ratio`AFTER CRAN
kin_summary
to kin
functions to determine whether a kin_summary should be created or not.First release:
usethis::use_cran_comments()
Title:
and Description:
@return
and @examples
Authors@R:
includes a copyright holder (role 'cph')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()
Wait for CRAN...
git push
usethis::use_github_release()
usethis::use_dev_version()
git push
Issue is non-existing. I just overlooked fx
in the returned object
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))
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.'
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).
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 evaluatingdplyr::all_of(agrupar)
.
Caused by error inas_indices_impl()
:
! object 'agrupar' not found
The function works perfectly if I don't load dtplyr before running kin
.
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?
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
I re-installed DemoKin, but get this error:
DemoKin::plot_diagram(
+ kins_total = stable[["kins"]]
+ , ego_age = 50
+ )
'plot_diagram' is not an exported object from 'namespace:DemoKin'..
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.
Similar to the maraab23 issues, maybe the same problem is happening in the kin2sex
function. I tested to exclude the , "value"
in names_to = "indicator", "value"
and it works well. The problem's lines are here and here, I guess.
Incosistency when focal is male: mixing
Add "sex=sex_focal" in line 124 of "kin_time_variant_2sex", because by default is female.
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
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.