R Programming

Stata tutorial: Adding the 95% Confidence Interval to a Two-way Line Plot

I created a tutorial on how to add the 95% CI to a two-way line plot in Stata. I use the “connected” command to generate a line plot in Stata, and then I added the 95% CI to each value. Surprisingly, Stata does not have a native feature to allow users to generate these 95% CI on a two-way line plot.

I used the AHRQ Medical Expenditure Panel Survey (MEPS) database for the motivating example. In this tutorial, we plotted the average total healthcare expenditure from 2008 to 2019.

I build this tutorial on Stata, but I used R Markdown to write the tutorial. The R Markdown code is located in my GitHub site (Stata - Line plot with 95% CI tutorial).

You can find the tutorial on my Github site and RPubs page.

I used Stata SE 17 to build this.

R plotly - Bar Charts

I wrote a tutorial on how to use plotly, an R package that allows users to include interactive charts in R Markdown projects.

Here is an example of the bar chart that was created using plotly in an R Markdown project:

The tutorial is available on RPubs, and the R Markdown code is available on my GitHub page.

I really like using plotly for my R Markdown projects because it has some nice interactive features. Hopefully, this tutorial will open the doors to more creativity with R Markdown projects.

Hosting an R Markdown HTML file on a GitHub page

INTRODUCTION

R Markdown is a great package for RStudio. You can create (or knit) an html file using R Markdown which will let you add text, snippets of code, and plots. Additionally, you can run R code in R Markdown and have the output as part of the html document.

Here is an example of an R Markdown html file that is currently hosted on RPubs.

You can also create an html file using R Markdown and host it on a GitHub page. This is a feature of GitHub to allow you to host html files from your GitHub account.

There is a lot of flexibility with GitHub; consequently, I’ve started to explore using this option to post tutorials I’ve generated in R Markdown on GitHub. In this article, I’ll review how I was able to host my R Markdown html files on my GitHub repository using the GitHub desktop application.

Step 1: Download the GitHub desktop application

You can download the GitHub desktop application from their website.

 

Step 2: Create a repository in GitHub

Next, you’ll need to create a repository in GitHub.

Open a browser and go to your GitHub page. Create a new repository. For this tutorial, I’ll create the “R Markdown GitHub Page” repository. This is where I will save my R Markdown html file.

Step 3: Open GitHub Desktop Application

