# Load packages -----------------------------------------------------------library(readr)library(ggplot2)library(dplyr)library(ggforestplot)library(forestmodel)library(ggcorrplot)# Read data ---------------------------------------------------------------hypoxia<-read_csv("data/hypoxia.csv")View(hypoxia)# Useful functions --------------------------------------------------------# An AHI of 5-14 is mild; 15-29 is moderate and# 30 or more events per hour characterizes severe sleep apnea.# 1 = (AHI < 5); 2 = (5 ≤ AHI < 15);# 3 = (15 ≤ AHI < 30); 4 = (AHI ≥ 30)# Convert binary `Female` column to characters for Male and Femaleconvert_to_factors<-function(data=hypoxia){output<-data|>mutate( Sex =if_else(.data$Female==1, "Female", "Male"), .after =.data$Female)|>mutate(AHI =factor(.data$AHI))return(output)}hypoxia<-convert_to_factors()# Exploratory plots -------------------------------------------------------# Histograms and bar chartsggplot(hypoxia)+geom_histogram(aes(Age))ggplot(hypoxia)+geom_histogram(aes(AHI))ggplot(hypoxia)+geom_bar(aes(AHI))# Correlationhypoxia|>select(where(is.numeric))|>cor(use ="complete.obs")|>ggcorrplot()ggsave("corr_plot.png", width =5, height =5)# Multiple factorsggplot(hypoxia)+geom_bar(aes(AHI))+facet_wrap(~Sex)ggplot(hypoxia)+geom_bar(aes(AHI))+facet_wrap(Race~Sex)ggplot(hypoxia)+geom_bar(aes(AHI))+facet_grid(Race~Sex)hypoxia<-hypoxia|>mutate(Diabetesfct =factor(Diabetes))ggplot(hypoxia)+geom_bar(aes(AHI, fill =Diabetesfct), position ="fill")+scale_fill_brewer(palette ="Dark2")+theme_minimal()# no colours# Summary statistics ------------------------------------------------------# Meanmean(hypoxia$Age)mean(hypoxia$BMI)mean(hypoxia$`Duration of Surg`)# Standard Deviationsd(hypoxia$Age)# Countstable(hypoxia$AHI)table(hypoxia$`Duration of Surg`)# Statistical tests -------------------------------------------------------# Chi-squared tests of AHI factorschisq.test(table(hypoxia$AHI, hypoxia$Sex))chisq.test(table(hypoxia$AHI, hypoxia$Race))chisq.test(table(hypoxia$AHI, hypoxia$Smoking))chisq.test(table(hypoxia$AHI, hypoxia$Diabetes))# T-tests for BMI and Sleep time# Assume variance equalBMI1<-hypoxia|>filter(BMI<=median(BMI))# nolintBMI2<-hypoxia|>filter(BMI>median(BMI))# nolintt.test(BMI1$Sleeptime, BMI2$Sleeptime, var.equal =TRUE)t.test(BMI1$Sleeptime, BMI2$Sleeptime, var.equal =TRUE)$p.value# Modelling ---------------------------------------------------------------mod_data<-hypoxia|>mutate(severeAHI =if_else(AHI==4, 1, 0))|>select(-c(Female, AHI))mod1<-glm(severe_AHI~., data =mod_data, family ="binomial")summary(mod1)mod2<-glm(severe_AHI~Age+Sex+BMI, data =mod_data, family ="binomial")summary(mod2)mod3<-glm(severe_AHI~Age+Sex, data =mod_data, family ="binomial")summary(mod3)mod4<-glm(severe_AHI~Age+BMI, data =mod_data, family ="binomial")summary(mod4)mod5<-glm(severe_AHI~Sex+BMI, data =mod_data, family ="binomial")summary(mod5)mod6<-glm(severe_AHI~Age, data =mod_data, family ="binomial")summary(mod6)mod7<-glm(severe_AHI~Sex, data =mod_data, family ="binomial")summary(mod7)mod8<-glm(severe_AHI~BMI, data =mod_data, family ="binomial")summary(mod8)# Results -----------------------------------------------------------------# AICaic_results<-c(mod2$aic, mod3$aic, mod4$aic,mod5$aic, mod6$aic, mod7$aic, mod8$aic)# Forest plot of best modelforest_model(mod2)ggsave("forestplot.png")
Exercises
Exercise 1: Introduction to Git and GitHub for R
Create a GitHub account if you don’t already have one, and make sure you have Git installed on your laptop.
Install and load the usethis and gitcreds packages.
Configure git then run create_github_token(), and follow the instructions to generate a token.
Run gitcreds_set() and paste in the token when prompted.
From your GitHub profile page, click Repositories then click New:
Go to File –> New Project – Version Control –> Git
Copy the URL from GitHub (ending with .git):
into the Repository URL and select where you want the project to be.
Exercise 3: Committing and pushing changes
Commit the initial files created when making a project.
Download the hypoxia.csv file above and save it into a folder called data in your git project. Download the analysis.R file and it to the project as well.
Add the data folder to your .gitignore file.
Commit and push your changes, and view them on GitHub.
Solution
From the Git tab in RStudio, click the check boxes of the files you want to commit:
Then click Commit and a pop-up window should appear. Enter an (informative) commit message. The click Commit again.
You should then see (another popup) which you can Close. Copy and paste the files from his mornings session into the git folder.You should see them appear in the Git tab:
Open up the .gitignore file in RStudio (you might need to set Show hidden files). Add the line data/ to the .gitignore file:
.Rproj.user
.Rhistory
.RData
.Ruserdata
data/
The data folder should no longer show in the Git tab, and the .gitignore file should should it has been modified.
Commit the changes as you did before. Then click Push.
Go back to the repository on github.com. You should see your R files, but not your data.
Exercise 4: Working in branches
Create a new branch from RStudio.
Make an edit to your R script and commit the changes.
Push your new branch to GitHub and open a pull request.
Solution
In the Git tab, click New Branch and give it a useful name. Then click Create.
Make an edit and commit your changes (as you did in the previous exercise). Push your changes.
Go back to the repository on github.com. You should see a box to Compare & pull request. Click it.
Add a description, then click Create pull request.
You can click Files changed to see what changes have been made.
Exercise 5: Handling git conflicts
Use the web editor to make a change on main.
Return to your pull request and fix any merge conflicts.
Merge the pull request and delete the new branch.
Solution
From the Code tab on the GitHub website, click through to the file you want to edit.
Make a change, then click Commit changes… in the top right.
Return to the *Pull requests** tab. You might see a message that says This branch has conflicts that must be resolved.
If not, go ahead and click Merge pull request.
If you have a conflict, there are different ways to resolve it. For small conflicts, it can be easiest to click *Use web editor**. You’ll see the two different versions of the code.
Edit the file, and select (or further edit) the code you want to keep.
The click Mark as resolved, then Commit merge.
You can then return to the Pull requests page, and Confirm merge:
Remember to delete the branch if you’re finished with it!
In RStudio, change the branch back to main, and Pull the changes.
Exercise 6: Sharing and reviewing
Create a new branch and edit your R script in some way (e.g. add some code or comments).
Commit your changes, open a pull request, and describe the changes.
Request a review from someone else in the room (and vice versa).
Note: you may need to add them as a collaborator to the repository. (You can remove them afterwards.)
Review someone else’s code - leave comments and decide whether to approve.
Bonus: add a README.md file to your repository, with a brief summary of the project and stating what packages and version of R you have used.
Solution
Create a branch, commit changes, and create a pull request as you did in the previous exercise.
Within the GitHub repository, go to Settings -> Collaborators, Click Add people and add the person next to you as a collaborator using their username.