Learn R Programming

peRiodiCS

Code for periodic versions of cubic splines and restricted cubic splines. The functions transform a numeric (time) variable x into a new basis, to model with either a periodic variant of cubic splines or periodic restricted cubic spline.

Install from GitHub with:

library(devtools)
install_github("crtahlin/peRiodiCS")

Example models for different variants:

# load example data; see help("viral_east_mediteranean")
data("viral_east_mediteranean")

# calculate location of knots to use
Knots <- Hmisc::rcspline.eval(x = viral_east_mediteranean$EpiWeek,
                              nk = 5, knots.only = TRUE)
                       
### RCS - restricted cubic splines (non-periodic, just for reference) #########
# model viral infections vs weeks
model_rcs <- glm(RSV ~ Hmisc::rcspline.eval(EpiWeek, inclx = TRUE, knots = Knots), data = viral_east_mediteranean)

# plot model (with many points, to make it smooth)
plot_per_mod(Model = model_rcs, XvarName = "EpiWeek", Smooth = TRUE)

### periodic RCS ##############################################################
# model viral infections vs weeks
model_rcs_per <- glm(RSV ~ rcs_per(EpiWeek, knots = Knots), data = viral_east_mediteranean)

# plot model (with many points, to make it smooth)
plot_per_mod(Model = model_rcs_per, XvarName = "EpiWeek", Smooth = TRUE)
                                  
### periodic CS (cubic spline) ###############################################
# model viral infections vs weeks
model_cs_per <- glm(RSV ~ cs_per(EpiWeek, knots = Knots), data = viral_east_mediteranean)

# plot model (with many points, to make it smooth)
plot_per_mod(Model = model_cs_per, XvarName = "EpiWeek", Smooth = TRUE)

Copy Link

Version

Install

install.packages('peRiodiCS')

Monthly Downloads

153

Version

0.5.0

License

GPL-3

Maintainer

Crt Ahlin

Last Published

July 5th, 2018

Functions in peRiodiCS (0.5.0)

rcs_per

Generate design matrix for periodic restricted cubic spline
b_rcs

Basis for restricted cubic splines
b_rcs_prime

Derive first derivatives of RCS
viral_east_mediteranean

Viral etiology, seasonality and severity of hospitalized patients with severe acute respiratory infections in the Eastern Mediterranean Region, 2007-2014
plot_per_mod

Plotting function for periodic curves model
cs_per

Generate design matrix for periodic cubic splines