 Home •Search •Dr. Hain •Clinic website •Information for Dizzy Patients •Music •FLW • Various and Sundry

### Dr. Hain's various/sundry has helpful information about things that Dr. Hain has discovered by trial and error.

This is part of our documentation how one can use the "open source" statistics program "R" to display testing data relevant to medicine. The VHIT test is a rapid method of evaluating vestibular function, that lends itself to serial measurements and a time series (as shown below).

# A time series of VHIT gains as a bar plot

## Step 1: Load data -- this loads in a "wide" dataset

target=file.choose(new=FALSE) ## interactively pick file. '

This is a subset of a very "wide" spreadsheet, using the usual formalism of a single row holding data. Not shown are columns "L_gain" and "R_gain", which are the interesting ones.

## Step 2: Restructure restructure our "wide" data to produce "long" data.

## GAIN by date and side
L_gain= data.frame(side=rep("L", length(vhit\$L_gain)), date=as.Date(vhit\$DOS), GAIN=vhit\$L_gain)
R_gain= data.frame(side=rep("R", length(vhit\$R_gain)), date=as.Date(vhit\$DOS), GAIN=vhit\$R_gain)
GAIN=rbind(L_gain,R_gain) # Sticks two data frames together vertically.

This is what our long data looks like: ## Step 2: Create a bar plot of VHIT values vs date using ggplot2 This graph was produced using the following R code:

library(ggplot2)

target=file.choose(new=FALSE) ## interactively pick file.

# This produces a dataframe. We want factors to be the ear(side) and date
# This means we need a dataframe that looks like (these two together)

## GAIN by date and side
L_gain= data.frame(side=rep("L", length(vhit\$L_gain)), date=as.Date(vhit\$DOS), GAIN=vhit\$L_gain)
R_gain= data.frame(side=rep("R", length(vhit\$R_gain)), date=as.Date(vhit\$DOS), GAIN=vhit\$R_gain)
GAIN=rbind(L_gain,R_gain) # Sticks two data frames together vertically.

p<-ggplot(data=GAIN, aes(x=date, y=GAIN, fill=side)) +
geom_bar(stat="identity", position=position_dodge(), width=20) +
facet_grid(side ~.) +
ggtitle(paste("GAIN by ear for ", vhit\$LastName)) +
scale_fill_manual(values=c("blue", "red")) +
coord_cartesian(ylim=c(0,1.2)) +
geom_line()

p ## VHIT test code by decade (very similar to rchair).

```setwd("../vhit")

library(ggplot2)
library(dplyr)
getwd()
vhit = vhit[!duplicated(vhit\$SSN),]	## toss out duplicates.
vhit\$R_gain[vhit\$R_gain >= 10]<-NA    # recode crazyGains
vhit\$L_gain[vhit\$L_gain > 10]<-NA    	# recode crazy Gains
vhit\$Gain[vhit\$R_gain <0]<-NA    	# recode crazy Gains
vhit\$Gain[vhit\$L_gain <0]<-NA    	# recode crazy Gains
vhit\$DOS1 = as.Date(vhit\$DOS, format="%Y-%m-%d")
vhit\$DOB1 = as.Date(vhit\$DOB, format="%Y-%m-%d")
vhit\$Age = as.numeric(format(vhit\$DOS1, "%Y")) - as.numeric(format(vhit\$DOB1, "%Y"))

# simplify the dataframe names.
R_gain=vhit\$R_gain
L_gain=vhit\$L_gain
A_gain =(vhit\$L_gain+vhit\$R_gain)/2
vhit2 = na.omit(vhit2)			# Get rid of the NAs.
#str(vhit2)

total <- sprintf("VHIT average gain vs decade for %d dizzy patients", nrow(vhit2))

p <- ggplot(vhit2, aes(x=Decade, y=A_gain)) +
geom_boxplot( fill="#69b3a2") + theme_linedraw() #c9b3a2 is a nice green
p + stat_summary(fun.y=mean, geom="point", shape=23, size=4)
p+ggtitle(total) + xlab("Decade") + theme(plot.title = element_text(hjust = 0.5))

#str(vhit)