Title: | Interactive Plotting for Functional Data Analyses |
---|---|
Description: | Produces Shiny applications for different types of popular functional data analyses. The functional data analyses are implemented in the refund package, then refund.shiny reads in the refund object and implements an object-specific set of plots based on the object class using S3. |
Authors: | Julia Wrobel [aut, cre], Jeff Goldsmith [aut] |
Maintainer: | Julia Wrobel <[email protected]> |
License: | GPL (>= 2) |
Version: | 1.0 |
Built: | 2024-12-11 05:43:26 UTC |
Source: | https://github.com/refunders/refund.shiny |
Very experimental function, primarily used to convert matrices storing functional data to data.frames with specific variable names.
as_refundObj(obj, ...)
as_refundObj(obj, ...)
obj |
Object to be converted. Currently supports class |
... |
additional arguments to be passed to methods. |
An object of classes data.frame
and refund.object
, the latter of
which is so far not used. Columns are id
(taken from the rownames of obj
,
if they exist), index
(with behavior described above), and value
(taken
from entries in obj
).
Jeff Goldsmith [email protected]
## Not run: library(ggplot2) library(refund) cca_df = as_refundObj(DTI$cca) ggplot(cca_df, aes(x = index, y = value, group = id)) + geom_line() ## End(Not run)
## Not run: library(ggplot2) library(refund) cca_df = as_refundObj(DTI$cca) ggplot(cca_df, aes(x = index, y = value, group = id)) + geom_line() ## End(Not run)
Convert matrices to dataframes for use in functional data analyses
## S3 method for class 'matrix' as_refundObj(obj, index = NULL, ...)
## S3 method for class 'matrix' as_refundObj(obj, index = NULL, ...)
obj |
Matrix object to be converted; rows contain functional observations on subjects. |
index |
Time grid on which functional data are observed; defaults to |
... |
additional arguments to be passed to methods (not used). |
An object of classes data.frame
and refund.object
, the latter of
which is so far not used. Columns are id
(taken from the rownames of obj
,
if they exist), index
(with behavior described above), and value
(taken
from entries in obj
).
Jeff Goldsmith [email protected]
library(ggplot2) library(refund) cca_df = as_refundObj(DTI$cca) ggplot(cca_df, aes(x = index, y = value, group = id)) + geom_line()
library(ggplot2) library(refund) cca_df = as_refundObj(DTI$cca) ggplot(cca_df, aes(x = index, y = value, group = id)) + geom_line()
Internal method used in conjunction with makeLasagna()
to create side-by-side lasagna plot and distribution plot.
The distribution plot gives distribution of sorting covariate.
bakeLasagna(data, data.long, covariate = NULL)
bakeLasagna(data, data.long, covariate = NULL)
data |
Dataset for lasagna plot. Same data used in |
data.long |
Sorted longform dataset for lasagna plot output by |
covariate |
User-selected covariate for sorting the rows in the lasagna plot. Defaults to NULL, in which case data is sorted by row number. |
Julia Wrobel [email protected]
function used in method for fast modified band depth (MBD) calculation
combinat(n, p)
combinat(n, p)
n |
number of columns in your dataset |
p |
number of rows in your dataset |
Ying Sun and Marc G.Genton
Internal method that constructs the input calls for plot_shiny.fosr(). The variable name and values are passed as arguments, and a corresponding slider (for numeric) or drop-down (for factor) input is constructed.
createInputCall(name, variable)
createInputCall(name, variable)
name |
variable name |
variable |
variable values from dataset |
Jeff Goldsmith [email protected]
Internal method that constructs the inverse link function for a generalized FPCA fit. This is used in toggling between plots on the natural scale and on the response scale.
createInvLink(family = NULL)
createInvLink(family = NULL)
family |
Family of the (generalized) FPCA. Currently supported families
are |
Jeff Goldsmith [email protected]
Internal method that creates UI with buttons to download a plot as a PDF or ggplot object.
downloadModule(input, output, session, plotObject, plotName)
downloadModule(input, output, session, plotObject, plotName)
input |
gets user input from UI |
output |
designates output for UI |
session |
Shiny variable for server modules |
plotObject |
Reactive plot object defined elsewhere in the server function. |
plotName |
Character string designating name of the plot for PDF output. |
Julia Wrobel [email protected]
Internal method that creates UI with buttons to download a plot as a PDF or ggplot object.
downloadModuleUI(id)
downloadModuleUI(id)
id |
name of module. Allows each call of this module to be uniquely identified. |
Julia Wrobel [email protected]
fast modified band depth calculation for fda
Method for fast modified band depth (fMBD) calculation
fMBD(data)
fMBD(data)
data |
name of dataset |
Ying Sun and Marc G.Genton
Get spaces between timepoints as widths for binary registration lasagna plot.
getWidth(z)
getWidth(z)
z |
time values for a specific subject |
Julia Wrobel [email protected]
Produces a ggplot with mean and sliders to change weighting of each PC; allows you to obtain range of potential fitted values.
make_linCom(obj, pc_weights, response_scale = FALSE)
make_linCom(obj, pc_weights, response_scale = FALSE)
obj |
fpca object to be plotted. |
pc_weights |
User-selected weights for FPCs |
response_scale |
Scale of reponse to be plotted. If TRUE results are plotted on response scale, if FALSE results are plotted on natural scale. |
Produces a ggplot with mean plus or minus two standard deviations of a selected FPC.
make_muPC(obj, pc_choice, response_scale = FALSE)
make_muPC(obj, pc_choice, response_scale = FALSE)
obj |
fpca object to be plotted. |
pc_choice |
FPC to be plotted. |
response_scale |
Scale of reponse to be plotted. If TRUE results are plotted on response scale, if FALSE results are plotted on natural scale. |
Internal method that takes a dataframe of observed data with an outcome matrix and user-selected covariate,
sorts outcome by the selected covariate, and assigns heights to each row based on value of the selected covariate.
The resulting dataframe is used with bakeLasagna()
to create lasagna plot.
makeLasagna(data, outcome, covariate = NULL)
makeLasagna(data, outcome, covariate = NULL)
data |
Dataset for lasagna plot. |
outcome |
Matrix of values where each row represents a functional observation. |
covariate |
User-selected covariate for sorting the rows in the lasagna plot. Defaults to NULL, in which case data is sorted by row number. |
Julia Wrobel [email protected]
Internal method that constructs the input calls for plot_shiny.mfpca(). The number of sliders to construct for each level is passed as an argument, and corresponding sliders for each FPC are constructed.
mfpcaCalls(plot.npc, plotObj, percents)
mfpcaCalls(plot.npc, plotObj, percents)
plot.npc |
list of 2 numeric entries giving number of sliders at each level |
plotObj |
the mfpca object plotted in the plot_shiny.mfpca() function. |
percents |
the percent variance calculated for each eigen values for levels 1 and 2. |
a list of numbers that indicate percent variance for selected level.
Julia Wrobel [email protected]
Internal method that assigns band depth values to curves based on exact fast MBD computation (Sun & Genton, 2012). Code modified from fbplot in fda package. A dataframe of residuals is passed as an argument, and depths and outlying curves are returned
outliers(data, factor = 1.5)
outliers(data, factor = 1.5)
data |
matrix or df of functional observations |
factor |
a constant that determines the fences for outliers. Defaults to 1.5, as in classical definition for Tukey outliers. |
Julia Wrobel [email protected]
Sun, Ying, Marc G. Genton, and Douglas W. Nychka. (2012). Exact fast computation of band depth for large functional datasets: How quickly can one million curves be ranked? Stat, 1, 68-74.
Sun, Ying, and Marc G. Genton. (2011). Functional boxplots. Journal of Computational and Graphical Statistics, 20, 313-334.
Interactive Plotting for Functional Data
plot_shiny(obj, ...)
plot_shiny(obj, ...)
obj |
object to be plotted. Currently, allowed data types are |
... |
additional arguments passed to plotting functions |
Function for interactive plotting of functional data analysis results.
This package builds on the refund
package: tools in refund
are used to
conduct analyses and functions in this package create interactive visualizations of the results
of those analyses. There are four major categories of analyses that can be viewed:
Functional principal components analyses implemented by fpca.sc
, fpca.face
,
fpca.ssvd
, and fpca2s
. Plots show the mean +/- 2SD times each FPC; scree plots;
linear combinations of score values and FPCs; reconstructions for each subject; and score scatterplots.
Function-on-scalar regression analyses implemented by bayes_fosr
. Plots show the raw data
colored by covariate values; fitted values depending on covariates; coefficient functions; and residuals.
Multilevel functional principal components analyses implemented by mfpca.sc
. Plots show the
mean +/- 2SD times each FPC; scree plots; linear combinations of score values and FPCs;
reconstructions for each subject; and score scatterplots for levels 1 and 2.
#'
Longitudinal functional principal components analyses
This function outputs a shiny app based on the class of the input object.
Jeff Goldsmith [email protected], Julia Wrobel [email protected]
plot_shiny.fpca
, plot_shiny.mfpca
, plot_shiny.fosr
## Not run: library(dplyr) ##### FPCA Example on real data ##### data(cd4) SC = fpca.sc(cd4) plot_shiny(SC) ##### FoSR Example ##### data(DTI) DTI = DTI[complete.cases(DTI),] fit.fosr = refund::bayes_fosr(cca ~ pasat + sex, data = DTI) plot_shiny(fit.fosr) ##### FoSR Example with outliers ##### DTI$cca[1,] = DTI$cca[1,] + .4 DTI$cca[2,] = DTI$cca[2,] + .4 fosr.dti2 = bayes_fosr(cca ~ pasat + sex, data = DTI) plot_shiny(fosr.dti2) ##### Longitudinal FoSR Examples ##### data(DTI2) class(DTI2$cca) = class(DTI2$cca)[-1] DTI2 = subset(DTI2, select = c(cca, id, pasat)) DTI2 = DTI2[complete.cases(DTI2),] fosr.dti3 = bayes_fosr(cca ~ pasat + re(id), data = DTI2, Kt = 10, Kp = 4, cov.method = "FPCA") plot_shiny(fosr.dti3) plot_shiny(fosr.dti3$fpca.obj) ##### LFPCA Example on real data ##### data(DTI) MS <- subset(DTI, case ==1) # subset data with multiple sclerosis (MS) case index.na <- which(is.na(MS$cca)) Y <- MS$cca; Y[index.na] <- fpca.sc(Y)$Yhat[index.na]; sum(is.na(Y)) id <- MS$ID visit.index <- MS$visit visit.time <- MS$visit.time/max(MS$visit.time) lfpca.dti1 <- fpca.lfda(Y = Y, subject.index = id, visit.index = visit.index, obsT = visit.time, LongiModel.method = 'lme', mFPCA.pve = 0.95) plot_shiny(lfpca.dti1) lfpca.dti2 <- fpca.lfda(Y = Y, subject.index = id, visit.index = visit.index, obsT = visit.time, LongiModel.method = 'fpca.sc', mFPCA.pve = 0.80, sFPCA.pve = 0.80) plot_shiny(lfpca.dti2) ## End(Not run)
## Not run: library(dplyr) ##### FPCA Example on real data ##### data(cd4) SC = fpca.sc(cd4) plot_shiny(SC) ##### FoSR Example ##### data(DTI) DTI = DTI[complete.cases(DTI),] fit.fosr = refund::bayes_fosr(cca ~ pasat + sex, data = DTI) plot_shiny(fit.fosr) ##### FoSR Example with outliers ##### DTI$cca[1,] = DTI$cca[1,] + .4 DTI$cca[2,] = DTI$cca[2,] + .4 fosr.dti2 = bayes_fosr(cca ~ pasat + sex, data = DTI) plot_shiny(fosr.dti2) ##### Longitudinal FoSR Examples ##### data(DTI2) class(DTI2$cca) = class(DTI2$cca)[-1] DTI2 = subset(DTI2, select = c(cca, id, pasat)) DTI2 = DTI2[complete.cases(DTI2),] fosr.dti3 = bayes_fosr(cca ~ pasat + re(id), data = DTI2, Kt = 10, Kp = 4, cov.method = "FPCA") plot_shiny(fosr.dti3) plot_shiny(fosr.dti3$fpca.obj) ##### LFPCA Example on real data ##### data(DTI) MS <- subset(DTI, case ==1) # subset data with multiple sclerosis (MS) case index.na <- which(is.na(MS$cca)) Y <- MS$cca; Y[index.na] <- fpca.sc(Y)$Yhat[index.na]; sum(is.na(Y)) id <- MS$ID visit.index <- MS$visit visit.time <- MS$visit.time/max(MS$visit.time) lfpca.dti1 <- fpca.lfda(Y = Y, subject.index = id, visit.index = visit.index, obsT = visit.time, LongiModel.method = 'lme', mFPCA.pve = 0.95) plot_shiny(lfpca.dti1) lfpca.dti2 <- fpca.lfda(Y = Y, subject.index = id, visit.index = visit.index, obsT = visit.time, LongiModel.method = 'fpca.sc', mFPCA.pve = 0.80, sFPCA.pve = 0.80) plot_shiny(lfpca.dti2) ## End(Not run)
Produces an interactive plot illustrating a functional linear concurrent regression analysis.
## S3 method for class 'flcm' plot_shiny(obj, xlab = "", ylab = "", title = "", ...)
## S3 method for class 'flcm' plot_shiny(obj, xlab = "", ylab = "", title = "", ...)
obj |
fosr object to be plotted. |
xlab |
x axis label |
ylab |
y axis label |
title |
plot title |
... |
additional arguments passed to plotting functions |
No object is returned. This function takes in objects of class 'fosr' and outputs a shiny application for that object
Jeff Goldsmith [email protected], Julia Wrobel [email protected]
Produces an interactive plot illustrating a function-on-scalar regression analysis.
## S3 method for class 'fosr' plot_shiny(obj, xlab = "", ylab = "", title = "", ...)
## S3 method for class 'fosr' plot_shiny(obj, xlab = "", ylab = "", title = "", ...)
obj |
fosr object to be plotted. |
xlab |
x axis label |
ylab |
y axis label |
title |
plot title |
... |
additional arguments passed to plotting functions |
No object is returned. This function takes in objects of class 'fosr' and outputs a shiny application for that object.
Jeff Goldsmith [email protected], Julia Wrobel [email protected]
Produces an interactive plot illustrating a functional principal component analysis.
## S3 method for class 'fpca' plot_shiny(obj, xlab = "", ylab = "", title = "", thin_data = FALSE, ...)
## S3 method for class 'fpca' plot_shiny(obj, xlab = "", ylab = "", title = "", thin_data = FALSE, ...)
obj |
fpca object to be plotted. |
xlab |
x axis label |
ylab |
y axis label |
title |
plot title |
thin_data |
If TRUE data is thinned for each subject to make plotting faster. Defaults to FALSE. |
... |
additional arguments passed to plotting functions |
No object is returned. This function takes in objects of class 'fpca' and outputs a shiny application for that object.
Julia Wrobel [email protected], Jeff Goldsmith [email protected]
Produces an interactive plot illustrating longitudinal functioanl data analysis (Park and Staicu, 2015).
## S3 method for class 'lfpca' plot_shiny(obj, xlab = "", ylab = "", title = "", ...)
## S3 method for class 'lfpca' plot_shiny(obj, xlab = "", ylab = "", title = "", ...)
obj |
lfpca object to be plotted. |
xlab |
x axis label |
ylab |
y axis label |
title |
plot title |
... |
additional arguments passed to plotting functions |
So Young Park [email protected], Ana-Maria Staicu [email protected]
Park, S.Y. and Staicu, A.M. (2015). Longitudinal functional data analysis. Stat 4 212-226.
plot_shiny
; fpca.lfda
in the refund package for estimation method.
Produces an interactive plot illustrating a multilevel functional principal component analysis.
## S3 method for class 'mfpca' plot_shiny(obj, xlab = "", ylab = "", title = "", ...)
## S3 method for class 'mfpca' plot_shiny(obj, xlab = "", ylab = "", title = "", ...)
obj |
mfpca object to be plotted. |
xlab |
x axis label |
ylab |
y axis label |
title |
plot title |
... |
additional arguments passed to plotting functions |
No object is returned. This function takes in objects of class 'mfpca' and outputs a shiny application for that object.
Julia Wrobel [email protected], Jeff Goldsmith [email protected]
Produces an interactive plot illustrating functional data before and after registration. Our registration method uses FPCA, the FPCA is plotted as well.
## S3 method for class 'registration' plot_shiny(obj, xlab = "", ylab = "", title = "", thin_data = FALSE, ...)
## S3 method for class 'registration' plot_shiny(obj, xlab = "", ylab = "", title = "", thin_data = FALSE, ...)
obj |
registration object to be plotted. |
xlab |
x axis label |
ylab |
y axis label |
title |
plot title |
thin_data |
If TRUE data is thinned for each subject to make plotting faster. Defaults to FALSE. |
... |
additional arguments passed to plotting functions |
No object is returned. This function takes in objects of class 'registration' and outputs a shiny application for that object.
Julia Wrobel [email protected]
Get registered and unregistered lasagna plots for binary data. Note: should make this compatible for other data types as well. Requires data to have t_hat and tstar variables.
registerLasagna(data)
registerLasagna(data)
data |
Dataset for lasagna plot. |
Julia Wrobel [email protected]
Internal method that saves plots as PDF.Can be used with all plotting methods in the package. The name of the plot object and it's name to be saved under are passed in and the plot is saved as a PDF.
savePDF(title, plotName)
savePDF(title, plotName)
title |
new name for the plot, and name of the PDF file created |
plotName |
name of the ggplot object |
Julia Wrobel [email protected]
Internal method that saves ggplot plots as .RData files.Can be used with all plotting methods in the package. The name of the plot object and it's name to be saved under are passed in and the plot is saved as an RData file.
savePlot(title, plotName)
savePlot(title, plotName)
title |
new name for the plot, and name of the RData file created. |
plotName |
name of the ggplot object. |
Julia Wrobel [email protected]
Internal method that creates UI with buttons to download a plot as a PDF or ggplot object.
tabPanelModule( input, output, session, plotObject = NULL, plotName = NULL, plotObject2 = NULL, plotName2 = NULL, is.plotly = FALSE, is.grid = FALSE )
tabPanelModule( input, output, session, plotObject = NULL, plotName = NULL, plotObject2 = NULL, plotName2 = NULL, is.plotly = FALSE, is.grid = FALSE )
input |
gets user input from UI |
output |
designates output for UI. |
session |
Shiny variable for server modules. |
plotObject |
Reactive plot object defined elsewhere in the server function. |
plotName |
Character string designating name of the plot for PDF output. |
plotObject2 |
Reactive plot object for the (optional) second plot. |
plotName2 |
Character string designating name of the (optional) second plot for the PDF output |
is.plotly |
Indicates if plots are plotly generated. Defaults to FALSE. |
is.grid |
Indicates if plot is generated using grid.arrange() to arrange ggplot objects. If TRUE, prints plot object implicitly rather than explicitly. |
Julia Wrobel [email protected]
Creates a UI tab with helptext, widgets for user input, a plot, and standardized layout. The default is to create one plot, but if the argument 'twoPlots' is set to TRUE, then the layout allows for two plots, where each can have separate helper text and Shiny widget calls.
tabPanelModuleUI( id, tabTitle, icon = NULL, calls = NULL, helperText = NULL, twoPlots = FALSE, calls2 = NULL, helperText2 = NULL, title2 = NULL, brushName = NULL, is.plotly = FALSE )
tabPanelModuleUI( id, tabTitle, icon = NULL, calls = NULL, helperText = NULL, twoPlots = FALSE, calls2 = NULL, helperText2 = NULL, title2 = NULL, brushName = NULL, is.plotly = FALSE )
id |
Name of module. Allows each call of this module to be uniquely identified. |
tabTitle |
Title of the tab, visible in UI |
icon |
Optional icon to appear on the tab. This attribute is only valid when using a tabPanel within a navbarPage. |
calls |
Unevaluated expression that stores Shiny widgets (for example, a call to a sliderInput function) for the tab. |
helperText |
Optional help text for the tab. |
twoPlots |
defaults to FALSE, and layout is generated for one plot. If TRUE, layout is generated for two plots |
calls2 |
Unevaluated expression that stores Shiny widgets for the (optional) second plot |
helperText2 |
Optional help text for the (optional) second plot |
title2 |
plot title for the (optional) second plot |
brushName |
character vector indicating the name of brush if you want brushing for the plot. For use in score scatterplots
for |
is.plotly |
Indicates if plots are plotly generated. Defaults to FALSE. |
Julia Wrobel [email protected]
Takes a dense functional dataset in long form and thins it so that there are 100 observations per subject, equally spaced.
thin_functional_data(Y, length_out = 100)
thin_functional_data(Y, length_out = 100)
Y |
functional dataframe |
length_out |
number of points per subject for dataframe that is returned |
Julia Wrobel [email protected]
Internal method that calculates percent variance of eigenvalues for specified level (1, 2, or total) for plot_shiny.mfpca(). The desired level is passed in as an argument (level = 12 for total) and a list of percent variances is returned.
varPercent(level, plotObj)
varPercent(level, plotObj)
level |
numeric, 1 or 2 for levels 1 or 2, respectively, 12 to calculate total variance. |
plotObj |
the mfpca object plotted in the plot_shiny.mfpca() function. |
a list of numbers that indicate percent variance for selected level.
Julia Wrobel [email protected]