Once you’ve downloaded the app, open it. You should see the “Let’s get started” message. We’ll clone the repository that was created in GitHub (“R Markdown GitHub Page”. Search for this repository and then click on “Clone”

Next, you’ll see a window pop up prompting you to select the location on your computer to clone your repository. Chose a location on your computer where you can easily remember and access.

Once you’ve done that, the GitHub application will open to your repository. You can view the files in the folder where you’ve cloned your repository. Click on “Show in Explorer” and a window will appear with a couple of files. Note: When you create your repository, you can elect to have a README file generated. If you did so, then you will see the “README.md” file in this folder. You can ignore the “.git” folder since this is a hidden folder.

Next, create a folder called “docs” in this cloned repository folder. This is where we will save our R Markdown and html files.

Step 4: Setup the GitHub pages features

Now, return to your GitHub repository that is open in your browser. You will need to setup the GitHub pages features. In the GitHub repository, click on the “Settings” tab. Then click on “Pages” in the “Code and automation” tab. This will allow you to set the GitHub Pages to the main branch and the “docs” folder.

Make sure to click “Save” after selecting the “main” branch and the “docs” folder.

GitHub will generate a url where you can host your R Markdown html file. This may take a few minutes to go live, so click on refresh every few minutes. Once the url has been generated, you will see it on this page.

Step 5: Create R Markdown html

Once you’ve setup your GitHub Pages and activated your url, start RStudio and create a new R Markdown file.

This will generate an R Markdown default template that we will use for our html example.

Next you want to click on “Knit” and select “Knit to HTML.” You will be asked to save the HTML file. Navigate to your “docs” folder and save the HTML file as “index.” This will save your HTML file as “index.html” in the “docs” folder. Next, save your R Markdown file as “index.Rmd” in the “docs” folder. You should have two files in the “docs” folder: “index.html” and “index.Rmd”

Step 6: Push the changes to the main branch

After saving your R Markdown file and html file in the “docs” folder, you will need to push these changes to the main branch. Return to your GitHub application and review the changes that are being made. We have two changes that reflect the addition of two files into the “docs” folder.

Every time you make a change to the main branch, you have to enter a short note. I entered “Build R Markdown HTML page” for the title of my short note, and then I include a short description of that change.

Next, I click on “Commit to main” to make the changes to the GitHub application. You will receive a message to push these changes to the main branch. Make sure to select “Push origin” to finalize the changes to the main branch.

Step 7: View your R Markdown html file on your GitHub url link

After you push your final changes to the main branch, the R Markdown html file will be hosted on the GitHub url link that you generated.

CONCLUSIONS

R Markdown is a great way to generate html pages on your R code and output. You can share these files using GitHub Pages in addition to the GitHub application. Additionally, you can work with other folks to make edits and leverage the GitHub applications to push these changes easily to the main branch

This is a work in progress, and I anticipate updating this article as I discover new and innovative ways to improve upon this tutorial.

 

REFERENCES

I used several references to learn how to post R Markdown html files on GitHub Pages. Here are a few of them:

GitHub Pages

YouTube video from EEHolmes-DataScience

YouTube video from Crump’s Computational Cognition Lab

Sample size estimation using the odds ratio in a case-control study

I wrote a short tutorial on how to use an odds ratio to estimate the sample size needed for a case-control study.

The tutorial is located on my RPubs page (link)

The R Markdown source code is located on my GitHub site (link)

Survival analysis in R

I wrote a tutorial on survival analysis using R, which is located on my RPubs page. The R Markdown code is located on my GitHub site.

I provide an introduction to survivor and hazard functions, Kaplan-Meier curves, and Cox proportional hazards model.

R tutorials on confounding/interaction and linear regression model - Updates

Last year, I created several tutorials on how to use R for identifying confounding/interaction and visualizing linear regression models. I updated these tutorials recently to address some errors and mistakes. They have received new hyperlinks:

R tutorial on confounding and interactions using the epitool and epiR packages is located on my RPubs page here. The R Markdown code is located on my GitHub page here.

R tutorial on linear regression model is located on my RPubs page here. The R Markdown code is located on my GitHub page here.

Sample size estimation and Power analysis in R

I wrote a tutorial on how to perform sample size estimations and power analysis using R “pwr” package. These are simple examples that will hopefully lead to more complicated estimations. The tutorial is available on RPubs (link). The R Markdown code is available on my Github site (link).

Logistic regression in R - Part 2 (Goodness of fit tests)

In a previous tutorial, I discussed how to perform logistic regression using R. I wrote a follow-up tutorial on how to conduct goodness of fit tests for logistic regression models in R and posted it on RPubs. The R Markdown code is available on my Github site.

I’ve learned how to assess model fit using Pearson correlations, deviance, and modified Hosmer-Lemeshow Goodness Of Fit (GOF) tests. I think these are important tools when assessing the fit of a logistic regression model. However, I wanted to focus on the HL GOF tests for this tutorial because there are a lot of nuances that I learned and wanted to share.

Additionally, I added the usefulness of visualizing whether the model over- or under-predicts the actual observed data using the calibration plot in R.

Logistic regression in R

I wrote a tutorial on how to construct logistic regression models in R. This tutorial was published on RPubs (link). I go through the use of the glm() command to perform a crude logistic regression model and a multivariable logistic regression model. The data (diabetes.csv) that I used for the motivating example is located here.

The RMarkdown code I used to create the tutorial is located on my GitHub site (link).

Visualizing linear regression models using R - Part 2

I continue my previous blog post on visualizing linear regression models using R (link). Part 2 focuses on using visualization to assess whether the model’s residuals were associated with the predicted values and whether they are normally distributed.

The R Markdown code that I wrote to create this tutorial is located on my GitHub site (link).

You can find the tutorials on my RPubs site:

  • Part 1 - Visualizing linear regression model using R (link)

  • Part 2 - Visualizing linear regression model using R (link)

(NOTE: on 30 January 2022, I updated these tutorials and they can be found in my RPubs page here. The R Markdown code is saved on my GitHub page here.)