-
Notifications
You must be signed in to change notification settings - Fork 0
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
Added monotonic spline-function for dx-values. #43
Conversation
Codecov Report
@@ Coverage Diff @@
## master #43 +/- ##
===========================================
- Coverage 96.97% 85.67% -11.31%
===========================================
Files 4 4
Lines 331 377 +46
===========================================
+ Hits 321 323 +2
- Misses 10 54 +44
Continue to review full report at Codecov.
|
NAMESPACE
Outdated
@@ -17,6 +17,7 @@ export(is.mortaar_life_table_list) | |||
export(life.table) | |||
export(prep.life.table) | |||
importFrom(Rdpack,reprompt) | |||
importFrom(demography,cm.spline) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably not necessary to add another dependency: cm.spline is only a wrapper for stats::spline, see below
R/analytical_functions.R
Outdated
x_spline <- a_cumsum$a[a_cumsum_select] | ||
|
||
# interpolating the values with a monotonic cubic spline | ||
dem <- cm.spline(x_spline, y_spline, n = (limit/5 + 1), xmin = 0, xmax = limit) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could be replaced by:
dem <- stats::spline(x_spline, y_spline, n = (limit/5 + 1), xmin = 0, xmax = limit, method = "hyman")
R/analytical_functions.R
Outdated
|
||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the whole function might benefit from refactoring and extracting functions. Can be done after merging pull request
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. Maybe -- as a first step -- it's best to create an individual calculation function for each variable in life.table()
. In case of dx
the code in life.table()
could look like this:
if (!(length(option_spline) > 0)) {
necdf['dx'] <- dx_default(necdf['Dx'])
} else {
necdf['dx'] <- dx_spline(necdf['a'], necdf['Dx'])
}
with dx_default()
like this
dx_default <- function(Dx) {
Dx / Dx * 100
}
and dx_spline()
with the code you wrote.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general very nice, thank you for adding this feature! We might not need to include the demography package, see comments in the code! In short,
dem <- demography::cm.spline(x_spline, y_spline, n = (limit/5 + 1), xmin = 0, xmax = limit)
might be replaced by
dem <- stats::spline(x_spline, y_spline, n = (limit/5 + 1), xmin = 0, xmax = limit, method = "hyman")
in line 311
Done. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
#' will interpolate the values for individuals of an age of 20 or older by 10- or 20- | ||
#' year cumulated values. To be used carefully, as diagnostic features of the life | ||
#' table might be smoothed and essentially removed. Only available when the methods | ||
#' 'Standard' or 'Equal5' in prep.life.table have been chosen. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This option should not soft-depend on an obscure (obscure for the user) option in the prep
function. The prep
function is not necessary to use life.table()
. Instead the conditions to use this option should be explained here directly. I also suggest to add a check at the beginning of the if (length(option_spline) > 0) {...}
block to test if the condition is met.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A check like this could look like this:
if (!condition) {
stop("The condition is not fulfilled. Please take a look at ?life.table to determine how your input data should look like for the option_spline option.")
}
R/analytical_functions.R
Outdated
|
||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. Maybe -- as a first step -- it's best to create an individual calculation function for each variable in life.table()
. In case of dx
the code in life.table()
could look like this:
if (!(length(option_spline) > 0)) {
necdf['dx'] <- dx_default(necdf['Dx'])
} else {
necdf['dx'] <- dx_spline(necdf['a'], necdf['Dx'])
}
with dx_default()
like this
dx_default <- function(Dx) {
Dx / Dx * 100
}
and dx_spline()
with the code you wrote.
Thank you for your input! I will try to implement these suggestions during the weekend. |
…y standard-options are used for creation of life table.
@nevrome , @MartinHinz: Could you please have a look at the new commit?
Somehow, |
@nevrome , @MartinHinz , @chrinne: In relation to the issues if have filed in the past (here and here), I have added a spline-function to offer the possibility to smooth the sometimes very rugged curves of the life-table for the age categories 20+. In my view this is much better than a moving window because within the range of adjacent knots the sums stay the same. Of course, this is still a severe manipulation of the data and should not applied lightly.
The function
cm.spline
derives from the demography-package and has the necessary advantage that it is forced to be monotonic, negative values are therefore not possible. It is applied to the original cumulative dx-values. The user can choose the wanted resolution (e. g. one knot every 20 years), and because every other column of the life table depends on dx, the interpolated dx-values bear on the life-table as a whole.E.g.:
life.table(prep_life,option_spline = 20)
In theory, the data can be split at every arbitrary age-interval (indeed, it is mainly used to get yearly census of already tabulated data), but I do not think that it makes a lot of sense to go beyond 5 year-intervals. Therefore, and because a non-standard vector of age-intervals will almost certainly collide with the regular spacing as done by
cm.spline
, I would only allow this method of interpolation to be called when the 'method' inprep.life.table
was either set to 'Standard' or to 'Equal5'. This, however, has not been implemented yet because I have not found an easy way to carry over these options from one function to the other.Please have a look; any comments are appreciated as well as help in the issue just mentioned, or in rewriting the doubtless very unelegant code.