home
HomeSearchDr. Hain Clinic website Information for Dizzy Patients MusicFLWVarious 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.
vhit=read.table(target, header=TRUE, sep=",",stringsAsFactors=FALSE)

 

Vhit Wide Data'

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:

Vhit Gain

Step 2: Create a bar plot of VHIT values vs date using ggplot2

VHIT time series

This graph was produced using the following R code:

library(ggplot2)

target=file.choose(new=FALSE) ## interactively pick file.
vhit=read.table(target, header=TRUE, sep=",",stringsAsFactors=FALSE)

# 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[1])) +
scale_fill_manual(values=c("blue", "red")) +
coord_cartesian(ylim=c(0,1.2)) +
geom_line()

p


  

VHIT gain by decade

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

setwd("../vhit")

library(ggplot2)
library(dplyr)
getwd()
## this reads a dump from download from the test_summary.php program.
vhit=read.csv("test_vhit_2021.csv", header=TRUE, skip=6, sep=",",stringsAsFactors=FALSE)
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"))

vhit$decade<-vhit$Age/10
vhit$decade<-as.integer(vhit$decade)
vhit$decade[vhit$decade<1]<-NA    # recode missing crazy ages
vhit$decade[vhit$decade>9]<-NA    # recode missing crazy ages
vhit$decade<-as.factor(vhit$decade)

# simplify the dataframe names.
R_gain=vhit$R_gain
L_gain=vhit$L_gain
A_gain =(vhit$L_gain+vhit$R_gain)/2
Decade=vhit$decade
vhit2=data.frame(R_gain,L_gain,A_gain, Decade)
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)
#aggregate(Gain_TC~Decade, FUN=mean)

f1 <- function(x) c(Mean = mean(x),  SD = sd(x), n=length(x))
# do.call(data.frame, aggregate(A_gain~Decade, vhit2, f1))
agg.data = do.call(data.frame, aggregate(A_gain~Decade, vhit2, f1))
agg.data
  

 

© Copyright May 3, 2021 , Timothy C. Hain, M.D. All rights reserved.
Dr Hain's CV Clinic dizziness-and-hearing.com FLW Various and Sundry Dr. Hain's CV