14
14
# See the License for the specific language governing permissions and
15
15
# limitations under the License.
16
16
17
- getTimeSeries <- function (
18
- connection = NULL ,
19
- tempEmulationSchema = NULL ,
20
- cdmDatabaseSchema ,
21
- cohortDatabaseSchema = cdmDatabaseSchema ,
22
- cohortTable = " cohort" ,
23
- runCohortTimeSeries = TRUE ,
24
- runDataSourceTimeSeries = FALSE ,
25
- timeSeriesMinDate = as.Date(" 1980-01-01" ),
26
- timeSeriesMaxDate = as.Date(Sys.Date()),
27
- stratifyByGender = TRUE ,
28
- stratifyByAgeGroup = TRUE ,
29
- cohortIds = NULL ) {
30
-
31
- if (all(! runCohortTimeSeries , ! runDataSourceTimeSeries )) {
32
- warning(
33
- " - Both Cohort Time Series and Data Source Time Series are set to FALSE. Exiting time series diagnostics."
34
- )
35
- return (NULL )
36
- }
37
- start <- Sys.time()
38
-
39
- ParallelLogger :: logTrace(" - Creating Andromeda object to collect results" )
40
- resultsInAndromeda <- Andromeda :: andromeda()
41
-
42
- if (runCohortTimeSeries ) {
43
- sqlCount <-
44
- " SELECT cohort_definition_id, COUNT(*) count
45
- FROM @cohort_database_schema.@cohort_table
46
- {@cohort_ids != ''} ? { where cohort_definition_id IN (@cohort_ids)}
47
- GROUP BY cohort_definition_id;"
48
- resultsInAndromeda $ cohortCount <- renderTranslateQuerySql(
49
- connection = connection ,
50
- sql = sqlCount ,
51
- cohort_database_schema = cohortDatabaseSchema ,
52
- cohort_ids = cohortIds ,
53
- cohort_table = cohortTable
54
- )
55
- if (resultsInAndromeda $ cohortCount %> %
56
- dplyr :: summarise(n = dplyr :: n()) %> %
57
- dplyr :: pull(.data $ n ) == 0 ) {
58
- warning(" Please check if cohorts are instantiated. Exiting cohort time series." )
59
- return (NULL )
60
- }
61
- }
62
- # # Calendar period----
17
+ createCalendarPeriodsTable <- function (connection , tempEmulationSchema , timeSeriesMinDate , timeSeriesMaxDate ) {
63
18
ParallelLogger :: logTrace(" - Preparing calendar table for time series computation." )
64
19
# note calendar span is created based on all dates in observation period table,
65
20
# with 1980 cut off/left censor (arbitrary choice)
@@ -70,7 +25,7 @@ getTimeSeries <- function(
70
25
) %> % as.integer())
71
26
maxYear <-
72
27
clock :: get_year(timeSeriesMaxDate ) %> % as.integer()
73
-
28
+
74
29
calendarQuarter <-
75
30
dplyr :: tibble(
76
31
periodBegin = clock :: date_seq(
@@ -81,7 +36,7 @@ getTimeSeries <- function(
81
36
) %> %
82
37
dplyr :: mutate(periodEnd = clock :: add_months(x = .data $ periodBegin , n = 3 ) - 1 ) %> %
83
38
dplyr :: mutate(calendarInterval = " q" )
84
-
39
+
85
40
calendarMonth <-
86
41
dplyr :: tibble(
87
42
periodBegin = clock :: date_seq(
@@ -92,7 +47,7 @@ getTimeSeries <- function(
92
47
) %> %
93
48
dplyr :: mutate(periodEnd = clock :: add_months(x = .data $ periodBegin , n = 1 ) - 1 ) %> %
94
49
dplyr :: mutate(calendarInterval = " m" )
95
-
50
+
96
51
calendarYear <-
97
52
dplyr :: tibble(
98
53
periodBegin = clock :: date_seq(
@@ -103,13 +58,13 @@ getTimeSeries <- function(
103
58
) %> %
104
59
dplyr :: mutate(periodEnd = clock :: add_years(x = .data $ periodBegin , n = 1 ) - 1 ) %> %
105
60
dplyr :: mutate(calendarInterval = " y" )
106
-
61
+
107
62
timeSeriesDateRange <- dplyr :: tibble(
108
63
periodBegin = timeSeriesMinDate ,
109
64
periodEnd = timeSeriesMaxDate ,
110
65
calendarInterval = " c"
111
66
)
112
-
67
+
113
68
calendarPeriods <-
114
69
dplyr :: bind_rows(
115
70
calendarMonth ,
@@ -120,7 +75,7 @@ getTimeSeries <- function(
120
75
dplyr :: distinct() %> %
121
76
dplyr :: arrange(.data $ periodBegin , .data $ periodEnd , .data $ calendarInterval ) %> %
122
77
dplyr :: mutate(timeId = dplyr :: row_number())
123
-
78
+
124
79
ParallelLogger :: logTrace(" - Inserting calendar periods" )
125
80
DatabaseConnector :: insertTable(
126
81
connection = connection ,
@@ -133,7 +88,60 @@ getTimeSeries <- function(
133
88
tempEmulationSchema = tempEmulationSchema ,
134
89
camelCaseToSnakeCase = TRUE
135
90
)
91
+ }
92
+
93
+ getTimeSeries <- function (
94
+ connection = NULL ,
95
+ tempEmulationSchema = NULL ,
96
+ cdmDatabaseSchema ,
97
+ cohortDatabaseSchema = cdmDatabaseSchema ,
98
+ cohortTable = " cohort" ,
99
+ runCohortTimeSeries = TRUE ,
100
+ runDataSourceTimeSeries = FALSE ,
101
+ timeSeriesMinDate = as.Date(" 1980-01-01" ),
102
+ timeSeriesMaxDate = as.Date(Sys.Date()),
103
+ stratifyByGender = TRUE ,
104
+ stratifyByAgeGroup = TRUE ,
105
+ cohortIds = NULL ) {
106
+
107
+ if (all(! runCohortTimeSeries , ! runDataSourceTimeSeries )) {
108
+ warning(
109
+ " - Both Cohort Time Series and Data Source Time Series are set to FALSE. Exiting time series diagnostics."
110
+ )
111
+ return (NULL )
112
+ }
113
+ start <- Sys.time()
114
+
115
+ ParallelLogger :: logTrace(" - Creating Andromeda object to collect results" )
116
+ resultsInAndromeda <- Andromeda :: andromeda()
136
117
118
+ if (runCohortTimeSeries ) {
119
+ sqlCount <-
120
+ " SELECT cohort_definition_id, COUNT(*) count
121
+ FROM @cohort_database_schema.@cohort_table
122
+ {@cohort_ids != ''} ? { where cohort_definition_id IN (@cohort_ids)}
123
+ GROUP BY cohort_definition_id;"
124
+ resultsInAndromeda $ cohortCount <- renderTranslateQuerySql(
125
+ connection = connection ,
126
+ sql = sqlCount ,
127
+ cohort_database_schema = cohortDatabaseSchema ,
128
+ cohort_ids = cohortIds ,
129
+ cohort_table = cohortTable
130
+ )
131
+ if (resultsInAndromeda $ cohortCount %> %
132
+ dplyr :: summarise(n = dplyr :: n()) %> %
133
+ dplyr :: pull(.data $ n ) == 0 ) {
134
+ warning(" Please check if cohorts are instantiated. Exiting cohort time series." )
135
+ return (NULL )
136
+ }
137
+ }
138
+
139
+ # # Create calendar periods table
140
+ createCalendarPeriodsTable(connection ,
141
+ tempEmulationSchema ,
142
+ timeSeriesMinDate ,
143
+ timeSeriesMaxDate )
144
+
137
145
tsSetUpSql <- " -- #time_series
138
146
DROP TABLE IF EXISTS #time_series;
139
147
DROP TABLE IF EXISTS #c_time_series1;
0 commit comments