R for Medical Research
Welcome to R for researchers
The purpose of this course is to teach you how to handle, analyze and present research data using R. In the modern era of data science and e-learning, researchers and students are increasingly aware of the importance of handling data themselves, irrespective of their background. Indeed, handling and analyzing data has become an integral part of many professions. The field of data science has exploded in recent years, such that students and researchers in all fields are exposed to data science in some way. The dissemination of powerful software and know-how has made it possible for anyone to handle data.
This course intends to teach you how to analyze and present data for research. The authors of this course are medical researchers with many years experience in teaching research methods, programming and data science.1–5 This includes both experimental studies and observational research. This course is an attempt to provide you with all you need to understand how to design a research study, select appropriate analyses and subsequently performing the analyses and present the research findings. We will be using R, which is an extremely powerful tool for research. There are currently more than 15.000 packages (i.e libraries with useful, ready-made functions) available in R. These packages facilitate all aspect of analyzing data in R.
R is an easy language to learn and it offers very powerful tools for researchers, irrespective of the field. Anyone can learn R in a few days and produce impressive reports.
This course will give you all tools necessary to conduct research, regardless of whether your interest lies in medicine or finance. However, all examples and emphasis will be one methods in medical research.
A quick example
Let us see how quickly we can create the core components of a clinical study. We will be using data on patients with colon cancer.
1. Load data
After loading the
survival package we can now load data on colon cancer patients.
2. View 10 random patients
Let’s see 10 random patients:
3. Quick overview of all columns (variables, features)
Let us get a quick overview of variable names, types, missingness, means, etc.
4. Create a correlation plot to inspect distributions among 8 selected variables
We will create a correlation matrix using the first 8 columns in the colon dataset. We will also color the data points according to the treatment received, which is specified by the
ggpairs(colon, columns=1:8, aes(color = rx))
5. Get a glimpse of missing patterns in data
This will result in an image that resembles our dataframe. The columns represent the variables and the rows the patients. The cells for variables with missing values are drawn in black, as denoted by
NA (Not Available) in the legend.
6. Use a sophisticated prediction model to impute (fill in) missing data
library(mice) myImputation <- mice(colon) colon2 <- complete(myImputation) vis_dat(colon2)
7. Create a regression model to study association between patient characteristics and survival
We will create a logistic regression model using the
glm() function. The outcome studied is the very first argument (
status) and the variables that we study are listed after
~ (tilde sign), separated by
+marks. We also specify which data frame to use, and that the distribution is binomial (i.e logistic regression). Finally, we tell R to create a publication ready forest plot with variable names, categories, number of observations, odds ratios and p-values.
# First, we create the logistic regression model my_model <- glm(status ~ age + sex + rx + nodes, data=colon2, family="binomial") # Second, we plot the results forest_model(my_model)
8. Report the exact regression equation
Our regression model is actually a survival model and the journal requires us to provide the exact regression equation.
extract_eq(my_model, use_coefs = TRUE)
Pretty good, right?
As you can see, R is amazing.