knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
Start by loading all usual libraries.
library(ClinReport) library(officer) library(flextable) library(dplyr) library(reshape2) library(nlme) library(emmeans) library(car)
Load your data.
# We will use fake data data(datafake) print(head(datafake))
Create a statistical output for a quantitative response and two explicative variables. For example a treatment group and a time variable corresponding to the visits of a clinical trial.
For that we use the report.quanti()
function:
tab1=report.quanti(data=datafake,y="y_numeric", x1="GROUP",x2="TIMEPOINT",at.row="TIMEPOINT", subjid="SUBJID") tab1
The at.row
argument is used to space the results between each visit and the subjid
argument is used
to add in the columns header the total number of subjects randomized by treatment group.
Generally we want also the corresponding graphics. So you can use the specific plot function to print the corresponding graphic of your table:
g1=plot(tab1,title="The title that you want to display") print(g1)
You can modify the plot by using the following arguments of the plot.desc()
function:
args(ClinReport:::plot.desc)
Then we can use the report.doc()
function which use the flextable package to format
the output into a flextable
object, ready to export to Microsoft Word
with the officer package.
The table will look like this (we can have a preview in HTML, just to check):
report.doc(tab1,title="Quantitative statistics (2 explicative variables)", colspan.value="Treatment group", init.numbering =T )
All output numbers will be increased automatically after each call of the function report.doc()
.
You can restart the numbering of the outputs by using init.numbering=T
argument in report.doc()
function.
Finally, we add those results to a rdocx
object:
doc=read_docx() doc=report.doc(tab1,title="Quantitative statistics (2 explicative variables)", colspan.value="Treatment group",doc=doc,init.numbering=T) doc=body_add_gg(doc, value = g1, style = "centered" )
Write the doc to a docx file:
file=paste(tempfile(),".docx",sep="") print(doc, target =file) #Open it #shell.exec(file)
An example of qualitative statistics with one explicative variable
tab=report.quali(data=datafake,y="y_logistic", x1="VAR",total=T,subjid="SUBJID") report.doc(tab,title="Qualitative table with two variables", colspan.value="A variable") ``` An example of qualitative statistics with two explicative variables ```r tab=report.quali(data=datafake,y="y_logistic", x1="GROUP",x2="TIMEPOINT",at.row="TIMEPOINT", total=T,subjid="SUBJID") report.doc(tab,title="Qualitative table with two variables", colspan.value="Treatment group") ``` ### Quantitative descriptive tables An example of quantitative statistics with one explicative variable ```r tab=report.quanti(data=datafake,y="y_numeric", x1="VAR",total=T,subjid="SUBJID") report.doc(tab,title="Quantitative table with one explicative variable", colspan.value="A variable") ``` An example of quantitative statistics with two explicative variables ```r tab=report.quanti(data=datafake,y="y_numeric", x1="GROUP",x2="TIMEPOINT",at.row="TIMEPOINT", total=T,subjid="SUBJID") report.doc(tab,title="Quantitative table with two explicative variables", colspan.value="Treatment group") ``` ### Mix descriptive statistics of quantitative and qualitative nature You can mix qualitative and quantitative outputs. But it's only possible for 1 explicative variable, and it should be the same variable for both response: ```r tab1=report.quanti(data=datafake,y="y_numeric", x1="GROUP",subjid="SUBJID",y.label="Y numeric") tab2=report.quali(data=datafake,y="y_logistic", x1="GROUP",subjid="SUBJID",y.label="Y logistic") tab3=regroup(tab1,tab2,rbind.label="The label of your choice") report.doc(tab3,title="Mixed Qualitative and Quantitative outputs", colspan.value="Treatment group")
Hierarchical descriptive statistics are reported when there are several events per statistical unit. It's often use for adverse events, medical history or concomitant treatments.
Typically, adverse event are classified according to System Organ Class (SOC) and then sub classified by Preferred Terms (PT). Several observations of a same adverse event can be observed several times on the same subject. It's then useful to know how many persons are concerned by at least one of those adverse events and report the frequencies for each classifications: SOC and PT.
To do that, you can use the report.quali.hlev
function.
# We use a fake standard adverse event data set # In this data sets there are several observations per subject # and the factor PTNAME is a sub classification of the factor SOCNAME data(adverse_event) # In the report.quali.hlev we specify which factor has the more levels in the var_upper # argument. The var_lower argument indicates the classification with less levels. # The x1 argument is used to split the results according to the levels of another factor. test=report.quali.hlev(data=adverse_event,subjid="SUBJID",var_upper="PTNAME", var_lower="SOCNAME",lower.levels="System Organ Class",upper.levels="Prefered Terms",x1="randtrt") # Frequencies and Percentages for each level are shown in the # formatted table in HTML, using the usual report.doc function ft=report.doc(test,valign=TRUE) ft
You can report the information relative to a model using the report.modelinfo
function. The function supports GLM, LME and Cox models. It is still under development.
# Removing baseline data for the model data.mod=droplevels(datafake[datafake$TIMEPOINT!="D0",]) mod=lme(y_numeric~baseline+GROUP+TIMEPOINT+GROUP*TIMEPOINT, random=~1|SUBJID,data=data.mod,na.action=na.omit) report.modelinfo(mod)
For the anova table reporting, it's basically a call
to the function xtable_to_flextable()
. The function
report.doc()
just handle the numbering of the output
and the header with the title.
# Removing baseline data for the model anov3=Anova(mod,3) # Make pretty names for the table rownames(anov3)=make.label(rownames(anov3), list(c("GROUP","Treatment"), c("TIMEPOINT","Visit"), c("Treatment:Visit","Interaction Treatment-Visit"))) report.doc(anov3,title="Mixed Qualitative and Quantitative output")
LS-means reporting are based on the package emmeans.
The function report.lsmeans()
enables to format the output:
lsm=emmeans(mod,~GROUP|TIMEPOINT) tab=report.lsmeans(lsm,at.row="TIMEPOINT") report.doc(tab,title="LS-Means example", colspan.value="Treatment Group")
It's the same usage
contr=contrast(lsm, "trt.vs.ctrl", ref = "A") # There is just only one explicative variable tab.contr=report.lsmeans(lsm=contr,at="TIMEPOINT") report.doc(tab.contr,title="LS-Means contrast example", colspan.value="Contrasts")
library(survival) data(time_to_cure) fit <- coxph(Surv(time, status) ~ Group, data = time_to_cure) em=emmeans(fit,~Group,type="response") pairs=pairs(em,adjust="none",exclude="Untreated") tab.pairs=report.lsmeans(pairs) tab.pairs report.doc(tab.pairs,title="Hazard ratios of a Cox model")
Finally, you can export the current information
of your R session by using the report.sessionInfo()
function
which will create a table containing the result of the call
to sessionInfo()
.
report.sessionInfo()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.