-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetBaseline.R
44 lines (32 loc) · 1.09 KB
/
getBaseline.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Author: H. Brantley
require(quantreg)
require(splines)
require(zoo)
getBaseline <- function(pid, timestamp, df=NA){
pid0 <- as.data.frame(pid)[,1]
time0 <- as.data.frame(timestamp)[,1]
# Input checks
if (NCOL(pid) != 1){ ## Updated 2019/10 by HL [ncol -> NCOL]
stop("pid must one column.")
}
if (any(is.na(time0))){
stop("timestamp cannot include missing values")
}
if (class(time0)[1] != "POSIXct") {
stop ("timestamp must be POSIXct class")
}
# Set degrees of freedom based on duration of time series
if (is.na(df)){
df <- 3*round(difftime(max(time0), min(time0), units="hours"))
}
# Remove missing values from pid using last observation carry forward
if(is.na(pid0[length(pid0)])) {pid0[length(pid0)] <-
pid0[max(which(!is.na(pid0)))]}
pid0 <- zoo::na.locf(pid0, fromLast = TRUE)
# Quantile regression with splines
fit <- rq(pid0~ns(time0, df), tau=.02)
baseline <- predict(fit)
# plot(pid0~time0, type="l")
# lines(baseline~time0, col="red")
return(baseline)
}