knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
print.to.this.directory = "C:/Users/ciaconangelo/Documents"

Here is an illustration of how to use these functions to your advantage.

Generate

Start by generating some data for a linear regression:

library(R2Word)
N = 100 
number.groups <- 2
number.timepoints <- 1
set.seed(3042021)

dat <- data.frame('USUBJID' = rep(paste0('Subject_', formatC(1:N, width = 4, flag = '0')), length.out= N*number.timepoints),
                  'Group' = rep(paste0('Group_', 1:number.groups), length.out = N*number.timepoints),
                  'Y_comp' = rep(NA, N*number.timepoints), 
                  'Time' = rep(paste0('Time_', 1:number.timepoints), each = N),
                   stringsAsFactors=F)


# Design Matrix
X <- model.matrix( ~ Group , data = dat) 

# Parameters:
Beta <- matrix(0, nrow = ncol(X), dimnames=list(colnames(X), 'param'))
Beta[] <- c(0.2, 1)
sigma2 <- 9 

# Generate Data
XB <- X %*% Beta
dat$XB <- as.vector(XB)
error <- rnorm(n = N, mean = 0, sd = sqrt(sigma2))
dat$Y <- dat$XB + error

# MCAR drop-out
dat$Y_mcar <- dat$Y
dat$Y_mcar[sample(x = 1:N, size = .4*N, replace = F)] <- NA

str(dat)

Descriptive Statistics

Compute some descriptive statistics using the aggregate() function. If you want to pass multiple functions, use the do.call function to create a dataframe. Use the sprintf() function instead of round() to create a nicely formatted output.

This is all basic R functionality. Then use the dump_df_mat_to_file() function from this R package to write out the table.

out <- do.call(data.frame, stats::aggregate(Y ~ Group,
        FUN = function(x) c(sum(!is.na(x)),
                            sprintf("%.2f (%.2f)", mean(x, na.rm = T),
                            sd(x, na.rm = T)),
                            sprintf("%.2f", quantile(x, c(0, 0.25, 0.5, 0.75, 1), na.rm = T))),
        data = dat, na.action = na.pass))

colnames(out) <- c('Group', 'N', 'Mean (SD)', 
                      'Min', '25th percentile', 'Median', '75th percentile', 'Max') 

dump_df_mat_to_file(out = out, 
                    #decimals = c(1, 2, 2, 3), 
                    table.title = 'Descriptive Statistics - Vignette Illustration', 
                    table.footnote = '**All data simulated',
                    file.name = 'Descr_Stat_output_vignette', 
                    print.dir = print.to.this.directory)

Another example of how useful the aggregate() function is:

out <- aggregate(cbind(Y, Y_mcar, 'Diff' = Y - 0.2) ~
                   Group + Time, 
                  FUN = function(x)  mean(x, na.rm = T), 
                 data = dat, 
                 na.action = na.pass)

out

You can also clean up the labels and then output that into a table. I these basic R functions are sufficient to accomplish most tasks.

Fit Model

Next, fit a linear regression model to the data.

mod <-lm(Y ~ Group, data = dat)

Output Results

Now that we have fit the model, we want to move quickly to create output. There are R packages that allow us to create output from fitted models. However, the function we will use here is very simple and clean, and allows us to output whatever we want. For example, with a few single steps we can dump this dataframe as MS Word output:

out <- coef(summary(mod))
out <- cbind.data.frame('Predictor' = c('Intercept', 'Group 2'), out)
# Now we have the variable names as a column in the dataframe

dump_df_mat_to_file(out = out, 
                    decimals = c(1, 2, 2, 3), 
                    table.title = 'OLS Output - Vignette Illustration', 
                    table.footnote = '**All data simulated',
                    file.name = 'OLS_output_vignette', 
                    print.dir = print.to.this.directory)

Frequency Tables AKA Cross Tabs AKA Pivot Tables

Okay now let's try out the create_tables() function. We can use that to create frequency tables.

# Generate ordinal data:
dat$Y_ord <- sample(x = c(0, 1, 2, 3), size = N, replace = T)
out <- xtabs( ~ Group + Y_ord, data = dat)

# Row sums
out <- create_tables(out, prop.table.margin = 1, add.margins = 1)
out <- cbind.data.frame('Group' = rownames(out), out)

dump_df_mat_to_file(out = out, 
                    table.title = 'Cross-tabs, N(%), row sums - Vignette Illustration', 
                    table.footnote = '**All data simulated',
                    file.name = 'cross_tabs_row_vignette', 
                    print.dir = print.to.this.directory)

We can also use column sums:

# Column sums
out <- xtabs( ~ Group + Y_ord, data = dat)

# Now just a few steps between the cross-tabs and a table in a MS Word doc
out <- create_tables(out, prop.table.margin = 2, add.margins = 2)
out <- cbind.data.frame('Group' = rownames(out), out)

# Dump output
dump_df_mat_to_file(out = out, 
                    table.title = 'Cross-tabs, N(%), col sums - Vignette Illustration', 
                    table.footnote = '**All data simulated',
                    file.name = 'cross_tabs_col_vignette', 
                    print.dir = print.to.this.directory)

There are a bunch of other options in the create_tables() function. The documentation should help clarify some of it.

Shell Tables

Imagine we are preparing a draft version of a report and want to incorporate shell tables. We could simulate code in R, run the analysis, and then print out shell tables that would later be filled in with the observed values. This can be done easily using the shell_tables function:

out <- shell_table(out = out, cols = 2:5)
# specify that we want columns 2 thru 5 to have all numbers converted to 'x'

dump_df_mat_to_file(out = out, 
                    table.title = 'Shell table, Cross-tabs, N(%), col sums - Vignette Illustration', 
                    table.footnote = '**All data simulated',
                    file.name = 'shell_table_cross_tabs_col_vignette', 
                    print.dir = print.to.this.directory)

As you can see, with just a couple of extra lines of code, you can move from the output of an R function like lm() or xtabs to a simple, clean MS Word doc. The flextable package is very flexible but for our purposes we just want a standard, easy way to dump our matrix or dataframe out to a MS Word file.



CJangelo/R2Word documentation built on June 26, 2021, 6:35 a.m.