## ----include=FALSE, results = 'hide'------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----message=FALSE, warning=FALSE---------------------------------------------
library(LTFGRS)
library(future)
library(dplyr)

## -----------------------------------------------------------------------------
sim <- simulate_under_LTM(fam_vec = c("m", "f", "s1"), h2 = 0.5, pop_prev = 0.05, n_sim = 100)

## -----------------------------------------------------------------------------
plan(multisession, workers = 2)
ltfgrs <- estimate_liability(.tbl = sim$thresholds %>% rename(pid = indiv_ID), h2 = 0.5)
plan(sequential) # always return to sequential after parallel compute is finished 

## ----message=FALSE------------------------------------------------------------
# the input is long, meaning each individual has their own row.
# Selection needs to happen on families, not individuals:
indx_list <- partition_indices(nrow = length(unique(sim$thresholds$fid)), n_partitions = 50)

# For the tibble-style (long format) input, we need to nest data into families
# before we are able to loop over it:
nested_thrs <- tidyr::nest(sim$thresholds, data = -fid)

# setting up parallel backend
plan(multisession, workers = 2)

ltfgrs_partitioned <- lapply(seq_along(indx_list), function(i) {
  
 estimate_liability(.tbl = nested_thrs %>% 
                      slice(indx_list[[i]]) %>% 
                      tidyr::unnest(data) %>% 
                      rename(pid = indiv_ID), 
                    h2 = 0.5)
  
}) %>% bind_rows

plan(sequential)

