Skip to content

Commit fbe5336

Browse files
committed
documentation of internal functions in datplot_utility.R
1 parent f3562ef commit fbe5336

9 files changed

Lines changed: 93 additions & 60 deletions

R/datplot_utility.R

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#' @title Determine stepsize
1+
#' @title Determine stepsize (internal)
22
#'
33
#' @description Determines stepsize by selecting the absolute minimum value between the upper and lower end of all dating ranges.
44
#'
@@ -18,7 +18,7 @@ generate.stepsize <- function(DAT_mat) {
1818
return(stepsize)
1919
}
2020

21-
#' @title Switch values where dating is in wrong order
21+
#' @title Switch values where dating is in wrong order (internal)
2222
#'
2323
#' @description Requires a dataframe with 4 variables: ID (ideally factor), group (ideally factor),
2424
#' minimum date (int/numeric) and maximum date (int/numeric) and DAT_err as a vector of indizes where
@@ -37,14 +37,14 @@ switch.dating <- function(DAT_df, DAT_err) {
3737
}
3838

3939

40-
#' @title Calculate the weights for each dated object
40+
#' @title Calculate the weights for each dated object (internal)
4141
#'
4242
#' @description Calculates the weights from two vectors of minimum and maximum dating for each object.
4343
#' Returns a dataframe with the weight in the first column and FALSE in the second if two rows have the
4444
#' same value in both min and max dating.
4545
#'
46-
#' @param DAT_min a vector containing the minimum date (int/num) of each object
47-
#' @param DAT_max a vector containing the maximum date (int/num) of each object
46+
#' @param DAT_min a vector containing the minimum date (a number) of each object
47+
#' @param DAT_max a vector containing the maximum date (a number) of each object
4848
#'
4949
#' @return the 'weight' value for the datsteps-dataframe, that is a quantification of
5050
#' how well the object is dated (lesser value means object is dated to larger
@@ -58,9 +58,12 @@ get.weights <- function(DAT_min, DAT_max) {
5858
weights[,1] <- abs(DAT_min - DAT_max)
5959
weights[,2] <- TRUE
6060
if (any(weights[,1] == 0)) {
61+
# store FALSE if any weights are zero to display the warning below
6162
weights[which(weights[,1] == 0),2] <- FALSE
63+
# then store a weight of 1 as to treat objects with same min and max dating (dated to one year precisely) as very influential
6264
weights[which(weights[,1] == 0),1] <- 1
6365
}
66+
# weights have to be below 1
6467
weights[,1] <- 1/weights[,1]
6568
if (any(weights[,2] == FALSE)) {
6669
warning(paste("Warning: DAT_min and DAT_max at Index: ", paste(which(weights[,2] == FALSE), collapse = ", "), ")",
@@ -71,18 +74,20 @@ get.weights <- function(DAT_min, DAT_max) {
7174

7275

7376

74-
#' @title Calculate output rows
77+
#' @title Calculate output rows (internal)
7578
#'
76-
#' @description approximation :(
79+
#' @description an approximation(!) of the rows that will be needed to fit all the steps of the dating
7780
#'
78-
#' @param DAT_mat todo
79-
#' @param stepsize todo
81+
#' @param DAT_mat a matrix as transformed by datsteps()
82+
#' @param stepsize the stepsize given to or by datsteps()
8083
#'
81-
#' @return outputrows
84+
#' @return the number of rows create.sub.objects should at least produce in order to fit all steps
8285
#'
8386
#' @export calculate.outputrows
8487

8588

89+
# TODO: as stated, this is still an approximation and overestimates the number. in testing, there were cases of
90+
# underestimating, but it hasnt happened since the last fix
8691

8792
calculate.outputrows <- function(DAT_mat, stepsize) {
8893
mean_year_index <- which(DAT_mat[,"datmax"] - DAT_mat[,"datmin"] < stepsize)
@@ -91,53 +96,68 @@ calculate.outputrows <- function(DAT_mat, stepsize) {
9196
outputrows <- ceiling(sum(((abs(DAT_mat[,"datmax"] - DAT_mat[,"datmin"])) / stepsize) + 3))
9297
} else {
9398
outputrows <- ceiling(sum(((abs(DAT_mat[-mean_year_index,"datmax"] - DAT_mat[-mean_year_index,"datmin"]))/stepsize)+3))
94-
outputrows <- outputrows+length(mean_year_index)
99+
outputrows <- outputrows + length(mean_year_index)
95100
}
96101
return(outputrows)
97102
}
98103

99104

100105

101106

102-
#' @title Calculate the sequence of dating steps
107+
#' @title Calculate the sequence of dating steps (internal)
103108
#'
104-
#' @description TO DO also i dont want to write documentation
109+
#' @description Produces an appropriate sequence of years between the minimum and maximum dating.
110+
#' If they cannot be properly devided by the stepsize set beforehand, either three values are generated for
111+
#' objects that are dated to a range of more then 60% of the stepsize (min, mean, max), or two values for
112+
#' objects dated to a timespan of less or equal to 60% of the stepsize.
113+
#' If they can be devided without residual, the normal sequence is returned. If there is a residual, the stepsize
114+
#' is modified depending on how large the residual is. (TODO: There is still a problem here that needs fixing.)
105115
#'
106-
#' @param datmin todo
107-
#' @param datmax todo
108-
#' @param stepsize todo
116+
#' @param datmin value of the minimum dating of one object
117+
#' @param datmax value of the maximum dating of one object
118+
#' @param stepsize the stepsize to be used
109119
#'
110-
#' @return sequence
120+
#' @return sequence of steps to be created by create.sub.objects()
111121
#'
112122
#' @export get.step.sequence
113123

114-
115-
116-
117-
118-
### THIS IS NOT DONE
119124
get.step.sequence <- function(datmin = 0, datmax = 100, stepsize = 25) {
125+
# Get the difference of the two dating values
120126
timespan <- datmax - datmin
127+
128+
# First: If the stepsize is larger than the timespan, two different strategies can be employed
121129
if (timespan %/% stepsize == 0) {
122130
if (timespan > (stepsize*0.6)) {
131+
# If the timespan exceeds 60% of the stepsize, three steps will be created corresponding to minimum, mean and maximum dating
123132
sequence <- c(datmin, round(((datmin+datmax)/2), digits = 0), datmax)
124133
} else {
134+
# if the timespan is less than 60% of the stepsize, just two values corresponding to minimum and maximum dating will be returned
125135
sequence <- c(datmin, datmax)
126136
}
127137
} else {
138+
# If the timespan can be devided at least once, first generate the sequence
128139
sequence <- seq(from = datmin, to = datmax, by = stepsize)
140+
# then check how many years the maximum dating would be off
129141
resid <- datmax-sequence[length(sequence)]
130142
if (resid >= (stepsize/2)) {
143+
# if the residual is larger or equals half the stepsize, the stepsize is temporarily modified to fit the as many values
144+
# as it would with the length of the sequence generated
131145
stepsize_mod <- (datmax-datmin)/length(sequence)
132146
sequence <- seq(datmin, datmax, stepsize_mod)
147+
# then rounds all values except first and last, which need to stay as minumum and maximum date
133148
sequence[-c(1,length(sequence))] <- round(sequence[-c(1,length(sequence))], digits = 0)
134149
} else if (resid != 0) {
150+
# if the residual is smaller but also not 0, the sequence values at moved by an appropriate fraction
135151
move <- round(resid/(length(sequence)-1), digits = 0)
136152
sequence[2:length(sequence)] <- sequence[2:length(sequence)] + move
153+
# and the end of the sequence is reset as the maximum dating
137154
sequence[length(sequence)] <- datmax
155+
# TODO: these two things do essentially the same? I need to fix the first one to use the largest possible division, maybe
138156
} else {
157+
# this implies that there was no residual, so the original sequence can be used
139158
}
140159
}
160+
# returns the sequence
141161
return(sequence)
142162
}
143163

@@ -149,17 +169,21 @@ get.step.sequence <- function(datmin = 0, datmax = 100, stepsize = 25) {
149169

150170

151171

152-
#' @title Create sub-objects for each object in a dataframe
172+
#' @title Create sub-objects for each object in a dataframe (internal)
153173
#'
154-
#' @description Requires a dataframe with 5 variables: ID (ideally factor), group (ideally factor),
155-
#' minimum date (int/numeric), maximum date (int/numeric) and weight (as created by get.weights). It's expected that dates BCE are
156-
#' displayed as negative values while dates CE are positive values. Ignoring this will cause problems
157-
#' in any case.
174+
#' @description Requires a matrix with 4 named columns as datsteps will hand to the function:
175+
#' * "index" (identifier so the values can later be reassigned to their ID and variable),
176+
#' * "datmin" (minimum dating as any kind of number),
177+
#' * "datmax" (maximum dating as any kind of number),
178+
#' * "weight" (as created by get.weights),
179+
#' * "step" (empty).
180+
#' It's expected that dates BCE are displayed as negative values while dates CE are positive values.
181+
#' Ignoring this will cause problems in any case, that would be fixed automatically by switch.dating().
158182
#'
159-
#' @param DAT_mat a matrix with 3 variables: ID, group, minimum date (int/num) maximum date (int/num), _must_ be in this order, colnames are irrelevant; each object _must_ be one row.
183+
#' @param DAT_mat a matrix with 3 variables as prepared by datsteps()
160184
#' @param stepsize Number of years that should be used as an interval for creating dating steps.
161185
#'
162-
#' @return a larger dataframe with a number of steps for each object as well as a 'weight' value, that is a quantification of how well the object is dated (lesser value means object is dated to larger timespans, i.e. with less confidence)
186+
#' @return a longer matrix of the same structure to be further processed by datsteps() with a number of steps for each object
163187
#'
164188
#' @export create.sub.objects
165189

@@ -195,7 +219,7 @@ create.sub.objects <- function(DAT_mat, stepsize) {
195219
return(result)
196220
}
197221

198-
#' @title Check for numbers
222+
#' @title Check for numbers (internal)
199223
#'
200224
#' @description Checks if value is either numeric, integer or double and and returns TRUE.
201225
#'
@@ -217,13 +241,13 @@ check.number <- function(value) {
217241
return(result)
218242
}
219243

220-
#' @title Check the Structure to be compatible with datsteps()
244+
#' @title Check the Structure to be compatible with datsteps() (internal)
221245
#'
222246
#' @description Checks if the object passed to datsteps() will work
223247
#'
224-
#' @param DAT_df A value to check
248+
#' @param DAT_df An object to check
225249
#'
226-
#' @return TRUE if df can be processen, FALSE if not
250+
#' @return TRUE if object can be processed by datsteps(), error / FALSE if not
227251
#'
228252
#' @export check.structure
229253

man/calculate.outputrows.Rd

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/check.number.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/check.structure.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/create.sub.objects.Rd

Lines changed: 11 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/generate.stepsize.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get.step.sequence.Rd

Lines changed: 11 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get.weights.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/switch.dating.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)