File for t test plot and test. The t-test is used here to determine if two populations of snails (Point Loma and Point Sur) differ in their shell length. We have two outcomes where they do not and do significantly differ.
snaillength.xls |
t-test plotting and test
Scott Gabara
10/2/2016
R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
Download the data at the top of this page Load packages
library(gdata)
## gdata: read.xls support for 'XLS' (Excel 97-2004) files ENABLED.
##
## gdata: read.xls support for 'XLSX' (Excel 2007+) files ENABLED.
##
## Attaching package: 'gdata'
## The following object is masked from 'package:stats':
##
## nobs
## The following object is masked from 'package:utils':
##
## object.size
## The following object is masked from 'package:base':
##
## startsWith
library(gtools)
library(lattice)
library(gridExtra)
##
## Attaching package: 'gridExtra'
## The following object is masked from 'package:gdata':
##
## combine
Set working directory
#Set working directory
#setwd("c:/path/to/my/directory/") FOR PC
#setwd("/Users/yourname/filefolderwithfiles/") FOR MAC
#Get file read in
Read in Data We are testing whether plant height through time differs among three different temperature groups
mydata <- read.xls("snaillength.xls", header = TRUE)
Create plot
par(mfrow=c(2,1))
p1=densityplot(~length, groups=snails, auto.key=T, xlim=(2:7), mydata)
p1b=bwplot(snails~length, xlim=(2:7), mydata)
p2=densityplot(~length2, groups=snails2, auto.key=T, xlim=(2:7), mydata)
p2b=bwplot(snails2~length2, xlim=(2:7), mydata)
grid.arrange(p1,p2,p1b,p2b)
May help now that you can visualize the data. Now you can see if assumptions are satisfied which should be done before you statistically compare them:
library(car)
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:gtools':
##
## logit
library(lme4)
## Loading required package: Matrix
leveneTest(length ~ snails, data=mydata)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 0.2578 0.6178
## 18
var.test(length ~ snails, data=mydata)
##
## F test to compare two variances
##
## data: length by snails
## F = 1.2772, num df = 9, denom df = 9, p-value = 0.7214
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
## 0.3172453 5.1421114
## sample estimates:
## ratio of variances
## 1.277228
leveneTest(length2 ~ snails2, data=mydata)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 1.4762 0.2401
## 18
var.test(length2 ~ snails2, data=mydata)
##
## F test to compare two variances
##
## data: length2 by snails2
## F = 2.558, num df = 9, denom df = 9, p-value = 0.178
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
## 0.6353759 10.2985728
## sample estimates:
## ratio of variances
## 2.55802
Test if mean snail length differs for the two populations:
t.test(length ~ snails, data=mydata)
##
## Welch Two Sample t-test
##
## data: length by snails
## t = 1.8792, df = 17.737, p-value = 0.07675
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.04527922 0.80527922
## sample estimates:
## mean in group pointloma mean in group pointsur
## 3.86 3.48
t.test(length2 ~ snails2, data=mydata)
##
## Welch Two Sample t-test
##
## data: length2 by snails2
## t = 7.5646, df = 15.104, p-value = 1.632e-06
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 1.373583 2.450417
## sample estimates:
## mean in group pointloma mean in group pointsur
## 5.392 3.480