#1
pirates <- read.table("http://nathanieldphillips.com/wp-content/uploads/2015/05/pirate_survey_noerrors.txt", sep = "\t", header = T, stringsAsFactors = F)

data <- c(1, -5, 2, 3, 2, 1, 3, 4)
result <- t.test(data)
result
## 
##  One Sample t-test
## 
## data:  data
## t = 1.4019, df = 7, p-value = 0.2037
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  -0.9443258  3.6943258
## sample estimates:
## mean of x 
##     1.375
head(pirates)
##   id    sex headband age college tattoos tchests.found parrots.lifetime
## 1  1 female      yes  35   JSSFP      18             8                9
## 2  2   male      yes  21    CCCC       6             5                1
## 3  3 female      yes  27    CCCC      12             8                1
## 4  4   male      yes  19    CCCC       9             8                1
## 5  5   male      yes  31    CCCC      11             2               13
## 6  6   male      yes  21    CCCC       7             1                0
##   favorite.pirate sword.type  sword.speed
## 1      Blackbeard    cutlass 0.0638977084
## 2      Blackbeard    cutlass 0.5601675763
## 3        Anicetus    cutlass 0.0005400172
## 4    Jack Sparrow    cutlass 3.8770396912
## 5    Jack Sparrow    cutlass 0.5080594239
## 6    Jack Sparrow    cutlass 0.6248019344
#2
t.test ( x=pirates$age, mu=25, alternative= "g")
## 
##  One Sample t-test
## 
## data:  pirates$age
## t = 14.5068, df = 999, p-value < 2.2e-16
## alternative hypothesis: true mean is greater than 25
## 95 percent confidence interval:
##  27.34393      Inf
## sample estimates:
## mean of x 
##    27.644
#Conclusion: t(999)=14.507, p <0.001 , 95% CI = [27.34, Inf]


#3
t.test (x=pirates$parrots.lifetime, mu= 2.7, alternative = "g")
## 
##  One Sample t-test
## 
## data:  pirates$parrots.lifetime
## t = 0.7298, df = 999, p-value = 0.2329
## alternative hypothesis: true mean is greater than 2.7
## 95 percent confidence interval:
##  2.615845      Inf
## sample estimates:
## mean of x 
##     2.767
#Conclusion: t(999)=0.729, p = 0.2329 , 95% CI [2.615, Inf]


#4
swordspeed.cccc <- subset(pirates, subset = college == "CCCC")$sword.speed
swordspeed.jssfp <- subset(pirates, subset = college == "JSSFP")$sword.speed

test.result <- t.test (x = swordspeed.cccc,
                        y = swordspeed.jssfp,
                        alternative = "l")
test.result
## 
##  Welch Two Sample t-test
## 
## data:  swordspeed.cccc and swordspeed.jssfp
## t = -1.4524, df = 540.345, p-value = 0.07348
## alternative hypothesis: true difference in means is less than 0
## 95 percent confidence interval:
##        -Inf 0.03610759
## sample estimates:
## mean of x mean of y 
##  1.068027  1.336603
#Conclusion: t(540.345)=-1.4524, p = 0.07348 , 95% CI [-Inf, 0.036]


#5
favoritepirate.blackbeard <- subset(pirates, subset = favorite.pirate =="Blackbeard")$tattoos
favoritepirate.jacksparrow<- subset(pirates, subset = favorite.pirate =="Jack Sparrow")$tattoos

test.result <- t.test (x = favoritepirate.blackbeard,
                        y = favoritepirate.jacksparrow,
                        alternative = "l")
test.result
## 
##  Welch Two Sample t-test
## 
## data:  favoritepirate.blackbeard and favoritepirate.jacksparrow
## t = 0.0333, df = 137.3, p-value = 0.5133
## alternative hypothesis: true difference in means is less than 0
## 95 percent confidence interval:
##       -Inf 0.6561283
## sample estimates:
## mean of x mean of y 
##  9.620000  9.607064
test.result <- t.test (formula= tattoos ~ favorite.pirate,
                       subset = favorite.pirate %in% c("Jack Sparrow", "Blackbeard"),
                       data = pirates,
                       alternative = "l")
test.result
## 
##  Welch Two Sample t-test
## 
## data:  tattoos by favorite.pirate
## t = 0.0333, df = 137.3, p-value = 0.5133
## alternative hypothesis: true difference in means is less than 0
## 95 percent confidence interval:
##       -Inf 0.6561283
## sample estimates:
##   mean in group Blackbeard mean in group Jack Sparrow 
##                   9.620000                   9.607064
 #Conclusion: t(137.3)= 0.0333, p = 0.5133 , 95% CI [-Inf, 0.6561283]


#6
test.result <- cor.test (x = pirates$age,
                        y = pirates$tchests.found)

test.result
## 
##  Pearson's product-moment correlation
## 
## data:  pirates$age and pirates$tchests.found
## t = 2.8263, df = 998, p-value = 0.004802
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.02726784 0.15027323
## sample estimates:
##        cor 
## 0.08911029
#Conclusion: t(998)= 2.8263, p = 0.004802 , 95% CI [0.027, 0.0150]


#7
lessthanten <- subset(pirates, subset =parrots.lifetime < 10)
favoritepirate.jacksparrow <- subset(pirates, subset = favorite.pirate =="Jack Sparrow")

test.result2 <- cor.test (formula =  ~ age + tchests.found, 
                          subset = favorite.pirate %in% c("Jack Sparrow") & parrots.lifetime < 10, 
                          data=pirates, 
                          alternative = "two.sided")
                                            
test.result2
## 
##  Pearson's product-moment correlation
## 
## data:  age and tchests.found
## t = 1.88, df = 437, p-value = 0.06077
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.004053305  0.181639084
## sample estimates:
##        cor 
## 0.08957122
#Conclusion: t(437)= 1.88, p = 0.06077 , 95% CI [0.004, 0.181]

#8
test.result <- with(pirates, chisq.test(x=college, y=favorite.pirate))
test.result
## 
##  Pearson's Chi-squared test
## 
## data:  college and favorite.pirate
## X-squared = 44.5956, df = 5, p-value = 1.753e-08
#Conclusion: X-squared(5)= 44.5956, p < 0.001