Load favorite libraries

library(Publish)
## Loading required package: prodlim
library(ggplot2)
library(knitr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tableone)
library(readxl)
library(reshape2)
library(tidyverse)
## ── Attaching packages ──────────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✔ tibble  2.1.3     ✔ purrr   0.3.3
## ✔ tidyr   1.0.0     ✔ stringr 1.4.0
## ✔ readr   1.3.1     ✔ forcats 0.4.0
## ── Conflicts ─────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
rm(list=ls())

Read in data file

df <- readxl::read_xlsx("Licorice Gargle.xlsx")
sumVars <- c("preOp_gender", "preOp_age", "preOp_pain", "preOp_mallampati")
df$Tx <- ifelse(df$treat ==1, "Drug", "Placebo")
tabber <- tableone::CreateTableOne(sumVars, "Tx", data= df)
print (tabber, toggle = FALSE)
##                               Stratified by Tx
##                                Drug          Placebo       p      test
##   n                              118           117                    
##   preOp_gender (mean (SD))      0.42 (0.49)   0.38 (0.49)   0.541     
##   preOp_age (mean (SD))        56.71 (14.86) 58.03 (16.08)  0.513     
##   preOp_pain (mean (SD))        0.00 (0.00)   0.02 (0.13)   0.155     
##   preOp_mallampati (mean (SD))  1.78 (0.63)   1.89 (0.65)   0.193

2. Identify all variables related to post op pain and cough. Hint: try

grep("throatPain|cough",names(df),value=TRUE)
## [1] "extubation_cough"       "pacu30min_cough"       
## [3] "pacu30min_throatPain"   "pacu90min_cough"       
## [5] "pacu90min_throatPain"   "postOp4hour_cough"     
## [7] "postOp4hour_throatPain" "pod1am_cough"          
## [9] "pod1am_throatPain"

##4. Compare the average pain and cough level between treatment and Placebo for all times.

painVar <- grep("throatPain|cough",names(df),value=TRUE)
print(painVar)
## [1] "extubation_cough"       "pacu30min_cough"       
## [3] "pacu30min_throatPain"   "pacu90min_cough"       
## [5] "pacu90min_throatPain"   "postOp4hour_cough"     
## [7] "postOp4hour_throatPain" "pod1am_cough"          
## [9] "pod1am_throatPain"

5. Use reshape2::melt to create a tall dataset:

##tallData <- ##melt(subset(df,select=c(“Tx”,painVariables)),id.vars=“Tx”).

tallData <- melt(subset(df,select= c("Tx",painVar)),id.vars="Tx")
##tallData$Sx <- ifelse(grepl("caugh", tallData$variable), "cough", "pain")
##tallData$time <- gsub("._*","",tallData$variable)
head(tallData)
##     Tx         variable value
## 1 Drug extubation_cough     0
## 2 Drug extubation_cough     0
## 3 Drug extubation_cough     0
## 4 Drug extubation_cough     0
## 5 Drug extubation_cough     0
## 6 Drug extubation_cough     0

##6. Create an ordered “Time” variable with hint in R script and tallData\(Time <- factor(tallData\)Time,levels=c(“pacu30min” ,“pacu90min” , “postOp4hour” ,“pod1am”))

##tallData\(painType <- ifelse(grepl("swallow", tallData\)variable), “swallow”, “throat”)

tallData$Sx <- ifelse(grepl("cough",tallData$variable),"cough","pain")
tallData$time <- gsub("_.*", "",tallData$variable)
tallData$Time <- factor(tallData$time,levels=c("pacu30min" ,"pacu90min"  , "postOp4hour" ,"pod1am"))
##tallData$Time <- factor(tallData$time,levels=c("pacu30min" ,"pacu90min"  , "postOp4hour" ,"pod1am"))

#7 Make a barchart of pain by time by treatment:

ggplot(subset(tallData, Sx=="pain"), aes(Time,value,fill = Tx))+geom_col(position ="dodge")
## Warning: Removed 8 rows containing missing values (geom_col).

##ggplot(tallData,aes(Time,value,fill=Tx))+ geom_bar(position="dodge",stat="summary").

##7 What is the range of the pain variable? Make a histogram of pain.

with(subset(tallData, Sx="pain"), range(value, na.rm = TRUE))
## [1] 0 7
hist(with(subset(tallData, Sx="pain"), value))

##8. Use lm to identify estimate the effect of licorice on pain at 30 minutes adjusting for preOp pain, age, gender, mallampati, and BMI.

reg<- lm(pacu30min_throatPain~preOp_pain+preOp_age+preOp_gender+preOp_calcBMI+preOp_mallampati, data = df)
summary(reg)
## 
## Call:
## lm(formula = pacu30min_throatPain ~ preOp_pain + preOp_age + 
##     preOp_gender + preOp_calcBMI + preOp_mallampati, data = df)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.9240 -0.7107 -0.4742  0.1487  5.0697 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)   
## (Intercept)       1.0335223  0.5373160   1.923  0.05567 . 
## preOp_pain        2.3844278  0.8723999   2.733  0.00677 **
## preOp_age        -0.0002752  0.0055255  -0.050  0.96031   
## preOp_gender     -0.3208315  0.1674354  -1.916  0.05660 . 
## preOp_calcBMI    -0.0253897  0.0209811  -1.210  0.22749   
## preOp_mallampati  0.2113141  0.1323734   1.596  0.11180   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.224 on 227 degrees of freedom
##   (2 observations deleted due to missingness)
## Multiple R-squared:  0.05828,    Adjusted R-squared:  0.03753 
## F-statistic:  2.81 on 5 and 227 DF,  p-value: 0.01749

#9. Plot the residuals as a function of the fitted values. These are in the object produced by lm.

plot(reg)

#10. Create an R markdown file that summarizes results at 30 minutes. Report the p-value, confidence interval and point estimate. Interpret results. Hint please see Markdown directory. All objects in the script are passed to the corresponding markdown file.