R Under development (unstable) (2026-11-21 r90959) -- "Unsuffered Consequences" Copyright (C) 2026 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > pkgname <- "heaven" > source(file.path(R.home("share"), "R", "examples-header.R")) > options(warn = 1) > library('heaven') > > base::assign(".oldSearch", base::search(), pos = 'CheckExEnv') > base::assign(".old_wd", base::getwd(), pos = 'CheckExEnv') > cleanEx() > nameEx("averageIncome") > ### * averageIncome > > flush(stderr()); flush(stdout()) > > ### Name: averageIncome > ### Title: averageIncome > ### Aliases: averageIncome > > ### ** Examples > > require(data.table) Loading required package: data.table Attaching package: ‘data.table’ The following object is masked from ‘package:base’: %notin% > dat <- data.table(pnr=c(1,2,3,3), dato=as.Date(paste0(2018:2021,"-11-11"))) > indkomst <- data.table(pnr=rep(c(1,2,3,3),5),year=rep(2015:2020,5), + income=rep(200:205,5)) Warning in as.data.table.list(x, keep.rownames = keep.rownames, check.names = check.names, : Item 1 has 20 rows but longest item has 30; recycled with remainder. > averageIncome(dat,indkomst,c("pnr","dato"),c("pnr","year","income")) Key: pnr year income 1: 1 2018 201 2: 2 2019 202 3: 3 2020 202 4: 3 2021 203 > > > > cleanEx() detaching ‘package:data.table’ > nameEx("charlsonIndex") > ### * charlsonIndex > > flush(stderr()); flush(stdout()) > > ### Name: charlsonIndex > ### Title: charlsonindex - define charlson index at a selected time > ### Aliases: charlsonIndex > > ### ** Examples > > require(data.table) Loading required package: data.table Attaching package: ‘data.table’ The following object is masked from ‘package:base’: %notin% > set.seed(211) > adm <- simAdmissionData(10) > adm[,charlson.date:=as.Date("2017-01-01")] > adm2 <- adm[1:5] > adm2[,charlson.date:=as.Date("2015-01-01")] > adm <- rbind(adm,adm2) > ci <- charlsonIndex(data=adm,ptid='pnr',vars='diag',data.date='inddto', + charlson.date="charlson.date") > > > > cleanEx() detaching ‘package:data.table’ > nameEx("createProject") > ### * createProject > > flush(stderr()); flush(stdout()) > > ### Name: createProject > ### Title: ordnung muss sein > ### Aliases: createProject > > ### ** Examples > > project <- tempfile("heaven-project-") > createProject(project) Empty project created. Please read /tmp/Rtmps9NfwB/heaven-project-25028556452036/README.txt > list.files(project) [1] "R-code" "README.txt" "data" "figures" "sandbox" [6] "sas-code" "tables" "temp" > unlink(project, recursive = TRUE) > > > > cleanEx() > nameEx("dsr") > ### * dsr > > flush(stderr()); flush(stdout()) > > ### Name: dsr > ### Title: Confidence intervals for age standardized rates and rate ratios > ### Aliases: dsr > > ### ** Examples > > library(data.table) Attaching package: ‘data.table’ The following object is masked from ‘package:base’: %notin% > set.seed(84) > n=160 > d <- data.table(e1=rpois(n,lambda=9), + rt1=rpois(n,lambda=1880), + e2=rpois(n,lambda=123), + rt2=rpois(n,lambda=80000)) > d[,agegroups:=factor(rep(c("40-50","45-50","50-55","55-60","60-65","65-70","70-75","75-80"),n/8))] > d[,sex:=factor(rep(c("f","m"),c(n/2,n/2)))] > d[,year:=rep(2001:2010,n/10)] > D=d[,list(e1=sum(e1),rt1=sum(rt1),e2=sum(e2),rt2=sum(rt2)),by=c("sex","agegroups")] > D[sex=="m",e1:=e1+rpois(.N,lambda=as.numeric(agegroups)*17)] > D[sex=="m",rt1:=rt1-rpois(.N,lambda=as.numeric(agegroups)*1600)] > dsr(count1=D[sex=="m",e1], pop1=D[sex=="m",rt1], + count0=D[sex=="f",e1], pop0=D[sex=="f",rt1], + stdpop=D[sex=="f",rt1]) type group rate rate.lower rate.upper ratio ratio.lower 1: standardized 0 0.004925192 0.004576909 0.005292949 1.000000 1.000000 2: standardized 1 0.017648266 0.016679074 0.018665619 3.583265 3.268853 3: crude 0 0.004925192 0.004576909 0.005292949 1.000000 1.000000 4: crude 1 0.014805927 0.014035521 0.015607622 3.006163 2.747509 ratio.upper 1: 1.000000 2: 3.931403 3: 1.000000 4: 3.291209 > > > > cleanEx() detaching ‘package:data.table’ > nameEx("exposureMatch") > ### * exposureMatch > > flush(stderr()); flush(stdout()) > > ### Name: exposureMatch > ### Title: Exact matching on time-dependent exposure > ### Aliases: exposureMatch > > ### ** Examples > > require(data.table) Loading required package: data.table Attaching package: ‘data.table’ The following object is masked from ‘package:base’: %notin% > case <- c(rep(0,40),rep(1,15)) > ptid <- paste0("P",1:55) > sex <- c(rep("fem",20),rep("mal",20),rep("fem",8),rep("mal",7)) > byear <- c(rep(c(2020,2030),20),rep(2020,7),rep(2030,8)) > case.Index <- c(seq(1,40,1),seq(5,47,3)) > startDisease <- rep(10,55) > control.Index <- case.Index > diabetes <- seq(2,110,2) > heartdis <- seq(110,2,-2) > diabetes <- c(rep(1,55)) > heartdis <- c(rep(100,55)) > library(data.table) > dat <- data.table(case,ptid,sex,byear,diabetes,heartdis,case.Index, + control.Index,startDisease) > # Exposure density matching > matchdat <- exposureMatch(ptid="ptid",event="case", + terms=c("byear","sex"),data=dat,n.controls=2, + case.index="case.Index",end.followup="control.Index") Matching terms define 4 subsets | | | 0% | |'''''' | 31% | |'''''''''' | 51% | |''''''''''''''''''''| 100% > matchdat Key: case.id case ptid sex byear diabetes heartdis case.Index 1: 41 0 P11 fem 2020 1 100 5 2: 41 0 P43 fem 2020 1 100 5 3: 41 1 P41 fem 2020 1 100 5 4: 42 0 P11 fem 2020 1 100 8 5: 42 0 P15 fem 2020 1 100 8 6: 42 1 P42 fem 2020 1 100 8 7: 43 0 P13 fem 2020 1 100 11 8: 43 0 P44 fem 2020 1 100 11 9: 43 1 P43 fem 2020 1 100 11 10: 44 0 P19 fem 2020 1 100 14 11: 44 0 P46 fem 2020 1 100 14 12: 44 1 P44 fem 2020 1 100 14 13: 45 0 P46 fem 2020 1 100 17 14: 45 0 P47 fem 2020 1 100 17 15: 45 1 P45 fem 2020 1 100 17 16: 46 0 P47 fem 2020 1 100 20 17: 46 1 P46 fem 2020 1 100 20 18: 47 1 P47 fem 2020 1 100 23 19: 48 1 P48 fem 2030 1 100 26 20: 49 0 P32 mal 2030 1 100 29 21: 49 0 P51 mal 2030 1 100 29 22: 49 1 P49 mal 2030 1 100 29 23: 50 0 P36 mal 2030 1 100 32 24: 50 0 P38 mal 2030 1 100 32 25: 50 1 P50 mal 2030 1 100 32 26: 51 0 P52 mal 2030 1 100 35 27: 51 0 P53 mal 2030 1 100 35 28: 51 1 P51 mal 2030 1 100 35 29: 52 0 P40 mal 2030 1 100 38 30: 52 0 P54 mal 2030 1 100 38 31: 52 1 P52 mal 2030 1 100 38 32: 53 0 P54 mal 2030 1 100 41 33: 53 0 P55 mal 2030 1 100 41 34: 53 1 P53 mal 2030 1 100 41 35: 54 0 P55 mal 2030 1 100 44 36: 54 1 P54 mal 2030 1 100 44 37: 55 1 P55 mal 2030 1 100 47 case.id case ptid sex byear diabetes heartdis case.Index control.Index startDisease n.controls 1: 11 10 2 2: 11 10 2 3: 5 10 2 4: 11 10 2 5: 15 10 2 6: 8 10 2 7: 13 10 2 8: 14 10 2 9: 11 10 2 10: 19 10 2 11: 20 10 2 12: 14 10 2 13: 20 10 2 14: 23 10 2 15: 17 10 2 16: 23 10 1 17: 20 10 1 18: 23 10 0 19: 26 10 0 20: 32 10 2 21: 35 10 2 22: 29 10 2 23: 36 10 2 24: 38 10 2 25: 32 10 2 26: 38 10 2 27: 41 10 2 28: 35 10 2 29: 40 10 2 30: 44 10 2 31: 38 10 2 32: 44 10 2 33: 47 10 2 34: 41 10 2 35: 47 10 1 36: 44 10 1 37: 47 10 0 control.Index startDisease n.controls > # Same with 2 cores > if (requireNamespace("doParallel", quietly = TRUE)) { + library(parallel) + library(foreach) + matchdat2 <- exposureMatch("ptid","case",c("byear","sex"),data=dat,2,case.index= + "case.Index",end.followup="control.Index" + ,cores=2) + matchdat2 + all.equal(matchdat,matchdat2) + } Matching terms define 4 subsets | | | 0% | |'''''' | 31% | |'''''''''' | 51% | |''''''''''''''''''''| 100% [1] TRUE > #Time dependent matching. In addition to fixed matching parameters there are > #two other sets of dates where it is required that if a case has that condi- > #tion prior to index, then controls also need to have the condition prior to > #the case index to be eligible - and if the control does not have the condi- > #tion prior to index then the same is required for the control. > matchdat <- exposureMatch("ptid","case",c("byear","sex"),data=dat,n.controls=2, + case.index="case.Index",end.followup="control.Index",cores=1, + date.terms=c("diabetes","heartdis")) Matching terms define 4 subsets | | | 0% | |'''''' | 31% | |'''''''''' | 51% | |''''''''''''''''''''| 100% > matchdat Key: case.id case ptid sex byear diabetes heartdis case.Index 1: 41 0 P11 fem 2020 1 100 5 2: 41 0 P43 fem 2020 1 100 5 3: 41 1 P41 fem 2020 1 100 5 4: 42 0 P11 fem 2020 1 100 8 5: 42 0 P15 fem 2020 1 100 8 6: 42 1 P42 fem 2020 1 100 8 7: 43 0 P13 fem 2020 1 100 11 8: 43 0 P44 fem 2020 1 100 11 9: 43 1 P43 fem 2020 1 100 11 10: 44 0 P19 fem 2020 1 100 14 11: 44 0 P46 fem 2020 1 100 14 12: 44 1 P44 fem 2020 1 100 14 13: 45 0 P46 fem 2020 1 100 17 14: 45 0 P47 fem 2020 1 100 17 15: 45 1 P45 fem 2020 1 100 17 16: 46 0 P47 fem 2020 1 100 20 17: 46 1 P46 fem 2020 1 100 20 18: 47 1 P47 fem 2020 1 100 23 19: 48 1 P48 fem 2030 1 100 26 20: 49 0 P32 mal 2030 1 100 29 21: 49 0 P51 mal 2030 1 100 29 22: 49 1 P49 mal 2030 1 100 29 23: 50 0 P36 mal 2030 1 100 32 24: 50 0 P38 mal 2030 1 100 32 25: 50 1 P50 mal 2030 1 100 32 26: 51 0 P52 mal 2030 1 100 35 27: 51 0 P53 mal 2030 1 100 35 28: 51 1 P51 mal 2030 1 100 35 29: 52 0 P40 mal 2030 1 100 38 30: 52 0 P54 mal 2030 1 100 38 31: 52 1 P52 mal 2030 1 100 38 32: 53 0 P54 mal 2030 1 100 41 33: 53 0 P55 mal 2030 1 100 41 34: 53 1 P53 mal 2030 1 100 41 35: 54 0 P55 mal 2030 1 100 44 36: 54 1 P54 mal 2030 1 100 44 37: 55 1 P55 mal 2030 1 100 47 case.id case ptid sex byear diabetes heartdis case.Index control.Index startDisease n.controls 1: 11 10 2 2: 11 10 2 3: 5 10 2 4: 11 10 2 5: 15 10 2 6: 8 10 2 7: 13 10 2 8: 14 10 2 9: 11 10 2 10: 19 10 2 11: 20 10 2 12: 14 10 2 13: 20 10 2 14: 23 10 2 15: 17 10 2 16: 23 10 1 17: 20 10 1 18: 23 10 0 19: 26 10 0 20: 32 10 2 21: 35 10 2 22: 29 10 2 23: 36 10 2 24: 38 10 2 25: 32 10 2 26: 38 10 2 27: 41 10 2 28: 35 10 2 29: 40 10 2 30: 44 10 2 31: 38 10 2 32: 44 10 2 33: 47 10 2 34: 41 10 2 35: 47 10 1 36: 44 10 1 37: 47 10 0 control.Index startDisease n.controls > > > > > cleanEx() detaching ‘package:foreach’, ‘package:parallel’, ‘package:data.table’ > nameEx("findCondition") > ### * findCondition > > flush(stderr()); flush(stdout()) > > ### Name: findCondition > ### Title: Find conditions in registry data > ### Aliases: findCondition > > ### ** Examples > > library(heaven) > library(data.table) Attaching package: ‘data.table’ The following object is masked from ‘package:base’: %notin% > > # find all diagnoses that start with "DT" > set.seed(800); adm <- simAdmissionData(800) > x <- findCondition(adm,vars=c("diag"), + keep=c("pnr","inddto","uddto"), + conditions=list(THIS=c("DT")), + match="start",condition.name="THAT") > x THAT pnr inddto uddto 1: THIS 6 1996-10-23 1996-11-01 2: THIS 7 2001-08-27 2001-09-04 3: THIS 8 2009-05-24 2009-06-30 4: THIS 13 2004-01-30 2004-02-16 5: THIS 28 1997-02-16 1997-02-17 --- 160: THIS 789 1995-01-09 1995-01-24 161: THIS 792 2004-05-09 2004-05-20 162: THIS 792 2007-01-08 2007-01-17 163: THIS 793 2010-01-01 2010-01-23 164: THIS 795 2013-07-08 2013-07-26 > # restrict to first by pnr > x[x[,.I[1],by=list(pnr)]$V1] THAT pnr inddto uddto 1: THIS 6 1996-10-23 1996-11-01 2: THIS 7 2001-08-27 2001-09-04 3: THIS 8 2009-05-24 2009-06-30 4: THIS 13 2004-01-30 2004-02-16 5: THIS 28 1997-02-16 1997-02-17 --- 143: THIS 788 1998-05-27 1998-06-12 144: THIS 789 1995-01-09 1995-01-24 145: THIS 792 2004-05-09 2004-05-20 146: THIS 793 2010-01-01 2010-01-23 147: THIS 795 2013-07-08 2013-07-26 > # restrict to last by pnr > x[x[,.I[.N],by=list(pnr)]$V1] THAT pnr inddto uddto 1: THIS 6 1996-10-23 1996-11-01 2: THIS 7 2001-08-27 2001-09-04 3: THIS 8 2009-05-24 2009-06-30 4: THIS 13 2004-01-30 2004-02-16 5: THIS 28 1997-02-16 1997-02-17 --- 143: THIS 788 1998-05-27 1998-06-12 144: THIS 789 1995-01-09 1995-01-24 145: THIS 792 2007-01-08 2007-01-17 146: THIS 793 2010-01-01 2010-01-23 147: THIS 795 2013-07-08 2013-07-26 > > opr <- data.table( + pnr=1:100,opr=paste0(rep(c('A','B'),50),seq(0,100,10)), + oprtil=paste0(rep(c('A','C'),50),seq(0,100,10)), + odto=101:200 + ) > search <- list(Cond1=c('A1','A2'),Cond2=c('B10','B40','B5'), + Cond3=c('A1','C20','B2')) > > excl <- list(Cond2='B100') > > out <- findCondition(opr,vars=c("opr","oprtil"), + keep=c("pnr","odto"), + conditions=search, exclusions=excl, + match="start",condition.name="cond") > ### And how to use the result: > # Find first occurence of each condition and then use "dcast" to create > # a data.table with vectors corresponding to each condition. > test <- out[,list(min=min(odto)),by=c("pnr","cond")] > # provide a list of variables with one value each > test2 <- dcast(pnr~cond,data=test,value.var="min") > test2 # A datatable with first dates of each condition for each pnr, but only Key: pnr Cond1 Cond2 Cond3 1: 2 NA 102 NA 2: 3 103 NA NA 3: 6 NA 106 NA 4: 11 111 NA 111 5: 13 113 NA 113 6: 14 NA NA 114 7: 16 NA 116 NA 8: 24 NA 124 NA 9: 25 125 NA NA 10: 28 NA 128 NA 11: 33 133 NA 133 12: 35 135 NA 135 13: 36 NA NA 136 14: 38 NA 138 NA 15: 46 NA 146 NA 16: 47 147 NA NA 17: 50 NA 150 NA 18: 55 155 NA 155 19: 57 157 NA 157 20: 58 NA NA 158 21: 60 NA 160 NA 22: 68 NA 168 NA 23: 69 169 NA NA 24: 72 NA 172 NA 25: 77 177 NA 177 26: 79 179 NA 179 27: 80 NA NA 180 28: 82 NA 182 NA 29: 90 NA 190 NA 30: 91 191 NA NA 31: 94 NA 194 NA 32: 99 199 NA 199 pnr Cond1 Cond2 Cond3 > # for pnr with at least one condition > # Define a condition as present when before a certain index date > dates <- data.table (pnr=1:100,basedate=sample(0:200,size=100,replace=TRUE)) > test3 <- merge(out,dates,by="pnr") > test3[,before:=as.numeric(odto<=basedate)] # 1 when condition fulfille > test3 <- test3[,list(before=max(before)),by=c("pnr","cond")] > test4 <- dcast(pnr~cond,value.var="before",data=test3) > test4[is.na(test4)] <- 0 # Converts NAs to zero > test4[] Key: pnr Cond1 Cond2 Cond3 1: 2 0 0 0 2: 3 0 0 0 3: 6 0 0 0 4: 11 1 0 1 5: 13 1 0 1 6: 14 0 0 1 7: 16 0 0 0 8: 24 0 1 0 9: 25 0 0 0 10: 28 0 0 0 11: 33 1 0 1 12: 35 1 0 1 13: 36 0 0 1 14: 38 0 0 0 15: 46 0 0 0 16: 47 0 0 0 17: 50 0 0 0 18: 55 0 0 0 19: 57 0 0 0 20: 58 0 0 0 21: 60 0 0 0 22: 68 0 1 0 23: 69 0 0 0 24: 72 0 0 0 25: 77 0 0 0 26: 79 0 0 0 27: 80 0 0 1 28: 82 0 0 0 29: 90 0 0 0 30: 91 0 0 0 31: 94 0 0 0 32: 99 0 0 0 pnr Cond1 Cond2 Cond3 > > > > > cleanEx() detaching ‘package:data.table’ > nameEx("getAdmLimits") > ### * getAdmLimits > > flush(stderr()); flush(stdout()) > > ### Name: getAdmLimits > ### Title: Get Admission time > ### Aliases: getAdmLimits > > ### ** Examples > > set.seed(8) > lpr <- simAdmissionData(10) > ## Variables have default names > adm <- getAdmLimits(lpr) No data errors. > > ## with errors > lpr1 <- lpr > lpr1 <- lpr1[uddto>as.Date("2010-01-01"),uddto:=NA] > adm1 <- getAdmLimits(lpr1) Warning in getAdmLimits(lpr1) : Data have errors in 4 row(s); inspect the returned error column. > adm1 <- getAdmLimits(lpr1,error="remove") > adm1 <- getAdmLimits(lpr1,error="flag") > > ## case where Variables have custom names > data.table::setnames(lpr, c('pnr','inddto','uddto'), c('personid', 'Entrydate','Outdate')) > adm2 <- getAdmLimits(lpr,pnr='personid',inddto='Entrydate',uddto='Outdate') Warning in getAdmLimits(lpr, pnr = "personid", inddto = "Entrydate", uddto = "Outdate") : Data have errors in 4 row(s); inspect the returned error column. > > ## duplicated and overlap > set.seed(8) > lpr3 <- simAdmissionData(10) > lpr3 <- lpr3[sample(1:NROW(lpr3),replace=TRUE,size=round(NROW(lpr3)*1.5))] > adm3 <- getAdmLimits(lpr3,collapse=TRUE) No data errors. Collapsed 45 rows with potentially multiple and overlapping periods into 24 rows. > > > > > cleanEx() > nameEx("groupAges") > ### * groupAges > > flush(stderr()); flush(stdout()) > > ### Name: groupAges > ### Title: groupAges > ### Aliases: groupAges > > ### ** Examples > > library(heaven) > set.seed(1) > x <- floor(runif(20, min = 0, max = 105)) > groupAges( + x, + lower = 15, + upper = 50, + by = 10, + below.char = "<", + above.char = "+" + ) Warning: age groups may not contain equal number of years. This probably means the age span between the upper and lower age limits is cannot be evenly divided by the group size [1] 25 - 34 35 - 44 50+ 50+ 15 - 24 50+ 50+ 50+ 50+ [10] <15 15 - 24 15 - 24 50+ 35 - 44 50+ 50+ 50+ 50+ [19] 35 - 44 50+ Levels: <15 15 - 24 25 - 34 35 - 44 50+ > > > > cleanEx() > nameEx("hypertensionMedication") > ### * hypertensionMedication > > flush(stderr()); flush(stdout()) > > ### Name: hypertensionMedication > ### Title: hypertensionMedication - define presence of hypertension based > ### on prescriptions > ### Aliases: hypertensionMedication > > ### ** Examples > > if (requireNamespace("zoo", quietly = TRUE)) { + require(data.table) + dat <-data.table( + ATC=sample(c('C02A','C03A','C02DD','C07A','C07F','C09AA','C09BA'),100, + replace=TRUE), + EKSD=as.Date("2012-01-01")+(sample(1:600,100,replace=TRUE)), + ptid=1:10, + index=c(rep(as.Date("2013-06-01"),50),rep(as.Date("2013-06-02"),50))) + setkey(dat,"ptid") + # Hypertension with indexdate: + index <- hypertensionMedication(dat,vars=c("ptid","ATC","EKSD"), + index.date="index") + index + noindex <- hypertensionMedication(dat,vars=c("ptid","ATC","EKSD")) + noindex + } Loading required package: data.table Attaching package: ‘data.table’ The following object is masked from ‘package:base’: %notin% ptid hypertension numDrugs 1: 1 2012-12-31 3 2: 3 2013-06-30 3 3: 5 2012-06-30 3 4: 6 2013-09-30 2 5: 7 2012-06-30 2 6: 8 2012-12-31 3 7: 10 2012-09-30 2 > > > > cleanEx() detaching ‘package:data.table’ > nameEx("importDREAM") > ### * importDREAM > > flush(stderr()); flush(stdout()) > > ### Name: importDREAM > ### Title: importDREAM > ### Aliases: importDREAM > > ### ** Examples > > # The following is a minute version of DREAM > library(data.table) Attaching package: ‘data.table’ The following object is masked from ‘package:base’: %notin% > microDREAM <-data.table(PNR=c(1,2),branche_2008_01=c("","3"), + branche_2008_02=c("1","3"), branche_2008_03=c("1",""), + y_9201=c("","1"),y_9202=c("","2"),y_9203=c("","2")) > # Explanation of codes for "branche" > branche <- data.table(branche=c("1","2","3"), + text=c("school","gardening","suage")) > support <- data.table(support=c("1","2"),text=c("education","sick")) > temp <- importDREAM(microDREAM,type="branche",pnr="PNR",explData=branche) > temp[] Key: branche PNR start end text 1: 1 2008-01-01 2008-01-31 2: 1 1 2008-02-01 2008-03-31 school 3: 3 2 2008-01-01 2008-03-02 suage 4: 2 2008-03-01 2008-03-31 > temp2 <- importDREAM(microDREAM,type="support",pnr="PNR",explData=support) > temp2[] Key: support PNR start end text 1: 1 2026-11-21 2026-11-28 2: 1 2 2026-11-21 2026-11-28 education 3: 2 2 2026-11-21 2026-11-28 sick > > > > cleanEx() detaching ‘package:data.table’ > nameEx("importSAS") > ### * importSAS > > flush(stderr()); flush(stdout()) > > ### Name: importSAS > ### Title: importSAS > ### Aliases: importSAS contentSAS > > ### ** Examples > > # Scratch files are written under tempdir() and removed after each call. > > ## Not run: > ##D # Requires a SAS installation and access to the private registry files. > ##D > ##D # Before importing a SAS data file it is useful to look into the > ##D # format of the variables: > ##D contentSAS(filename="X:/Data/Rawdata_Hurtig/704791/diag_indl") > ##D contentSAS(filename="x:/Data/Rawdata_Hurtig/704791/pop.sas7bdat") > ##D > ##D # Also, it is often a good idea to initially only read a limited amount of data > ##D df101 <- importSAS(filename="X:/Data/Rawdata_Hurtig/704791/diag_indl",obs=101,save.tmp=TRUE) > ##D # and to examine the result > ##D str(df101) > ##D df101 > ##D > ##D # Format, dates, numeric, character > ##D df101 <- importSAS(filename="X:/Data/Rawdata_Hurtig/704791/diag_indl",obs=101, > ##D save.tmp=TRUE,date.vars="inddto", > ##D numeric.vars="pnr",character.vars="packsize") > ##D > ##D # we can also use the pre.hook to limit the number of observations via sas options: > ##D importSAS(filename="X:/Data/Rawdata_Hurtig/704791/diag_indl", > ##D pre.hook="options obs=17;",where="diag='DN899'",keep=c("PNR","diag"),show.sas.code=1L) > ##D > ##D # NOTE: In combination with a where statement SAS will find the first 101 observations that > ##D # satisfy the where statement. When the where statement finds nothing then SAS > ##D # will run through the whole file without finding anything > ##D > ##D # To import the whole file just remove the limitations: > ##D # df <- importSAS(filename="X:/Data/Rawdata_Hurtig/704791/diag_indl",show.sas.code=TRUE) > ##D # But: this is not useful when files are huge and you should to look > ##D # at the examples below which show how to read selected columns and rows only > ##D > ##D # In huge data it is efficient to select only the required columns > ##D # and to read only the rows of the data set that meet a criterion. > ##D # The following example shows how to select columns with the keep option > ##D # and how to specify the "where statement" of the SAS data step: > ##D df0 <- importSAS(filename="X:/Data/Rawdata_Hurtig/704791/diag_indl", > ##D obs=101, > ##D keep=c("diag","diagtype"), > ##D where="diagtype = 'A' and diag = '45490'") > ##D > ##D # Examine the imported data.table > ##D str(df0) > ##D df0 > ##D # using where contains is powerful > ##D importSAS(filename="X:/Data/Rawdata_Hurtig/704791/diag_indl", > ##D obs=10, > ##D save.tmp=TRUE, > ##D where="diag contains 'DI2'", > ##D keep=c("PNR","diag","inddto","uddto")) > ##D > ##D > ##D # Another typical task is to import only the rows which correspond to > ##D # a set of pnr numbers or a set of diagnoses or both. > ##D # To achieve this the function importSAS is merging files during the import. > ##D # The feature is called 'filter' and illustrated in the following examples: > ##D # Example 1: import only rows of diag_indl which correspond to one of the > ##D # first 23 pnr's in pop > ##D pop <- importSAS(filename="x:/Data/Rawdata_Hurtig/704791/pop.sas7bdat", > ##D obs=23,keep="PNR") > ##D df1 <- importSAS(filename="X:/Data/Rawdata_Hurtig/704791/diag_indl", > ##D obs=8501,filter=pop) > ##D str(df1) > ##D df1 > ##D > ##D # Example 2: import only rows of diag_indl which correspond the > ##D # first pnr's in pop for which also diag is equal to DK409 > ##D pop2 <- importSAS(filename="x:/Data/Rawdata_Hurtig/704791/pop.sas7bdat", > ##D obs=4223,keep="PNR") > ##D pop2 <- pop2[,diag:="DZ508"] > ##D df2 <- importSAS(filename="X:/Data/Rawdata_Hurtig/704791/diag_indl", > ##D obs=8501,keep=c("pnr","diag","inddto","uddto","pattype"), > ##D where="pattype ne 3",filter=pop2) > ##D str(df2) > ##D df2 > ##D > ##D ## Sometimes the sas data file cannot be read due to unknown formats > ##D ## here is how to solve this: > ##D df2a <- importSAS(filename="X:/Data/Rawdata_Hurtig/704791/diag_indl", > ##D obs=101, > ##D pre.hook="options nofmterr;") > ##D > ##D > ##D # The hooks set.hook and step.hook can be used as follows: > ##D df3 <- importSAS(filename="X:/Data/Rawdata_Hurtig/704791/diag_indl", > ##D obs=101, > ##D keep=c("PNR","diag","diagtype"), > ##D set.hook="firstobs=5", > ##D step.hook="by PNR; firstPNR=first.PNR;", > ##D where="diag contains 'I'") > ##D str(df3) > ##D df3 > ##D > ##D # if you are more fluent in SAS than in R it may help to > ##D # know how to communicate e.g., a keep statement, instead of > ##D # using the keep argument: > ##D > ##D df4 <-importSAS(filename="X:/Data/Rawdata_Hurtig/999999/Dream201801", > ##D set.hook="keep=pnr branch;", > ##D obs=1000) > ##D > ##D # Because the "overwrite" argument is FALSE, running the above code again will abort the import > ##D # to not overwrite the temporary files. > ##D # Setting "overwrite=TRUE" will allow the function to overwrite the files. > ##D > ## End(Not run) > > > > cleanEx() > nameEx("incidenceMatch") > ### * incidenceMatch > > flush(stderr()); flush(stdout()) > > ### Name: incidenceMatch > ### Title: incidenceMatch - Risk set matching for nested case-control > ### designs > ### Aliases: incidenceMatch > > ### ** Examples > > require(data.table) Loading required package: data.table Attaching package: ‘data.table’ The following object is masked from ‘package:base’: %notin% > case <- c(rep(0,40),rep(1,15)) > ptid <- paste0("P",1:55) > sex <- c(rep("fem",20),rep("mal",20),rep("fem",8),rep("mal",7)) > byear <- c(rep(c(2020,2030),20),rep(2020,7),rep(2030,8)) > case.Index <- c(seq(1,40,1),seq(5,47,3)) > startDisease <- rep(10,55) > control.Index <- case.Index > diabetes <- seq(2,110,2) > heartdis <- seq(110,2,-2) > diabetes <- c(rep(1,55)) > heartdis <- c(rep(100,55)) > library(data.table) > dat <- data.table(case,ptid,sex,byear,diabetes,heartdis,case.Index, + control.Index,startDisease) > # Risk set matching > matchdat <- incidenceMatch(ptid="ptid",event="case", + terms=c("byear","sex"),data=dat,n.controls=2, + case.index="case.Index", + end.followup="control.Index",seed=8) Matching terms define 4 subsets | | | 0% | |'''''' | 31% | |'''''''''' | 51% | |''''''''''''''''''''| 100% > matchdat Key: case.id case ptid sex byear diabetes heartdis case.Index 1: 41 0 P42 fem 2020 1 100 5 2: 41 0 P47 fem 2020 1 100 5 3: 41 1 P41 fem 2020 1 100 5 4: 42 0 P19 fem 2020 1 100 8 5: 42 0 P47 fem 2020 1 100 8 6: 42 1 P42 fem 2020 1 100 8 7: 43 0 P46 fem 2020 1 100 11 8: 43 0 P47 fem 2020 1 100 11 9: 43 1 P43 fem 2020 1 100 11 10: 44 0 P15 fem 2020 1 100 14 11: 44 0 P45 fem 2020 1 100 14 12: 44 1 P44 fem 2020 1 100 14 13: 45 0 P46 fem 2020 1 100 17 14: 45 0 P47 fem 2020 1 100 17 15: 45 1 P45 fem 2020 1 100 17 16: 46 0 P47 fem 2020 1 100 20 17: 46 1 P46 fem 2020 1 100 20 18: 47 1 P47 fem 2020 1 100 23 19: 48 1 P48 fem 2030 1 100 26 20: 49 0 P50 mal 2030 1 100 29 21: 49 0 P55 mal 2030 1 100 29 22: 49 1 P49 mal 2030 1 100 29 23: 50 0 P40 mal 2030 1 100 32 24: 50 0 P55 mal 2030 1 100 32 25: 50 1 P50 mal 2030 1 100 32 26: 51 0 P54 mal 2030 1 100 35 27: 51 0 P55 mal 2030 1 100 35 28: 51 1 P51 mal 2030 1 100 35 29: 52 0 P53 mal 2030 1 100 38 30: 52 0 P55 mal 2030 1 100 38 31: 52 1 P52 mal 2030 1 100 38 32: 53 0 P54 mal 2030 1 100 41 33: 53 0 P55 mal 2030 1 100 41 34: 53 1 P53 mal 2030 1 100 41 35: 54 0 P55 mal 2030 1 100 44 36: 54 1 P54 mal 2030 1 100 44 37: 55 1 P55 mal 2030 1 100 47 case.id case ptid sex byear diabetes heartdis case.Index control.Index startDisease n.controls 1: 8 10 2 2: 23 10 2 3: 5 10 2 4: 19 10 2 5: 23 10 2 6: 8 10 2 7: 20 10 2 8: 23 10 2 9: 11 10 2 10: 15 10 2 11: 17 10 2 12: 14 10 2 13: 20 10 2 14: 23 10 2 15: 17 10 2 16: 23 10 1 17: 20 10 1 18: 23 10 0 19: 26 10 0 20: 32 10 2 21: 47 10 2 22: 29 10 2 23: 40 10 2 24: 47 10 2 25: 32 10 2 26: 44 10 2 27: 47 10 2 28: 35 10 2 29: 41 10 2 30: 47 10 2 31: 38 10 2 32: 44 10 2 33: 47 10 2 34: 41 10 2 35: 47 10 1 36: 44 10 1 37: 47 10 0 control.Index startDisease n.controls > matchReport(matchdat) Matching success Number of controls found / number of cases: n.controls 0 1 2 3 2 10 Reuse/use of controls Number of times selected / number of subjects: n.reuse 1 2 5 6 7 2 1 1 > # Same with 2 cores > library(parallel) > library(foreach) > if (requireNamespace("doParallel", quietly = TRUE)) { + matchdat2 <- incidenceMatch("ptid","case",c("byear","sex"),data=dat, + n.controls=2,case.index="case.Index",end.followup="control.Index" + ,cores=2,seed=8) + matchdat2 + all.equal(matchdat,matchdat2) + } Matching terms define 4 subsets | | | 0% | |'''''' | 31% | |'''''''''' | 51% | |''''''''''''''''''''| 100% [1] TRUE > > # Case control matching with requirement of minimum exposure time in each > # group > ew <- incidenceMatch(ptid="ptid",event="case",terms=c("byear","sex"), + data=dat,n.controls=2,case.index="case.Index", + end.followup="control.Index",cores=1, + duration.terms=list(list(start="startDisease",min=15))) Matching terms define 4 subsets | | | 0% | |'''''' | 31% | |'''''''''' | 51% | |''''''''''''''''''''| 100% > ew Key: case.id case ptid sex byear diabetes heartdis case.Index 1: 41 0 P11 fem 2020 1 100 5 2: 41 0 P43 fem 2020 1 100 5 3: 41 1 P41 fem 2020 1 100 5 4: 42 0 P11 fem 2020 1 100 8 5: 42 0 P15 fem 2020 1 100 8 6: 42 1 P42 fem 2020 1 100 8 7: 43 0 P13 fem 2020 1 100 11 8: 43 0 P44 fem 2020 1 100 11 9: 43 1 P43 fem 2020 1 100 11 10: 44 0 P19 fem 2020 1 100 14 11: 44 0 P46 fem 2020 1 100 14 12: 44 1 P44 fem 2020 1 100 14 13: 45 0 P46 fem 2020 1 100 17 14: 45 0 P47 fem 2020 1 100 17 15: 45 1 P45 fem 2020 1 100 17 16: 46 0 P47 fem 2020 1 100 20 17: 46 1 P46 fem 2020 1 100 20 18: 47 1 P47 fem 2020 1 100 23 19: 48 1 P48 fem 2030 1 100 26 20: 49 0 P32 mal 2030 1 100 29 21: 49 0 P51 mal 2030 1 100 29 22: 49 1 P49 mal 2030 1 100 29 23: 50 0 P36 mal 2030 1 100 32 24: 50 0 P38 mal 2030 1 100 32 25: 50 1 P50 mal 2030 1 100 32 26: 51 0 P52 mal 2030 1 100 35 27: 51 0 P53 mal 2030 1 100 35 28: 51 1 P51 mal 2030 1 100 35 29: 52 0 P40 mal 2030 1 100 38 30: 52 0 P54 mal 2030 1 100 38 31: 52 1 P52 mal 2030 1 100 38 32: 53 0 P54 mal 2030 1 100 41 33: 53 0 P55 mal 2030 1 100 41 34: 53 1 P53 mal 2030 1 100 41 35: 54 0 P55 mal 2030 1 100 44 36: 54 1 P54 mal 2030 1 100 44 37: 55 1 P55 mal 2030 1 100 47 case.id case ptid sex byear diabetes heartdis case.Index control.Index startDisease n.controls 1: 11 10 2 2: 11 10 2 3: 5 10 2 4: 11 10 2 5: 15 10 2 6: 8 10 2 7: 13 10 2 8: 14 10 2 9: 11 10 2 10: 19 10 2 11: 20 10 2 12: 14 10 2 13: 20 10 2 14: 23 10 2 15: 17 10 2 16: 23 10 1 17: 20 10 1 18: 23 10 0 19: 26 10 0 20: 32 10 2 21: 35 10 2 22: 29 10 2 23: 36 10 2 24: 38 10 2 25: 32 10 2 26: 38 10 2 27: 41 10 2 28: 35 10 2 29: 40 10 2 30: 44 10 2 31: 38 10 2 32: 44 10 2 33: 47 10 2 34: 41 10 2 35: 47 10 1 36: 44 10 1 37: 47 10 0 control.Index startDisease n.controls > > > > > cleanEx() detaching ‘package:foreach’, ‘package:parallel’, ‘package:data.table’ > nameEx("innerMedicinMacro") > ### * innerMedicinMacro > > flush(stderr()); flush(stdout()) > > ### Name: innerMedicinMacro > ### Title: The heart of the medicin macro > ### Aliases: innerMedicinMacro > > ### ** Examples > > prescriptions <- data.frame(pnr = 1, eksd = 0, strnum = 1, + packsize = 5, apk = 1) > admissions <- data.frame(pnr = 1, inddto = 2, uddto = 4) > doses <- list(value = 1, min = 1, max = 1, def = 1) > index <- data.frame(id.start = 0, id.end = 0, + id.start.adm = 0, id.end.adm = 0) > innerMedicinMacro(prescriptions, admissions, doses, idunique = 1, + index = index, prescriptionwindow = 2, + maxdepot = 0, verbose = FALSE) [[1]] pnr X B E 1 1 1 0 7 > > > > cleanEx() > nameEx("kommuneRegion") > ### * kommuneRegion > > flush(stderr()); flush(stdout()) > > ### Name: kommuneRegion > ### Title: kommuneRegion - Converts kommune number to Region > ### Aliases: kommuneRegion > > ### ** Examples > > kommuneRegion(410) # Sydjylland [1] "Sydjylland" > > > > > cleanEx() > nameEx("lexisFromTo") > ### * lexisFromTo > > flush(stderr()); flush(stdout()) > > ### Name: lexisFromTo > ### Title: lexisFromTo > ### Aliases: lexisFromTo > > ### ** Examples > > library(data.table) Attaching package: ‘data.table’ The following object is masked from ‘package:base’: %notin% > dat <- data.table(id=c("A","A","B","B","C","D"), + start=as.Date(c(0,100,0,100,200,400),origin='1970-01-01'), + end=as.Date(c(100,200,100,200,300,500),origin='1970-01-01'), + event=c(0,1,0,0,1,1)) > split <- data.table (id=c("A","A","A","A","B","B","B","D","D"), + start=as.Date(c(0,50,25,150,110,150,400,300,500),origin='1970-01-01'), + end= as.Date(c(25,75,150,151,120,250,500,400,500),origin='1970-01-01'), + value=c(1,2,3,4,1,2,3,6,7), + name=c("d1","d1","d2","d2","d1","d1","d2","d3","d4")) > #Show the dataset: > dat[] id start end event 1: A 1970-01-01 1970-04-11 0 2: A 1970-04-11 1970-07-20 1 3: B 1970-01-01 1970-04-11 0 4: B 1970-04-11 1970-07-20 0 5: C 1970-07-20 1970-10-28 1 6: D 1971-02-05 1971-05-16 1 > split[] id start end value name 1: A 1970-01-01 1970-01-26 1 d1 2: A 1970-02-20 1970-03-17 2 d1 3: A 1970-01-26 1970-05-31 3 d2 4: A 1970-05-31 1970-06-01 4 d2 5: B 1970-04-21 1970-05-01 1 d1 6: B 1970-05-31 1970-09-08 2 d1 7: B 1971-02-05 1971-05-16 3 d2 8: D 1970-10-28 1971-02-05 6 d3 9: D 1971-05-16 1971-05-16 7 d4 > temp <- lexisFromTo(dat # inddato with id/in/out/event + ,split # Data with id and dates + ,c("id","start","end","event") #names of id/in/out/event - in that order + ,c("id","start","end","value","name")) #Nmes var date-vars to split by > > > > cleanEx() detaching ‘package:data.table’ > nameEx("lexisSeq") > ### * lexisSeq > > flush(stderr()); flush(stdout()) > > ### Name: lexisSeq > ### Title: lexisSeq > ### Aliases: lexisSeq > > ### ** Examples > > library(data.table) Attaching package: ‘data.table’ The following object is masked from ‘package:base’: %notin% > > dat <- data.table(ptid=c("A","A","B","B","C","C","D","D"), + start=as.Date(c(0,100,0,100,0,100,0,100),origin="1970-01-01"), + end=as.Date(c(100,200,100,200,100,200,100,200),origin="1970-01-01"), + dead=c(0,1,0,0,0,1,0,1), + Bdate=as.Date(c(-5000,-5000,-2000,-2000,0,0,100,100),origin="1970-01-01")) > #Example 1 - Splitting on a vector with 3 values to be added to "Bdate" > out <- lexisSeq(indat=dat,invars=c("ptid","start","end","dead"), + varname="Bdate", + splitvector=as.Date(c(0,150,5000),origin="1970-01-01"), + format="vector") > out[] start end dead value ptid Bdate 1: 1970-01-01 1970-01-01 0 2 A 1956-04-24 2: 1970-01-01 1970-04-11 0 3 A 1956-04-24 3: 1970-04-11 1970-07-20 1 3 A 1956-04-24 4: 1970-01-01 1970-04-11 0 2 B 1964-07-11 5: 1970-04-11 1970-07-20 0 2 B 1964-07-11 6: 1970-01-01 1970-01-01 0 0 C 1970-01-01 7: 1970-01-01 1970-04-11 0 1 C 1970-01-01 8: 1970-04-11 1970-05-31 0 1 C 1970-01-01 9: 1970-05-31 1970-07-20 1 2 C 1970-01-01 10: 1970-01-01 1970-04-11 0 0 D 1970-04-11 11: 1970-04-11 1970-04-11 0 0 D 1970-04-11 12: 1970-04-11 1970-07-20 1 1 D 1970-04-11 > #Example 2 - splitting on a from-to-by sequence with no adding (calender time?) > out2 <- lexisSeq(indat=dat,invars=c("ptid","start","end","dead"), + varname=NULL,splitvector=c(0,200,50), + format="seq",value="myvalue") > out2[] start end dead myvalue ptid Bdate 1: 1970-01-01 1970-01-01 0 0 A 1956-04-24 2: 1970-01-01 1970-02-20 0 1 A 1956-04-24 3: 1970-02-20 1970-04-11 0 2 A 1956-04-24 4: 1970-04-11 1970-04-11 0 2 A 1956-04-24 5: 1970-04-11 1970-05-31 0 3 A 1956-04-24 6: 1970-05-31 1970-07-20 1 4 A 1956-04-24 7: 1970-01-01 1970-01-01 0 0 B 1964-07-11 8: 1970-01-01 1970-02-20 0 1 B 1964-07-11 9: 1970-02-20 1970-04-11 0 2 B 1964-07-11 10: 1970-04-11 1970-04-11 0 2 B 1964-07-11 11: 1970-04-11 1970-05-31 0 3 B 1964-07-11 12: 1970-05-31 1970-07-20 0 4 B 1964-07-11 13: 1970-01-01 1970-01-01 0 0 C 1970-01-01 14: 1970-01-01 1970-02-20 0 1 C 1970-01-01 15: 1970-02-20 1970-04-11 0 2 C 1970-01-01 16: 1970-04-11 1970-04-11 0 2 C 1970-01-01 17: 1970-04-11 1970-05-31 0 3 C 1970-01-01 18: 1970-05-31 1970-07-20 1 4 C 1970-01-01 19: 1970-01-01 1970-01-01 0 0 D 1970-04-11 20: 1970-01-01 1970-02-20 0 1 D 1970-04-11 21: 1970-02-20 1970-04-11 0 2 D 1970-04-11 22: 1970-04-11 1970-04-11 0 2 D 1970-04-11 23: 1970-04-11 1970-05-31 0 3 D 1970-04-11 24: 1970-05-31 1970-07-20 1 4 D 1970-04-11 start end dead myvalue ptid Bdate > > > > cleanEx() detaching ‘package:data.table’ > nameEx("lexisTwo") > ### * lexisTwo > > flush(stderr()); flush(stdout()) > > ### Name: lexisTwo > ### Title: lexisTwo > ### Aliases: lexisTwo > > ### ** Examples > > library(data.table) Attaching package: ‘data.table’ The following object is masked from ‘package:base’: %notin% > > dat <- data.table(pnr=c("123456","123456","234567","234567","345678","345678" + ,"456789","456789"), + start=as.Date(c(0,100,0,100,0,100,0,100),origin="1970-01-01"), + end=as.Date(c(100,200,100,200,100,200,100,200),origin="1970-01-01"), + event=as.integer(c(0,1,0,0,0,1,0,1))) > > split <- data.table (pnr=c("123456","234567","345678","456789"), + como1.onset=as.integer(c(0,NA,49,50)), como2.onset=as.integer(c(25,75,49,49)), + como3.onset=as.integer(c(30,NA,49,48)), como4.onset=as.integer(c(50,49,49,47))) > #Show the datasets: > dat[] pnr start end event 1: 123456 1970-01-01 1970-04-11 0 2: 123456 1970-04-11 1970-07-20 1 3: 234567 1970-01-01 1970-04-11 0 4: 234567 1970-04-11 1970-07-20 0 5: 345678 1970-01-01 1970-04-11 0 6: 345678 1970-04-11 1970-07-20 1 7: 456789 1970-01-01 1970-04-11 0 8: 456789 1970-04-11 1970-07-20 1 > split[] pnr como1.onset como2.onset como3.onset como4.onset 1: 123456 0 25 30 50 2: 234567 NA 75 NA 49 3: 345678 49 49 49 49 4: 456789 50 49 48 47 > lexisTwo(dat # inddato with id/in/out/event + ,split # Data with id and dates + ,c("pnr","start","end","event") #names of id/in/out/event - in that order + ,c("pnr","como1.onset","como2.onset","como3.onset","como4.onset")) start end event como1.onset como2.onset como3.onset como4.onset 1: 1970-01-01 1970-01-26 0 1 0 0 0 2: 1970-01-26 1970-01-31 0 1 1 0 0 3: 1970-01-31 1970-02-20 0 1 1 1 0 4: 1970-02-20 1970-04-11 0 1 1 1 1 5: 1970-04-11 1970-07-20 1 1 1 1 1 6: 1970-01-01 1970-02-19 0 0 0 0 0 7: 1970-02-19 1970-03-17 0 0 0 0 1 8: 1970-03-17 1970-04-11 0 0 1 0 1 9: 1970-04-11 1970-07-20 0 0 1 0 1 10: 1970-01-01 1970-02-19 0 0 0 0 0 11: 1970-02-19 1970-04-11 0 1 1 1 1 12: 1970-04-11 1970-07-20 1 1 1 1 1 13: 1970-01-01 1970-02-17 0 0 0 0 0 14: 1970-02-17 1970-02-18 0 0 0 0 1 15: 1970-02-18 1970-02-19 0 0 0 1 1 16: 1970-02-19 1970-02-20 0 0 1 1 1 17: 1970-02-20 1970-04-11 0 1 1 1 1 18: 1970-04-11 1970-07-20 1 1 1 1 1 pnr 1: 123456 2: 123456 3: 123456 4: 123456 5: 123456 6: 234567 7: 234567 8: 234567 9: 234567 10: 345678 11: 345678 12: 345678 13: 456789 14: 456789 15: 456789 16: 456789 17: 456789 18: 456789 > #Names of date-vars to split by > # And with splittingguide in long format > splitvars <- c("como1.onset","como2.onset","como3.onset","como4.onset") > split <- data.table::melt(data=split,id.vars="pnr",measure.vars=splitvars, + variable.name="name",value.name="value") > split[,value:=as.Date(value,origin="1970-01-01")] > split[] pnr name value 1: 123456 como1.onset 1970-01-01 2: 234567 como1.onset 3: 345678 como1.onset 1970-02-19 4: 456789 como1.onset 1970-02-20 5: 123456 como2.onset 1970-01-26 6: 234567 como2.onset 1970-03-17 7: 345678 como2.onset 1970-02-19 8: 456789 como2.onset 1970-02-19 9: 123456 como3.onset 1970-01-31 10: 234567 como3.onset 11: 345678 como3.onset 1970-02-19 12: 456789 como3.onset 1970-02-18 13: 123456 como4.onset 1970-02-20 14: 234567 como4.onset 1970-02-19 15: 345678 como4.onset 1970-02-19 16: 456789 como4.onset 1970-02-17 > split <- split[!is.na(value)] # remove missing values > lexisTwo(dat # in-data with id/in/out/event + ,split # Data with id/name/date + ,c("pnr","start","end","event") #names of id/in/out/event - in that order + ,c("pnr","name","value") + ,format="long") start end event como1.onset como2.onset como3.onset como4.onset 1: 1970-01-01 1970-01-26 0 1 0 0 0 2: 1970-01-26 1970-01-31 0 1 1 0 0 3: 1970-01-31 1970-02-20 0 1 1 1 0 4: 1970-02-20 1970-04-11 0 1 1 1 1 5: 1970-04-11 1970-07-20 1 1 1 1 1 6: 1970-01-01 1970-02-19 0 0 0 0 0 7: 1970-02-19 1970-03-17 0 0 0 0 1 8: 1970-03-17 1970-04-11 0 0 1 0 1 9: 1970-04-11 1970-07-20 0 0 1 0 1 10: 1970-01-01 1970-02-19 0 0 0 0 0 11: 1970-02-19 1970-04-11 0 1 1 1 1 12: 1970-04-11 1970-07-20 1 1 1 1 1 13: 1970-01-01 1970-02-17 0 0 0 0 0 14: 1970-02-17 1970-02-18 0 0 0 0 1 15: 1970-02-18 1970-02-19 0 0 0 1 1 16: 1970-02-19 1970-02-20 0 0 1 1 1 17: 1970-02-20 1970-04-11 0 1 1 1 1 18: 1970-04-11 1970-07-20 1 1 1 1 1 pnr 1: 123456 2: 123456 3: 123456 4: 123456 5: 123456 6: 234567 7: 234567 8: 234567 9: 234567 10: 345678 11: 345678 12: 345678 13: 456789 14: 456789 15: 456789 16: 456789 17: 456789 18: 456789 > > > > cleanEx() detaching ‘package:data.table’ > nameEx("listRawdata") > ### * listRawdata > > flush(stderr()); flush(stdout()) > > ### Name: listRawdata > ### Title: List sas data files > ### Aliases: listRawdata > > ### ** Examples > > rawdir <- tempfile("heaven-rawdata-") > dir.create(rawdir) > file.create(file.path(rawdir, "pop.sas7bdat")) [1] TRUE > listRawdata(rawdir) [1] "pop.sas7bdat" > unlink(rawdir, recursive = TRUE) > > > > cleanEx() > nameEx("matchReport") > ### * matchReport > > flush(stderr()); flush(stdout()) > > ### Name: matchReport > ### Title: matchReport - report of matching from incidenceMatch and > ### exposureMatch > ### Aliases: matchReport > > ### ** Examples > > require(data.table) Loading required package: data.table Attaching package: ‘data.table’ The following object is masked from ‘package:base’: %notin% > case <- c(rep(0,40),rep(1,15)) > ptid <- paste0("P",1:55) > sex <- c(rep("fem",20),rep("mal",20),rep("fem",8),rep("mal",7)) > byear <- c(rep(c(2020,2030),20),rep(2020,7),rep(2030,8)) > caseIndex <- c(seq(1,40,1),seq(5,47,3)) > controlIndex <- caseIndex > library(data.table) > dat <- data.table(ptid,case,sex,byear,caseIndex,controlIndex) > # Very simple match without reuse - no dates to control for > matched.data <- incidenceMatch(ptid="ptid",event="case",terms=c("byear","sex"), + data=dat,n.controls=2, + case.index="caseIndex",end.followup="controlIndex") Matching terms define 4 subsets | | | 0% | |'''''' | 31% | |'''''''''' | 51% | |''''''''''''''''''''| 100% > matchReport(matched.data) Matching success Number of controls found / number of cases: n.controls 0 1 2 3 2 10 Reuse/use of controls Number of times selected / number of subjects: n.reuse 1 2 12 5 > > > > cleanEx() detaching ‘package:data.table’ > nameEx("medicinMacro") > ### * medicinMacro > > flush(stderr()); flush(stdout()) > > ### Name: medicinMacro > ### Title: Medicin macro to estimate prescription lengths and averages > ### Aliases: medicinMacro > > ### ** Examples > > set.seed(05021992) > library(data.table) Attaching package: ‘data.table’ The following object is masked from ‘package:base’: %notin% > N=20 > packs = list("R03AK11"=list(c(750,75),c(500,200),c(400,200)), + "R03AL03"=list(c(750,75),c(500,200),c(400,200)), + "C01CA01"=list(c(200,100),c(750,30))) > lmdb=simPrescriptionData(N,packages=packs) > lpr=simAdmissionData(N) > lpr <- getAdmLimits(lpr,collapse=TRUE) No data errors. Nothing to collapse. > drug1 = list(atc=c("R03AK11","R03AL03","R03AC02","R03AC04","R03AC19", + "R03AL02","R03AA01","R03AC18","R03AL01"), + maxdepot=4000, + period=as.Date(c("1995-01-01", "2012-12-31")), + prescriptionwindow=2, + doses=list(value=c(750,500,400,200,75), + min = c(250,200,200,100,25), + max = c(1000,600,800,600,100), + def = c(750,500,400,200,75))) > drug2=list(atc=c("C01CA01","C01AA05"), + maxdepot=4000, + period=as.Date(c("1995-01-01", "2012-12-31")), + prescriptionwindow=2, + doses=list(value=c(200, 400, 500,750), + min = c(100, 200, 250,750), + max = c(300, 800, 1000,750), + def = c(200, 400, 500,750))) > x=medicinMacro(drugs=list("drug1"=drug1,"drug2"=drug2),drugdb=lmdb,admdb=lpr) > x$drug1 Key: pnr dose firstday lastday exposure.days 1: 1 75 1995-01-01 1995-05-18 137 days 2: 1 100 1995-05-18 2008-06-15 4777 days 3: 2 75 1995-01-01 1995-02-16 46 days 4: 2 100 1995-02-16 2002-05-22 2652 days 5: 3 75 1995-01-01 1995-01-22 21 days 6: 3 100 1995-01-22 2003-02-24 2955 days 7: 4 75 1995-01-01 1995-03-03 61 days 8: 4 100 1995-03-03 2001-08-15 2357 days 9: 5 75 1995-01-01 1995-01-10 9 days 10: 5 100 1995-01-10 1996-03-08 423 days 11: 5 0 1996-03-08 1996-03-24 16 days 12: 5 100 1996-03-24 2001-02-12 1786 days 13: 6 75 1995-01-01 1995-01-20 19 days 14: 6 100 1995-01-20 2000-04-14 1911 days 15: 7 75 1995-01-01 1995-03-18 76 days 16: 7 100 1995-03-18 2002-09-13 2736 days 17: 8 75 1995-01-01 1995-02-04 34 days 18: 8 100 1995-02-04 2005-09-06 3867 days 19: 9 75 1995-01-01 1995-01-21 20 days 20: 9 100 1995-01-21 2004-02-05 3302 days 21: 10 75 1995-01-01 1995-06-27 177 days 22: 10 100 1995-06-27 2000-03-11 1719 days 23: 11 75 1995-01-01 1995-01-05 4 days 24: 11 100 1995-01-05 2004-02-12 3325 days 25: 12 75 1995-01-01 1995-03-02 60 days 26: 12 100 1995-03-02 2001-03-09 2199 days 27: 13 75 1995-01-01 1995-02-06 36 days 28: 13 100 1995-02-06 2000-05-28 1938 days 29: 14 75 1995-02-26 1995-03-14 16 days 30: 14 100 1995-03-14 1998-08-15 1250 days 31: 14 0 1998-08-15 1998-10-23 69 days 32: 14 100 1998-10-23 2001-04-24 914 days 33: 15 75 1995-01-01 1995-02-26 56 days 34: 15 100 1995-02-26 2001-12-19 2488 days 35: 16 75 1995-01-01 1995-01-06 5 days 36: 16 100 1995-01-06 2002-01-20 2571 days 37: 17 75 1995-01-01 1995-03-28 86 days 38: 17 100 1995-03-28 1995-12-22 269 days 39: 17 0 1995-12-22 1996-03-04 73 days 40: 17 100 1996-03-04 2006-04-02 3681 days 41: 18 75 1995-01-01 1995-05-05 124 days 42: 18 100 1995-05-05 2004-02-08 3201 days 43: 19 75 1995-01-01 1995-01-09 8 days 44: 19 100 1995-01-09 2008-04-15 4845 days 45: 20 75 1995-01-01 1995-05-20 139 days 46: 20 100 1995-05-20 2000-09-18 1948 days pnr dose firstday lastday exposure.days > > ## without admdata > x1=medicinMacro(drugs=list("drug1"=drug1,"drug2"=drug2),drugdb=lmdb,admdb=NULL) innerMedicinMacro.cpp:124:37: runtime error: -1 is outside the range of representable values of type 'unsigned int' #0 0x7f8ce45c111c in innerMedicinMacro(Rcpp::DataFrame_Impl, Rcpp::DataFrame_Impl, Rcpp::Vector<19, Rcpp::PreserveStorage>, Rcpp::Vector<14, Rcpp::PreserveStorage>, Rcpp::DataFrame_Impl, double, double, double) /data/localhost/ripley/R/packages/tests-clang-UBSAN/heaven/src/innerMedicinMacro.cpp:124:37 #1 0x7f8ce45a1bf5 in _heaven_innerMedicinMacro /data/localhost/ripley/R/packages/tests-clang-UBSAN/heaven/src/RcppExports.cpp:79:34 #2 0x56261efa774b in R_doDotCall (/data/localhost/ripley/R/R-clang/bin/exec/R+0x9274b) #3 0x56261eff274c in bcEval_loop eval.c #4 0x56261efe043b in bcEval eval.c #5 0x56261efdfbf4 in Rf_eval (/data/localhost/ripley/R/R-clang/bin/exec/R+0xcabf4) #6 0x56261eff8278 in R_execClosure eval.c #7 0x56261eff774a in applyClosure_core eval.c #8 0x56261efe0b06 in Rf_applyClosure (/data/localhost/ripley/R/R-clang/bin/exec/R+0xcbb06) #9 0x56261efe0047 in Rf_eval (/data/localhost/ripley/R/R-clang/bin/exec/R+0xcb047) #10 0x56261effd5d7 in do_set (/data/localhost/ripley/R/R-clang/bin/exec/R+0xe85d7) #11 0x56261efdfe1f in Rf_eval (/data/localhost/ripley/R/R-clang/bin/exec/R+0xcae1f) #12 0x56261f02dc37 in Rf_ReplIteration (/data/localhost/ripley/R/R-clang/bin/exec/R+0x118c37) #13 0x56261f02f72e in run_Rmainloop (/data/localhost/ripley/R/R-clang/bin/exec/R+0x11a72e) #14 0x56261f02f79a in Rf_mainloop (/data/localhost/ripley/R/R-clang/bin/exec/R+0x11a79a) #15 0x56261ef16dc7 in main (/data/localhost/ripley/R/R-clang/bin/exec/R+0x1dc7) #16 0x7f8cebf0b680 in __libc_start_call_main (/lib64/libc.so.6+0x3680) (BuildId: 85075e2ae2e13352a1413d41c327dacf6461526c) #17 0x7f8cebf0b797 in __libc_start_main@GLIBC_2.2.5 (/lib64/libc.so.6+0x3797) (BuildId: 85075e2ae2e13352a1413d41c327dacf6461526c) #18 0x56261ef16ce4 in _start (/data/localhost/ripley/R/R-clang/bin/exec/R+0x1ce4) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior innerMedicinMacro.cpp:124:37 innerMedicinMacro.cpp:124:50: runtime error: -2 is outside the range of representable values of type 'unsigned int' #0 0x7f8ce45c1143 in innerMedicinMacro(Rcpp::DataFrame_Impl, Rcpp::DataFrame_Impl, Rcpp::Vector<19, Rcpp::PreserveStorage>, Rcpp::Vector<14, Rcpp::PreserveStorage>, Rcpp::DataFrame_Impl, double, double, double) /data/localhost/ripley/R/packages/tests-clang-UBSAN/heaven/src/innerMedicinMacro.cpp:124:50 #1 0x7f8ce45a1bf5 in _heaven_innerMedicinMacro /data/localhost/ripley/R/packages/tests-clang-UBSAN/heaven/src/RcppExports.cpp:79:34 #2 0x56261efa774b in R_doDotCall (/data/localhost/ripley/R/R-clang/bin/exec/R+0x9274b) #3 0x56261eff274c in bcEval_loop eval.c #4 0x56261efe043b in bcEval eval.c #5 0x56261efdfbf4 in Rf_eval (/data/localhost/ripley/R/R-clang/bin/exec/R+0xcabf4) #6 0x56261eff8278 in R_execClosure eval.c #7 0x56261eff774a in applyClosure_core eval.c #8 0x56261efe0b06 in Rf_applyClosure (/data/localhost/ripley/R/R-clang/bin/exec/R+0xcbb06) #9 0x56261efe0047 in Rf_eval (/data/localhost/ripley/R/R-clang/bin/exec/R+0xcb047) #10 0x56261effd5d7 in do_set (/data/localhost/ripley/R/R-clang/bin/exec/R+0xe85d7) #11 0x56261efdfe1f in Rf_eval (/data/localhost/ripley/R/R-clang/bin/exec/R+0xcae1f) #12 0x56261f02dc37 in Rf_ReplIteration (/data/localhost/ripley/R/R-clang/bin/exec/R+0x118c37) #13 0x56261f02f72e in run_Rmainloop (/data/localhost/ripley/R/R-clang/bin/exec/R+0x11a72e) #14 0x56261f02f79a in Rf_mainloop (/data/localhost/ripley/R/R-clang/bin/exec/R+0x11a79a) #15 0x56261ef16dc7 in main (/data/localhost/ripley/R/R-clang/bin/exec/R+0x1dc7) #16 0x7f8cebf0b680 in __libc_start_call_main (/lib64/libc.so.6+0x3680) (BuildId: 85075e2ae2e13352a1413d41c327dacf6461526c) #17 0x7f8cebf0b797 in __libc_start_main@GLIBC_2.2.5 (/lib64/libc.so.6+0x3797) (BuildId: 85075e2ae2e13352a1413d41c327dacf6461526c) #18 0x56261ef16ce4 in _start (/data/localhost/ripley/R/R-clang/bin/exec/R+0x1ce4) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior innerMedicinMacro.cpp:124:50 > x1$drug1 Key: pnr dose firstday lastday exposure.days 1: 1 75 1995-01-01 1995-05-18 137 days 2: 1 100 1995-05-18 2008-03-29 4699 days 3: 2 75 1995-01-01 1995-02-16 46 days 4: 2 100 1995-02-16 2002-05-09 2639 days 5: 3 75 1995-01-01 1995-01-22 21 days 6: 3 100 1995-01-22 2003-02-24 2955 days 7: 4 75 1995-01-01 1995-03-03 61 days 8: 4 100 1995-03-03 2001-08-15 2357 days 9: 5 75 1995-01-01 1995-01-10 9 days 10: 5 100 1995-01-10 1996-03-08 423 days 11: 5 0 1996-03-08 1996-03-24 16 days 12: 5 100 1996-03-24 1996-10-08 198 days 13: 5 75 1996-10-08 1997-04-05 179 days 14: 5 100 1997-04-05 2001-02-12 1409 days 15: 6 75 1995-01-01 1995-01-20 19 days 16: 6 100 1995-01-20 2000-04-14 1911 days 17: 7 75 1995-01-01 1995-03-18 76 days 18: 7 100 1995-03-18 2002-09-13 2736 days 19: 8 75 1995-01-01 1995-02-04 34 days 20: 8 100 1995-02-04 2005-07-19 3818 days 21: 9 75 1995-01-01 1995-01-21 20 days 22: 9 100 1995-01-21 2003-12-17 3252 days 23: 10 75 1995-01-01 1995-06-27 177 days 24: 10 100 1995-06-27 2000-03-11 1719 days 25: 11 75 1995-01-01 1995-01-05 4 days 26: 11 100 1995-01-05 2004-01-14 3296 days 27: 12 75 1995-01-01 1995-03-02 60 days 28: 12 100 1995-03-02 2001-03-09 2199 days 29: 13 75 1995-01-01 1995-02-06 36 days 30: 13 100 1995-02-06 2000-05-28 1938 days 31: 14 75 1995-02-26 1995-03-14 16 days 32: 14 100 1995-03-14 1998-08-15 1250 days 33: 14 0 1998-08-15 1998-10-23 69 days 34: 14 100 1998-10-23 2001-04-24 914 days 35: 15 75 1995-01-01 1995-02-26 56 days 36: 15 100 1995-02-26 2001-12-19 2488 days 37: 16 75 1995-01-01 1995-01-06 5 days 38: 16 100 1995-01-06 1995-08-11 217 days 39: 16 0 1995-08-11 1995-09-11 31 days 40: 16 100 1995-09-11 2002-01-20 2323 days 41: 17 75 1995-01-01 1995-03-28 86 days 42: 17 100 1995-03-28 1995-12-22 269 days 43: 17 0 1995-12-22 1996-03-04 73 days 44: 17 100 1996-03-04 2006-04-02 3681 days 45: 18 75 1995-01-01 1995-05-05 124 days 46: 18 100 1995-05-05 2004-01-04 3166 days 47: 19 75 1995-01-01 1995-01-09 8 days 48: 19 100 1995-01-09 2008-03-28 4827 days 49: 20 75 1995-01-01 1995-05-20 139 days 50: 20 100 1995-05-20 2000-09-18 1948 days pnr dose firstday lastday exposure.days > > ## Examples with other types > ## NB: Calculations with maxdepot>0 is not implemented yet for type different from "dynamic". > ## Not specifying maxdepot is the same at setting maxdepot=0. > library(data.table) > drug1 = list(atc=c("R03AK11","R03AL03","R03AC02","R03AC04","R03AC19", + "R03AL02","R03AA01","R03AC18","R03AL01"), + doses=list(value=c(750,500,400,200,75), + min = c(250,200,200,100,25), + max = c(1000,600,800,600,100), + def = c(750,500,400,200,75))) > drug2=list(atc=c("C01CA01","C01AA05"), + doses=list(value=c(200, 400, 500,750), + min = c(100, 200, 250,750), + max = c(300, 800, 1000,750), + def = c(200, 400, 500,750))) > ## Note that we define the periods separately, as they are now individual: > ind.periods <- data.table(pnr=1:N, + start=rep(as.Date("1996-01-01"), N)+floor(rnorm(N,sd=100)), + end=rep(as.Date("1998-05-30"), N)+floor(rnorm(N,sd=100))) > z=medicinMacro(drugs=list("drug1"=drug1,"drug2"=drug2),drugdb=lmdb,admdb=lpr, + type="period",datesdb=ind.periods) > z$drug1 Key: pnr eksd eksd.end total.drug average.drug start end 1: 1 1995-11-04 1996-01-17 417750 750 1995-11-04 1997-11-03 2: 1 1996-01-17 1996-01-19 20000 200 1995-11-04 1997-11-03 3: 1 1996-01-19 1996-02-05 240000 400 1995-11-04 1997-11-03 4: 1 1996-02-05 1996-03-22 80000 400 1995-11-04 1997-11-03 5: 1 1996-03-22 1996-03-29 75000 750 1995-11-04 1997-11-03 --- 365: 20 1997-02-23 1997-05-25 168750 750 1995-09-13 1998-01-17 366: 20 1997-05-25 1997-06-27 300000 750 1995-09-13 1998-01-17 367: 20 1997-06-27 1997-07-29 67500 750 1995-09-13 1998-01-17 368: 20 1997-07-29 1997-09-29 160000 400 1995-09-13 1998-01-17 369: 20 1997-09-29 1998-01-17 450000 750 1995-09-13 1998-01-17 days.in.period h.days non.h.days hospitalized.at.end 1: 74 days 0 74 days FALSE 2: 2 days 0 2 days FALSE 3: 17 days 0 17 days FALSE 4: 46 days 0 46 days FALSE 5: 7 days 0 7 days FALSE --- 365: 91 days 0 91 days FALSE 366: 33 days 0 33 days FALSE 367: 32 days 0 32 days FALSE 368: 62 days 0 62 days FALSE 369: 110 days 0 110 days FALSE drug.strength.estimate min.dose max.dose def.dose estimated.daily.dose 1: 750 250 1000 750 750 2: 200 100 600 200 200 3: 400 200 800 400 400 4: 400 200 800 400 400 5: 750 250 1000 750 750 --- 365: 750 250 1000 750 750 366: 750 250 1000 750 750 367: 750 250 1000 750 750 368: 400 200 800 400 400 369: 750 250 1000 750 750 drug.supply.days leftover prop.covered.period prop.covered.total 1: 557 362250 1 1.0000000 2: 100 19600 1 1.0000000 3: 600 233200 1 1.0000000 4: 200 61600 1 1.0000000 5: 100 69750 1 1.0000000 --- 365: 225 100500 1 0.9859977 366: 400 275250 1 0.9859977 367: 90 43500 1 0.9859977 368: 400 135200 1 0.9859977 369: 600 367500 1 0.9859977 > ## Using different method: > z=medicinMacro(drugs=list("drug1"=drug1,"drug2"=drug2),drugdb=lmdb,admdb=lpr, + type="period",datesdb=ind.periods, method="number.of.days") > z$drug1 Key: pnr eksd eksd.end total.drug average.drug start end 1: 1 1995-11-04 1996-01-17 407000 750 1995-11-04 1997-11-03 2: 1 1996-01-17 1996-01-19 20000 200 1995-11-04 1997-11-03 3: 1 1996-01-19 1996-02-05 240000 400 1995-11-04 1997-11-03 4: 1 1996-02-05 1996-03-22 80000 400 1995-11-04 1997-11-03 5: 1 1996-03-22 1996-03-29 75000 750 1995-11-04 1997-11-03 --- 365: 20 1997-02-23 1997-05-25 168750 750 1995-09-13 1998-01-17 366: 20 1997-05-25 1997-06-27 300000 750 1995-09-13 1998-01-17 367: 20 1997-06-27 1997-07-29 67500 750 1995-09-13 1998-01-17 368: 20 1997-07-29 1997-09-29 160000 400 1995-09-13 1998-01-17 369: 20 1997-09-29 1998-01-17 450000 750 1995-09-13 1998-01-17 days.in.period h.days non.h.days hospitalized.at.end 1: 74 days 0 74 days FALSE 2: 2 days 0 2 days FALSE 3: 17 days 0 17 days FALSE 4: 46 days 0 46 days FALSE 5: 7 days 0 7 days FALSE --- 365: 91 days 0 91 days FALSE 366: 33 days 0 33 days FALSE 367: 32 days 0 32 days FALSE 368: 62 days 0 62 days FALSE 369: 110 days 0 110 days FALSE drug.strength.estimate min.dose max.dose def.dose estimated.daily.dose 1: 750 250 1000 750 1000 2: 200 100 600 200 600 3: 400 200 800 400 800 4: 400 200 800 400 800 5: 750 250 1000 750 1000 --- 365: 750 250 1000 750 1000 366: 750 250 1000 750 1000 367: 750 250 1000 750 1000 368: 400 200 800 400 800 369: 750 250 1000 750 800 drug.supply.days leftover prop.covered.period prop.covered.total 1: 407.00000 333000 1 1 2: 33.33333 18800 1 1 3: 300.00000 226400 1 1 4: 100.00000 43200 1 1 5: 75.00000 68000 1 1 --- 365: 168.75000 77750 1 1 366: 300.00000 267000 1 1 367: 67.50000 35500 1 1 368: 200.00000 110400 1 1 369: 562.50000 362000 1 1 > > ## Using type = "cross-sectional" to check if individuals are exposed at a given time > ## Note that we now only give one timepoint but then also need to supply the window argument > ind.periods2 <- copy(ind.periods) > ind.periods2[,time:=start][,c("start","end"):=NULL] > y=medicinMacro(drugs=list("drug1"=drug1,"drug2"=drug2),drugdb=lmdb,admdb=lpr, + type="cross-sectional",datesdb=ind.periods2, window=365.25) > y$drug1 Key: Index: pnr time last.potential.purch total.drug average.drug 1: 1 1995-11-04 1995-09-22 450000 750 2: 2 1995-07-01 1995-06-03 300000 500 3: 3 1995-09-08 1995-07-22 150000 750 4: 4 1996-01-01 1995-12-15 100000 500 5: 5 1995-08-31 1995-05-28 150000 750 6: 6 1996-03-18 1996-03-09 18000 200 7: 7 1995-12-29 1995-10-13 200000 500 8: 8 1996-10-26 1996-09-11 450000 750 9: 9 1996-10-08 1996-09-12 450000 750 10: 10 1995-11-13 1995-06-27 150000 750 11: 11 1996-02-25 1996-02-10 75000 750 12: 12 1996-02-13 1996-02-03 12000 200 13: 13 1996-01-14 1995-09-25 18000 200 14: 14 1996-03-03 1995-12-29 240000 400 15: 15 1996-05-23 1996-05-16 60000 200 16: 16 1995-07-14 1995-03-04 12000 200 17: 17 1995-10-15 1995-05-16 18000 200 18: 18 1995-10-30 1995-10-25 160000 400 19: 19 1995-11-13 1995-07-29 45000 750 20: 20 1995-09-13 1995-09-09 56250 750 pnr time last.potential.purch total.drug average.drug drug.strength.estimate min.dose max.dose def.dose estimated.daily.dose 1: 750 250 1000 750 750 2: 500 200 600 500 500 3: 750 250 1000 750 750 4: 500 200 600 500 500 5: 750 250 1000 750 750 6: 200 100 600 200 200 7: 500 200 600 500 500 8: 750 250 1000 750 750 9: 750 250 1000 750 750 10: 750 250 1000 750 750 11: 750 250 1000 750 750 12: 200 100 600 200 200 13: 200 100 600 200 200 14: 400 200 800 400 400 15: 200 100 600 200 200 16: 200 100 600 200 200 17: 200 100 600 200 200 18: 400 200 800 400 400 19: 750 250 1000 750 750 20: 750 250 1000 750 750 drug.strength.estimate min.dose max.dose def.dose estimated.daily.dose drug.supply.days hospitalized.at.time h.days.since.potential.purch 1: 600 FALSE 0 2: 600 FALSE 0 3: 200 FALSE 0 4: 200 FALSE 0 5: 200 FALSE 0 6: 90 FALSE 0 7: 400 FALSE 0 8: 600 FALSE 0 9: 600 FALSE 0 10: 200 FALSE 0 11: 100 FALSE 0 12: 60 FALSE 0 13: 90 FALSE 0 14: 600 FALSE 0 15: 300 FALSE 0 16: 60 FALSE 0 17: 90 FALSE 0 18: 400 FALSE 0 19: 60 FALSE 0 20: 75 FALSE 0 drug.supply.days hospitalized.at.time h.days.since.potential.purch exposed.at.time 1: TRUE 2: TRUE 3: TRUE 4: TRUE 5: TRUE 6: TRUE 7: TRUE 8: TRUE 9: TRUE 10: TRUE 11: TRUE 12: TRUE 13: FALSE 14: TRUE 15: TRUE 16: FALSE 17: FALSE 18: TRUE 19: FALSE 20: TRUE exposed.at.time > ## Changing the window size of course affects our estimates: > y=medicinMacro(drugs=list("drug1"=drug1,"drug2"=drug2),drugdb=lmdb,admdb=lpr, + type="cross-sectional",datesdb=ind.periods2, window=10) > y$drug1 Key: Index: pnr time last.potential.purch total.drug average.drug 1: 1 1995-11-04 1995-10-25 0 0 2: 2 1995-07-01 1995-06-21 0 0 3: 3 1995-09-08 1995-08-29 0 0 4: 4 1996-01-01 1995-12-22 0 0 5: 5 1995-08-31 1995-08-21 0 0 6: 6 1996-03-18 1996-03-09 18000 200 7: 7 1995-12-29 1995-12-19 0 0 8: 8 1996-10-26 1996-10-16 0 0 9: 9 1996-10-08 1996-09-28 0 0 10: 10 1995-11-13 1995-11-03 0 0 11: 11 1996-02-25 1996-02-15 0 0 12: 12 1996-02-13 1996-02-03 12000 200 13: 13 1996-01-14 1996-01-04 0 0 14: 14 1996-03-03 1996-02-22 0 0 15: 15 1996-05-23 1996-05-16 60000 200 16: 16 1995-07-14 1995-07-04 0 0 17: 17 1995-10-15 1995-10-05 0 0 18: 18 1995-10-30 1995-10-25 160000 400 19: 19 1995-11-13 1995-11-03 0 0 20: 20 1995-09-13 1995-09-09 56250 750 pnr time last.potential.purch total.drug average.drug drug.strength.estimate min.dose max.dose def.dose estimated.daily.dose 1: 0 0 0 0 0 2: 0 0 0 0 0 3: 0 0 0 0 0 4: 0 0 0 0 0 5: 0 0 0 0 0 6: 200 100 600 200 200 7: 0 0 0 0 0 8: 0 0 0 0 0 9: 0 0 0 0 0 10: 0 0 0 0 0 11: 0 0 0 0 0 12: 200 100 600 200 200 13: 0 0 0 0 0 14: 0 0 0 0 0 15: 200 100 600 200 200 16: 0 0 0 0 0 17: 0 0 0 0 0 18: 400 200 800 400 400 19: 0 0 0 0 0 20: 750 250 1000 750 750 drug.strength.estimate min.dose max.dose def.dose estimated.daily.dose drug.supply.days hospitalized.at.time h.days.since.potential.purch 1: 0 FALSE 0 2: 0 FALSE 0 3: 0 FALSE 0 4: 0 FALSE 0 5: 0 FALSE 0 6: 90 FALSE 0 7: 0 FALSE 0 8: 0 FALSE 0 9: 0 FALSE 0 10: 0 FALSE 0 11: 0 FALSE 0 12: 60 FALSE 0 13: 0 FALSE 0 14: 0 FALSE 0 15: 300 FALSE 0 16: 0 FALSE 0 17: 0 FALSE 0 18: 400 FALSE 0 19: 0 FALSE 0 20: 75 FALSE 0 drug.supply.days hospitalized.at.time h.days.since.potential.purch exposed.at.time 1: FALSE 2: FALSE 3: FALSE 4: FALSE 5: FALSE 6: TRUE 7: FALSE 8: FALSE 9: FALSE 10: FALSE 11: FALSE 12: TRUE 13: FALSE 14: FALSE 15: TRUE 16: FALSE 17: FALSE 18: TRUE 19: FALSE 20: TRUE exposed.at.time > ## Above, we renamed the date variables before plugging it in, but we could also have used the > ## datesdb.datevars argument: > y=medicinMacro(drugs=list("drug1"=drug1,"drug2"=drug2),drugdb=lmdb,admdb=lpr, + type="cross-sectional",datesdb=ind.periods, window=365.25, + datesdb.datevars="start") > y$drug1 Key: Index: pnr time last.potential.purch total.drug average.drug 1: 1 1995-11-04 1995-09-22 450000 750 2: 2 1995-07-01 1995-06-03 300000 500 3: 3 1995-09-08 1995-07-22 150000 750 4: 4 1996-01-01 1995-12-15 100000 500 5: 5 1995-08-31 1995-05-28 150000 750 6: 6 1996-03-18 1996-03-09 18000 200 7: 7 1995-12-29 1995-10-13 200000 500 8: 8 1996-10-26 1996-09-11 450000 750 9: 9 1996-10-08 1996-09-12 450000 750 10: 10 1995-11-13 1995-06-27 150000 750 11: 11 1996-02-25 1996-02-10 75000 750 12: 12 1996-02-13 1996-02-03 12000 200 13: 13 1996-01-14 1995-09-25 18000 200 14: 14 1996-03-03 1995-12-29 240000 400 15: 15 1996-05-23 1996-05-16 60000 200 16: 16 1995-07-14 1995-03-04 12000 200 17: 17 1995-10-15 1995-05-16 18000 200 18: 18 1995-10-30 1995-10-25 160000 400 19: 19 1995-11-13 1995-07-29 45000 750 20: 20 1995-09-13 1995-09-09 56250 750 pnr time last.potential.purch total.drug average.drug drug.strength.estimate min.dose max.dose def.dose estimated.daily.dose 1: 750 250 1000 750 750 2: 500 200 600 500 500 3: 750 250 1000 750 750 4: 500 200 600 500 500 5: 750 250 1000 750 750 6: 200 100 600 200 200 7: 500 200 600 500 500 8: 750 250 1000 750 750 9: 750 250 1000 750 750 10: 750 250 1000 750 750 11: 750 250 1000 750 750 12: 200 100 600 200 200 13: 200 100 600 200 200 14: 400 200 800 400 400 15: 200 100 600 200 200 16: 200 100 600 200 200 17: 200 100 600 200 200 18: 400 200 800 400 400 19: 750 250 1000 750 750 20: 750 250 1000 750 750 drug.strength.estimate min.dose max.dose def.dose estimated.daily.dose drug.supply.days hospitalized.at.time h.days.since.potential.purch 1: 600 FALSE 0 2: 600 FALSE 0 3: 200 FALSE 0 4: 200 FALSE 0 5: 200 FALSE 0 6: 90 FALSE 0 7: 400 FALSE 0 8: 600 FALSE 0 9: 600 FALSE 0 10: 200 FALSE 0 11: 100 FALSE 0 12: 60 FALSE 0 13: 90 FALSE 0 14: 600 FALSE 0 15: 300 FALSE 0 16: 60 FALSE 0 17: 90 FALSE 0 18: 400 FALSE 0 19: 60 FALSE 0 20: 75 FALSE 0 drug.supply.days hospitalized.at.time h.days.since.potential.purch exposed.at.time 1: TRUE 2: TRUE 3: TRUE 4: TRUE 5: TRUE 6: TRUE 7: TRUE 8: TRUE 9: TRUE 10: TRUE 11: TRUE 12: TRUE 13: FALSE 14: TRUE 15: TRUE 16: FALSE 17: FALSE 18: TRUE 19: FALSE 20: TRUE exposed.at.time > > > > cleanEx() detaching ‘package:data.table’ > nameEx("patientProfile") > ### * patientProfile > > flush(stderr()); flush(stdout()) > > ### Name: patientProfile > ### Title: Profile of common covariate combinations among patients > ### Aliases: patientProfile > > ### ** Examples > > n <- 1e2 > set.seed(13) > x <- data.table::data.table( + age = sample(20:35, size = n, replace = TRUE), + v2 = sample(c("yes", "no"), n, replace = TRUE), + v3 = sample(c("yes", "no"), n, replace = TRUE, prob = c(0.2, 0.8)) + ) > patientProfile(dt = x, primary.cov = "age", "v2", "v3") Key: join.id age v2 v3 count N.in.primary.cov prop.by.primary.cov 1: 20:no:no 20 no no 3 10 0.3000000 2: 20:no:yes 20 no yes 3 10 0.3000000 3: 20:yes:no 20 yes no 4 10 0.4000000 4: 20:yes:yes 20 yes yes 0 10 0.0000000 5: 21:no:no 21 no no 1 4 0.2500000 6: 21:no:yes 21 no yes 0 4 0.0000000 7: 21:yes:no 21 yes no 2 4 0.5000000 8: 21:yes:yes 21 yes yes 1 4 0.2500000 9: 22:no:no 22 no no 3 8 0.3750000 10: 22:no:yes 22 no yes 0 8 0.0000000 11: 22:yes:no 22 yes no 2 8 0.2500000 12: 22:yes:yes 22 yes yes 3 8 0.3750000 13: 23:no:no 23 no no 0 3 0.0000000 14: 23:no:yes 23 no yes 0 3 0.0000000 15: 23:yes:no 23 yes no 2 3 0.6666667 16: 23:yes:yes 23 yes yes 1 3 0.3333333 17: 24:no:no 24 no no 2 5 0.4000000 18: 24:no:yes 24 no yes 0 5 0.0000000 19: 24:yes:no 24 yes no 3 5 0.6000000 20: 24:yes:yes 24 yes yes 0 5 0.0000000 21: 25:no:no 25 no no 6 7 0.8571429 22: 25:no:yes 25 no yes 0 7 0.0000000 23: 25:yes:no 25 yes no 1 7 0.1428571 24: 25:yes:yes 25 yes yes 0 7 0.0000000 25: 26:no:no 26 no no 4 7 0.5714286 26: 26:no:yes 26 no yes 0 7 0.0000000 27: 26:yes:no 26 yes no 2 7 0.2857143 28: 26:yes:yes 26 yes yes 1 7 0.1428571 29: 27:no:no 27 no no 2 8 0.2500000 30: 27:no:yes 27 no yes 1 8 0.1250000 31: 27:yes:no 27 yes no 4 8 0.5000000 32: 27:yes:yes 27 yes yes 1 8 0.1250000 33: 28:no:no 28 no no 1 3 0.3333333 34: 28:no:yes 28 no yes 0 3 0.0000000 35: 28:yes:no 28 yes no 2 3 0.6666667 36: 28:yes:yes 28 yes yes 0 3 0.0000000 37: 29:no:no 29 no no 1 3 0.3333333 38: 29:no:yes 29 no yes 1 3 0.3333333 39: 29:yes:no 29 yes no 1 3 0.3333333 40: 29:yes:yes 29 yes yes 0 3 0.0000000 41: 30:no:no 30 no no 1 1 1.0000000 42: 30:no:yes 30 no yes 0 1 0.0000000 43: 30:yes:no 30 yes no 0 1 0.0000000 44: 30:yes:yes 30 yes yes 0 1 0.0000000 45: 31:no:no 31 no no 2 7 0.2857143 46: 31:no:yes 31 no yes 0 7 0.0000000 47: 31:yes:no 31 yes no 3 7 0.4285714 48: 31:yes:yes 31 yes yes 2 7 0.2857143 49: 32:no:no 32 no no 3 7 0.4285714 50: 32:no:yes 32 no yes 0 7 0.0000000 51: 32:yes:no 32 yes no 3 7 0.4285714 52: 32:yes:yes 32 yes yes 1 7 0.1428571 53: 33:no:no 33 no no 5 9 0.5555556 54: 33:no:yes 33 no yes 0 9 0.0000000 55: 33:yes:no 33 yes no 3 9 0.3333333 56: 33:yes:yes 33 yes yes 1 9 0.1111111 57: 34:no:no 34 no no 1 9 0.1111111 58: 34:no:yes 34 no yes 1 9 0.1111111 59: 34:yes:no 34 yes no 6 9 0.6666667 60: 34:yes:yes 34 yes yes 1 9 0.1111111 61: 35:no:no 35 no no 2 9 0.2222222 62: 35:no:yes 35 no yes 0 9 0.0000000 63: 35:yes:no 35 yes no 6 9 0.6666667 64: 35:yes:yes 35 yes yes 1 9 0.1111111 join.id age v2 v3 count N.in.primary.cov prop.by.primary.cov > > > > > cleanEx() > nameEx("sasDateRange") > ### * sasDateRange > > flush(stderr()); flush(stdout()) > > ### Name: sasDateRange > ### Title: sasDateRange > ### Aliases: sasDateRange > > ### ** Examples > > sasDateRange("inddto", as.Date("2015-01-01"), as.Date("2016-01-01")) [1] "inddto >= '01Jan2015'd AND inddto <= '01Jan2016'd" > > > > cleanEx() > nameEx("sasSubstr") > ### * sasSubstr > > flush(stderr()); flush(stdout()) > > ### Name: sasSubstr > ### Title: sasSubstr > ### Aliases: sasSubstr > > ### ** Examples > > library(heaven) > x <- c("DI05", "DI06") > sasSubstr(x, target.var="A") [1] "(substr (A, 1, 4) = 'DI05' OR substr (A, 1, 4) = 'DI06')" > > > > > > cleanEx() > nameEx("scanRawdata") > ### * scanRawdata > > flush(stderr()); flush(stdout()) > > ### Name: scanRawdata > ### Title: Scan sas data files > ### Aliases: scanRawdata > > ### ** Examples > > ## Not run: > ##D # Requires SAS and access to the registry files at the supplied path. > ##D scanRawdata("x:/Data/Rawdata_Hurtig/706818/",which=c("pop","lmdb")) > ## End(Not run) > > > > cleanEx() > nameEx("simAdmissionData") > ### * simAdmissionData > > flush(stderr()); flush(stdout()) > > ### Name: simAdmissionData > ### Title: Admission data simulation function > ### Aliases: simAdmissionData > > ### ** Examples > > ## a single subject > set.seed(1) > simAdmissionData(1) Key: pnr recnum inddto uddto indexdate diag pattype 1: 1 1 2002-06-11 2002-07-06 1999-01-13 DO472 0 > ## 7 subjects > set.seed(28) > simAdmissionData(7) Key: pnr recnum inddto uddto indexdate diag pattype 1: 1 1 1996-10-07 1996-10-28 1996-10-30 DV1180 0 2: 2 7 2006-04-17 2006-05-23 2004-03-07 DN982 0 3: 2 10 2007-04-29 2007-05-29 1998-12-08 DD484 0 4: 2 12 2008-03-18 2008-04-04 2013-09-18 DF0122 0 5: 2 13 2009-01-18 2009-02-01 1999-07-12 DN330 0 6: 2 18 2013-12-08 2013-12-30 2000-02-13 DT635 0 7: 3 6 2003-12-11 2004-01-21 2001-08-19 DK254E 3 8: 4 4 2003-06-21 2003-07-03 2009-07-07 DM92 1 9: 4 5 2003-08-25 2003-08-25 2010-04-10 DP389C 1 10: 5 3 2001-12-08 2001-12-10 1997-11-18 DQ794A 2 11: 5 8 2006-08-25 2006-09-03 2014-04-01 DE519 2 12: 5 14 2009-08-31 2009-09-17 2005-04-16 DQ728D 2 13: 5 15 2011-05-22 2011-06-10 1998-05-11 DK265D 2 14: 6 2 2001-09-11 2001-10-05 1999-02-13 DV1180 3 15: 6 16 2012-08-07 2012-08-17 2001-06-25 DQ728D 3 16: 7 9 2007-03-26 2007-05-09 2000-03-13 DD223Z 2 17: 7 11 2007-05-28 2007-07-07 2010-01-16 DE519 2 18: 7 17 2013-01-14 2013-02-04 1996-08-29 DD239E 2 > > > > > cleanEx() > nameEx("simPop") > ### * simPop > > flush(stderr()); flush(stdout()) > > ### Name: simPop > ### Title: Population simulation > ### Aliases: simPop > > ### ** Examples > > set.seed(7) > simPop(7) pnr sex birthdate status doddate 1: 1 male 1938-11-04 0 2026-11-21 2: 2 female 1995-04-12 0 2026-11-21 3: 3 female 1974-10-01 0 2026-11-21 4: 4 female 1994-11-12 0 2026-11-21 5: 5 female 1990-09-07 1 2004-05-31 6: 6 male 1952-10-16 0 2026-11-21 7: 7 female 2000-02-23 0 2026-11-21 > > > > cleanEx() > nameEx("simPrescriptionData") > ### * simPrescriptionData > > flush(stderr()); flush(stdout()) > > ### Name: simPrescriptionData > ### Title: Prescription data simulation function > ### Aliases: simPrescriptionData > > ### ** Examples > > ## see https://www.dst.dk/da/TilSalg/Forskningsservice/Data/Andre_Styrelser > ## for details of the contents of lmdb > > ## a single subject > set.seed(1) > simPrescriptionData(1) Key: pnr atc eksd strnum packsize apk 1: 1 A09AA02 1998-12-27 75 500 3 2: 1 A12AX 1998-03-18 750 250 3 3: 1 B02BC 1995-04-18 75 250 1 4: 1 C03CB02 1998-01-09 750 100 2 5: 1 D06BB12 1999-01-20 750 250 2 6: 1 J07BB 1996-05-08 75 500 1 7: 1 J07BB 1998-11-06 400 30 2 8: 1 L01AA02 1998-08-03 400 30 1 9: 1 L01AA02 1999-05-20 75 100 2 10: 1 L01AD02 1995-01-01 750 500 3 11: 1 L01AD02 1997-06-27 400 30 1 12: 1 L01AD02 1999-12-18 750 500 1 13: 1 L03AA10 1996-03-13 750 100 2 14: 1 M03AX01 1998-04-10 750 250 2 15: 1 N04BD03 1999-12-17 400 30 2 16: 1 N07BB05 1997-01-18 75 500 1 17: 1 R03DA03 1997-04-11 75 250 1 18: 1 R03DA03 1998-03-02 750 100 2 19: 1 R05CB15 1996-10-07 750 100 1 > ## 7 subjects > set.seed(29) > simPrescriptionData(7) Key: pnr atc eksd strnum packsize apk 1: 1 A06AB06 1997-03-30 750 250 2 2: 1 A09AA02 1999-04-30 75 100 3 3: 1 A09AA02 1999-06-16 750 250 2 4: 1 A12AX 1999-04-01 750 100 2 5: 1 B02BC 1998-12-10 750 250 1 --- 280: 7 N01BB51 1996-09-17 400 30 2 281: 7 R05CB15 1995-12-08 750 100 2 282: 7 R06AC01 1995-10-31 75 100 3 283: 7 R06AC01 1998-03-11 75 500 2 284: 7 R06AC01 1999-12-30 750 100 3 > > > > > cleanEx() > nameEx("splitFromTo") > ### * splitFromTo > > flush(stderr()); flush(stdout()) > > ### Name: splitFromTo > ### Title: splitFromTo > ### Aliases: splitFromTo > > ### ** Examples > > library(data.table) Attaching package: ‘data.table’ The following object is masked from ‘package:base’: %notin% > dat <- data.table(id=c("A","A","B","B","C","D"), + start=as.Date(c(0,100,0,100,200,400),origin='1970-01-01'), + end=as.Date(c(100,200,100,200,300,500),origin='1970-01-01')) > split <- data.table (id=c("A","A","A","A","B","B","B","D","D"), + start=as.Date(c(0,50,25,150,110,150,400,300,500),origin='1970-01-01'), + end= as.Date(c(25,75,150,151,120,250,500,400,500),origin='1970-01-01'), + value=c(1,2,3,4,1,2,3,6,7), + name=c("d1","d1","d2","d2","d1","d1","d2","d3","d4")) > #Show the dataset: > dat[] id start end 1: A 1970-01-01 1970-04-11 2: A 1970-04-11 1970-07-20 3: B 1970-01-01 1970-04-11 4: B 1970-04-11 1970-07-20 5: C 1970-07-20 1970-10-28 6: D 1971-02-05 1971-05-16 > split[] id start end value name 1: A 1970-01-01 1970-01-26 1 d1 2: A 1970-02-20 1970-03-17 2 d1 3: A 1970-01-26 1970-05-31 3 d2 4: A 1970-05-31 1970-06-01 4 d2 5: B 1970-04-21 1970-05-01 1 d1 6: B 1970-05-31 1970-09-08 2 d1 7: B 1971-02-05 1971-05-16 3 d2 8: D 1970-10-28 1971-02-05 6 d3 9: D 1971-05-16 1971-05-16 7 d4 > temp <- splitFromTo(dat # inddato with id/in/out/event + ,split # Data with id and dates + ,c("id","start","end") #names of id/in/out/event - in that order + ,c("id","start","end","value","name")) #Names var date-vars to split by > temp[] Key: start end d1 d2 d3 d4 id 1: 1970-01-01 1970-01-26 1 0 0 0 A 2: 1970-01-26 1970-02-20 0 3 0 0 A 3: 1970-02-20 1970-03-17 2 3 0 0 A 4: 1970-03-17 1970-04-11 0 3 0 0 A 5: 1970-04-11 1970-05-31 0 3 0 0 A 6: 1970-05-31 1970-06-01 0 4 0 0 A 7: 1970-06-01 1970-07-20 0 0 0 0 A 8: 1970-01-01 1970-04-11 0 0 0 0 B 9: 1970-04-11 1970-04-21 0 0 0 0 B 10: 1970-04-21 1970-05-01 1 0 0 0 B 11: 1970-05-01 1970-05-31 0 0 0 0 B 12: 1970-05-31 1970-07-20 2 0 0 0 B 13: 1970-07-20 1970-10-28 0 0 0 0 C 14: 1971-02-05 1971-05-16 0 0 0 0 D > > > > cleanEx() detaching ‘package:data.table’ > nameEx("splitSeq") > ### * splitSeq > > flush(stderr()); flush(stdout()) > > ### Name: splitSeq > ### Title: splitSeq > ### Aliases: splitSeq > > ### ** Examples > > library(data.table) Attaching package: ‘data.table’ The following object is masked from ‘package:base’: %notin% > > dat <- data.table(ptid=c("A","A","B","B","C","C","D","D"), + start=as.Date(c(0,100,0,100,0,100,0,100),origin="1970-01-01"), + end=as.Date(c(100,200,100,200,100,200,100,200),origin="1970-01-01"), + Bdate=as.Date(c(-5000,-5000,-2000,-2000,0,0,100,100),origin="1970-01-01")) > #Example 1 - Splitting on a vector with 3 values to be added to "Bdate" > out <- splitSeq(indat=dat,invars=c("ptid","start","end"), + varname="Bdate", + splitvector=as.Date(c(0,150,5000),origin="1970-01-01"), + format="vector") > out[] start end value ptid Bdate 1: 1970-01-01 1970-01-01 2 A 1956-04-24 2: 1970-01-01 1970-04-11 3 A 1956-04-24 3: 1970-04-11 1970-07-20 3 A 1956-04-24 4: 1970-01-01 1970-04-11 2 B 1964-07-11 5: 1970-04-11 1970-07-20 2 B 1964-07-11 6: 1970-01-01 1970-01-01 0 C 1970-01-01 7: 1970-01-01 1970-04-11 1 C 1970-01-01 8: 1970-04-11 1970-05-31 1 C 1970-01-01 9: 1970-05-31 1970-07-20 2 C 1970-01-01 10: 1970-01-01 1970-04-11 0 D 1970-04-11 11: 1970-04-11 1970-04-11 0 D 1970-04-11 12: 1970-04-11 1970-07-20 1 D 1970-04-11 > #Example 2 - splitting on a from-to-by sequence with no adding (calender time?) > out2 <- splitSeq(indat=dat,invars=c("ptid","start","end"), + varname=NULL,splitvector=c(0,200,50), + format="seq",value="myvalue") > out2[] start end myvalue ptid Bdate 1: 1970-01-01 1970-01-01 0 A 1956-04-24 2: 1970-01-01 1970-02-20 1 A 1956-04-24 3: 1970-02-20 1970-04-11 2 A 1956-04-24 4: 1970-04-11 1970-04-11 2 A 1956-04-24 5: 1970-04-11 1970-05-31 3 A 1956-04-24 6: 1970-05-31 1970-07-20 4 A 1956-04-24 7: 1970-01-01 1970-01-01 0 B 1964-07-11 8: 1970-01-01 1970-02-20 1 B 1964-07-11 9: 1970-02-20 1970-04-11 2 B 1964-07-11 10: 1970-04-11 1970-04-11 2 B 1964-07-11 11: 1970-04-11 1970-05-31 3 B 1964-07-11 12: 1970-05-31 1970-07-20 4 B 1964-07-11 13: 1970-01-01 1970-01-01 0 C 1970-01-01 14: 1970-01-01 1970-02-20 1 C 1970-01-01 15: 1970-02-20 1970-04-11 2 C 1970-01-01 16: 1970-04-11 1970-04-11 2 C 1970-01-01 17: 1970-04-11 1970-05-31 3 C 1970-01-01 18: 1970-05-31 1970-07-20 4 C 1970-01-01 19: 1970-01-01 1970-01-01 0 D 1970-04-11 20: 1970-01-01 1970-02-20 1 D 1970-04-11 21: 1970-02-20 1970-04-11 2 D 1970-04-11 22: 1970-04-11 1970-04-11 2 D 1970-04-11 23: 1970-04-11 1970-05-31 3 D 1970-04-11 24: 1970-05-31 1970-07-20 4 D 1970-04-11 start end myvalue ptid Bdate > > > > cleanEx() detaching ‘package:data.table’ > nameEx("splitTwo") > ### * splitTwo > > flush(stderr()); flush(stdout()) > > ### Name: splitTwo > ### Title: splitTwo > ### Aliases: splitTwo > > ### ** Examples > > library(data.table) Attaching package: ‘data.table’ The following object is masked from ‘package:base’: %notin% > > dat <- data.table(pnr=c("123456","123456","234567","234567","345678","345678" + ,"456789","456789"), + start=as.Date(c(0,100,0,100,0,100,0,100),origin="1970-01-01"), + end=as.Date(c(100,200,100,200,100,200,100,200),origin="1970-01-01")) > > split <- data.table (pnr=c("123456","234567","345678","456789"), + como1.onset=as.integer(c(0,NA,49,50)), como2.onset=as.integer(c(25,75,49,49)), + como3.onset=as.integer(c(30,NA,49,48)), como4.onset=as.integer(c(50,49,49,47))) > #Show the datasets: > dat[] pnr start end 1: 123456 1970-01-01 1970-04-11 2: 123456 1970-04-11 1970-07-20 3: 234567 1970-01-01 1970-04-11 4: 234567 1970-04-11 1970-07-20 5: 345678 1970-01-01 1970-04-11 6: 345678 1970-04-11 1970-07-20 7: 456789 1970-01-01 1970-04-11 8: 456789 1970-04-11 1970-07-20 > split[] pnr como1.onset como2.onset como3.onset como4.onset 1: 123456 0 25 30 50 2: 234567 NA 75 NA 49 3: 345678 49 49 49 49 4: 456789 50 49 48 47 > splitTwo(dat # inddato with id/in/out + ,split # Data with id and dates + ,c("pnr","start","end") #names of id/in/out - in that order + ,c("pnr","como1.onset","como2.onset","como3.onset","como4.onset")) start end como1.onset como2.onset como3.onset como4.onset 1: 1970-01-01 1970-01-26 1 0 0 0 2: 1970-01-26 1970-01-31 1 1 0 0 3: 1970-01-31 1970-02-20 1 1 1 0 4: 1970-02-20 1970-04-11 1 1 1 1 5: 1970-04-11 1970-07-20 1 1 1 1 6: 1970-01-01 1970-02-19 0 0 0 0 7: 1970-02-19 1970-03-17 0 0 0 1 8: 1970-03-17 1970-04-11 0 1 0 1 9: 1970-04-11 1970-07-20 0 1 0 1 10: 1970-01-01 1970-02-19 0 0 0 0 11: 1970-02-19 1970-04-11 1 1 1 1 12: 1970-04-11 1970-07-20 1 1 1 1 13: 1970-01-01 1970-02-17 0 0 0 0 14: 1970-02-17 1970-02-18 0 0 0 1 15: 1970-02-18 1970-02-19 0 0 1 1 16: 1970-02-19 1970-02-20 0 1 1 1 17: 1970-02-20 1970-04-11 1 1 1 1 18: 1970-04-11 1970-07-20 1 1 1 1 pnr 1: 123456 2: 123456 3: 123456 4: 123456 5: 123456 6: 234567 7: 234567 8: 234567 9: 234567 10: 345678 11: 345678 12: 345678 13: 456789 14: 456789 15: 456789 16: 456789 17: 456789 18: 456789 > #Names of date-vars to split by > # And with splittingguide in long format > splitvars <- c("como1.onset","como2.onset","como3.onset","como4.onset") > split <- data.table::melt(data=split,id.vars="pnr",measure.vars=splitvars, + variable.name="name",value.name="value") > split[,value:=as.Date(value,origin="1970-01-01")] > split[] pnr name value 1: 123456 como1.onset 1970-01-01 2: 234567 como1.onset 3: 345678 como1.onset 1970-02-19 4: 456789 como1.onset 1970-02-20 5: 123456 como2.onset 1970-01-26 6: 234567 como2.onset 1970-03-17 7: 345678 como2.onset 1970-02-19 8: 456789 como2.onset 1970-02-19 9: 123456 como3.onset 1970-01-31 10: 234567 como3.onset 11: 345678 como3.onset 1970-02-19 12: 456789 como3.onset 1970-02-18 13: 123456 como4.onset 1970-02-20 14: 234567 como4.onset 1970-02-19 15: 345678 como4.onset 1970-02-19 16: 456789 como4.onset 1970-02-17 > split <- split[!is.na(value)] # remove missing values > splitTwo(dat # in-data with id/in/out + ,split # Data with id/name/date + ,c("pnr","start","end") #names of id/in/out - in that order + ,c("pnr","name","value") + ,format="long") start end como1.onset como2.onset como3.onset como4.onset 1: 1970-01-01 1970-01-26 1 0 0 0 2: 1970-01-26 1970-01-31 1 1 0 0 3: 1970-01-31 1970-02-20 1 1 1 0 4: 1970-02-20 1970-04-11 1 1 1 1 5: 1970-04-11 1970-07-20 1 1 1 1 6: 1970-01-01 1970-02-19 0 0 0 0 7: 1970-02-19 1970-03-17 0 0 0 1 8: 1970-03-17 1970-04-11 0 1 0 1 9: 1970-04-11 1970-07-20 0 1 0 1 10: 1970-01-01 1970-02-19 0 0 0 0 11: 1970-02-19 1970-04-11 1 1 1 1 12: 1970-04-11 1970-07-20 1 1 1 1 13: 1970-01-01 1970-02-17 0 0 0 0 14: 1970-02-17 1970-02-18 0 0 0 1 15: 1970-02-18 1970-02-19 0 0 1 1 16: 1970-02-19 1970-02-20 0 1 1 1 17: 1970-02-20 1970-04-11 1 1 1 1 18: 1970-04-11 1970-07-20 1 1 1 1 pnr 1: 123456 2: 123456 3: 123456 4: 123456 5: 123456 6: 234567 7: 234567 8: 234567 9: 234567 10: 345678 11: 345678 12: 345678 13: 456789 14: 456789 15: 456789 16: 456789 17: 456789 18: 456789 > > > > cleanEx() detaching ‘package:data.table’ > nameEx("standardize.prodlim") > ### * standardize.prodlim > > flush(stderr()); flush(stdout()) > > ### Name: standardize.prodlim > ### Title: Standardize absolute risks > ### Aliases: standardize.prodlim > > ### ** Examples > > if (requireNamespace("survival", quietly = TRUE) && + requireNamespace("prodlim", quietly = TRUE)) { + library(survival) + library(prodlim) + library(data.table) + data(pbc) + d <- na.omit(pbc[,c("time","status","trt","age","sex")]) + d$agegroup <- cut(d$age,breaks=c(0,40,50,60,100)) + fit <- prodlim(Hist(time,status==1)~trt+agegroup,data=d) + standardize.prodlim(fit,var1="trt",var2="agegroup",data=d,times=1000) + standardize.prodlim(fit,var1="trt",var2="agegroup",data=d,times=c(1000,1500)) + } Attaching package: ‘data.table’ The following object is masked from ‘package:base’: %notin% trt risk1000 risk1500 1: 1 0.014045584 0.024154517 2: 1 0.015634772 0.025162211 3: 1 0.006614839 0.006614839 4: 1 0.000000000 0.000000000 5: 2 0.000000000 0.007745726 6: 2 0.006541078 0.020050028 7: 2 0.000000000 0.000000000 8: 2 0.000000000 0.000000000 > > > > cleanEx() detaching ‘package:data.table’, ‘package:prodlim’, ‘package:survival’ > nameEx("standardize.proportion") > ### * standardize.proportion > > flush(stderr()); flush(stdout()) > > ### Name: standardize.proportion > ### Title: Standardize proportions and absolute risks to a given age > ### distribution > ### Aliases: standardize.proportion > > ### ** Examples > > set.seed(84) > d <- data.table::data.table( + X1 = factor(sample(0:1, 120, replace = TRUE)), + X2 = factor(sample(0:1, 120, replace = TRUE)), + X4 = rep(c("a", "b"), each = 60), + agegroups = factor(rep(c("young", "middle", "old"), 40))) > standardize.proportion(vars=c("X1","X2"),agevar="agegroups",byvar="X4",data=d) X4 prop.X1 lower.prop.X1 upper.prop.X1 prop.X2 lower.prop.X2 1: a 38.33333 26.04551 50.62116 50 37.39073 2: b 50.00000 37.90914 62.09086 40 27.64722 upper.prop.X2 1: 62.60927 2: 52.35278 > > > > > cleanEx() > nameEx("standardize.rate") > ### * standardize.rate > > flush(stderr()); flush(stdout()) > > ### Name: standardize.rate > ### Title: Standardize proportions and absolute risks to a given age > ### distribution > ### Aliases: standardize.rate > > ### ** Examples > > library(data.table) Attaching package: ‘data.table’ The following object is masked from ‘package:base’: %notin% > set.seed(84) > n=160 > d <- data.table(e1=rpois(n,lambda=9), + rt1=rpois(n,lambda=1880), + e2=rpois(n,lambda=123), + rt2=rpois(n,lambda=80000)) > d[,agegroups:=factor(rep(c("40-50","45-50","50-55","55-60","60-65","65-70","70-75","75-80"),n/8))] > d[,sex:=factor(rep(c("f","m"),c(n/2,n/2)))] > d[,year:=rep(2001:2010,n/10)] > D=d[,list(e1=sum(e1),rt1=sum(rt1),e2=sum(e2),rt2=sum(rt2)),by=c("sex","agegroups")] > D[sex=="m",e1:=e1+rpois(.N,lambda=as.numeric(agegroups)*17)] > D[sex=="m",rt1:=rt1-rpois(.N,lambda=as.numeric(agegroups)*1600)] > standardize.rate(x=list(c("e1","rt1")), + age="agegroups",exposure="sex",data=D,standardize.to="f") Key: x type group rate rate.lower rate.upper ratio 1: e1 crude f 0.004925192 0.004576909 0.005292949 1.000000 2: e1 crude m 0.014805927 0.014035521 0.015607622 3.006163 3: e1 standardized f 0.004973861 0.004606105 0.005365984 1.000000 4: e1 standardized m 0.014805927 0.014035521 0.015607622 2.976747 ratio.lower ratio.upper 1: 1.000000 1.000000 2: 2.747509 3.291209 3: 1.000000 1.000000 4: 2.711811 3.268304 > standardize.rate(x=list(c("e1","rt1")), + age="agegroups",exposure="sex",data=D,standardize.to="m") Key: x type group rate rate.lower rate.upper ratio 1: e1 crude f 0.004925192 0.004576909 0.005292949 1.000000 2: e1 crude m 0.014805927 0.014035521 0.015607622 3.006163 3: e1 standardized f 0.004973861 0.004606105 0.005365984 1.000000 4: e1 standardized m 0.014805927 0.014035521 0.015607622 2.976747 ratio.lower ratio.upper 1: 1.000000 1.000000 2: 2.747509 3.291209 3: 1.000000 1.000000 4: 2.711811 3.268304 > standardize.rate(x=list(c("e1","rt1")), + age="agegroups",exposure="sex",data=D,standardize.to="mean") Key: x type group rate rate.lower rate.upper ratio 1: e1 crude f 0.004925192 0.004576909 0.005292949 1.000000 2: e1 crude m 0.014805927 0.014035521 0.015607622 3.006163 3: e1 standardized f 0.004943826 0.004591813 0.005316896 1.000000 4: e1 standardized m 0.016560015 0.015677353 0.017483777 3.349636 ratio.lower ratio.upper 1: 1.000000 1.000000 2: 2.747509 3.291209 3: 1.000000 1.000000 4: 3.057000 3.672563 > standardize.rate(x=list("rate1"=c("e1","rt1"),"rate2"=c("e2","rt2")), + age="agegroups",exposure="sex",data=d,by="year") Key: year x type group rate rate.lower rate.upper 1: 2001 rate1 crude f 0.004263540 0.003283445 0.005444445 2: 2001 rate1 crude m 0.004925453 0.003867540 0.006183455 3: 2001 rate1 standardized f 0.004263540 0.003283445 0.005444445 4: 2001 rate1 standardized m 0.004920528 0.003863527 0.006181322 ratio ratio.lower ratio.upper 1: 1.000000 1.0000000 1.000000 2: 1.155250 0.8155303 1.640339 3: 1.000000 1.0000000 1.000000 4: 1.154094 0.8146928 1.639754 > > # For more than two exposure groups, select the pair to compare. > d[,groups:=factor(rep(paste0("G",1:4),rep(n/4,4)))] > D=d[,list(e1=sum(e1),rt1=sum(rt1),e2=sum(e2),rt2=sum(rt2)),by=c("groups","agegroups")] > D[groups=="G3",e1:=e1+rpois(.N,lambda=as.numeric(agegroups)*17)] > D[groups=="G3",rt1:=rt1-rpois(.N,lambda=as.numeric(agegroups)*1600)] > pair <- D[groups %in% c("G3", "G4")] > pair[,groups:=droplevels(groups)] > standardize.rate(x=list(c("e1","rt1")), + age="agegroups",exposure="groups", + data=pair,standardize.to="mean") Warning in stats::qgamma(alpha/2, shape = (DSR0^2)/varDSR0, scale = varDSR0/DSR0) : NaNs produced Warning in stats::qgamma(1 - alpha/2, shape = ((DSR0 + wM0)^2)/(varDSR0 + : NaNs produced Key: x type group rate rate.lower rate.upper ratio 1: e1 crude G3 0.057133102 0.053653285 0.060779373 1.00000000 2: e1 crude G4 0.004946743 0.004456769 0.005475875 0.08658278 3: e1 standardized G3 -0.038423335 NaN NaN 1.00000000 4: e1 standardized G4 0.004956918 0.004442353 0.005520357 -0.12900802 ratio.lower ratio.upper 1: 1.00000000 1.0000000 2: 0.07666418 0.0976182 3: 1.00000000 1.0000000 4: -0.10130103 -0.1687159 > > > > cleanEx() detaching ‘package:data.table’ > nameEx("view_parquet") > ### * view_parquet > > flush(stderr()); flush(stdout()) > > ### Name: view_parquet > ### Title: View a Parquet File or Directory > ### Aliases: view_parquet > > ### ** Examples > > if (requireNamespace("arrow", quietly = TRUE) && + requireNamespace("dplyr", quietly = TRUE)) { + path <- tempfile(fileext = ".parquet") + arrow::write_parquet(data.frame(id = 1:3, value = c(4, 5, 6)), path) + view_parquet(path, action = "head") + view_parquet(path, action = "str") + unlink(path) + } Reading: /tmp/Rtmps9NfwB/file250285612f7cbe.parquet # A tibble: 3 × 2 id value 1 1 4 2 2 5 3 3 6 Reading: /tmp/Rtmps9NfwB/file250285612f7cbe.parquet Rows: 3 Columns: 2 $ id 1, 2, 3 $ value 4, 5, 6 > > > > > cleanEx() > nameEx("xrecepter") > ### * xrecepter > > flush(stderr()); flush(stdout()) > > ### Name: xrecepter > ### Title: wrapper for medicin macro > ### Aliases: xrecepter > > ### ** Examples > > library(heaven) > ## Not run: > ##D # Requires SAS and a local copy of the external SAS medication macro. > ##D library(heaven) > ##D library(data.table) > ##D set.seed(05021992) > ##D N=18 > ##D packs = list("R03AK11"=list(c(10,1))) > ##D lmdb=simPrescriptionData(N,packages=packs,max.packages=1) > ##D lpr=simAdmissionData(N) > ##D ## very simple data > ##D lmdb <- lmdb[c(1,4,18)] > ##D R03 = list(atc=c("R03AK11"), > ##D maxdepot=100, > ##D period=as.Date(c("1995-01-01", "2012-12-31")), > ##D prescriptionwindow=2, > ##D doses=list(value=c(5,10), > ##D min = c(.5,.5), > ##D max = c(2,2), > ##D def = c(1,1))) > ##D xrecepter(drugdb=lmdb,adm=lpr,window=5,remote=FALSE,wd=tempdir(),save.tmp=FALSE, > ##D value=rep(R03$doses$value,2),min=rep(R03$doses$min,2),max=rep(R03$doses$max,2), > ##D def=rep(R03$doses$def,2),maxdepot=100,period=c(12784,20089),atc="R03AK11", > ##D name="blaupill",sas.program="/usr/local/bin/sas", > ##D macro="/path/to/medicin_macro_version_31.sas", > ##D verbose=FALSE) > ## End(Not run) > > > > > > ### *