问题
I have performed a bootstrapping with 2.000 resamples of the Lee Carter model for mortality projection. The question is not specific for mortality studies, but on more general dimensions in R.
After performing the bootstrapping I get a list with 2000 elements, each for every of 2.000 re-estimations of the model. For each model, there are estimates of my 3 variables: a_x, b_x and k_t. Both a_x and b_x are age-specific, so the "x" denotes an age in the interval [0:95].
I would now like to plot a histogram of all the b_x values for age x = 70.
### Performing the bootstrap:
JA_lc_fitM_boot1 <- bootstrap(LCfit_JA_M, nBoot = 2000, type = "semiparametric")
### Plotting the histogram with all b_x for x = 70:
JA_lc_fitM_boot1[["bootParameters"]][1:2000][["bx"]][[70]]
I have tried multiple options, but I cannot make it work. The thing that triggers me, is that I am working with a double within a list of a list.
I have added a picture of the data below:
Does anybody have a solution to this?
回答1:
It looks like you need the apply
family of functions. Your data is not reproducible, so I can't confirm this will work, but if you do:
result <- sapply(JA_lc_fitM_boot1[["bootParameters"]], function(var) var[["bx"]][[70]])
You should get what you're looking for.
回答2:
You may want to have a look at the purrr
package and the family of map
functions, or tidyr
and the hoist
funtion.
(If you want code that works, you indeed need to provide some data!)
来源:https://stackoverflow.com/questions/61078813/problems-with-accessing-double-elements-in-a-list-in-r