# t test paired data # following to obtain my sample data: # http://www.robin-beaumont.co.uk/virtualclassroom/book2data/ttest_paired_hindmarsh_1987.dat mydataframe <-read.delim("http://www.robin-beaumont.co.uk/virtualclassroom/book2data/ttest_paired_hindmarsh_1987.dat") ### local copy ignore: # mydataframe <- read.delim("D:\\web_sites_mine\\HIcourseweb # new\\book2data\\ttest_paired_hindmarsh_1987.dat", header=TRUE) ### names(mydataframe) attach(mydataframe) str(mydataframe) mydataframe # create the difference variable mydataframe$difference <- mydataframe$X1_year - mydataframe$baseline_std #or as created by R Commander: # mydataframe$difference <- with(mydataframe, X1_year - baseline_std) # get summary stats summary(mydataframe) #boxplot it; two methods: library(Rcmdr) Boxplot( ~ difference, data=mydataframe, id.method="y") # or just using the standard boxplot function() boxplot(difference, data=mydataframe) # now perform the t test t.test(mydataframe$baseline_std, mydataframe$X1_year, alternative='two.sided', conf.level=.95, paired=TRUE) # # effect size effect_size <- mean(difference)/ sd(difference) effect_size need to delete the difference variable: mydataframe$difference <- NULL # now to draw # install.packages("reshape2", dependencies = TRUE) library(reshape2) stacked_data <- melt(mydataframe, id.vars = "subject") stacked_data stacked_data$variable stacked_data$value # now we need to order the new stacked_data dataframe by # subject id so the display is correct stacked_data <- stacked_data[order(stacked_data$subject),] stacked_data #now graph it library(lattice) xyplot(value ~ variable, data = stacked_data, type="b") ## more complex one xyplot(value ~ variable, data = stacked_data, type="b", ylab= "standardised height cm", xlab = "before after", main = "standardised height changes over 1 year") # the last line below not required is allows you the option of # specifying which categories you want displayed. xyplot(value ~ variable, data = stacked_data, type="b", ylab= "standardised height cm", xlab = "before after", main = "standardised height changes over 1 year", scales = list(x = list(at = c(1,2))))