55# ' displayed as negative values while dates CE are positive values. Ignoring this will cause problems
66# ' in any case.
77# '
8- # ' @param df a dataframe with 4 variable : ID, group, minimum date (int/num) maximum date (int/num), _must_ be in this order, colnames are irrelevant; each object _must_ be one row.
8+ # ' @param df a dataframe with 4 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.
99# ' @param stepsize defaults to 5. Number of years that should be used as an interval for creating dating steps.
1010# '
1111# ' @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)
1212# '
1313# ' @export datsteps
1414
15- datsteps <- function (df , stepsize = " auto" ) {
15+
16+ datsteps <- function (df , stepsize = 25 ) {
17+ result <- as.data.frame(NULL )
1618 if (stepsize == " auto" ) {
1719 timespans <- abs(df [,3 ] - df [,4 ])
18- if (min(timespans ) < 1 ) {
19- stepsize <- 1
20- } else {
21- stepsize <- min(timespans )
22- }
23- print(paste(" Using stepsize = " , stepsize , " . (auto)" , sep = " " ))
20+ stepsize <- generate.stepsize(timespans )
2421 } else if (! is.numeric(stepsize )) {
25- print(" Error: stepsize has to be numeric." )
26- stop()
22+ stop(error(" stepsize has to be either 'auto' or numeric." ))
2723 }
28- result <- as.data.frame(NULL )
29- if (any(df [,3 ] > df [,4 ]) == TRUE ) {
30- print(paste(" Error: Dating seems to be in wrong order at " ,
31- df [which(df [,3 ] > df [,4 ]),1 ],
32- " (Index: " , which(df [,3 ] > df [,4 ]), " )" ,
33- " . Please supply minimum date in 3rd Column, maximum date in 4th." , sep = " " ))
34- } else {
35- weights <- get.weights(df [,3 ], df [,4 ])
3624
25+ if (any(df [,3 ] > df [,4 ])) {
26+ warning(paste(" Warning: Dating seems to be in wrong order at ID " , paste(df [which(df [,3 ] > df [,4 ]),1 ], collapse = " , " ), " (Index: " ,
27+ paste(which(df [,3 ] > df [,4 ]), collapse = " , " ), " )" ,
28+ " . Dates have been switched, but be sure to check your original data for possible mistakes." , sep = " " ))
29+ DAT_err <- which(df [,3 ] > df [,4 ])
30+ df <- switch .dating(df , DAT_err )
31+ }
3732
38- if (any(weights [,2 ] == FALSE )) {
39- print(paste(" Warning: DAT_min and DAT_max in " ,
40- df [which(weights == FALSE ),1 ],
41- " (Index: " , which(weights == FALSE ), " )" ,
42- " have the same value! Is this correct? Please check the table for possible errors." , sep = " " ))
43- }
33+ weights <- get.weights(df [,3 ], df [,4 ])
4434
45- df $ weight <- weights [,1 ]
46- result <- create.sub.objects(df , stepsize )
47- }
35+ df $ weight <- weights [,1 ]
36+ result <- create.sub.objects(df , stepsize )
4837 return (result )
4938}
5039
40+
41+ # ' Generate stepsize
42+ # '
43+ # ' Requires a dataframe with 4 variables: ID (ideally factor), group (ideally factor),
44+ # ' minimum date (int/numeric) and maximum date (int/numeric).
45+ # '
46+ # ' @param df a dataframe with 4 variable: ID, group, minimum date (int/num) maximum date (int/num)
47+ # '
48+ # ' @return stepsize
49+ # '
50+ # ' @export switch.dating
51+
52+ generate.stepsize <- function (timespans ) {
53+ stepsize <- min(abs(df [,4 ] - df [,3 ]))
54+ if (stepsize < 1 ) {
55+ stepsize <- 1
56+ }
57+ print(paste(" Using stepsize = " , stepsize , " (auto)." , sep = " " ))
58+ return (stepsize )
59+ }
60+
61+ # ' Switch values where dating is in wrong order
62+ # '
63+ # ' Requires a dataframe with 4 variables: ID (ideally factor), group (ideally factor),
64+ # ' minimum date (int/numeric) and maximum date (int/numeric).
65+ # '
66+ # ' @param df a dataframe with 4 variable: ID, group, minimum date (int/num) maximum date (int/num)
67+ # ' @param DAT_err a vector containing the dates in wrong order
68+ # '
69+ # ' @return corrected df
70+ # '
71+ # ' @export switch.dating
72+
73+ switch .dating <- function (df , DAT_err ) {
74+ df [DAT_err ,3 : 4 ] <- df [DAT_err ,4 : 3 ]
75+ return (df )
76+ }
77+
78+
5179# ' Calculate the weights for each dated object
5280# '
5381# ' Requires a dataframe with 4 variables: ID (ideally factor), group (ideally factor),
@@ -72,9 +100,18 @@ get.weights <- function(DAT_min, DAT_max) {
72100 weights [which(weights [,1 ] == 0 ),1 ] <- 1
73101 }
74102 weights [,1 ] <- 1 / weights [,1 ]
103+ if (any(weights [,2 ] == FALSE )) {
104+ warning(paste(" Warning: DAT_min and DAT_max in ID " ,
105+ paste(df [which(weights [,2 ] == FALSE ),1 ], collapse = " , " ),
106+ " (Index: " , paste(which(weights [,2 ] == FALSE ), collapse = " , " ), " )" ,
107+ " have the same value! Is this correct? Please check the table for possible errors." , sep = " " ))
108+ }
75109 return (weights )
76110}
77111
112+
113+
114+
78115# ' Create sub-objects for each object in a dataframe
79116# '
80117# ' Requires a dataframe with 5 variables: ID (ideally factor), group (ideally factor),
@@ -103,11 +140,18 @@ create.sub.objects <- function(df, stepsize) {
103140 result <- as.data.frame(matrix (ncol = ncol(df )+ 1 , nrow = outputnr + 100 ))
104141
105142 colnames(result ) <- c(colnames(df ), " DAT_step" )
143+ diffs <- df [,4 ]- df [,3 ]
144+
145+ if (any(diffs < stepsize )) {
146+ diffs <- diffs [diffs < stepsize ]
147+ warning(paste(" stepsize is larger than the range of the closest dated object: " ,
148+ paste(df [which(diffs < stepsize ),1 ], collapse = " , " ), " (Index = " ,
149+ paste(which(diffs < stepsize ), collapse = " , " ), " ). Using mean as year." , sep = " " ))
150+ }
151+
106152 for (i in 1 : nrow(df )) {
107153 sequence <- NULL
108154 if ((df [i ,4 ]- df [i ,3 ]) < stepsize ) {
109- print(paste(" stepsize is larger than the range of the closest dated object: " ,
110- df [i ,1 ], " (Index = " , i , " ). Using mean as year." , sep = " " ))
111155 sequence <- (df [i ,3 ]+ df [i ,4 ])/ 2
112156 } else {
113157 sequence <- seq(df [i ,3 ], df [i ,4 ], by = stepsize )
0 commit comments