Title: | Functions for Generating Periodic Curves |
---|---|
Description: | Functions for generating variants of curves: restricted cubic spline, periodic restricted cubic spline, periodic cubic spline. Periodic splines can be used to model data that has periodic nature / seasonality. |
Authors: | Crt Ahlin [aut, cre], Lara Lusa [aut] |
Maintainer: | Crt Ahlin <[email protected]> |
License: | GPL-3 |
Version: | 0.5.1.9002 |
Built: | 2025-03-06 03:18:21 UTC |
Source: | https://github.com/crtahlin/periodics |
Function that derives the restricted cubic splines for a value/vector of values, given the knots; obtains exactly the same results as the rcs function included in the rms package.
b_rcs(x, knots, inclx = FALSE)
b_rcs(x, knots, inclx = FALSE)
x |
numerical vector |
knots |
vector specifying the knot locations |
inclx |
logical, if TRUE returns also the x vector |
function that derives the first derivative of the restricted cubic splines for a value/vector of values, given the knots
b_rcs_prime(x, knots)
b_rcs_prime(x, knots)
x |
vector of values |
knots |
vector of knot locations |
Generate design matrix for periodic cubic splines.
cs_per(x, knots = NULL, nk = 5, xmax = max(x, na.rm = TRUE), xmin = min(x, na.rm = TRUE))
cs_per(x, knots = NULL, nk = 5, xmax = max(x, na.rm = TRUE), xmin = min(x, na.rm = TRUE))
x |
numerical x values to transform to new basis |
knots |
vector with locations of the knots of the spline |
nk |
number of knots, used only if the knots are not specified, overridden otherwise |
xmax |
value of the (theoretical) minimum of x |
xmin |
value of the (theoretical) maximum of x |
# 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) # model viral infections vs weeks model <- 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, XvarName = "EpiWeek")
# 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) # model viral infections vs weeks model <- 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, XvarName = "EpiWeek")
Plots graph of periodic curves with confidence intervals. Data should be included in the model.
plot_per_mod(Model, XvarName, Ylab = "Response", Xlab = "Covariate", Ylim = NULL, Xlim = NULL, Xmin = NULL, Xmax = NULL, Knots = NULL, Title = NULL, Vlines = NULL, Hlines = NULL, Cex.lab = NULL, Cex.main = NULL, Cex.axis = NULL, Axes = TRUE, Add = FALSE, Col = "black", PlotCI = TRUE, nPoints = 100)
plot_per_mod(Model, XvarName, Ylab = "Response", Xlab = "Covariate", Ylim = NULL, Xlim = NULL, Xmin = NULL, Xmax = NULL, Knots = NULL, Title = NULL, Vlines = NULL, Hlines = NULL, Cex.lab = NULL, Cex.main = NULL, Cex.axis = NULL, Axes = TRUE, Add = FALSE, Col = "black", PlotCI = TRUE, nPoints = 100)
Model |
The built model |
XvarName |
Name of the x variable in the dataset (column name) |
Ylab |
Label on vertical (y) axis |
Xlab |
Label on horizontal (x) axis |
Ylim |
Limits of y axis |
Xlim |
Limits of x axis |
Xmin |
The min X of data to be predicted |
Xmax |
The max X of data to be predicted |
Knots |
Locations of knots of the splines |
Title |
Title of the plot |
Vlines |
Where to plot vertical lines |
Hlines |
Where to plot horizontal lines |
Cex.lab |
Character expansion (aka "size of font") for the labels |
Cex.main |
Character expansion for main text |
Cex.axis |
Character expansion for the axis text |
Axes |
Plot axes |
Add |
Add to existing plot |
Col |
Color of the plotted lines |
PlotCI |
Plot confidence intervals |
nPoints |
Number of points to use on the x axis |
Generate design matrix for periodic restricted cubic spline.
rcs_per(x, knots = NULL, nk = 5, xmin = min(x, na.rm = TRUE), xmax = max(x, na.rm = TRUE))
rcs_per(x, knots = NULL, nk = 5, xmin = min(x, na.rm = TRUE), xmax = max(x, na.rm = TRUE))
x |
numerical x values to transform to new basis |
knots |
vector with locations of the knots of the spline |
nk |
number of knots, used only if the knots are not specified, overridden otherwise |
xmin |
value of the (theoretical) minimum of x |
xmax |
value of the (theoretical) maximum of x |
# 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) # model viral infections vs weeks model <- 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, XvarName = "EpiWeek")
# 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) # model viral infections vs weeks model <- 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, XvarName = "EpiWeek")
Data about infections with different viruses across several years.
For more information see Source and References section.
viral_east_mediteranean
viral_east_mediteranean
A data frame with variables:
record identification number
Did the patient consent and enroll in the study?: 1=Yes, 0=No
Country of enrollment: Egypt, Jordan, Oman, Qatar, Yemen
Year of enrollment: Integers (2007-2014)
Month of enrollment: Integers (1-12)
Week of enrollment: Integers (1-53)
Number of days between onset of symptoms and hospitalization: Integer
Number of days between hospitalization and outcome: Integer
Sex: 1=Female, 0=Male
Age group: 1=<1 year, 2=1-4 years, 3=5-49 years, 4=50+ years
Does the patient have any pre-existing chronic disease?: 1=Yes, 0=No
Did the patient receive oxygen therapy during hospitalization?: 1=Yes, 0=No
Was the patient ventilated during hospitalization?: 1=Yes, 0=No
Was the patient admitted to an intensive care unit during hospitalization?: 1=Yes, 0=No
What was the patient"'"s final hospitalization outcome?: 1=Discharge, 2=Transfer, 3=Death
Results for respiratory syncytial virus: 1=Positive, 0=Negative
Results for adenovirus: 1=Positive, 0=Negative
Results for human metapneumovirus: 1=Positive, 0=Negative
Results for human parainfluenzavirus type 1: 1=Positive, 0=Negative
Results for human parainfluenzavirus type 2: 1=Positive, 0=Negative
Results for human parainfluenzavirus type 3: 1=Positive, 0=Negative
Results for influenza: 1=Positive, 0=Negative
http://journals.plos.org/plosone/article?id=10.1371/journal.pone.0180954
Horton, Katherine C. AND Dueger, Erica L. AND Kandeel, Amr AND Abdallat, Mohamed AND El-Kholy, Amani AND Al-Awaidy, Salah AND Kohlani, Abdul Hakim AND Amer, Hanaa AND El-Khal, Abel Latif AND Said, Mayar AND House, Brent AND Pimentel, Guillermo AND Talaat, Maha (2017). Viral etiology, seasonality and severity of hospitalized patients with severe acute respiratory infections in the Eastern Mediterranean Region, 2007-2014. PLOS ONE, 12, 1-17.